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
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.
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).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With