刚开始我的Elixir之旅.在书中读到这个:
"&运算符将后面的表达式转换为函数."
好吧,我想我明白了......
iex(70)> f = &(&1 * &2)
#Function<12.80484245 in :erl_eval.expr/5>
iex(72)> f.(2,3)
6
Run Code Online (Sandbox Code Playgroud)
好的,&符号是匿名函数的简写及其参数.但是,为什么下次通话不起作用?!
iex(73)> &(&1 * &2).()
#Function<12.80484245 in :erl_eval.expr/5>
Run Code Online (Sandbox Code Playgroud)
......我可以继续这样看待永远:
iex(76)> &(&1 * &2).().().()
#Function<12.80484245 in :erl_eval.expr/5>
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
在elixir中定义匿名函数时,会得到这样的结果.
#Function<6.90072148/1 in :erl_eval.expr/5>
我注意到的是这个数字是基于函数的arity.所以总是有1个arg函数
#Function<6.90072148/1 in :erl_eval.expr/5>
总是有两个arg函数
#Function<12.90072148/2 in :erl_eval.expr/5>
总是有三个arg功能
#Function<18.90072148/3 in :erl_eval.expr/5>
返回的数字是多少,它是如何派生的?
我正试图给我的表格上课.通过文档查看,似乎没有办法做到这一点,并且你只能传递像这样的名字之类的东西
form_for @changeset, @action, [name: :search], fn f -> %>
有没有办法在类中传递作为一个选项,所以我的表单html元素可以有一个类?也许是这样的?
form_for @changeset, @action, [class: 'form-horizontal'], fn f ->
我正在挖掘凤凰源代码,但似乎无法找到任何东西.
我在凤凰城使用addict:1.0.3,我已经按照README指南在我的应用程序中使用登录.当我打电话登录时,我收到以下错误:
[error] #PID<0.2576.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /login
** (exit) an exception was raised:
** (UndefinedFunctionError) undefined function: MyApp.Addict.Controller.init/1 (module MyApp.Addict.Controller is not available)
MyApp.Addict.Controller.init(:login)
(my_app) web/router.ex:1: anonymous fn/1 in MyApp.Router.match/4
(my_app) lib/phoenix/router.ex:255: MyApp.Router.dispatch/2
(my_app) web/router.ex:1: MyApp.Router.do_call/2
(my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.phoenix_pipeline/1
(my_app) lib/plug/debugger.ex:90: MyApp.Endpoint."call (overridable 3)"/2
(my_app) lib/phoenix/endpoint/render_errors.ex:34: MyApp.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
Run Code Online (Sandbox Code Playgroud) 我尝试了以下内容
def index(conn, _params) do
Logger.debug conn
......
Run Code Online (Sandbox Code Playgroud)
但我明白了
protocol String.Chars not implemented for %Plug.Conn
Run Code Online (Sandbox Code Playgroud)
我甚至尝试过Apex,但那也没有用.
我在Ecto项目中遇到了这个问题.没有任何查询正在运行.我做了一些谷歌搜索和github问题搜索.有几个但与我的问题无关.
这个问题是从这个问题开始的https://github.com/elixir-lang/ecto/issues/602#issuecomment-145596702(主要与我的问题有关)
query = from u in Univer, where: u.id > 4, select: u
Run Code Online (Sandbox Code Playgroud)
爆发了** (RuntimeError) undefined function: u/0.不仅是那个型号,还有其他型号.我的朋友.
{:postgrex, "~> 0.9.1"},
{:poison, "~> 1.5"},
{:httpoison, "~> 0.7.2"},
{:ecto, "~> 1.0.4"},
{:floki, "~> 0.5"}
Run Code Online (Sandbox Code Playgroud)
目前所有从db读取都是通过psql.它做的工作,但很烦人.:)
供参考.
defmodule Univer do
use Ecto.Model
import Ecto.Query
schema "univers" do
field :ref, :integer
field :name, :string
field :legal_name, :string
field :city, :string
field :type, :string
field :address, :string
field :contacts, {:array, :string}
field :fax, :string
field :phones, {:array, …Run Code Online (Sandbox Code Playgroud) 我在elixir中有一个属性的模块:
defmodule MyAwesomeModule do
@awesome_number 7
# other stuff...
end
Run Code Online (Sandbox Code Playgroud)
我无法访问@awesome_number模块外部.我尝试过使用该Module.get_attribute/2方法,但它会抛出此错误:
iex(79)> Module.get_attribute(MyAwesomeModule, :awesome_number)
** (ArgumentError) could not call get_attribute on module MyAwesomeModule because it was already compiled
(elixir) lib/module.ex:1101: Module.assert_not_compiled!/2
(elixir) lib/module.ex:1016: Module.get_attribute/3
Run Code Online (Sandbox Code Playgroud)
所以现在,我将模块属性包装在一个方法中来访问它,但它对我来说并没有多大意义.我可以简单地使用该方法并停止一起使用该属性:
defmodule MyAwesomeModule do
@awesome_number 7
def awesome_number, do: @awesome_number
# other stuff...
end
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,有更好/正确的方法吗?
在Ruby中,我可以使用
target_files = Dir["/some/dir/path/*.rb"]
#=> ["/some/dir/path/foo.rb", "/some/dir/path/bar.rb", "/some/dir/path/baz.rb"]
Run Code Online (Sandbox Code Playgroud)
它将返回目录中所有匹配文件的数组.我怎样才能在Elixir中做类似的事情?
ERROR 22001(string_data_right_truncation):类型字符变化的值太长(255)
我理解(并假设)字符串将限制为一定数量的字符; 但是,我不确定哪种类型最适合这种情况.
我应该使用什么类型的凤凰框架博客的"内容"部分?
数据将是文本段落,不能限制大小.
提前致谢.
我喜欢在我的测试中使用断点来查看出现了什么问题,但测试中的30秒超时使我无法自由地环顾四周.
有没有办法禁用它?以下不起作用:
@tag timeout: 0
test "something" do
assert something == 42
end
Run Code Online (Sandbox Code Playgroud)