我正在使用 EEx 进行邮件模板...
我已将其集成到我的模块中,如下所示:
defmodule Blackbox.ReportHandler do
use GenServer
import Swoosh.Email
@email EEx.compile_file("web/templates/mail.html.eex")
def init([actions: actions]) do
{:ok, %{actions: actions, name: "none_yet", report: []}}
end
[...]
def handle_info({:test_step, :done, true}, s) do
email = @email |> Code.eval_quoted([name: s.name, report: s.report]) |> elem(0)
sent = new()
...
Run Code Online (Sandbox Code Playgroud)
由于它在 Blackbox.ReportHandler(即已更改)时编译 EEx 文件,因此我需要在控制台中手动重新加载模块,或者更改 ReportHandler 模块上的某些内容,以便这次使用最新的 EEx 文件再次编译.
有没有办法将 ReportHandler 的编译链接到另一个文件的修改?
例如,这行不通:
puts "hello"
"""
var1 = 123
var2 = "#{var3}" # exception!
"""
Run Code Online (Sandbox Code Playgroud)
这将在运行时抛出异常,即使代码应该作为注释被跳过。
有没有办法中断理解?
我正在考虑类似 Python 的for- break- else。例如,假设我正在遍历一个整数列表,当我从中生成一个新列表时,我想检查它是否不包含0.
我可以做这样的事情:
for x <- [1, 2, 0, 3] do
case x do
0 -> break
_ -> x
end
end
if broke do
:error
else
:ok
end
Run Code Online (Sandbox Code Playgroud)
我想我想得太重要了,正确的函数方法应该是这样的,带有递归和累加器:
def traverse_and_check(list, acc \\ []) do
case list do
[] -> {:ok, reverse(acc)}
[0 | _tail] -> :error
[x | tail] -> traverse_and_check(tail, [x | acc])
end
end
Run Code Online (Sandbox Code Playgroud)
Elixir 有这样的控制流程吗?
我是否可以在 Elixir 中执行此操作,例如对命名列表进行排序?
list = [9,1,2,3]
Enum.sort(list)
Run Code Online (Sandbox Code Playgroud)
生产
[1,2,3,9]
Run Code Online (Sandbox Code Playgroud)
因为我不知道列表中有哪些数字,所以无法具体输入。
Erlang 有inet.gethostname来获取主机名:
iex(1)> {:ok, hostname} = :inet.gethostname
{:ok, 'Michaels-MacBook-Pro'}
Run Code Online (Sandbox Code Playgroud)
但是如何获取域名呢?
嘿,我正在尝试进行查询,我的代码如下所示:
def show(conn, _params) do
user = Guardian.Plug.current_resource(conn)
team = Web.get_team!(user.team.id)
score_query =
from(
u in User,
where: u.team.id == team.id,
select: sum(u.score)
)
team_score = Repo.all(score_query)
IO.puts("score")
IO.inspect(team_score)
conn |> render("team.json", team: team)
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我收到一条错误消息:
** (Ecto.Query.CompileError) unbound variable `team` in query
Run Code Online (Sandbox Code Playgroud)
但为什么是未绑定的?我该如何修复它以及为什么会发生这种情况?
我ets在一个长生不老药应用程序中有一个设置表。我需要清理其 updated_at 字段超过 10 秒的记录。有没有一种方法可以设置到期时间或手动完成而不遍历所有记录?我根据大于给定时间的时间戳匹配记录。
示例记录:
key: key_1
record: %{id: key_1, updated_at: ~N[2018-12-19 10:08:47.803075]}
Run Code Online (Sandbox Code Playgroud)
到目前为止我有这个代码
def clean_stale(previous_key) do
if previous_key == :"$end_of_table" do
:ok
else
device = get(previous_key)
next_key = :ets.next(__MODULE__, previous_key)
if NaiveDateTime.diff(NaiveDateTime.utc_now, device.last_recorded_at) > 10 do
remove(device.id)
end
clean_stale(next_key)
end
end
Run Code Online (Sandbox Code Playgroud) 我正在使用 Elixir 为二叉搜索树编写一些程序,但遇到了一个带有递归计算高度函数的障碍。
树高的基本递归公式如下。
别的
递归获取左子树的最大深度,即调用 maxDepth( tree->left-subtree)
递归获取右子树的最大深度,即调用 maxDepth( tree->right-subtree)
获取左右子树最大深度的最大值,并为当前节点加1。
max_depth = max(max dept of left subtree,max depth of right subtree) + 1
返回最大深度
就我而言,当我尝试使用通用节点结构测试该函数时,出现以下错误。
** (ArithmeticError) 算术表达式中的错误参数
我尝试将 1 添加到 left_depth 和 right_depth 中。这消除了算术错误,但我也没有得到任何数字结果(高度)显示。
这是我的高度函数。如您所见,它几乎完全遵循递归格式。
# calculate the height
@spec height(bst_node) :: number
def height(node) do
if is_nil(node) do
IO.puts "No tree exists. The height is 0"
else
left_depth = height(node.left)
right_depth = height(node.right)
if left_depth >= right_depth do
left_depth + 1
IO.puts "The height …Run Code Online (Sandbox Code Playgroud) ElixirRegex基于Erlang:re模块,该模块在其文档中指出
Perl 支持字符类的 Posix 表示法。这使用由 [: 和 :] 括在方括号内的名称。PCRE 也支持这种表示法。
POSIX 字符类可以用\p操作符 à la Unicode Properties或括号类表示法表示,如此处和:re文档中所述。
然而,这些是我在使用 Elixir 1.9 的系统上看到的结果:
iex(1)> Regex.run(~r/\p{L}+/, "Götterfunken")
[<<71, 195>>]
Run Code Online (Sandbox Code Playgroud)
我希望上面的内容匹配搜索词中的所有字符。
iex(2)> Regex.run(~r/[[:alpha:]]+/, "Götterfunken")
[<<71, 195>>]
Run Code Online (Sandbox Code Playgroud)
同样,我希望所有字符都匹配,但我不确定是否:alpha:应该匹配非拉丁字符。
iex(3)> Regex.run(~r/[[:punct:]]/, "Götterfunken")
[<<182>>]
Run Code Online (Sandbox Code Playgroud)
在这里,我预计根本没有比赛。
这是 Elixir 中损坏的 Regex 实现,还是我无法理解正确的用法?
我正在尝试使用该Enum.map函数迭代一个字符列表,但它的行为与我期望的行为不同。
第一行不将字符列表视为字符列表,在第二行中我不明白为什么修改匿名函数会这样做。
iex(32)> Enum.map('ABCD',fn(char)->char end)
'ABCD'
iex(33)> Enum.map('ABCD',fn(char)->[char] end)
['A', 'B', 'C', 'D']
Run Code Online (Sandbox Code Playgroud)
我期待第一行返回['A','B','C','D'],第二行返回[['A'],['B'],['C'],['D']]。为什么不是这种情况?