Elixir

Elixir是一个基于Erlang虚拟机函数式、面向并行的通用编程语言。Elixir以Erlang为基础,支持分布式、高容错、实时应用程序的开发,亦可通过实现元编程对其进行扩展,并通过协议支持多态[1]

Elixir
多范式函数式并行式面向进程同像性
2012年
穩定版本
1.9.0
( 2019年6月24日 (2019-06-24)
型態系統动态类型强类型
許可證Apache License
文件扩展名.ex、.exs
網站elixir-lang.org
啟發語言
ClojureErlangRuby

历史

José Valim是Elixir语言的设计者。他创造该语言的目标是在维持与现有Erlang工具链及生态环境兼容性的同时,让人们可以在Erlang虚拟机上进行扩展性更好的、高生产率的开发。[2]

特性

示例

以下示例可以在iex shell中运行或保存在文件中,并通过命令行键入命令运行 elixir <filename>.

经典的 Hello world 例子:

iex> IO.puts("Hello World!")
Hello World!

Enumerable 推导

iex> for n <- [1,2,3,4,5], rem(n, 2) == 1, do: n*n
[1, 9, 25]

模式匹配(解构)

iex> [1, a] = [1, 2]
iex> a
2

iex> {:ok, [hello: a]} = {:ok, [hello: "world"]}
iex> a
"world"

模式匹配(多子句)

iex> case File.read("path/to/file") do
iex>   {:ok, contents} -> IO.puts("found file: #{contents}")
iex>   {:error, reason} -> IO.puts("missing file: #{reason}")
iex> end

管道操作符

iex> "1" |> String.to_integer() |> Kernel.*(2)
2

模块

defmodule Fun do
  def fib(0), do: 0
  def fib(1), do: 1
  def fib(n), do: fib(n-2) + fib(n-1)  
end

顺序产生1000个进程

for num <- 1..1000, do: spawn fn -> IO.puts("#{num * 2}") end

执行异步任务

task = Task.async fn -> perform_complex_action() end
other_time_consuming_action()
Task.await task

参考资料

  1. . José Valim. [2013-02-17]. (原始内容存档于2017-09-30).
  2. . [2013-02-17]. (原始内容存档于2012-11-29).

外部链接

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.