zoukankan      html  css  js  c++  java
  • [Elm] Functions in Elm

    Functions are an important building block in Elm. In this lesson we will review stateless functions, function composition, anonymous functions, Currying, and more.

    Write Pure function:

    import Htrml exposing (text)
    
    main = 
        view "Hello World"
    
    view message = 
        text message

    We created a function call 'view', and pass the value ("Hello World") to 'message' param.

    And inside view function, we output message value.

    Add type:

    import Htrml exposing (text)
    
    main = 
        view "Hello World"
    
    view: String -> Html msg
    view message = 
        text message

    For view function, we add ':String' to say the input value should be a string type.

    If we change type to "Int":

    It will output the error message on the screen.

    Function execute from inside to outside:

    import Html exposing (Html, text)
    import String
    
    main = 
     view "Hello World!!!"
    
    view: String -> Html msg
    view message =
     text (String.repeat 3((String.toUpper message)))

    Output:

    HELLO WORLD!!!HELLO WORLD!!!HELLO WORLD!!!

    Forward:

    import Html exposing (Html, text)
    import String
    
    main = 
     view "Hello World!!!"
    
    view: String -> Html msg
    view message =
     --text (String.repeat 3((String.toUpper message)))
     message
      |> String.toUpper
      |> String.repeat 3
      |> text

    '|>' fowards symbol, so the message will go thought 'toUpper' --> 'repeat 3' --> text.

    The 'repeat 3': Since we only provide the first value, it returns a function instead of a value. When two upper is evaluated, the message is being passed in. Finally, a value is returned and passed to the next line. This is known as currying. 

    Then later, we can provide the rest of the parameters. This really helps us to build new functions from others.

    For example, let's replace repeat with a function of our own creation called triple.

    import Html exposing (Html, text)
    import String
    
    main = 
     view "Hello World!!!"
    
    triple: String -> String
    triple =
     -- repeat : Int --> String -> String  repeat function take int and string param and return string
     String.repeat 3
    
    view: String -> Html msg
    view message =
     --text (String.repeat 3((String.toUpper message)))
     message
      |> String.toUpper
      |> triple
      |> text

    Let's add an anonymous function to add a comma and a space between each repetition. Anonymous functions begin with a back slash. 

    We define str to take the value being passed in from the previous line. We use the double plus or string concatenation operator to add a comma and a space.

    import Html exposing (Html, text)
    import String
    
    main =
     view "Hello World"
    
    triple: String -> String
    triple =
     -- repeat : Int --> String -> String  repeat function take int and string param and return string
     String.repeat 3
    
    view: String -> Html msg
    view message =
     --text (String.repeat 3((String.toUpper message)))
     message
      |> String.toUpper
      |> str -> str ++ ", "
      |> triple
      |> text
  • 相关阅读:
    jmeter_04_常用取样器
    jmeter_03_鉴权
    jmeter_02_目录文档说明
    jmeter_01_常用快捷键
    Web Api 与 Andriod 接口对接开发经验
    Eclipse自动生成作者、日期注释等功能设置
    c#解析XML到DATASET及dataset转为xml文件函数
    Jquery 仿 android Toast效果
    正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法
    异步网络加载开源框架AsyncHttpClient使用
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6126472.html
Copyright © 2011-2022 走看看