我是Elixir的新手,所以我对这三个陈述感到困惑
a = [ [ 1, 2, 3 ] ]
[a] = [ [ 1, 2, 3 ] ]
[[a]] = [ [ 1, 2, 3 ] ]
Run Code Online (Sandbox Code Playgroud)
第一个和第二个语句按预期返回结果,但第三个语句引发错误
**(MatchError)右侧值不匹配:[[1,2,3]]
我想知道为什么第三句话会引起错误
我正在尝试像这样初始化模块属性
response = HTTPoison.get! url
{:ok, response} = Poison.decode(response.body)
@attr response
Run Code Online (Sandbox Code Playgroud)
我以前用一个文件来做,就像这样:
@external_resource file = Path.join([__DIR__, "file.txt"])
Module.register_attribute __MODULE__, :attr, accumulate: true
for line <- File.stream!(file, [], :line) do
@attr line
...
Run Code Online (Sandbox Code Playgroud)
使用HTTPoison并获取API的响应是否一样?我收到此错误:
== Compilation error in file lib/module.ex ==
** (ArgumentError) argument error
(stdlib) :ets.lookup_element(:hackney_config, :mod_metrics, 2)
/project/deps/hackney/src/hackney_metrics.erl:27: :hackney_metrics.get_engine/0
/project/deps/hackney/src/hackney_connect.erl:69: :hackney_connect.create_connection/5
/project/deps/hackney/src/hackney_connect.erl:37: :hackney_connect.connect/5
/project/deps/hackney/src/hackney.erl:316: :hackney.request/5
lib/httpoison/base.ex:630: HTTPoison.Base.request/9
lib/httpoison.ex:66: HTTPoison.request!/5
lib/module.ex:4: (module)
Run Code Online (Sandbox Code Playgroud) 我有一个动作:
def my_action(conn, params) do
# ..............
Logger.info("test: params #{IO.inspect(params)};\r\n\r\n")
Run Code Online (Sandbox Code Playgroud)
这将导致异常:
** (Protocol.UndefinedError) protocol String.Chars not implemented for %{"key1" => "var1", "key2" => "var2", # ........}.
This protocol is implemented for: Atom, BitString.................
Run Code Online (Sandbox Code Playgroud)
我理解这是什么意思,但我不明白为什么会这样?我正在使用“检查”。我应该用什么来修复它?
If I have two lists:
First list => [1,2,3]
Second List => [3,4,5]
I want this to return true because they both contain 3? If 3 was replaced by 6 it would return false because no elements would match.
Current Attempt
Enum.member?(first_list, fn(x) -> x == second_list end)
Run Code Online (Sandbox Code Playgroud)
Ecto queries:
user_teams = from(
t in MyApp.Team,
left_join: a in assoc(t, :accounts),
where: p.owner_id == ^user.id or (a.user_id == ^user.id and t.id == a.project_id)
) |> Repo.all
current_user_teams = from( …Run Code Online (Sandbox Code Playgroud) defmodule DemoMacro do
defmacro i_am_macro do
quote do
# ……
end
end
i_am_macro # (CompileError) example.exs:10: undefined function i_am_macro/0
end
Run Code Online (Sandbox Code Playgroud)
但是从其他模块导入,可以成功调用:
defmodule SecondModule do
require DemoMacro
DemoMacro.i_am_macro
end
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能编译第一段代码?
我正在尝试将Ecto Repo.get_by与多个子句一起使用,但是在查找语法示例时遇到了麻烦。我需要查询数据库中的一个或两个字段匹配的位置,因此子句之间的“或”条件。我不知道这是否有可能。
Repo.get_by(User, [username: username, email: username], prefix: :accounts)
Run Code Online (Sandbox Code Playgroud) 您如何简单地从本示例的末尾删除20?
[46, 238, 64, 30, 105, 136, 98, 75, 23, 157, 11, 20]
Run Code Online (Sandbox Code Playgroud) 通过混合在iex会话中运行凤凰服务器,如:
iex -S mix phx.server
Run Code Online (Sandbox Code Playgroud)
有时候会列出一些警告:
Compiling 1 file (.ex)
warning: variable "user" is unused
lib/app_web/controllers/user_controller.ex:37
Run Code Online (Sandbox Code Playgroud)
但似乎只是第一次运行服务器,之后我认为它是缓存的,所以它没有显示出来.我怎么能在以后看到这些警告来修复它们?谢谢.
这是我的模块:
defmodule Test do
def try(10 = num, other_num \\ 10) do
num + other_num
end
def try(_num, _other_num) do
raise "This is incorrect"
end
end
Run Code Online (Sandbox Code Playgroud)
当我跑步时,iex我得到这个警告:
warning: def try/2 has multiple clauses and also declares default values. In such cases, the default values should be defined in a header. Instead of:
def foo(:first_clause, b \\ :default) do ... end
def foo(:second_clause, b) do ... end
one should write:
def foo(a, b \\ :default)
def foo(:first_clause, b) do …Run Code Online (Sandbox Code Playgroud) 我有一个长生不老药游戏,可以在STDOUT上打印一个“木板”,我想在打印下一个木板版本之前清除屏幕。我检查了长生不老药的文档,我认为这就像调用一样简单,System.cmd("clear", [])但这似乎并不能解决问题:
iex(1)> System.cmd("clear", [])
{"\e[H\e[2J", 0}
Run Code Online (Sandbox Code Playgroud)
似乎返回了一些转义字符,但我不确定这意味着什么。我的问题是为什么没有System.cmd("clear", [])工作?还是我期望它应该清除屏幕错误(以及为什么)?
显然,iex具有clear清除iex的便利功能。我发现此帮助程序方法来自该IEx.Helpers模块,现在我可以运行IEx.Helpers.clear并清除屏幕,然后再打印另一块板。