Jivan
Jivan

Reputation: 23058

Simplify zipWith and case using LambdaCase

With the following zipWith expression:

zipWith3 (\foos bars bazs -> case (foos, bars, bazs) of
    (foo, bar, Just baz)  -> Right "hell yeah"
    (foo, bar, Nothing) -> Left "tough luck"
) ["foo1", "foo2"] ["bar1", "bar2"] [Just "baz1", Just "baz2"]

Is it possible to use LambdaCase to simplify the case expression with something like (doesn't compile):

zipWith3 (\case
    (foo, bar, Just baz)  -> Right "hell yeah"
    (foo, bar, Nothing) -> Left "tough luck"
) ["foo1", "foo2"] ["bar1", "bar2"] [Just "baz1", Just "baz2"]

In the first (working) version, case receives a tuple, but in the (failing) LambdaCase version, it seems that case would receive the three arguments instead of a tuple. I don't know if this is possible to do something like that.

Upvotes: 0

Views: 187

Answers (2)

Daniel Wagner
Daniel Wagner

Reputation: 152707

Another option, since the pattern is only on the final argument, is to bind the first two with a normal lambda and the last with a lambdacase:

zipWith3 (\foo bar -> \case
    Just baz -> Right "hell yeah"
    Nothing -> Left "tough luck"
) [] [] []

I personally find this more visually pleasing, but have no particular technical reason to prefer this or the curry3 solution.

Upvotes: 1

SergeyKuz1001
SergeyKuz1001

Reputation: 875

You need to add curry3 (for example, from extra package):

zipWith3 (curry3 $ \case
    (foo, bar, Just baz)  -> Right "hell yeah"
    (foo, bar, Nothing) -> Left "tough luck"
) ["foo1", "foo2"] ["bar1", "bar2"] [Just "baz1", Just "baz2"]

Upvotes: 4

Related Questions