标签: elixir

Elixir引用记录(将其转换为元组)并保留数据?

使用quote do:记录引用时不会转换为包含记录字段的元组:

 iex(1)> quote do: is_bitstring("blah")
 {:is_bitstring, [context: Elixir, import: Kernel], ["blah"]}
 iex(2)> quote do: Computer.new("Test")
 {{:., [], [{:__aliases__, [alias: false], [:Computer]}, :new]}, [], [[name: "Test"]]}
 iex(3)> quote do: Computer.new("Test")
 {{:., [], [{:__aliases__, [alias: false], [:Computer]}, :new]}, [], [[name: "Test"]]}
 iex(4)> c = Computer.new("Test")
 Computer[name: "Test", type: nil, processor: nil, hard_drives: []]
 iex(5)> c
 Computer[name: "Test", type: nil, processor: nil, hard_drives: []]
 iex(6)> quote do: c
 {:c, [], Elixir}
Run Code Online (Sandbox Code Playgroud)

此外,当我尝试在我的代码中执行此操作时:

defmacro computer([do: code]) do
  # macro login here …
Run Code Online (Sandbox Code Playgroud)

macros erlang record elixir quote

1
推荐指数
1
解决办法
950
查看次数

Erlang中的Elixir协议和一个奇怪的警告

我想((IStringer)object).ToString()在Erlang中写一些(在C#中).经过一番研究后,我了解到Elixir有一种称为Protocols的东西,它几乎与C#相同(以内向外的方式).然后我在Erlang中提出了这个想法/代码 - 这对我来说非常好:

?stringer(my_val):to_string().
Run Code Online (Sandbox Code Playgroud)

它要么返回预期值,要么返回not_implemented原子!

但有2个问题:

1 - 为什么没有人在Erlang中使用这个或基于有状态模块来推广事物?(OTP除了和一些Erlangers谈话,他们不知道实际上OTP是围绕着这个建立的!所以真的需要改变Erlang的教学和推广方式.我可能会感到困惑.)

2 - 为什么我收到这个警告?那个电话实际上永远不会失败.

警告:

stringer.erl:18: Warning: invalid module and/or function name; this call will always fail
stringer.erl:19: Warning: invalid module and/or function name; this call will always fail
stringer.erl:20: Warning: invalid module and/or function name; this call will always fail
Run Code Online (Sandbox Code Playgroud)

代码:

-module(stringer).
-export([to_string/1,sample/0]).

-define(stringer(V), {stringer, V}).

to_string({stringer, V}) when is_list(V) ->
    to_string(V, nop);
to_string({stringer, V}) when is_atom(V) ->
    to_string(V, nop);
to_string({stringer, _V}) ->
    not_implemented.

to_string(V, _Nop) -> …
Run Code Online (Sandbox Code Playgroud)

erlang protocols interface elixir

1
推荐指数
1
解决办法
574
查看次数

Miguel Camba教程Elixir快捷功能错误

我正在关注Elixir lang的Miguel Camba教程.

在我尝试使用以下代码段之前一​​切正常:

get "/play/:story_name" do
    conn = conn.resp_content_type("text/event-stream")
    conn = conn.send_chunked(200)

    iterator = File.iterator!("#{conn.params[:story_name]}.txt")

    Enum.each iterator, fn(line) ->
      { :ok, conn } = conn.chunk "data: #{line}\n"
      await conn, 1000, on_wake_up(&1, &2), on_time_out(&1)
    end
    conn
  end

  defp on_wake_up(arg1, arg2) do
    # Nothing
  end

  defp on_time_out(arg1) do
    # Nothing
  end

我尝试过以下方法:

await conn, 1000, on_wake_up&(&1, &2), on_time_out&(&1)
await conn, 1000, on_wake_up(&(&1, &2)), on_time_out(&(&1))
await conn, 1000, on_wake_up(), on_time_out()
   end
    conn
  end

  defp on_wake_up() do
    # Nothing …

erlang elixir-mix elixir

1
推荐指数
1
解决办法
154
查看次数

你是如何做这项工作的?为什么它不起作用?

我试过这个:

iex(7)> String.split ("hello world") |> String.upcase |> Enum.join(" // ")

我得到了这个:

** (Protocol.UndefinedError) protocol Enumerable not implemented for "HELLO WORLD"
(elixir) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) lib/enum.ex:112: Enumerable.reduce/3
(elixir) lib/enum.ex:1124: Enum.reduce/3
Run Code Online (Sandbox Code Playgroud)

