标签: elixir

Elixir(Ecto)中的包含方法是什么?

我们在Rails中使用包含或其他类似的连接方法(或SQL);
那么函数在Elixir(Ecto)中解决了这个问题?

elixir ecto phoenix-framework

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

管道操作员如何在最后一行返回结果?

我可以time_stamp在最后一行返回而不用大括号提取它吗?

{:ok, time_stamp} = Myapp.Repo.insert(changeset) # |> Map.get time_stamp
time_stamp

# {:ok, %Myapp.TimeStamp{__meta__: #Ecto.Schema.Metadata<:loaded>, active: true...}
Run Code Online (Sandbox Code Playgroud)

elixir ecto phoenix-framework

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

应用程序无法启动,出现错误"无法启动子项:Odt.Endpoint"

命令"mix phoenix.server"后总是收到错误:

=INFO REPORT==== 14-Dec-2015::20:55:48 ===

    application: logger
    exited: stopped
    type: temporary

** (Mix) Could not start application odt: Odt.start(:normal, []) returned an error: shutdown: failed to start child: Odt.Endpoint

** (Exit) shutdown: failed to start child: Phoenix.Endpoint.Server

** (Exit) shutdown: failed to start child: {:ranch_listener_sup, Odt.Endpoint.HTTP}

** (Exit) shutdown: failed to start child: :ranch_acceptors_sup

** (Exit) {:listener_error, Odt.Endpoint.HTTP, :eaddrinuse}
Run Code Online (Sandbox Code Playgroud)

我该如何解决?我究竟做错了什么?

ubuntu elixir phoenix-framework

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

在elixir sigil中逃脱闭括号

我想要一个自定义的sigil,这样我每行可以有一个元素.

这是我的代码

def sigil_l(text,[]), do: String.split(text,"\n")
Run Code Online (Sandbox Code Playgroud)

这适用于

~l(Clash of Titans
   The day after Tomorrow
   The Transporter
 )
Run Code Online (Sandbox Code Playgroud)

这失败了

~l(The man from Earth (2007)
   Gone Girl (2014)
   )
Run Code Online (Sandbox Code Playgroud)

请注意上面的括号

这是错误消息

"{" starting at line 38 is missing terminator "}". Unexpected token: )
(elixir) lib/kernel/parallel_compiler.ex:97: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/8
Run Code Online (Sandbox Code Playgroud)

预期的结果是

["The man from Earth (2007)",
"Gone Girl (2014)"]
Run Code Online (Sandbox Code Playgroud)

需要更改哪些代码.我是否还需要在输入中添加任何字符并处理sigil defination?

更新

@AbM提供的解决方案是正确的.我在elixir版本1.0.4上,所以它不起作用.它适用于1.1.x.

解

~l(The man from Earth (2007\)
   Gone Girl (2014\)
   )
Run Code Online (Sandbox Code Playgroud)

sigils elixir

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

Elixir Agent元组返回

我正在研究以下教程

https://howistart.org/posts/elixir/1

我正在与代理商合作,我不明白这条线是如何工作的.

def pop(door) do
    Agent.get_and_update(door, fn
        []      -> {:error, []}
        [h|t]   -> {{:ok, h}, t}     ## returns {:ok, h} why not {{:ok, h}, t}
    end  )
end
Run Code Online (Sandbox Code Playgroud)

产量

iex(5)> Portal.Door.push(:pink, 2)
:ok
iex(6)> Portal.Door.push(:pink, 3)
:ok
iex(7)> Portal.Door.push(:pink, 4)
:ok
iex(8)> Portal.Door.pop(:pink)    
{:ok, 4}
Run Code Online (Sandbox Code Playgroud)

具有不同结果的简单函数

person = fn ( first, last ) -> {{:ok, first}, last} end
Run Code Online (Sandbox Code Playgroud)

输出:

iex(13)> person.("John", "Doe")                                 
{{:ok, "John"}, "Doe"}
Run Code Online (Sandbox Code Playgroud)

为什么一个返回内部元组和外部元组,代理只返回内部元组.

提前致谢!

elixir

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

协议可枚举未实现3

失败的代码:

Enum.map(1..30, &(&1 * 2)) |> Enum.map &(&1 + 1) |> Enum.sum
** (Protocol.UndefinedError) protocol Enumerable not implemented for 3
    (elixir) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir) lib/enum.ex:112: Enumerable.reduce/3
    (elixir) lib/enum.ex:1400: Enum.reduce/3
    (elixir) lib/enum.ex:1043: anonymous fn/3 in Enum.map/2
    (elixir) lib/enum.ex:1387: Enum."-reduce/3-lists^foldl/2-0-"/3
    (elixir) lib/enum.ex:1043: Enum.map/2
Run Code Online (Sandbox Code Playgroud)

但这完全正常:

