博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Elm学习指南
阅读量:7028 次
发布时间:2019-06-28

本文共 4630 字,大约阅读时间需要 15 分钟。

Elm基础语法

常用数据结构

列表List : 只能容纳单一类型元素,是类型的容器而不是类型,List String才是类型

元祖Tuples:可为不同类型,固定长度小于10
Records : 具有键值对的数据结构

List (列表)

list 方法List.drop 1 [1,2,3,4]   —  [1,2,3]List.length [1,2,3,4]   — 4List.head [1,2,3,4]  	— Just 1 : Maybe.Maybe numberList.tail [1,2,3,4] 	— Just [2,3,4] : Maybe.Maybe (List number)List.reverse [1,2,3,4]	— [4,3,2,1] : List number   		反序List.sort [1,4,3,2]	— [1,2,3,4] : List number   		排序 List.maximum a	    — Just 4 : Maybe.Maybe number	最大值List.minimum a		— Just 1 : Maybe.Maybe number	最小值	0::[1,2,3,4] 		— [0,1,2,3,4] : List number		“::”向列表添加元素0 [-1,-2]++[0,1,2,3,4]	— [-1,-2,0,1,2,3,4] : List number  	“++”拼接列表复制代码

tuples (元组)

records (记录)

point = { x = 3, y = 4 }                -- create a recordpoint.x                                 -- 访问字段List.map .x [point,{x=0,y=0}]           -- field access function{ point | x = 6 }                            -- update 字段{ point | x = point.x + 1, y = point.y + 1}  -- update 多个字段dist {x,y} = sqrt (x^2 + y^2)                -- pattern matching on fieldstype alias Location =    { line : Int , column : Int}    -- type aliases for records{x=3,y=7}.x  or .y {x=3,y=7} //.y为一个函数bill = {name = “cyq”, age = 20}bill2 = {bill | name = “222”}  	copy一份bill — { name = “222”, age = 20 } : { age : number, name : String }复制代码
  • 你不可以使用不存在的field.
  • field不可為 undefined 或是 null.
  • 你不可以使用 this 或 self 等關鍵字來創造遞迴的 records

模式匹配

有点类似es6的desttructuring

中缀运算符 (可自定义)

您可以创建自定义中缀运算符。 优先级从0到9,其中9是最紧密的。默认优先级为9,默认 关联性为左。您可以自行设置,但不能覆盖内置运算符。

模块

module MyModule exposing (..)-- qualified importsimport List                    -- List.map, List.foldlimport List as L               -- L.map, L.foldl-- open importsimport List exposing (..)               -- map, foldl, concat, ...import List exposing ( map, foldl )     -- map, foldl复制代码

优先进口合格。模块名称必须与其文件名匹配,因此模块Parser.Utils需要在文件中Parser/Utils.elm。

类型注释

一些常见的类型:Int,Float,String,Bool

型別別名 (type alias)

有時候,單純的基本型別可能無法完全詮釋我們想要表達的意思,也有可能是資料結構太複雜,我們想用一個簡單的名稱稱呼它,這時型別別名就可派上用場。

type alias Name = Stringtype alias Age = Inttype alias Person =    { name: String    , age: Int    }createPerson: Name -> Age -> PersoncreatePerson name age =    { name = name    , age = age    }复制代码

type alias Name = String 的意思可以翻譯為 將 Name 設定為 String 的另一個別名。如此一來,我們就可以像這樣定義: createPerson: Name -> Age -> Person 而不是 createPerson: String -> Int -> Person

自定义类型:(建立自創型別與 Union Type)

自創型別的方法如下: type Directions = Up | Down | Left | Right

type Maybe a = Nothing | Just a 我們可以創造兩種不同款式的型別,第一種像是上例的 Directions 比較直觀簡單,在這型別下只會有四種可能的值,就是我們定義的 Up,Down,Left,Right。在使用上的話我們可以這麼做:

convertDirectionsToInt: Directions -> IntconvertDirectionsToInt dir =    case dir of        Up -> 1        Down -> 2        Left -> 3        Right -> 4convertDirectionsToInt Up -- 1复制代码

