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 入门的更多相关文章

  1. Erlang 和 Elixir 互相调用 (转)

    lixr设计目标之一就是要确保兼容性,可以兼容Erlang和其生态系统.Elixir和Erlang 都是运行同样的虚拟机平台(Erlang Virtual Machine).不管是在Erlang使用E ...

  2. Erlang 和 Elixir的差异

    原文: http://elixir-lang.org/crash-course.html 函数调用 Elixir允许你调用函数的时候省略括号, Erlang不行. Erlang Elixir some ...

  3. 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 ...

  4. CentOS 7.7安装Erlang和Elixir

    安装之前,先看一下它们的简要说明 Erlang Erlang是一种开源编程语言,用于构建对高可用性有要求的大规模可扩展的软实时系统.它通常用于电信,银行,电子商务,计算机电话和即时消息中.Erlang ...

  5. [Erlang 0113] Elixir 编译流程梳理

    注意:目前Elixir版本还不稳定,代码调整较大,本文随时失效      之前简单演示过如何从elixir ex代码生成并运行Erlang代码,下面仔细梳理一遍elixir文件的编译过程,书接上文,从 ...

  6. [Erlang 0112] Elixir Protocols

    Why Elixir   为什么要学习Elixir?答案很简单,为了更好的学习Erlang.这么无厘头的理由? Erlang语法设计几乎没有考虑过取悦开发者,所以学习之初的门槛略高.对于已经克服了最初 ...

  7. elixir 入门笔记

    安装 MAC 平台用 brew 安装 brew update brew install elixir 如果没有 erlang 环境,上面的命令会自定安装 erlang 的环境. 基本数据类型 iex& ...

  8. Erlang语言学习入门

    这是一个命令行程序,可以直接在里面输入表达式进行计算,例如来一个简单的: Erlang R15B01 (erts-5.9.1) [smp:4:4] [async-threads:0] Eshell V ...

  9. [Erlang 0114] Erlang Resources 小站 2013年7月~12月资讯合集

    Erlang Resources 小站 2013年7月~12月资讯合集,方便检索.     附 2013上半年盘点: Erlang Resources 小站 2013年1月~6月资讯合集    小站地 ...

随机推荐

  1. 小菜学习Winform(六)剪切板和拖放复制

    前言 在做winform项目的时候有时候会用到复制粘贴,在.net中提供了Clipboard类来操作剪切板,我们来看下. clipbrd.exe clipbrd是系统剪切板程序,但是在vista及以上 ...

  2. SQL Tuning 基础概述09 - SQL Access Advisor

    Oracle官方文档对SQL Access Advisor的描述如下: SQL Access Advisor, which is a tuning tool that provides advice ...

  3. Spring配置c3p0数据源时出错报:java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

    今天在使用Spring配置c3p0数据源时,使用的数据库是mysql,服务器是tomcat,运行时报了一个 java.lang.NoClassDefFoundError: com/mchange/v2 ...

  4. 读谭浩强C语言数据结构有感(1)

    1.什么是数据结构? 数据结构,就是我们计算机内部的运算,编程语言的基础工作模式吧,个人总结的 = = !! 数据:说简单一点,就是计算机二进制机器码,然后通过一些复杂的操作,变为复杂的语言. 数据元 ...

  5. HTTP首部

    前面有几篇博文介绍了HTTP协议.HTTP请求方法详解.Javascript中Cookie的那些事儿.HTTPS,今天我们来聊一聊关于HTTP首部的那些事儿 HTTP协议的请求和响应报文中肯定包含HT ...

  6. Ubuntu实现树莓派交叉编译

    一.交叉编译 在一个平台上生成另一个平台上的可执行代码.为什么要大费周折的进行交叉编译呢?一句话:不得已而为之.有时是因为目的平台上不允许或不能够安装所需要的编译器,而又需要这个编译器的某些特征:有时 ...

  7. Nancy之区域和分部视图的使用

    一.前言 在MVC中,区域(Area)和分部视图(PartialView)应该是我们用的十分频繁的两个东西 今天我们就在Nancy中,把这两个东西简单的用起来!   二.简单使用之区域 区域,无论是对 ...

  8. C# GDI+ 处理文本的两个小技巧

    private void button7_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); g.FillR ...

  9. MVC学习系列——Filter扩展

    在MVC中,Filter也是可以扩展的.在此,本人对Filter的理解就是AOP,不知道各位大侠,有什么高的见解,呵呵... 首先MVC四大过滤神器IAuthorizationFilter,IActi ...

  10. tomcat启动时候报错Can't convert argument: null

    一.启动报错: 为了避免导入的项目重名,我先修改了前一个项目的名称. 重新启动该项目至tomcat,报错:java.lang.IllegalArgumentException: Cant conver ...