iex(18)> arr = Enum.map(1..30, &(&1 * 2)) |> Enum.map &(&1 + 1)
[3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43,
 45, 47, 49, 51, 53, …
Run Code Online (Sandbox Code Playgroud)

elixir elixir-framework

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

如何设置Phoenix PubSub订阅者回调

我有一个相当简单的要求,围绕凤凰城内置的2个服务(现在):

ServiceA负责注册用户.注册用户时,ServiceA广播包含有关新创建用户的信息的消息.现在使用Controller操作中的以下代码完成此操作:

ServiceA.Endpoint.broadcast("activity:all", "new:user", %{email: "test@test.com"})

ServiceB 负责监听所有这些活动广播并与他们合作(基本上建立一个活动源).

我遇到了一个绊脚石,因为我可以看到ServiceA向Redis播放消息(使用Phoenix.PubSub.Redis),但不完全了解如何让订阅者ServiceB处理它...

下面这段代码是据我已经成功地获得,这确实的东西时,广播消息,然后抛出一个例外.

部分订户模块

defmodule ServiceB.UserSubscriber do

  def start_link do
    sub = spawn_link &(process_feed/0)
    ServiceB.Endpoint.subscribe(:user_pubsub, "activity:all")
    {:ok, sub}
  end

  def process_feed do
    receive do
      params ->
        IO.inspect "processing goes here..."
    end
    process_feed
  end

end
Run Code Online (Sandbox Code Playgroud)

例外

[error] GenServer :user_pubsub terminating
** (FunctionClauseError) no function clause matching in Phoenix.PubSub.RedisServer.handle_info/2
Run Code Online (Sandbox Code Playgroud)

我猜我在GenServer某个地方错过了一大堆工作,但似乎无法在网上发现任何暗示在哪里.

elixir publish-subscribe redis phoenix-framework

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

为什么这个erlang函数未定义?

iex(1)> a = :erlang.timestamp
{1458, 585479, 931609}
iex(2)> b = :erlang.timestamp
{1458, 585484, 197713}
iex(3)> :erlang.now_diff(a,b)
** (UndefinedFunctionError) undefined function: :erlang.now_diff/2
    :erlang.now_diff({1458, 585479, 931609}, {1458, 585484, 197713})
Run Code Online (Sandbox Code Playgroud)

所以我可以访问elixir中的erlang函数,访问它们:erlang,但是now_diff/2是未定义的.

我有一个想法,这个计时器不是标准的erlang模块,需要/需要/导入,但我怎么弄不清楚?

PS如果没有像这个erlang函数那样高精度计算时间戳diff的函数,请不要建议timex.我没有找到阅读文档.

elixir

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

凤凰这么慢?请帮助改善其性能

我之所以学习灵丹妙药只是因为互联网上的很多文章都很有希望它的响应时间不到一毫秒.我昨天构建了我的第一个应用程序,我发现它的性能比rails更快.发生了什么?

让我来解释一下我的应用 它实现了ecto-secure-password包提供的基本认证机制:

defmodule HelloPhoenix.User do
  use HelloPhoenix.Web, :model
  use SecurePassword

  schema "users" do
    field :first_name, :string
    field :last_name, :string
    field :username, :string

    has_secure_password
  end
end
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

defmodule HelloPhoenix.SessionController do
  use HelloPhoenix.Web, :controller

  def create(conn, %{ "session" => %{ "username" => username, "password" => password }}) do
    if user = HelloPhoenix.User.authenticate(HelloPhoenix.Repo.get_by(HelloPhoenix.User, username: username), password) do
      conn
      |> put_session(:user_id, user.id)
      |> json(UserSerializer.run(user))
    else
      conn
      |> put_status(422)
      |> json(%{})
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

所以,我正在使用这些命令:

MIX_ENV=prod mix compile
MIX_ENV=prod mix phoenix.server
Run Code Online (Sandbox Code Playgroud)

失败和成功认证的响应时间约为~130ms,而此处的rails响应时间约为150ms.我做错了什么?

elixir phoenix-framework

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

这是什么意思:警告!排除的依赖项(不是Hex包的一部分)?

当我尝试在hex上发布我的包的新版本时,它会输出以下警告:

  WARNING! Excluded dependencies (not part of the Hex package):
    ex_doc
Run Code Online (Sandbox Code Playgroud)

我运行命令的全文:

$ mix hex.publish
Publishing usefulness 0.0.5
  Dependencies:
    earmark >= 0.0.0 
  Files:
    lib/usefulness.ex
    lib/usefulness/stream.ex
    lib/usefulness/string.ex
    config/config.exs
    test/test_helper.exs
    test/usefulness_test.exs
    mix.exs
    README.md
    LICENSE
  App: usefulness
  Name: usefulness
  Description: Useful things
  Version: 0.0.5
  Build tools: mix
  Licenses: Apache 2.0
  Maintainers: afasdasd
  Links: 
    Github: https://github.com/b-filip/usefulness
  Elixir: ~> 1.2
  WARNING! Excluded dependencies (not part of the Hex package):
    ex_doc
Before publishing, please read Hex Code of Conduct: https://hex.pm/docs/codeofconduct
Proceed? [Yn]
Run Code Online (Sandbox Code Playgroud)

我不知道这个警告意味着什么

这是我project.deps在 …

hex elixir-mix elixir

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