DEV Community

Discussion on: What language features/concepts do insiders of the language love and outsiders hate?

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

Functions are so central to functional programming that they get their own operators. This demonstrates function piping and composition:

-- basic version of a function with 3 steps
-- takes in a string which is supposed to be numeric
-- and creates a list of integers up to that number
createRange str =
    List.range 1 (Result.withDefault 0 (String.toInt str))

-- the problem above is that it reads inside-out
-- this piped equivalent reads just like my brain thinks of the steps
createRange str =
    str
        |> String.toInt
        |> Result.withDefault 0
        |> List.range 1

-- composed "point-free" equivalent
createRange =
    String.toInt >> Result.withDefault 0 >> List.range 1

The last one is considered hard to understand at first. >> takes the output of the first function and provides it as an input to the second. Basically welding the functions together into one.