Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the dollar operator ($) supported in elm?

In Haskell, you can use the $ operator to clean up bits of code, removing the need for parens.

Does elm support this operator, or something like it?

I can define it myself but I was hoping that this was something built-in.

Here's how it works:

import Html
import List exposing (map, foldr)

datas = [("a", 1), ("b", 2), ("c", 3)]

{--}
($) : (a -> b) -> (a -> b)
($) a b = a b
infixr 0 $
--}

main =
  {-- replace all these parens
  Html.text (toString (foldr (++) "" (map fst datas)))
  --}
  Html.text $ toString $ foldr (++) "" $ map fst datas
like image 936
Conrad.Dean Avatar asked Dec 07 '15 05:12

Conrad.Dean


People also ask

What is dollar Sign Haskell?

The dollar sign, $ , is a controversial little Haskell operator. Semantically, it doesn't mean much, and its type signature doesn't give you a hint of why it should be used as often as it is. It is best understood not via its type but via its precedence.

What does the operator do in Haskell?

Haskell provides special syntax to support infix notation. An operator is a function that can be applied using infix syntax (Section 3.4), or partially applied using a section (Section 3.5).


1 Answers

Yes, we use <| instead of $. We borrowed it from F# along with the flipped version |> and << for composition . and the flipped version >>.
Once these were introduced, people naturally gravitated towards a style dubbed 'pipelining', where you take some data and transform it in a couple of steps using the |> operator. These days this is a more common code pattern in Elm code than using <|.

For example:

update : (Float, Keys) -> Model -> Model
update (dt, keys) mario =
  mario
  |> gravity dt
  |> jump keys
  |> walk keys
  |> physics dt

(Taken from the Mario example on the website)

like image 118
Apanatshka Avatar answered Oct 17 '22 00:10

Apanatshka