如果我创建一个文件loop.exs:
Enum.each(1..40000, fn (n) -> spawn fn -> IO.puts(n) end end)
Run Code Online (Sandbox Code Playgroud)
并运行它,计算输出行数:
elixir loop.exs | wc -l
Run Code Online (Sandbox Code Playgroud)
在随后的运行中,我可能会看到预期的40000行,但可能会看到更少的行。在我的测试中,我已经看到了39752,39934,39673等这表明,我认为某些进程没有得到调用IO.puts,那么什么是发生在他们身上,为什么没有我警告他们已经失踪了,和我是什么做错了导致这种情况发生?
我对原子以及如何使用原子感到困惑.
说实话,我正在阅读文档,只是不了解原子:(
有人可以提供一些可以解释这个问题的例子吗?
谢谢
在我运行的nodejs中
var os = require("os")
os.hostname()
Run Code Online (Sandbox Code Playgroud)
我怎样才能进入长生不老药?
我是灵药开发的新手.我在elixir中解析字符串时遇到问题.假设我有字符串"来自地狱的Hello World".我知道我可以像这样拆分它String.split("Hello World from the hell").我想知道无论如何要将此字符串的元素分配到elixir中列出?
我正在尝试以timestamp inserted_atjson的形式提供自动字段.
问题是Chrome和Firefox在分析发送内容方面存在分歧.问题似乎是菲尼克斯默认提供的服务"2017-01-12T19:49:000".这由Firefox和IE解析为UTC,而Chrome将此转换为本地时间.添加一个Z("2017-01-12T19:49:000Z"),所有都给当地时间.
结果我将我的代码转换为:
def render("message.json", %{message: message}) do
%{id: message.id,
content: message.content,
date: Ecto.DateTime.to_iso8601(message.inserted_at)<>"Z"}
end
Run Code Online (Sandbox Code Playgroud)
但这感觉非常黑客,必须有一个更好的方法.
有没有办法简化这段代码:
def test do
var1 = func1()
|> func2()
|> func3()
var2 = if var1 == nil do
var1
else
func4(var1)
end
func5(var2)
end
Run Code Online (Sandbox Code Playgroud)
我想避免创建var2并返回结果.
如何在架构中添加功能?我想创建用于动态地将字段添加到模型模式的函数.例:
def func do
# .. loop to create dynamic fields
field :street, :string
end
schema "objects" do
func
end
... Error:
** (CompileError) web/models/objects.ex:12: undefined function func/0
Run Code Online (Sandbox Code Playgroud) 我有自己的插头模块:
defmodule Module1 do
def init(opts) do
opts
end
def call(conn, _opts) do
# if some_condition1
# something
end
# other stuff
end
Run Code Online (Sandbox Code Playgroud)
在router.ex中
pipeline :pipeline1 do
plug(Module1)
end
scope "/scope", MyApp, as: :scope1 do
pipe_through([:browser, :pipeline1])
# .......
Run Code Online (Sandbox Code Playgroud)
现在我想使用相同的模块Module1创建第二个管道和范围:
pipeline :pipeline2 do
plug(Module1)
end
scope "/scope2", MyApp, as: :scope2 do
pipe_through([:browser, :pipeline2])
# .......
Run Code Online (Sandbox Code Playgroud)
但是,如果我要创建第二个模块,那么区别仅在于:
def call(conn, _opts) do
# if some_condition1 and some_condition2
# something
end
Run Code Online (Sandbox Code Playgroud)
也就是说,我只添加了"some_condition2",其他一切都保持不变.
现在,我该怎么做?我是否必须创建与Module1完全相同的模块Module2并轻轻地更改"call"?它会导致代码重复.
我在iex>中更新我的@doc以测试它的外观.我遇到的问题是我必须退出iex才能查看更新的@doc文档.有没有办法在使用r()时重新加载模块@doc变量?
iex -S mix
iex> h Coordinate.island/1
## Examples
iex> {:ok, coord } = Cordinate.start_link
Cordinate.island(coord)
:falls_town
Run Code Online (Sandbox Code Playgroud)
更新@doc以返回:none而不是:falls_town并保存文件.
iex> r(Coordinate)
iex> h Coordinate.island/1
# issue: still showing the old @doc example
## Examples
iex> {:ok, coord } = Cordinate.start_link
Cordinate.island(coord)
:falls_town # should be :none
Run Code Online (Sandbox Code Playgroud) 根据这里的答案/评论,我知道如何生成iso860日期时间字符串:
iex> Timex.now() |> Timex.format("{ISO:Extended:Z}")
{:ok, "2015-12-01T09:40:44.716499ZZ"}
Run Code Online (Sandbox Code Playgroud)
但是,有没有最佳实践方法来生成上述没有时间?到目前为止,我有:
iex> to_string(Timex.today) <> "Z"
Run Code Online (Sandbox Code Playgroud)