Search code examples
functional-programmingelmpurely-functional

How to use |> operator with a function which expects two parameters?


kll : Float
kll =
    let
        half x =
            x / 2
    in
    List.sum (List.map half (List.map toFloat (List.range 1 10)))

converting using |>

can you also explain how to use the |> correctly with some examples cant find any online? Thanks This is my code:

kll : List Float
kll =
    let
        half x =
            x / 2
    in
    ((1 |> 1 |> List.range) |> toFloat |> List.map) (|>half |> List.map))|> List.sum

Solution

  • |> doesn't work with 2-parameter functions. It only feeds into functions that take one parameter.

    Use currying to supply leading parameters. I think what you want is this:

    List.range 1 10 |> List.map toFloat |> List.map half |> List.sum
    

    Or more simply:

    List.range 1 10 |> List.map (\x -> toFloat x / 2) |> List.sum