是否可以Enumerable.t在elixir中参数化类型?
所以目前我有一个带有foos 列表的函数:
@spec the_awesome([foo]) :: any
def the awesome(foos) do
Enum.reduce(foos, &(bar(&2, &1)))
end
Run Code Online (Sandbox Code Playgroud)
而且它真的不一定是一个清单!因为唯一的函数调用来自Enum模块我想更改typespec以获取任何Enumerable,但保持Enumerable必须完全由foos 组成的要求
就像是
@spec the_awesome(Enumerable.t(foo)) :: any
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我是Phoenix Framework的新用户,我正在尝试设置一个简单的HTTP POST服务,它对传入的数据执行计算并返回结果,但是我收到以下错误:
** (RuntimeError) expected connection to have a response but no response was set/sent
stacktrace:
(phoenix) lib/phoenix/conn_test.ex:311: Phoenix.ConnTest.response/2
(phoenix) lib/phoenix/conn_test.ex:366: Phoenix.ConnTest.json_response/2
test/controllers/translation_controller_test.exs:20
Run Code Online (Sandbox Code Playgroud)
我的测试用例:
test "simple POST" do
post conn(), "/api/v1/foo", %{"request" => "bar"}
IO.inspect body = json_response(conn, 200)
end
Run Code Online (Sandbox Code Playgroud)
我的路由器定义:
scope "/api", MyWeb do
pipe_through :api
post "/v1/foo", TranslationController, :transform
end
Run Code Online (Sandbox Code Playgroud)
我的控制器:
def transform(conn, params) do
doc = Map.get(params, "request")
json conn, %{"response" => "grill"}
end
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
假设,我在一台4核CPU机器上.
如果我在我的elixir VM中运行以下命令:
1..4 |> Enum.map fn(x) -> spawn(computationally_heavy_process) end
Run Code Online (Sandbox Code Playgroud)
这是否使用我机器的所有4个核心.每个计算繁重的过程之一?
在使用WebSockets时,我正在尝试将Phoenix Channels与新的Rails ActionCable进行比较.
对于某些上下文,ActionCable在向所有客户端广播消息时使用Redis来处理PubSub.示例场景:单独节点上的3个rails进程中的1个将能够广播到所有Web服务器上连接的客户端.这是通过点击Redis完成的,Redis又发布到所有rails服务器,然后推送到所有连接的客户端.
我最近读到了关于Phoenix websocket连接实现的200万个 websocket连接.
还发现了这个宝石:Phoenix 1.0发行说明提到了有关频道的信息:
即使在一组计算机上,您的消息也会自动在节点间广播
Phoenix如何能够跨节点向客户端广播?它是否在引擎盖下使用邮箱和/或其他一些进程间通信?
这类似于在这个问题2)职位.
谢谢!
# mix.exs
defp deps do
[{:phoenix, "~> 1.1.4"},
{:postgrex, ">= 0.0.0"},
{:phoenix_ecto, "~> 3.0.0-rc"},
{:gettext, "~> 0.9"},
...
]
end
Run Code Online (Sandbox Code Playgroud)
安装的ecto版本是 "2.0.0-rc.5"
iex(1)> categories = [%{name: "stackoverflow", url: "stackoverflow.com"}]
iex(2)> App.Repo.insert_all App.Category, categories
** (Postgrex.Error) ERROR (not_null_violation): null value in column "inserted_at" violates not-null constraint
table: categories
column: inserted_at
Failing row contains (1, stackoverflow, stackoverflow.com, null, null).
(ecto) lib/ecto/adapters/sql.ex:176: Ecto.Adapters.SQL.query!/5
(ecto) lib/ecto/adapters/sql.ex:350: Ecto.Adapters.SQL.insert_all/8
(ecto) lib/ecto/repo/schema.ex:42: Ecto.Repo.Schema.do_insert_all/6
iex(2)>
Run Code Online (Sandbox Code Playgroud)
从文档来看,它似乎inserted_at是自动生成的.我应该手动做这部分吗?
schema "categories" do
field :name, :string
field :url, :string
timestamps …Run Code Online (Sandbox Code Playgroud) Erlang(以及扩展名Elixir)支持浮点数.
一些可能的浮点数:
1.23451.0e101.0e-42Erlang支持NaN(nan.在Erlang中)(但我还没有发现一个输出nan自身的方法).
但是,Erlang没有支持Infinity.虽然像IEEE-754这样的通用标准表明,Infinity在做某事时应该返回1.0/0.0,而Erlang会抛出一个bad arithmetic error.
尝试制作"太大"的浮子时也会发生同样的情况1.0e400.
这背后可能有一些(历史?)原因.
我想删除所有换行符号:
aaa = """
fdsfds fdsfds
fdsfdsfds
fdsfdsfds
""" |> String.strip("\r\n")
Run Code Online (Sandbox Code Playgroud)
我得到:
argument error
Run Code Online (Sandbox Code Playgroud)
这有什么问题?
我对零停机部署系统感兴趣,该系统不使用Elixir/Erlang热升级(由于代码运行时数据迁移的复杂性).
我听说我可以在将服务器绑定到适配器时使用SO_REUSEPORT选项,这样我就可以运行绑定到同一地址和端口的同一应用程序的两个实例.我的意图是在与运行版本1相同的服务器上部署版本2,启动版本2,然后优雅地停止版本1,这应该允许传入连接自然地开始专门连接到版本2.
无论这是否与我的计划完全一致 - 我的意图是测试这种配置,因为它知道它在不同的操作系统上的行为不同 - 我想知道配置Phoenix执行此操作所需的具体步骤,因为这似乎更低 - 内部的级别配置:gen_tcp.
或者,如果有一种方法可以配置OS或Erlang VM以默认启用此选项的所有连接,那就更好了.
正如标题所述,我的问题是:
有没有办法确定Elixir中名称存在的模块?
在环顾四周后,我在Elixir论坛中遇到了这个帖子,但并不完全是我正在寻找的.在这个帖子中他们提到Code.ensure_loaded/1,但我认为这不是我需要的.
现在我正在通过以下方式解决问题:
def module_exists?(module_name) where is_atom(module_name) do
!is_nil(module_name.module_info)
rescue
e in UndefinedFunctionError -> false
end
Run Code Online (Sandbox Code Playgroud)
但我不相信.
任何帮助表示赞赏,谢谢!
在本教程之后尝试使用Phoenix v1.3构建API :
https ://dreamconception.com/tech/phoenix-full-fledged-api-in-five-minutes
(确保按照每一步操作)
当我试图跑 mix ecto.setup
我收到以下错误:

Compiling 17 files (.ex)
== Compilation error on file lib/myapp/users/users.ex ==
** (CompileError) lib/myapp/users/users.ex:65: Myapp.Users.User.__struct__/0 is undefined, cannot expand struct Myapp.Users.User
(stdlib) lists.erl:1354: :lists.mapfoldl/3
lib/myapp/users/users.ex:65: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
Run Code Online (Sandbox Code Playgroud)
这lib/myapp/users/users.ex是非常大的:https://github.com/nelsonic/phoenix-api-example/blob/master/lib/myapp/users/users.ex#L65
GitHub上的完整代码:https://github.com/nelsonic/phoenix-api-example
任何帮助调试它将非常感激.