有谁知道请告诉我为什么这不起作用?!

elixir

1
推荐指数
1
解决办法
361
查看次数

Elixir Guard特定的协议

我想知道Elixir是否有可能保护特定的协议.

def some_fun(f) when implement?(f, Dict.Behaviour), do: ...
Run Code Online (Sandbox Code Playgroud)

或者有什么可以断言f特别是HashDict吗?

谢谢 !

elixir

1
推荐指数
1
解决办法
296
查看次数

在Elixir的混音中有类似test_requires的东西吗?

在Python的setup.py中,我们可以通过在test_requires需求下指定来指定应下载并作为egg安装的依赖项.在Elixir的混合中,如何指定我只想用于测试的依赖项?

elixir

1
推荐指数
1
解决办法
472
查看次数

Elixir中的Code.compile_string和Code.eval

我想在Elixir中运行时使用包含模块代码的字符串创建一个模块.在这个问题中,有人说过Code.compile_string和Code.eval方法.他们不支持Elixir吗?我在使用它时遇到编译错误.

** (UndefinedFunctionError) undefined function: Code.eval/2
Run Code Online (Sandbox Code Playgroud)

erlang elixir

1
推荐指数
1
解决办法
2902
查看次数

在Elixir中设置gen_tcp的参数

我正在尝试使用Erlang通过Elixir中的TCP连接接受数据 gen_tcp

{:ok, socket} = :gen_tcp.connect("127.0.0.1" ,2000,[:binary, {:packet, 0}])
receive_data(Socket, [])
Run Code Online (Sandbox Code Playgroud)

但由于不好的论点,这不起作用connect.如何在Elixir中设置二进制和数据包值?

这里解释了参数含义.但我需要支持在Elixir中设置参数.

erlang tcp elixir erlang-otp

1
推荐指数
1
解决办法
1090
查看次数

无法用凤凰创建Elixir应用程序

我试图用凤凰框架创建一个Web应用程序Elixir,我正在关注github的官方教程(https://github.com/phoenixframework/phoenix)

当我执行命令来创建应用程序时,我收到一条错误消息.我无法理解我在这里缺少的东西.你能帮帮我吗?

e.g.
  mix phoenix.new photo_blog /home/johndoe/

mr.black :) > mix phoenix.new photo_blog /home/johndoe/
* creating /home/johndoe/photo_blog
** (UndefinedFunctionError) undefined function: Path.wildcard/2
    (elixir) Path.wildcard("/home/mrblack/projects/eli/phoenix/template/**/*", [match_dot: true])
    lib/mix/tasks/phoenix/new.ex:21: Mix.Tasks.Phoenix.New.run/1
    (mix) lib/mix/cli.ex:66: Mix.CLI.run_task/2
    (elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/2
    (elixir) lib/code.ex:296: Code.require_file/2
Run Code Online (Sandbox Code Playgroud)

我在系统中使用的Elixir和Erlang版本

mr.black :) > iex -v
Erlang/OTP 17 [erts-6.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Elixir 0.14.0
Run Code Online (Sandbox Code Playgroud)

elixir phoenix-framework

1
推荐指数
1
解决办法
614
查看次数

如何检查Elixir中XML的格式是否正确

我收到的XML文件可能格式不正确,在这种情况下,我需要忽略它们。

我正在使用包装Xmerl的SweetXml。

我有一个格式错误的XML实例,它的两个属性之间没有空格。

没有is_well_formed函数-具有简单布尔响应的函数会很棒。

Xmerl尝试解析文件,但不喜欢它,因此发送出口。

我还没有了解主管,但是在我看来,这对他们来说是一个案例。

是否有新手或简单的方法来处理退出信号?

defmodule XmlIsWellFormed.WellFormed do
  def is_well_formed(xml) do
    import SweetXml
    xml_string = to_string xml
    result = xml_string |> parse # parse sends exit.

    # FYI - SweetXml.parse :
    # def parse(doc) do
    #     {parsed_doc, _} = :xmerl_scan.string(doc)
    #     parsed_doc
    # end

    # Note:     inspecting result is no use because xmerl sends an exit with:
    #           "whitespace_required_between_attributes"

    # Something like this would be handy:
    # try do
    #     result = :xmerl_scan.string(xml)
    # rescue
    #     :exit, _ …
Run Code Online (Sandbox Code Playgroud)

xml elixir well-formed

1
推荐指数
1
解决办法
712
查看次数