Is there anything similar to Haskell’s `$` in Unison?

Viewed 24

In Haskell, f $ g a = f (g a). Is there a right-associated operator like that in Unison?

1 Answers

There are a few options.

  1. You can use let. For example, f let g a is the same as f (g a). This is because let opens a block, which is a sequence of statements. In this case the only statement in the block is the expression g a which becomes the value the block returns. So you could write e.g.

    isSome let head let List.map increment [1,2,3]

  2. There is a function application operator:

    (base.Function.<|) : (a ->{e} b) -> a ->{e} b

    so you could write f <| g a. However, Unison does not have any operator precedence rules, so all operators associate to the left. So if you want to do nested applications like

    isSome <| (head <| List.map increment [1,2,3])

    you will need to use parentheses anyway. However, since all operators associate to the left, you can just use the |> operator instead:

    (base.Function.|>) : a -> (a ->{e} b) ->{e} b

    Then you can write (flipping the order of the functions):

    [1,2,3] |> List.map increment |> head |> isSome

  3. You can use function composition:

    (base.Function.<<) : (b ->{e} c) -> (a ->{e} b) -> a ->{e} c

    This lets you write

    isSome << head << List.map increment <| [1,2,3]

    Which is equivalent to the Haskell expression:

    isJust . listToMaybe . map (+1) $ [1,2,3]

Related