jfmengels / elm-review-simplify

Provides elm-review rules to simplify your Elm code
https://package.elm-lang.org/packages/jfmengels/elm-review-simplify/latest/
BSD 3-Clause "New" or "Revised" License
20 stars 9 forks source link

Add wrapper mapN simplifications #173

Closed lue-bird closed 11 months ago

lue-bird commented 11 months ago

Since we don't want unused code around, this is already applied to Result.map2-5:

-- the following simplifications for map3 work for all Result.mapN
Result.map3 f (Ok a) (Ok b) (Ok c)
--> Ok (f a b c)

Result.map3 f (Ok a) (Err x) thirdResult
--> Err x

Result.map3 f firstResult (Err x) thirdResult
--> Result.map2 f firstResult (Err x)

includes all combinations with currying, for example

Result.map3 f firstResult (Err x)
--> always (Result.map2 f firstResult (Err x))

Result.map4 f firstResult (Err x)
--> (\_ _ -> Result.map2 f firstResult (Err x))

The same checks can be applied to Task, Json.Decode, Fuzz etc. with minimal effort.

Note that we don't simplify for example

Result.map2 (\a b -> a + b) firtResult (Ok second)
--> Result.map (\a -> a + second) firstResult

because some consider this a valid code style, e.g.

Result.map2 YourRecord
    (Json.Decode.field "a" aDecoder)
    (Json.Decode.succeed second)
    (Json.Decode.field "c" cDecoder)

I personally disagree strongly but at the same time I'd prefer this to be a separate rule (the fixes are also not trivial, not sure we want to alter the existing code this much).