[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
时间: 2024-10-06 12:27:53

[Elm] Functions in Elm的相关文章

[Elm] Installing and setting up Elm

Before writing any Elm we need to first install the runtime locally. In this lesson we install the Elm runtime locally and set up a simple application to verify everything is working properly. Install: npm install -g elm elm package install elm packa

zepto.js 源码注释

/* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(function(undefined) { if (String.prototype.trim === undefined) // fix for iOS 3.2 String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''

zepto源码

/** * * ┏┓ ┏┓ * ┏┛┻━━━┛┻┓ * ┃ ┃ * ┃ ━ ┃ * ┃ ┳┛ ┗┳ ┃ * ┃ ┃ * ┃ ┻ ┃ * ┃ ┃ * ┗━┓ ┏━┛Code is far away from bug with the animal protecting * ┃ ┃ 神兽保佑,代码无bug * ┃ ┃ * ┃ ┗━━━┓ * ┃ ┣┓ * ┃ ┏┛ * ┗┓┓┏━┳┓┏┛ * ┃┫┫ ┃┫┫ * ┗┻┛ ┗┻┛ * */ /* Zepto 1.1.4 - zepto event aj

移动端的silder,未封装,基于zepto的touch模块,有参照修改过touch的bug

<!--html模块--> <header class="appoin-head"> <ul> <li class="aa"> <a href="#"> <img src="img/banner1.png" alt=""> </a> </li> <li> <a href="#"

html5 聊天 宿舍 宿舍列表

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-

js实现可拉伸移动的div

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta http-equiv="Cont

学习JavaScript应该尽早知道的几个技巧

Shortcut Notations 简洁写法 One of the things I love most about JavaScript now is shortcut notations to generate objects and arrays. So, in the past when we wanted to create an object, we wrote: 在过去,如果你想创建一个对象,你需要这样: 1 var car = new Object(); 2 car.colou

zepto的源代码注释(转)

/* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(function(undefined) { if (String.prototype.trim === undefined) // fix for iOS 3.2 String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''

zepto源码注解

1 /* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ 2 ;(function(undefined) { 3 if (String.prototype.trim === undefined) // fix for iOS 3.2 4 String.prototype.trim = function() { 5 return this.replace(/^\s+|