elm-review-simplify icon indicating copy to clipboard operation
elm-review-simplify copied to clipboard

combine map, then map(N)

Open miniBill opened this issue 1 year ago • 4 comments

Example of things the rule would report:

List.map2 (\x y -> f x y) (List.map g xs) ys
--> List.map2 (\x y -> f (g x) y) xs ys

(and all the variations: mapN, or multiple maps in children)

I am looking for:

  • Someone to implement it with/for me [I may get around to it but my plate is full at the moment]

miniBill avatar Oct 23 '23 23:10 miniBill

Sounds fun and also be quite hard to get right. Simple cases like

List.map g (List.map (\x -> f x) list)
--> List.map (\x -> g (f x))

seem alright while for example

List.map (\string -> g string) (List.map String.fromInt list)
--> List.map (\string -> g (String.fromInt string))

plain uses wrong variable names (your example has the same mistake btw, g needs to be a lambda so we have a name for its argument) so we can't reasonably fix it.

And many other more complex situations need to be avoided I'm guessing

List.map2 (\x y -> g x y) (List.map (\y -> f y) aList) bList
--> List.map (\y?? y -> g (f y) y)

List.map (\x -> g x x) (List.map (\y -> f y) aList) bList
--> List.map (\y -> let x = f y in g x x) -- should we do this fix?

List.map2 (\f -> f) aList (List.map g bList)
--> List.map2 (\f -> f << g) aList bList

for the same reason we don't currently simplify

List.map g (List.map f list)
--> List.map (g << f) list

etc for code style reasons

lue-bird avatar Oct 23 '23 23:10 lue-bird

List.map (\string -> g string) (List.map String.fromInt list)
--> List.map (\string -> g (String.fromInt string)) list

Wait, what is wrong with this transformation?


List.map2 (\x y -> g x y) (List.map (\y -> f y) aList) bList

should go to

List.map2 (\x y -> g x ((\y_ -> f y_) y)) aList bList

I guess? Either that, or not touch it.


List.map (\x -> g x x) (List.map (\y -> f y) aList)

This should not be touched


List.map2 (\f -> f) aList (List.map g bList)

Oh, this is probably also in the "don't touch" category

miniBill avatar Oct 24 '23 00:10 miniBill

List.map (\string -> g string) (List.map String.fromInt list)
--> List.map (\string -> g (String.fromInt string)) list

Wait, what is wrong with this transformation?

Notice how the code says String.fromInt string. The string argument from the later map refers to the already transformed value.

2 of your suggestions fall into the same problem category

List.map2 (\x y -> f x y) (List.map g xs) ys
--> List.map2 (\x y -> f (g x) y) xs ys

List.map2 (\x y -> g x y) (List.map (\y -> f y) aList) bList
--> List.map2 (\x y -> g x ((\y_ -> f y_) y)) aList bList

lue-bird avatar Oct 24 '23 00:10 lue-bird

I see! Then maybe it makes sense to just not autofix it except for the simplest cases?

miniBill avatar Oct 29 '23 22:10 miniBill