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 ((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 ((String.toUpper message)))
message
|> String.toUpper
|> String.repeat
|> 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 view: String -> Html msg
view message =
--text (String.repeat ((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 view: String -> Html msg
view message =
--text (String.repeat ((String.toUpper message)))
message
|> String.toUpper
|> \str -> str ++ ", "
|> triple
|> text

[Elm] Functions in Elm的更多相关文章

  1. RTT下spi flash+elm fat文件系统移植小记

    背景: MCU:STM32F207 SPI flash: Winbond W25Q16BV OS: RTT V1.1.1 bsp: STM32F20x 1 将spi_core.c,spi_dev.c及 ...

  2. 超限学习机 (Extreme Learning Machine, ELM) 学习笔记 (一)

    1. ELM 是什么 ELM的个人理解: 单隐层的前馈人工神经网络,特别之处在于训练权值的算法: 在单隐层的前馈神经网络中,输入层到隐藏层的权值根据某种分布随机赋予,当我们有了输入层到隐藏层的权值之后 ...

  3. [Elm] Installing and setting up Elm

    Before writing any Elm we need to first install the runtime locally. In this lesson we install the E ...

  4. zepto.js 源码解析

    http://www.runoob.com/w3cnote/zepto-js-source-analysis.html Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jqu ...

  5. zepto源码注解

    /* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...

  6. zepto源代码解读

    /** * Created by nono on 14-11-16. */ /* Zepto v1.1.4 - zepto event ajax form ie - zeptojs.com/licen ...

  7. Zepto源码注释

    /* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...

  8. Angular源代码学习笔记-原创

    时间:2014年12月15日 14:15:10 /** * @license AngularJS v1.3.0-beta.15 * (c) 2010-2014 Google, Inc. http:// ...

  9. zepto.js 源码注释备份

    /* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...

随机推荐

  1. 智能指针shared_ptr, auto_ptr, scoped_ptr, weak_ptr总结

    看这里: http://blog.csdn.net/lollipop_jin/article/details/8499530 shared_ptr可以多线程同时读,但是涉及到写,需要加锁. share ...

  2. Axios再记录

    一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端(可实现ajax的请求) 有关学习网址:https://www.tuicool.com/articles/eMb2yuY    ...

  3. mac下的词典翻译快捷键

    mac下的词典翻译快捷键:cmd+ctl+d;很方便

  4. Linux下的led驱动程序,ok6410

    本程序採用动态映射的方法控制led.硬件平台为飞凌的ok6410 led.h:定义控制命令 #ifndef _LED_H #define _LED_H #define LED_MAGIC 'M' #d ...

  5. userAgent判断客户端,以及各个浏览器的ua

    userAgent判断客户端,以及各个浏览器的ua http://blog.csdn.net/yoyoosyy/article/details/70142884 navigator.userAgent ...

  6. android图像处理系列之五--给图片添加边框(中)

    前面一篇讲到给图片加边框的方式,只能给图片加一些有规则的边框,如果想加一些比较精美的效果,就有点麻烦了.下面就给出解决这个问题的思路. 思路是:一些比较精美的花边图片我们是很难用代码控制,就目前本人水 ...

  7. 5.Node.js 安装配置

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html Node.js安装包及源码下载地址为:https://nodejs.org/en/downlo ...

  8. http的原理

          工作原理:客户机与服务器建立连接之后,发送一个请求给服务器,请求格式为统一资源标识符.协议版本号.(请求行.请求头.请求体),服务器接收请求后给予相应,包括相应行,响应头,响应体.     ...

  9. jquery ui 分页插件 传入后台的连个參数名

    參数名: page .rows page=int(request.form.get('page',1).encode('u8')) rows1=int(request.form.get('rows', ...

  10. ES6 数组、对象的扩展

    8. 数组的扩展 扩展运算符(...),将一个数组转为用逗号分隔的参数序列. 复制数组 const a2=[...a1] 合并数组 [...arr1, ...arr2, ...arr3]; arr1. ...