我想一次迭代两个列表,但无法弄清楚如何.for循环,允许你有多个列表作为循环嵌套,我不熟悉Enum,但也许有一个解决方案.
Python有zip(list_1,list_2)和enumerate(list_1)做到这一点
在Tcl对我来说更容易:
% foreach var1 [list 1 3] var2 [list 2 4] {
puts "$var1 $var2"
}
1 2
3 4
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何在Elixir中做到这一点.
我想这样做的原因是因为我有这样的地图列表
[%{"a" => "1539", "b" => "300"},
%{"a" => "4095", "b" => "0"},
%{"a" => "5371", "b" => "0"},
%{"a" => "7524", "b" => "0"},
%{"a" => "8267", "b" => "27"}]
Run Code Online (Sandbox Code Playgroud)
我有一个相同大小的项目列表:
["a","b","c","d","e"]
Run Code Online (Sandbox Code Playgroud)
如果地图中的某个元素是某个条件(不是0),我想通过循环一起运行它们来替换地图中的项目:
[%{"a" => "a", "b" => "300"}, # notice the change in element "a"
%{"a" => "4095", "b" …Run Code Online (Sandbox Code Playgroud) 我正在使用 phoenix 创建服务器并使用编辑器 VSCode。
当我启动 server:mix phx.server并且我更改了代码时,它不会重新编译,我必须关闭并再次运行。
应该在可以自动重新编译的地方设置扩展或配置吗?
文件dev.exs
config :jwtuser, Jwtuser.Endpoint,
http: [port: 5000],
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin",
cd: Path.expand("../assets", __DIR__)]]
Run Code Online (Sandbox Code Playgroud)
在mix.exs 中
def project do
[
app: :jwtuser,
version: "0.0.1",
elixir: "~> 1.4",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix, :gettext] ++ Mix.compilers,
start_permanent: Mix.env == :prod,
aliases: aliases(),
deps: deps(),
erlc_options: erlc_options()
]
end
Run Code Online (Sandbox Code Playgroud) 在凤凰框架中
def show(conn, %{"id" => id}) do
json conn, Repo.get(User, id)
end
Run Code Online (Sandbox Code Playgroud)
匹配正常,但使用:符号不匹配模式
def show(conn, %{"id": id}) do
json conn, Repo.get(User, id)
end
Run Code Online (Sandbox Code Playgroud)
当我从测试中调用以下代码时
conn
|> get(user_path(conn, :show, user.id))
|> json_response(200)
Run Code Online (Sandbox Code Playgroud) 我有一个路由器模块,可以将请求转发到其他路由器。在这个路由器中,我有一个由plug(:match)和组成的管道plug(:dispatch)。
defmodule Example.Router do
use Plug.Router
plug(:match)
plug(:dispatch)
forward("/check", to: Example.Route.Check)
get("/", do: send_resp(conn, 200, "router"))
end
Run Code Online (Sandbox Code Playgroud)
在第二个模块中,我有相同的管道:
defmodule Example.Route.Check do
use Plug.Router
plug(:match)
plug(:dispatch)
get "/", do: send_resp(conn, 200, "ok")
end
Run Code Online (Sandbox Code Playgroud)
我在这里看到的问题是我似乎总是需要plug(:match)并且plug(:dispatch)在所有Plug路由器中。所以我有以下问题:
在Aleksei回答的最后一个问题中,我试图查找给定日期的上个月。现在,我正尝试做相反的事情:
defmodule Dating do
def next_month(%Date{year: year, month: month, day: day} = date) do
first_day_of_next_month = Date.add(date, Calendar.ISO.days_in_month(year, month) - day + 1)
%{year: year, month: month} = first_day_of_next_month
Date.add(first_day_of_next_month, min(day, Calendar.ISO.days_in_month(year, month)) - 1)
end
end
Run Code Online (Sandbox Code Playgroud)
尽管代码可以正常运行,但我希望有更好的方法可以做到这一点:
iex|1 ? Dating.next_month(~D[2018-12-31])
#? ~D[2019-01-31]
iex|2 ? Dating.next_month(~D[2018-02-28])
#? ~D[2018-03-28]
iex|3 ? Dating.next_month(~D[2018-01-31])
#? ~D[2018-02-28]
iex|3 ? Dating.next_month(~D[2018-01-30])
#? ~D[2018-02-28]
Run Code Online (Sandbox Code Playgroud)
注意:请不要建议使用第三方Elixir软件包
我尝试了几次不同的迭代,但是在编译时始终出现此错误:
Routes.session_path/2 is undefined (module Routes is not available)
Run Code Online (Sandbox Code Playgroud)
我的代码:
Routes.session_path/2 is undefined (module Routes is not available)
Run Code Online (Sandbox Code Playgroud)
在Phoenix 1.4中有所不同,尝试在此处参考文档https://hexdocs.pm/phoenix/Phoenix.Router.html,但仍然没有运气。
我最近开始学习 Elixir。来自面向对象的编程背景,我无法理解 Elixir 函数。
我正在关注 Dave Thomas 的书Programming Elixir >= 1.6,但我不太明白函数是如何工作的。
在书中,他举了以下例子:
handle_open = fn
{:ok, file} -> "Read data: #{IO.read(file, :line)}"
{_, error} -> "Error: #{:file.format_error(error)}"
end
handle_open.(File.open(?"??code/intro/hello.exs"?)) ?# this file exists?
-> "Read data: IO.puts \"Hello, World!\"\n"
handle_open.(File.open(?"??nonexistent"?)) ?# this one doesn't?
-> Error: no such file or directory"
Run Code Online (Sandbox Code Playgroud)
我不明白参数是如何工作的。是否有隐含的 if, else 语句隐藏在某处?
我目前正在使用此代码重新启动我的 Android 设备:
try {
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot" });
proc.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
现在,我想在HotBoot (or "Hot Reboot")我的 Android 手机上使用 Java。有谁知道这样做的方法?
(如果您不知道 HotBoot 是什么,请参阅此链接)
在我了解细节之前,我想清楚地表明我确实理解Java是按值传递而不是传递参考,但是我有点困惑,因为我看到一些函数就是这样做的.我想知道他们是怎么做到的.
我想发生什么:
public void yo() {
int a = 1;
String b = "hello";
Boolean c = false;
doSomething(a,b,c);
// a,b,c should have new values
}
public void doSomething(int x, String y, Boolean z) {
// Do Something
}
Run Code Online (Sandbox Code Playgroud)
我见过的功能是这样的:
// Works in Android Development
int[] location = new int[2];
view.getLocationInWindow(location);
// `location` has a new value now
// Same happens when you run getLocationOnScreen(arg)
Run Code Online (Sandbox Code Playgroud)
这是如何运作的?
我试图获得17位数的列表数字,然后将其拆分Integer.digits,并得到这些数字的总和.不幸的是我一直在意外行为(我的结果包含一串字母)并且不确定这是一个错误还是个人错误.
下面是iex错误的控制台,结果给出'Q $':
iex(4)> numbers = [
...(4)> [1, 4, 8, 1, 3, 6, 9, 4, 2, 5, 5, 6, 3, 1, 8, 8, 7],
...(4)> [1,5, 9, 4, 1, 3, 2, 7],
...(4)> [1, 5, 4, 6, 5, 7, 8]
...(4)> ]
iex(5)> Enum.map(numbers, fn x -> Enum.sum(x) end)
'Q $'
Run Code Online (Sandbox Code Playgroud)
但是当我从最后一个列表中删除一个数字时,它再次正常工作,并给出预期的结果
iex(1)> numbers = [
...(1)> [1, 4, 8, 1, 3, 6, 9, 4, 2, 5, 5, 6, 3, 1, 8, 8, …Run Code Online (Sandbox Code Playgroud)