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. LeetCode_ZigZag Conversion

    一.题目 ZigZag Conversion Total Accepted: 31399 Total Submissions: 140315My Submissions The string &quo ...

  2. 【AIM Tech Round 4 (Div. 2) A】Diversity

    [链接]http://codeforces.com/contest/844/problem/A [题意] 大水题 [题解] 看看不同的个数num是不是小于k,小于k,看看len-num够不够补的 [错 ...

  3. hibernate中的事务管理是怎么概念?

    1.JDBC事务 JDBC 事务是用 Connection 对象控制的.JDBC Connection 接口( java.sql.Connection )提供了两种事务模式:自动提交和手工提交. ja ...

  4. 86.八千万qq密码按相似度排序并统计密码出现次数,生成密码库

    存储qq的文件地址以及按照密码相似度排序的文件地址 //存储qq的文件的地址 ] = "QQ.txt"; //按照密码相似度排序的文件地址 ] = "QQpassword ...

  5. css实现背景半透明文字不透明的效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. [ACM] POJ 1046 Color Me Less

    Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30146   Accepted: 14634 D ...

  7. eclipse 使用jetty调试时,加依赖工程的源码调试方法

    [1] 添加source eclipse-->debug as-->debug configurations-->source [2]若source不起作用 重新编译一下,mvn c ...

  8. Java判断1个字符串中出现了几次其他字符串

    public class Test { public static int count(String text,String sub){ , start =; ){ start += sub.leng ...

  9. PyCharm下载主题以及个性化设置(详细)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一.下载主题 1.在http://www.themesmap.com/theme.html上选择自己喜欢的主题点进去后进行下载. ...

  10. 驱动学习3-make

    在向内核中添加驱动的时候要完成3项工作 (1)在Kconfig中添加新代码对应项目的编译条件(下面Makefile文件中需要用到它定义的的宏变量) (2)将驱动源码添加到对应的目录中 (3)在目录Ma ...