我开始学习Elixir,这也是我的第一个动态语言,所以我真的失去了没有类型声明的函数.
我想做什么:def create_training_data(file_path, indices_path, result_path) do
file_path
|> File.stream!
|> Stream.with_index
|> filter_data_with_indices(indices_path)
|> create_output_file(result_path)
end
def filter_data_with_indices(raw_data, indices_path) do
Stream.filter raw_data, fn {_elem, index} ->
index_match?(index, indices_path)
end
end
defp index_match?(index, indices_path) do
indices_path
|> File.stream!
|> Enum.any? fn elem ->
(elem
|> String.replace(~r/\n/, "")
|> String.to_integer
|> (&(&1 == index)).())
end
end
defp create_output_file(data, path) do
File.write(path, data)
end
Run Code Online (Sandbox Code Playgroud)
当我调用该函数时:
create_training_data("./resources/data/usps.csv","./resources/indices/17.csv","./output.txt")
Run Code Online (Sandbox Code Playgroud)
它返回{:error,:badarg}.我已经检查过,错误发生在create_output_file函数上.
如果我注释掉函数create_output_file,我得到的是一个流(有点意义).问题可能是我不能给Stream to File.write吗?如果是问题,我该怎么办?我在文档上没有找到任何相关内容.
所以,问题是File.write的路径应该没问题,我修改了这个函数:
defp create_output_file(data, path) do
IO.puts("You are trying to write to: …Run Code Online (Sandbox Code Playgroud) 我想在Phoenix Framework中为模型设置默认值,我尝试过:
def new(conn, _params) do
user = get_session(conn, :user)
if is_nil user do
user = Repo.get_by(User, name: "guest")
conn = put_session(conn, :user, user)
end
changeset = Module.changeset(%Module{})
changeset
|> Ecto.Changeset.put_change(:user, user)
|> Ecto.Changeset.put_change(:version, "0.0.0")
|> Ecto.Changeset.put_change(:visibility, "public")
render(conn, "new.html", user: user, changeset: changeset)
end
Run Code Online (Sandbox Code Playgroud)
如何在模型中设置默认值,以便在呈现new.html时显示它们?
顺便说一句,这是我的默认变更集功能.我无法弄清楚如何使用它?我假设在演员之后在管道中使用Ecto.changeset.put_change?
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
Run Code Online (Sandbox Code Playgroud) 我正在使用 elixir_talk 库。连接后,我想在连接到 beanstalkd 后调用私有函数。我刚刚添加了 typespecs 并运行了 Dialyzer(通过dialyxir)。我收到错误:
my_module.ex:3: The specification for 'Elixir.MyModule':f/0 states that the function might also return 'ok' | {'error',_} but the inferred return is none()
my_module.ex:4: Function f/0 has no local return
my_module.ex:14: Function g/1 will never be called
Run Code Online (Sandbox Code Playgroud)
我能找到的产生这个的最小例子是
defmodule MyModule do
@spec f() :: :ok | {:error, term}
def f() do
case ElixirTalk.connect('127.0.0.1', 11300) do
{:ok, conn} ->
g(conn)
{:error, err} ->
{:error, err}
end
end
@spec g(pid) :: :ok
defp g(pid) do
:ok …Run Code Online (Sandbox Code Playgroud) 我正在尝试将映射转换为结构,如下所示:
我有一张地图:
iex(6)> user
%{"basic_auth" => "Basic Ym1hOmphYnJhMTc=", "firstname" => "foo",
"lastname" => "boo"}
Run Code Online (Sandbox Code Playgroud)
该值应应用于结构:
iex(7)> a = struct(UserInfo, user)
%SapOdataService.Auth.UserInfo{basic_auth: nil, firstname: nil, lastname: nil}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,struct 的值是 nil,为什么呢?
我想使用Registry模块来注册动态创建的子进程。所以我将注册表添加到我的主管树中:
def init([]) do
children = [
supervisor(Registry, [:unique, :my_registry]),
supervisor(MyManager, []),
# ...
]
opts = [strategy: :one_for_one, name: MySupervisor]
supervise(children, opts)
end
Run Code Online (Sandbox Code Playgroud)
孩子的注册是这样发生的:
def start_link(%{id: id}) do
GenServer.start_link(__MODULE__, [], name: {:via, Registry, {:my_registry, id}})
end
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是 – 如何在我的注册表中获取所有键(子 pid)。我尝试使用Registry.keys\2,但没有成功。
def get_registry_pid do
Supervisor.which_children(MySupervisor)
|> Enum.filter(fn {id, _, _, _} -> id == Registry end)
|> List.first
|> elem(1) # get a Regisry pid from a tuple {id, pid, type, modules}
end
Registry.keys(:my_registry, get_registry_pid()) # …Run Code Online (Sandbox Code Playgroud) 文档根本没有提到这一点。是否允许嵌套作用域定义,如果允许,web/router.ex 中以下内容的定义行为是什么:
scope "/:locale", MyApp do
pipe_through [:browser, :locale]
scope "/auth", MyApp do
pipe_through [:auth]
get "/", PageController, :dummy
end
get "/", PageController, :dummy
end
Run Code Online (Sandbox Code Playgroud)
管道是否链接,即 /:locale/auth 请求是否通过 :browser、:locale AND :auth 路由?有什么问题吗?
我是 Phoenix 框架的新手,正在寻找一种在应用程序中设置环境变量的方法。我找到的一种方法是here。
但是,我不喜欢这种方式,因为source .env每当我重新启动应用程序时都需要输入。
我尝试如下设置 ENV 变量prod.secret.exs:
config :oauth,
GITHUB_CLIENT_ID: "(CLIENT ID)",
GITHUB_CLIENT_SECRET: "(SECRET)"
Run Code Online (Sandbox Code Playgroud)
我尝试通过以下方式检索 ENV 变量:
config :ueberauth, Ueberauth.Strategy.Github.OAuth,
client_id: System.get_env("GITHUB_CLIENT_ID"),
client_secret: System.get_env("GITHUB_CLIENT_SECRET")
Run Code Online (Sandbox Code Playgroud)
但是,当我使用 运行服务器时mix phoenix.server,它给了我一个如下所示的错误,我不确定它的真正含义:
You have configured application :oauth in your configuration
file, but the application is not available.
This usually means one of:
1. You have not added the application as a dependency in a mix.exs file.
2. You are configuring an application that does not really …Run Code Online (Sandbox Code Playgroud) 在我的 Phoenix 应用程序中,我有一个像Reviews.create(attributes). 它会立即完成一些工作,并开始一项任务(使用Task.async)来完成额外的工作。
我可以这样测试:
{:ok, new_review, task} = Reviews.create(attrs)
# verify that new_review is correct
Task.await(task)
# verify that the task did what it should
Run Code Online (Sandbox Code Playgroud)
但是,我想使用的主要地方Reviews.create/1是来自控制器。从那里开始,我想将任务视为“即发即忘”。这很简单:我只是不调用Task.await.
这工作正常,除了当我测试控制器时,我收到如下错误:
[error] Postgrex.Protocol (#PID<0.344.0>) disconnected: **
(DBConnection.ConnectionError) owner #PID<0.753.0> exited
while client #PID<0.755.0> is still running with: shutdown
Run Code Online (Sandbox Code Playgroud)
换句话说,它告诉我在控制器测试结束时任务被终止了。我不在乎。
有什么方法可以使这更明确地“即发即忘”?例如,如果我可以从控制器调用Task.forget_about(task),那就太好了,但这并不存在。
我已经能够使用 App Engine 设置生产环境,但现在我想考虑设置一个临时环境,但这似乎比我预期的要难。
要设置 prod env,我需要创建一个app.yaml文件:
env: flex
runtime: gs://elixir-runtime/elixir.yaml
runtime_config:
release_app: statcasters
beta_settings:
cloud_sql_instances: statcasters:us-central1:statcastersproduction
Run Code Online (Sandbox Code Playgroud)
这是有道理的,而且非常简单,但是暂存环境呢?如何告诉应用引擎部署不同的应用?我没有在任何地方找到任何好的文档。