[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 ((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的更多相关文章
- 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及 ...
- 超限学习机 (Extreme Learning Machine, ELM) 学习笔记 (一)
1. ELM 是什么 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 E ...
- zepto.js 源码解析
http://www.runoob.com/w3cnote/zepto-js-source-analysis.html Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jqu ...
- zepto源码注解
/* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...
- zepto源代码解读
/** * Created by nono on 14-11-16. */ /* Zepto v1.1.4 - zepto event ajax form ie - zeptojs.com/licen ...
- Zepto源码注释
/* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...
- Angular源代码学习笔记-原创
时间:2014年12月15日 14:15:10 /** * @license AngularJS v1.3.0-beta.15 * (c) 2010-2014 Google, Inc. http:// ...
- zepto.js 源码注释备份
/* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...
随机推荐
- Maven学习笔记5
Web项目的部署: web部署 配置步骤 生成项目方式不是quickstart,而是webapp. 默认目录结构,需要修改配置. 重新配置project facets和java compiler.并重 ...
- 配置mysql的ODBC数据源
如果你已经安装好了mysql和mysql连接驱动,则可以向下进行了 打开控制面板,以小图标的形式查看,找到管理工具 打开管理工具,找到数据源(odbc),打开 在图片中所圈出的三个标签中随便选一个,点 ...
- 地图上显示div点位
功能核心: 地理坐标转屏幕坐标 用到的插件:jquery animo动画插件 核心代码: var point = new Point(lon, lat, map.spatialReference) ...
- zookeeper 配置文件说明(zoo.cfg)
clientPort # 客户端连接server的port,即对外服务port,一般设置为2181. dataDir # 存储快照文件snapshot的文件夹. 默认情况下.事 ...
- actionbar-displayOptions 属性分析
displayOptions 这个属性主要是控制这actionbar 上返回按钮.标题等的显示.它作为 actionBarStyle 的一个item,如下 <style name="A ...
- Linux下读写寄存器
arm裸机下读写寄存器很容易,各个寄存器和内存的地址是单一地址空间,他们是用相同的指令进行读写操作的.而在linux下就要复杂很多,因为linux支持多个体系架构的CPU.比如arm和x86就不一样, ...
- 1.2 Use Cases中 Stream Processing官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Stream Processing 流处理 Many users of Kafka ...
- 洛谷 P2430 严酷的训练
P2430 严酷的训练 题目背景 Lj的朋友WKY是一名神奇的少年,在同龄人之中有着极高的地位... 题目描述 他的老师老王对他的程序水平赞叹不已,于是下决心培养这名小子. 老王的训练方式很奇怪,他会 ...
- LoadRunner--HTML与URL录制方式区别
Recording录制选项 这里提供了两个大类的录制方式: 1. HTML-based script基于HTML的脚本 这种方式录制出来的脚本是基于HTML基础的,为每个用户操作生成单独的步骤,这种脚 ...
- unmapping error
否则,会映射一个Getch的器件,就会报unmapping 的error