Elm Language Functions and Partial Application

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

Syntax

  • -- defining a function with no arguments looks the same as simply defining a value
    language = "Elm"
  • -- calling a function with no arguments by stating its name
    language
  • -- parameters are separated by spaces and follow the function's name
    add x y = x + y
  • -- call a function in the same way
    add 5 2
  • -- partially apply a function by providing only some of its parameters
    increment = add 1
  • -- use the |> operator to pass the expression on the left to the function on the right
    ten = 9 |> increment
  • -- the <| operator passes the expression on the right to the function on the left
    increment <| add 5 4
  • -- chain/compose two functions together with the >> operator
    backwardsYell = String.reverse >> String.toUpper
  • -- the << works the same in the reverse direction
    backwardsYell = String.toUpper << String.reverse
  • -- a function with a non-alphanumeric name in parentheses creates a new operator
    (#) x y = x * y
    ten = 5 # 2
  • -- any infix operator becomes a normal function when you wrap it in parentheses
    ten = (+) 5 5
  • -- optional type annotations appear above function declarations
    isTen : Int -> Bool
    isTen n = if n == 10 then True else False


Got any Elm Language Question?