Union Types

用来表示一组可能的值,每个值叫做一个Tag

指的是我們可以將其它已存在的型別組合進我們自創的型別中。範例中我們建立了一個叫 Maybe 的型別,後面跟著的 a 是所謂的 型別變數,再更後面則是型別的值。我們可以自由在型別變數中帶入其他的型別如下:

-- 把 String 帶入 amaybeString: Maybe StringmaybeString = Just "I am a string"-- 把 Int 帶入 amaybeInt: Maybe IntmaybeInt = Just 1anotherMaybeInt: Maybe IntmaybeInt = Nothing复制代码

以上三個變數都是屬於 Maybe a 型別,但隨著帶入的 a 不同,型別裡的值也會跟著改變。當然你也可以建立更複雜的 Union Type,例如: type Directions a b = Up a | Down b | Left a b | Right

javascript 互操作

  1. 在HTML中嵌入Elm,
  2. 在Elm和JavaScript之间来回发送消息

函数

multiply a b = a*bdouble = multiply 2List.map double [1,2,3,4]-- 通过匿名函数形式。List.map (\a -> a * 2) [1..4] -- [2, 4, 6, 8]复制代码

\counterMsg -> Modify id counterMsg是Elm中的匿名函数,在Elm中,匿名函数使用\开头紧接着参数, 并在->后书写返回值表达式,形如\a -> b

-- 通过let...in...语句来定义来定义一些将要立即使用的值。

volume {width, height, depth} = let area = width * height in area * depthvolume { width = 3, height = 2, depth = 7 } -- 42复制代码

函数名 参数 =

控制语句

if true then “WHOA” else if true then “n” else “0”

case alist of[]->””[x]->””x::xs->””复制代码

elm 工程化

elm-webpack-project 脚手架git clone https://github.com/moarwick/elm-webpack-starter rm -rf .git        git init   git add .   git commit -m 'first commit'npm run reinstall  安装依赖复制代码

自己搭建

----package.json "scripts": {    "elm-install": "elm-package install",    "build": "elm-make Main.elm --output=build/index.js",    "start": "elm-live Main.elm --output=build/index.js --open"   },  "devDependencies": {    "elm": "^0.18.0",    "elm-live": "^2.7.4" //热更新,热加载模块  }-----elm-package.json "dependencies": {        "elm-lang/core": "5.0.0 <= v < 6.0.0",        "elm-lang/html": "2.0.0 <= v < 3.0.0",        "elm-lang/http": "1.0.0 <= v < 2.0.0",        "evancz/elm-markdown": "3.0.1 <= v < 4.0.0"  },  "elm-version": "0.18.0 <= v < 0.19.0"-------index.html
-----npm inpm run elm-installnpm run start复制代码

官网示例库

git clone

cd elm-architecture-tutorial

学习资源

Elm入门实践系列
初识Elm语言你只需要Y分钟
Elm架构教程

转载于:https://juejin.im/post/5c91e260e51d4529fc414c9e

你可能感兴趣的文章
springboot架构下运用shiro后在configuration,通过@Value获取不到值,总是为null
查看>>
SQLServer 数据库镜像+复制切换方案
查看>>
Postman初探
查看>>
仿淘宝头像上传功能(一)——前端篇。
查看>>
Eclipse通过集成svn实现版本控制
查看>>
OS开发过程中常用开源库
查看>>
关于在多个UItextield切换焦点
查看>>
STL: HDU1004Let the Balloon Rise
查看>>
hdu 2768
查看>>
git记住用户名密码
查看>>
ElasticSearch(2)-安装ElasticSearch
查看>>
从mysql数据表中随机取出一条记录
查看>>
ORACLE 锁表处理,解锁释放session
查看>>
二.hadoop环境搭建
查看>>
深海机器人问题
查看>>
ios开发之 -- invalid nib registered for identifier
查看>>
MySQL 通过semi join 优化子查询
查看>>
socket编程-服务器端
查看>>
L181 The microscopic structure of a cat’s tongue helps keep its fur clean
查看>>
django序列化单表的4种方法的介绍
查看>>