elixir代码有可移植格式吗?除了明显的焦油.
例如,如果我想向客户端或其他团队的开发人员提供一个或多个模块(不使用github).我该怎么办?
其他一些熟悉的语言的答案是:Ruby - gem,Java - jar,Python - egg等.
例如,在Python中,可以使用'\'字符(是的,必要的邪恶)来打破行.是不是可以在Elixir中打破线?
有没有办法预先加载ecto关联而不显式使用preload:?
类似于架构中的选项?
schema "gadgets" do
field :foo,
has_many :bars, Myapp.Bar, preload: true
end
Run Code Online (Sandbox Code Playgroud)
我正在做类似的事情
Repo.get(Gadget, id)
|> Repo.preload: [:bars]
Run Code Online (Sandbox Code Playgroud)
编辑:我试图这样做的原因是因为我想将相关模型预加载到已经预加载的相关模型,如
preload: [:invoices preload: :items]
Run Code Online (Sandbox Code Playgroud) 是否有任何网站允许测试Elixir片段,保存它们并分享如下:
我找到类似的东西:http:
//www.tryerlang.org/和http://try-elixir.herokuapp.com/但他们不允许共享代码,第二次使用Elixir v0.10.2.
我正在关注编程凤凰书,正在编写实现登录/注销经过身份验证的用户的部分,并且收到错误我不知道该怎么做:
mix phoenix.server
Compiled lib/rumbl.ex
Compiled lib/rumbl/repo.ex
Compiled web/channels/user_socket.ex
Compiled web/web.ex
Compiled web/models/user.ex
== Compilation error on file web/router.ex ==
** (UndefinedFunctionError) undefined function: Rumbl.Auth.init/1 (module Rumbl.Auth is not available)
Rumbl.Auth.init([repo: Rumbl.Repo])
(plug) lib/plug/builder.ex:198: Plug.Builder.init_module_plug/3
(plug) lib/plug/builder.ex:186: anonymous fn/4 in Plug.Builder.compile/3
(elixir) lib/enum.ex:1387: Enum."-reduce/3-lists^foldl/2-0-"/3
(plug) lib/plug/builder.ex:186: Plug.Builder.compile/3
web/router.ex:4: (module)
(stdlib) erl_eval.erl:669: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:100: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/8
== Compilation error on file lib/rumbl/endpoint.ex ==
** (UndefinedFunctionError) undefined function: Rumbl.Router.init/1 (module Rumbl.Router is not available)
Rumbl.Router.init([])
(plug) lib/plug/builder.ex:198: Plug.Builder.init_module_plug/3
(plug) …Run Code Online (Sandbox Code Playgroud) 基于哈希表的字典/映射结构提供O(1)查找时间.然而,我一直看到在Elixir中的含义,找到匹配的函数头比在地图中查找某些东西更快.
例如,Elixir String.Unicode 将一个unicode字符列表编译成许多函数头,因此通过找到函数head得到upcase"é" 来回答"é的最佳版本是什么".
我不知道为什么这比upcase在地图中查找"é" 的单个功能头更快或更高的内存效率.
同样,在展示如何在"Metaprogramming Elixir"中构建I18n库时,Chris McCord为每个翻译键提供了自己的功能头,并说:
"通过为每个转换映射生成函数头,我们再次让虚拟机接管以进行快速查找."
Elixir中的地图不提供O(1)查找吗?找到匹配的函数头O(1)?为什么选择将静态列表编译为多个函数头而不是仅将其存储在映射中?
我似乎找不到在函数头中的地图键上进行模式匹配的方法.有没有办法做到这一点?我正在尝试做的是根据某个键是否已存在于地图中而运行不同的代码(并且还想避免if/else等)
这就是我的代码
def my_func(key, %{key => _} = map), do: ...
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误
**(CompileError)在映射键匹配内非法使用变量键,映射只能使用^键匹配现有变量
当然我也试过用它 ^
def my_func(key, %{^key => _} = map), do: ...
Run Code Online (Sandbox Code Playgroud)
然后给出
**(CompileError)未绑定变量^键
我在Windows 8.1机器上使用elixir 1.3.1/erlang 19.0 x64.谢谢阅读!
我正在尝试在屏幕上显示一个表单.但是当我尝试启动服务器时,我一直收到此错误.locations_controller.ex ==
** (CompileError) web/controllers/locations_controller.ex:5: Locations.__struct__/1 is undefined, cannot expand struct Locations.顺便说一下,我是elixir的新手,所以我可能做了一些非常明显错误的事情.
这是我的代码:
def new(conn, _params) do
changeset = Locations.changeset(%Locations{})
render conn, "new.html", changeset: changeset
end
def create(conn, %{"locations" => %{ "start" => start, "end" => finish }}) do
changeset = %AwesomeLunch.Locations{start: start, end: finish}
Repo.insert(changeset)
redirect conn, to: locations_path(conn, :index)
end
Run Code Online (Sandbox Code Playgroud)
<h1>Hey There</h1>
<%= form_for @changeset, locations_path(@conn, :create), fn f -> %>
<label>
Start: <%= text_input f, :start %>
</label>
<label>
End: <%= text_input f, …Run Code Online (Sandbox Code Playgroud)