[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 ...
随机推荐
- 漫话Unity(二)
三.Unity编辑器介绍 Unity是一个商业级的3d游戏引擎.一个引擎的专业程度事实上并非体如今它多么牛b 的次世代效果,说实话那些效果即便你会用也不敢用.由于没有哪个手机是次世代的. 游戏引擎的专 ...
- vue.js有什么用,是用来做什么的(整理)
vue.js有什么用,是用来做什么的(整理) 一.总结 一句话总结:用数据绑定的思想,vue可以简单写单个页面,也可以写一个大的前端系统,也可以做手机app的界面. 1.Vue.js是什么? 渐进式框 ...
- PHP glob() 函数详解
PHP glob() 函数详解 一.总结 glob()作用:glob() 函数返回匹配指定模式的文件名或目录. glob()返回值:该函数返回一个包含有匹配文件 / 目录的数组.如果出错返回 fals ...
- 如果把父组件的数据实时的传递到子组件:用watch
1.在子组件使用watch来监听传递给子组件的数据,然后更新子组件的数据. 2.watch和computed结合使用效果非常好. 参考链接:https://blog.csdn.net/zhouweix ...
- (转)修改 ubuntu 默认启动项
转自: http://jingyan.baidu.com/article/afd8f4de58959134e386e969.html 当我们安装windows和ubuntu双系统以后,默认启动变成ub ...
- 7.Linux 输入子系统分析
为什么要引入输入子系统? 在前面我们写了一些简单的字符设备的驱动程序,我们是怎么样打开一个设备并操作的呢? 一般都是在执行应用程序时,open一个特定的设备文件,如:/dev/buttons .... ...
- 【习题 7-6 UVA - 12113】Overlapping Squares
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先预处理出来一个正方形. 然后每次枚举新加的正方形左上角的坐标就可以. 注意覆盖的规则,控制一下就可以. 然后暴力判断是否相同. 暴 ...
- install-软件安装跟push的区别
今天在做项目的时候,需要往一个user版本的手机中安装一个应用,就在网上查了相应的方法,可以使用如下命令 adb install -r out/target/product/vanzo6752_lwt ...
- elasticsearch 源码概述
从功能上说,可以分为两部分,分布式功能和数据功能.分布式功能主要是节点集群及集群附属功能如restful借口.集群性能检测功能等,数据功能主要是索引和搜索.代码上这些功能并不是完全独立,而是由相互交叉 ...
- Weblogic问题汇总
1. weblogic unable to get file lock问题 在项目使用过程中,非正常结束Weblogic进程导致Weblogic无法启动,出现以下错误: <BEA-141281& ...