What does the `<<` operator mean in elm?

前端 未结 5 1469
悲&欢浪女
悲&欢浪女 2021-01-30 12:24

In the following code taken from Elm Form Example, line 122, what does the << operator mean?

Field.field Field.defaultStyle (Signal.send updat         


        
5条回答
  •  伪装坚强ぢ
    2021-01-30 13:08

    My second attempt :D

    << vs <|

    The difference between << and <| is that << is used to compose functions and <| is used to omit parentheses.

    Why it works like that

    Let's look at type annotation found here:

    << : (b -> c) -> (a -> b) -> a -> c
    

    This definition tells us, that when you pass two functions to function <<, you will get function a -> c.

    Example with demo

    hi a =
        a + 2
    hello a =
        a * 2
    bye =
        hello << hi
    c =
        bye 3
    

    c returns value 10.

    Read more about:

    • infix operators - first argument on the left of function,
    • partial application - when you pass one argument to function expecting two arguments, you get function expecting one argument.

提交回复
热议问题