[Erlang 0108] Elixir 入门
Erlang Resources里面关于Elixir的资料越来越多,加上Joe Armstrong的这篇文章,对Elixir的兴趣也越来越浓厚,投入零散时间学习了一下.零零散散,测试代码写了一些,Evernote中笔记更是混乱,还是逐步整理出来.
Elixir is a functional, meta-programming aware language built on top of the Erlang VM. It is a dynamic language with flexible syntax and macro support that leverages Erlang's abilities to build concurrent, distributed and fault-tolerant applications with hot code upgrades.
Git首页 https://github.com/elixir-lang/elixir
Elixir官网 http://elixir-lang.org/
各种环境安装Elixir
需要Erlang R16B或更新版本,安装Elixir参考下面的列表.之前有人问是否可以在windows中使用Elixir,下面列表中包含了Chocolatey安装的方案:
Homebrew for Mac OS X
Update your homebrew to latest with brew update
Install Elixir: brew install elixir
Fedora 17+ and Fedora Rawhide
sudo yum -y install elixir
Arch Linux (on AUR)
yaourt -S elixir
openSUSE (and SLES 11 SP3+)
Add Erlang devel repo with zypper ar -f obs://devel:languages:erlang/ erlang
Install Elixir: zypper in elixir
Gentoo
emerge --ask dev-lang/elixir
Chocolatey for Windows
cinst elixir
源码编译
如果是从Git获取源代码编译使用
$ git clone https://github.com/elixir-lang/elixir.git
$ cd elixir
$ make test
好吧,Windows
虽然不建议在Windows中搞Erlang相关的东西,但考虑到还是有很多小伙伴习惯在Windows中做开发,还是尝试一下在Windows环境中安装Elixir.我们使用的工具是Chocolatey,它依附于Windows PowerShell的软件库工具,官网有一键安装的批处理命令.安装Chocolatey之后,直接执行cinst elixir即可完成elixir及其依赖库的安装以及环境变量的配置,安装目录在C:\tools\Elixir ,不过进入bin目录执行脚本会闪退,把错误重定向出来如下
c:\tools\Elixir\bin>iex
'ETLOCAL' is not recognized as an internal or external command,
operable program or batch file.
'et' is not recognized as an internal or external command,
operable program or batch file.
'or' is not recognized as an internal or external command,
operable program or batch file.
'f' is not recognized as an internal or external command,
operable program or batch file.
Usage: elixir.bat [options] [.exs file] [data]
'cho.' is not recognized as an internal or external command,
这是因为脚本解析错误,解决方法很简单:打开elixir.bat脚本用正则在每行的头添加个空格即可.修改后运行:
Interactive Elixir (0.10.) - press Ctrl+C to exit (type h() ENTER for help)
iex()>
我后续的测试都将在Centos中完成,不再考虑Windows环境的情况.
开发环境
还用说,Sublime Text通过Package Control安装Elixir插件即可,看截图
iex
在Erlang学习过程中有一个强大的REPL工具EShell,绝大部分的测试和调试都可以在EShell中完成.Elixir同样提供了一个这样的工具iex,而且更为强大(比如可以在Shell中可以定义module).
熟悉一下iex,h()可以看到一些快捷的方法,比如查看一个方法的文档,清屏什么的,看下面的例子
iex()> h()
# IEx.Helpers Welcome to Interactive Elixir. You are currently
seeing the documentation for the module `IEx.Helpers`
which provides many helpers to make Elixir's shell
more joyful to work with. This message was triggered by invoking the helper
`h()`, usually referred to as `h/` (since it expects
arguments). There are many other helpers available: * `c/` — compiles a file at the given path
* `cd/` — changes the current directory
* `clear/` — clears the screen
* `flush/` — flushes all messages sent to the shell
* `h/` — prints this help message
* `h/` — prints help for the given module, function or macro
* `l/` — loads the given module's beam code and purges the current version
* `ls/` — lists the contents of the current directory
* `ls/` — lists the contents of the specified directory
* `m/` — prints loaded modules
* `pwd/` — prints the current working directory
* `r/` — recompile and reload all modules that were previously reloaded
* `r/` — recompiles and reloads the given module's source file
* `s/` — prints spec information
* `t/` — prints type information
* `v/` — prints the history of commands evaluated in the session
* `v/` — retrieves the nth value from the history
* `import_file/`
— evaluates the given file in the shell's context Help for functions in this module can be consulted
directly from the command line, as an example, try: h(c/) You can also retrieve the documentation for any module
or function. Try these: h(Enum)
h(Enum.reverse/) To learn more about IEx as a whole, just type `h(IEx)`.
iex()> h(size)
* def size(arg) Returns the size of the given argument, which must be a tuple
or a binary. If possible, please use `tuple_size` or `byte_size`. iex()> h(length)
* def length(list) Returns the length of `list`. Allowed in guard tests. ## Examples iex> length([, , , , , , , , ]) iex()>
如何退出iex? Ctrl+c
然后 a
基本数据类型
iex> # integer
iex> 0x1F # integer
iex> 1.0 # float
iex> :atom # atom / symbol
iex> {,,} # tuple
iex> [,,] # list
iex> <<,,>> # bitstring
注意上面:atom这种风格和Ruby是一致的,且在Ruby中这种数据类型就是 symbol.作为一种为了取悦开发者而生的开发语言,Ruby提供了很多让开发者爽到的便利,Elixir从Ruby偷师不少,后续讨论中可以看到.
iex()> 0x1F iex()>
iex()> size {,,} iex()> length [,,,]
字符串!字符串!
先看一个取长度的测试:
iex()> length("hello")
** (ArgumentError) argument error
:erlang.length("hello")
erl_eval.erl:: :erl_eval.do_apply/
src/elixir.erl:: :elixir.eval_forms/ iex()> size("hello") iex()>
细心的你一定发现了上面列举基本数据类型的时候没有提到string.上面用到了length和size,两者的区别是什么?length适用于需要遍历计算才能得到长度的场景,Size用于长度已经预计算的情况.换句话说,"hello"是长度是预计算出来的,没有遍历,换句话说,"hello"在Erlang中是list在Elixir中不是!那string到底是什么呢?Elixir还有单引号形式的字符串,这两种又有什么区别?抓狂了吧,其实string只不过是一堆排列在一起的字节而已,而二进制数据只不过是一串数据位而已.看测试:
iex()> is_binary('zen')
false
iex()> is_list('zen')
true
iex()>
nil
iex()> is_binary("zen")
true
iex()> is_list("zen")
false
iex()> <<"zen">> == "zen"
true
iex()> <<'zen'>> == "zen"
true
iex()> <<'zen'>> == 'zen'
false
iex()> <<,,>> == "zen"
true
深究一下size和length,我们打开elixir-master\lib\elixir\lib \kernel.ex文件,可以找到下面的代码
@doc """
Returns the size of the given argument, which must be a tuple
or a binary. If possible, please use `tuple_size` or `byte_size`.
"""
@spec size(tuple|binary) :: non_neg_integer
def size(arg) do
:erlang.size(arg)
end @doc """
Returns the length of `list`. Allowed in guard tests. ## Examples iex> length([1, 2, 3, 4, 5, 6, 7, 8, 9])
9
"""
@spec length(list) :: non_neg_integer
def length(list) do
:erlang.length(list)
end
在Elixir中双引号和单引号表示的字符串是不同的,看上面的例子, "zen", <<"zen">>,<<'zen'>>都是 <<122,101,110>>数据的语法糖;而'zen'本质上是char list,看下面的代码:
iex()> [,,] == "zen"
false
iex()> [,,] == 'zen'
true
iex()>
提到string就不可避免提到对unicode的支持,Elixir的基础是Erlang R16B01,所以支持unicode不费力,我们先从一
在Erlang中取ASCII的快捷运算符 $a 在Elixir中使用的是"?",不过这个是增强版的,它可以字符的Codepoint
iex()> ?a iex()> [?a,?b,?c]
'abc'
iex()> [?a,?b,?c,]
[, , , ]
iex()> ?开 iex()> ?心 iex()> String.codepoints "a我b们c开心"
["a", "我", "b", "们", "c", "开", "心"] iex()> <<content::utf8,restcontent::binary >>="我们很开心abc"
"我们很开心abc"
iex()> content iex()> ?我 iex()> restcontent
"们很开心abc" iex()> is_bitstring("hello开心")
true
iex()> bit_size("hello开心") iex()> bit_size("开心") iex()> size("wo们")
之前我们分析过Erlang字符串截断的例子,里面提到了"开心"这两个汉字都是三字节模板,所以这里的bit_size是48,"wo们"是两个单字节加上一个三字节所以是5.
继续看Elixir字符串处理还有哪些特色:
字符串连接:
iex(74)> "foo" <> "bar"
"foobar"
字符串占位符:
iex()> name="zen"
"zen"
iex()> "hello,I am #{name}"
"hello,I am zen"
iex()>
Ruby里面把上面占位替换成为表达式替换"Expression Substitution",看Ruby的例子
x, y, z = 12, 36, 72
puts "The value of x is #{ x }.
puts "The sum of x and y is #{ x + y }.
puts "The average was #{ (x + y + z)/3 }."
更多字符串操作 http://elixir-lang.org/docs/stable/String.html
先到这里,小图一张 小黄人 最近它们很火:
[Erlang 0108] Elixir 入门的更多相关文章
- Erlang 和 Elixir 互相调用 (转)
lixr设计目标之一就是要确保兼容性,可以兼容Erlang和其生态系统.Elixir和Erlang 都是运行同样的虚拟机平台(Erlang Virtual Machine).不管是在Erlang使用E ...
- Erlang 和 Elixir的差异
原文: http://elixir-lang.org/crash-course.html 函数调用 Elixir允许你调用函数的时候省略括号, Erlang不行. Erlang Elixir some ...
- Install Erlang and Elixir in CentOS 7
In this tutorial, we will be discussing about how to install Erlang and Elixir in CentOS 7 minimal s ...
- CentOS 7.7安装Erlang和Elixir
安装之前,先看一下它们的简要说明 Erlang Erlang是一种开源编程语言,用于构建对高可用性有要求的大规模可扩展的软实时系统.它通常用于电信,银行,电子商务,计算机电话和即时消息中.Erlang ...
- [Erlang 0113] Elixir 编译流程梳理
注意:目前Elixir版本还不稳定,代码调整较大,本文随时失效 之前简单演示过如何从elixir ex代码生成并运行Erlang代码,下面仔细梳理一遍elixir文件的编译过程,书接上文,从 ...
- [Erlang 0112] Elixir Protocols
Why Elixir 为什么要学习Elixir?答案很简单,为了更好的学习Erlang.这么无厘头的理由? Erlang语法设计几乎没有考虑过取悦开发者,所以学习之初的门槛略高.对于已经克服了最初 ...
- elixir 入门笔记
安装 MAC 平台用 brew 安装 brew update brew install elixir 如果没有 erlang 环境,上面的命令会自定安装 erlang 的环境. 基本数据类型 iex& ...
- Erlang语言学习入门
这是一个命令行程序,可以直接在里面输入表达式进行计算,例如来一个简单的: Erlang R15B01 (erts-5.9.1) [smp:4:4] [async-threads:0] Eshell V ...
- [Erlang 0114] Erlang Resources 小站 2013年7月~12月资讯合集
Erlang Resources 小站 2013年7月~12月资讯合集,方便检索. 附 2013上半年盘点: Erlang Resources 小站 2013年1月~6月资讯合集 小站地 ...
随机推荐
- 升级 Visual Studio 2015 CTP 5 的坑、坑、坑
前两天,微软发布了 Visual Studio 2015 CTP 5,全称为 Visual Studio 2015 Community Technology Preview 5,意为社区技术预览版,之 ...
- Cookbook of QUnit
本篇文章是QUnit的简介,可以作为很好的入门教程.文章原址 介绍 自动化测试时软件开发过程中必不可少的一部分,而单元测试则是自动化测试的最为基本的一块,软件的每一个组件, 每一个功能单元都需要经过不 ...
- 用JqueryUI的Dialog+IFrame实现仿模态窗口效果
大家有没有想过这样一个问题,当我点击某个图片的时候,我想弹出这个图片信息的详情并修改,于是你首先想到的是不是window.open?window.open方法确实可以,但是有它的局限性,比如,标题显示 ...
- 基于 Hive 的文件格式:RCFile 简介及其应用
转载自:https://my.oschina.net/leejun2005/blog/280896 Hadoop 作为MR 的开源实现,一直以动态运行解析文件格式并获得比MPP数据库快上几倍的装载速度 ...
- OpenCV2:Mat属性type,depth,step
在OpenCV2中Mat类无疑使占据着核心地位的,前段时间初学OpenCV2时对Mat类有了个初步的了解,见OpenCV2:Mat初学.这几天试着用OpenCV2实现了图像缩小的两种算法:基于等间隔采 ...
- 微信小程序,我的英雄列表
最近微信小程序炒得火热,就跟成都的这个房价一样.昨天我也尝试了一下,做了一个自己的英雄列表.今天将自己的制作过程记录于此. 1.下载微信开发者工具 官网链接:https://mp.weixin.qq. ...
- jquery如何判断checkbox(复选框)是否被选中 ...
<script type="text/javascript" src="../lib/jQuery1.11.1.js"></script> ...
- .NET正则表达式基础入门(三)
括号 正则表达式中的括号能将多个字符或者表达式当做一组,即将他们看成一个整体.这样量词就可以修饰这一组表达式.阅读本章前,建议先下载我于CSDN上传的示例代码,下载无需分数,下载链接. 1.分组 假设 ...
- html5跟随鼠标炫酷网站引导页动画特效
html5跟随鼠标炫酷网站引导页动画特效一款非常不错的引导页,文字效果渐变,鼠标跟随出绚丽的条纹.html5炫酷网站引导页,鼠标跟随出特效. 体验效果:http://hovertree.com/tex ...
- 25、ASP.NET MVC入门到精通——Spring.net-业务层仓储
本系列目录:ASP.NET MVC4入门到精通系列目录汇总 上一节,我们已经把项目框架的雏形搭建好了,那么现在我来开始业务实现,在业务实现的过程当中,不断的来完善我们现有的框架. 1.假设我们来做一个 ...