zoukankan      html  css  js  c++  java
  • 基于Erlang VM的函数式编程语言Elixir

    Elixir官网:http://elixir-lang.org/

    Elixir是一种函数式动态语言,用于构建可伸缩、易维护的应用程序。

    Elixir是基于Erlang VM的,其广为人知的特点是运行低延时、分布式、可容错的系统,并成功用于Web开发与嵌入式软件领域。

    1. 平台特性

    1.1 可伸缩

    所有Elixir代码运行于相互隔离的轻量级线程中,相互间通过message通信:

    parent = self()
    
    # Spawns an Elixir process (not an operating system one!)
    spawn_link(fn ->
      send parent, {:msg, "hello world"}
    end)
    
    # Block until the message is received
    receive do
      {:msg, contents} -> IO.puts contents
    end
    

    1.2 容错性

    关于软件运行在产品环境的一个无法避开的真相:总是会出错,加上网络、文件系统及其他第三方资源的问题会更多。

    为了应对各种失误,Elixir提供了supervisor - 描述当事情变得扭曲时如何重启你的系统的某些部分,返回已知的能保证正常运行的初始状态:

    import Supervisor.Spec
    
    children = [
      supervisor(TCP.Pool, []),
      worker(TCP.Acceptor, [4040])
    ]
    
    Supervisor.start_link(children, strategy: :one_for_one)
    

    2. 语言特性

    2.1 函数式编程

    函数式编程提升了编码风格、帮助开发者写出简短、快速及可维护的代码。例如,模式匹配允许开发者轻易就能结构数据并访问它的内容:

    %User{name: name, age: age} = User.get("John Doe")
    name #=> "John Doe"
    

    当你想加入guard时,模式匹配允许我们优雅的匹配与声明某些代码执行的特定条件:

    def serve_drinks(%User{age: age}) when age >= 21 do
      # Code that serves drinks!
    end
    
    serve_drinks User.get("John Doe")
    #=> Fails if the user is under 21
    

    Elixir强烈依赖那些特性,来确保你的软件运行于预期的约束下。如果没有,也别担心,supervisors会帮你擦屁股。

    2.2 可扩展能力及领域编程语言(DSL)特性支持

    Elixir被设计成了可扩展的语言,让开发者自然的扩展语言到特定领域,以便增加他们的生产力。

    举个例子,让我们使用Elixir的测试框架-ExUnit来写个简单的测试用例:

    defmodule MathTest do
      use ExUnit.Case, async: true
    
      test "can add two numbers" do
        assert 1 + 1 == 2
      end
    end
    

    async: true 选项允许测试运行在并行模式,尽可能使用更多的CPU核,同时断言函数可以内省(introspect)你的代码,提供失败情况下的强大报告。那些特性是用Elixir的宏来构建的,让其可以添加新的结构并作为语言本身的一部分存在。

    3. 工具

    3.1 发展中的生态系统

    Elixir发布了整套工具来简化开发。Mix是一套build工具,它让你很简单的就能创建项目、管理任务、运行测试以及更多:

    $ mix new my_app
    $ cd my_app
    $ mix test
    .
    
    Finished in 0.04 seconds (0.04s on load, 0.00s on tests)
    1 tests, 0 failures
    

    Mix也能管理依赖 - 通过与Hex包管理器整合来实现,其提供了依赖解决、远程抓取包的功能。

    3.2 交互式开发

    像IEx (Elixir的交互式shell)能提供语言和平台各方面的特性,如自动完成、调试工具、代码重新载入、良好格式化的文档:

    $ iex
    Interactive Elixir - press Ctrl+C to exit (type h() ENTER for help)
    iex> c "my_file.ex"        # Compiles a file
    iex> t Enum                # Prints types defined in the module Enum
    iex> h IEx.pry             # Prints the documentation for IEx pry functionality
    

    3.3 兼容Erlang

    Elixir运行于Erlang VM,给开发者提供了Erlang生态系统的完整访问能力,Heroku, Whatsapp, Klarna, Basho等等很多公司都在使用它来构建分布式、高容错的应用程序。Elixir程序员可以调用Erlang函数并且没有任何运行时开销:

    iex> :crypto.md5("Using crypto from Erlang OTP")
    <<192,223,75,115,...>>
    
  • 相关阅读:
    XML中对于一个books.xml的详情显示,删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能(XmlDocument)。
    如何写一个验证码
    Binary Search
    数据库排行榜
    mac os 下 sublime text 2 和 iterm2 便捷配置
    HttpGet,HttpPost,HttpPut,HttpDelete
    Compile C/C++ In Eclipse for Android
    To Use EGit(Git for Eclipse)
    Android NDK about Library (static library , share library and 3rd party library)
    Dealing with bitmap object in android NDK
  • 原文地址:https://www.cnblogs.com/x3d/p/elixir-lang-intro.html
Copyright © 2011-2022 走看看