我有一些结构定义
defmodule Foo do
defstruct [:a, :b]
end
Run Code Online (Sandbox Code Playgroud)
我有一些结构
a = %Foo{a: 1, b: 2}
Run Code Online (Sandbox Code Playgroud)
我能够使用管道(如地图)获得新的结构
%Foo{a | b: 3}
%Foo{a: 1, b: 3}
Run Code Online (Sandbox Code Playgroud)
但与 map 不同的是,当变量中的键时,我无法获取新结构:
iex(4)> key = :b
:b
iex(5)> %Foo{a | key => 3}
** (CompileError) iex:5: unknown key key for struct Foo
Run Code Online (Sandbox Code Playgroud)
问题:当我有更新变量的密钥时如何获得新结构?
该功能hello没有任何冲突并且工作正常。
defmodule User do
defstruct [:name]
end
defmodule Greeter do
def hello(%User{} = user) do
"Hello #{user.name}"
end
def hello(name) do
"Hello #{name}"
end
end
Run Code Online (Sandbox Code Playgroud)
但是,如果我向第一个函数添加可选参数,则会出现冲突错误。
...
def hello(%User{} = user, opts \\ []) do
"Hello #{user.name}"
end
...
Run Code Online (Sandbox Code Playgroud)
错误
def hello/1 conflicts with defaults from hello/2
任何人都可以解释为什么以及如何有意义?
我开始在教程之外使用 Phoenix 作为一个小用例。到目前为止很好,但我对以下内容感到有些吃惊。
我有一个资源“录音”,我想在 JSON 中的 /api/recordings 和带有模板的 /recording 访问,结果是 HTML。
理想情况下,Ecto 表示将是唯一的,甚至控制器的一部分也将被共享?
现在我要么有 2 个资源记录 API 和记录 HTML,要么有 2 个控制器和 1 个资源。
有什么例子吗?我一直在寻找其中一个,但没有找到用于同一资源的 :api 管道和 :browser 管道。
谢谢
我正在开发一个 elixir API,我正在使用 JaSerializer 但它在我调用 post API 时给了我一个类似这样的错误。
function Poison.encode_to_iodata!/1 is undefined (module Poison is not available)
Run Code Online (Sandbox Code Playgroud)
但是在我的配置文件中,我添加了 Poison 并使用 mime 重新编译
mix deps.clean mime --build
mix deps.get
Run Code Online (Sandbox Code Playgroud)
我正在调用的 api 也被插入到数据库中,但它没有响应任何 Json 并给我那个错误。
这是我的配置文件:
use Mix.Config
config :banking,
ecto_repos: [Banking.Repo]
# Configures the endpoint
config :banking, BankingWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "LMcWbj2QramA5ulI0MZnFobOtrKg/Z2x/gmDl6NwH7hxUbRksPzZjuXwyk8QKGyx",
render_errors: [view: BankingWeb.ErrorView, accepts: ~w(json)],
pubsub: [name: Banking.PubSub, adapter: Phoenix.PubSub.PG2]
# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]
config :mime, :types, %{
"application/vnd.api+json" …Run Code Online (Sandbox Code Playgroud) Elixir 函数末尾的!or?是什么意思?这是语言的特殊之处还是特定包的特殊之处?
ExAws.S3.list_buckets() |> ExAws.request!()
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习 Elixir 和函数编程,但在理解 Elixir in Action 一书中的这个例子时遇到了困难。
defmodule ListHelper do
def sum([]), do: 0
def sum([head | tail]) do
head + sum(tail)
end
end
ListHelper.sum([12,3,4])
Run Code Online (Sandbox Code Playgroud)
这个的返回值是 19,但我不明白的是这些值是如何累积的。
我认为 head 正在不断更新,然后当模式匹配到[]然后累积的 head 将被添加到0并且该函数将退出,但是在使用它之后我现在认为这不是正在发生的事情。有人可以为这个例子中发生的事情提供另一种解释吗?如果我需要解释更多,我可以尝试重新审视这个。
在涵盖理解的 Elixir 文档中,我遇到了以下示例:
iex> for <<c <- " hello world ">>, c != ?\s, into: "", do: <<c>>
"helloworld"
Run Code Online (Sandbox Code Playgroud)
我现在有点理解整个表达式,但我无法弄清楚“?\ s”是什么意思。我知道它以某种方式匹配并因此过滤掉了空格,但这就是我的理解结束的地方。
编辑:我现在已经发现它解析为 32,这是一个空格的字符代码,但我仍然不知道为什么。
我正在尝试在 Elixir 程序和 C 程序之间移动一些数据。在 Elixir 中,我有一个我想要移动的结构。我使用:erlang.term_to_binary将其转换为二进制文件,然后将其混搭为 C。
但是,当我解码它时,它只会返回NULL?? 并且没有错误消息或任何东西。一堆其他长生不老药/erlang 术语工作正常;当我尝试通过地图或结构(这是一种地图)发送时,它特别失败。
我在 Elixir 中做这样的事情:
msg = %Message{ title: "hello", body: "world" }
binmsg = :erlang.term_to_binary(msg)
send(state.port, {self(), {:command, binmsg}})
Run Code Online (Sandbox Code Playgroud)
.. 和 C(不包括接收消息,我已经确认它已经以正确的长度到达,甚至确认 C 和 elixir 中的字节是相同的):
uint8_t *buf = read_cmd();
ETERM *map = erl_decode(buf);
erl_print_term(map, stdout);
Run Code Online (Sandbox Code Playgroud) 就像我们regs().在 Erlang shell 中使用来列出所有在 BEAM 上运行的进程一样,它们是否与 Elixir 或 iex 中的相同?
我现在正在阅读Learn Functional Programming with Elixir,在第 4 章中,作者谈到了尾调用优化,即尾递归函数将使用比体递归函数更少的内存。但是当我尝试了书中的例子时,结果却相反。
# tail-recursive
defmodule TRFactorial do
def of(n), do: factorial_of(n, 1)
defp factorial_of(0, acc), do: acc
defp factorial_of(n, acc) when n > 0, do: factorial_of(n - 1, n * acc)
end
TRFactorial.of(200_000)
# body-recursive
defmodule Factorial do
def of(0), do: 1
def of(n) when n > 0, do: n * of(n - 1)
end
Factorial.of(200_000)
Run Code Online (Sandbox Code Playgroud)
在我的电脑中,tail-recursive版本的beam.smp会使用2.5G~3G内存,而body-recursive只使用1G左右。我误解了什么吗?