elixir

Elixir: Merge list with same map keys to one map

北城以北 提交于 2019-12-11 06:48:36
问题 How can I convert this list in elixir [ %{key1: 1, key2: 2, key3: 3}, %{key1: 4, key2: 5, key3: 6}, %{key1: 7, key2: 8, key3: 9} ] to this map %{key1: [1, 4, 7], key2: [2, 5, 8], key3: [3, 6, 9]) ? 回答1: Enum.reduce(input, %{key1: [], key2: [], key3: []}, fn m, acc -> Map.merge(acc, m, fn _k, v1, v2 -> v1 ++ [v2] end) end) #⇒ %{key1: [1, 4, 7], key2: [2, 5, 8], key3: [3, 6, 9]} or, without preliminar hardcoded accumulator: Enum.reduce(input, %{}, fn m, acc -> Map.merge(acc, m, fn _k, v1, v2

Why Can't I Delete Files With A Tilde?

帅比萌擦擦* 提交于 2019-12-11 06:44:37
问题 Elixir 1.3.2 Erlang/OTP 19 Windows 7 (Standard User and Administrator) I'm trying to hack together a small Elixir script to wrap around commiting files to git. The first thing I do, in order to insure I don't accidentally stage temporary files created by emacs is File.rm("*~"). However, this is what I get: iex(2)> File.rm("*~") {:error, :eio} So I tried File.rm("*.*~") . Same result. I get the same behavior when I run as either a Standard User (which is how I normally work on Windows) or as

Dialyzer error for struct

走远了吗. 提交于 2019-12-11 06:36:32
问题 Here's a minimum broken example in Elixir 1.3: defmodule Foo do @type t :: %__MODULE__{x: non_neg_integer} defstruct x: 0 @spec test(t) :: t def test(%__MODULE__{} = foo), do: test2(foo) @spec test2(t) :: t defp test2(%__MODULE__{} = foo), do: %__MODULE__{foo | x: 5} end This fails to type check with: foo.ex:9: The variable _@1 can never match since previous clauses completely covered the type #{'__struct__':='Elixir.Foo', _=>_} . I've searched and searched and cannot for the life of me find

bad request to Lxphnx.ArticleController.create, no matching action clause to process request

江枫思渺然 提交于 2019-12-11 06:24:31
问题 I can do a GET request but I can't seem to get a POST request working. These are my routes article_path GET /api/articles Lxphnx.ArticleController :index article_path GET /api/articles/:id Lxphnx.ArticleController :show article_path POST /api/articles Lxphnx.ArticleController :create article_path PATCH /api/articles/:id Lxphnx.ArticleController :update PUT /api/articles/:id Lxphnx.ArticleController :update article_path DELETE /api/articles/:id Lxphnx.ArticleController :delete I haven't really

Extracting params from a form with deeply nested arrays of values

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 06:07:21
问题 Let's say that I have a form that is used for adding a new product. Each product may have multiple sizes where each size, in its turn, may have multiple options. The form would look something like that: <form accept-charset="UTF-8" action="/products?_format=json" method="post"> <input id="product_name" name="product[name]" type="text"> <input id="product_product_sizes_name" name="product[product_sizes][][name]" type="text"> <input id="product_product_sizes_product_size_options_name" name=

What is the difference between param with name and no name param on Phoenix

扶醉桌前 提交于 2019-12-11 06:04:11
问题 If I write params like (A) and (B) in router.ex . get "/index/:first/:second", IndexController, :index #(A) get "/index/:first", IndexController, :index #(B) which one should be collect (1) or (2)? conn |> redirect(to: index_path(conn, :index, first: first, second: second)) #(1) conn |> redirect(to: index_path(conn, :index, first, second: second)) #(2) I have no idea of difference to explain. 回答1: I am not sure I understood the question but off the top of my head, I think, first, after

Read files in Phoenix in production mode

本秂侑毒 提交于 2019-12-11 06:03:46
问题 I've Phoenix app (I created it with --api flag) that has some additional data required to process requests that I store in priv/data/filters.csv . So it works fine in dev mode, I can read this file like: File.stream!("priv/data/filters.csv") , but once app is delivered using mix edeliver update this file is not accessible anymore. I can see this folder in build directory, but after app is delivered there is no such folder priv . I'm not sure why it's not copied to deliver folder. What I'm

Retry function on failure after x amount of time

折月煮酒 提交于 2019-12-11 06:02:46
问题 PROBLEM: I have a function that gets triggered VIA Cronjob. This cronjob fires the function at 10:00pm but let's say the data required is not present for the function to properly be handled. I want to fail but then have the function retry an 1 hour. How would I handle this problem? CODE: def check_question do case question = Repo.get_by(Question, active: true, closed: true) do question when not(is_nil(question)) -> case ActiveQuestion.ready_for_answer_status(conn, question) do end _ -> #

Mix.exs file changes - (SyntaxError) mix.exs:65: unexpected token: “” (column 1, codepoint U+0000)

家住魔仙堡 提交于 2019-12-11 05:59:18
问题 saving any changes made to mix.exs file on Mac OS High Sierra 10.13.6 results in null character at beginning of file. I originally opened this against the intellij elixir plugin, but the problem is created from using ANY text editor (sublime, vim, intellij). demo of issue: https://github.com/KronicDeth/intellij-elixir/issues/1165 What Mac setting is this? How do I fix? ** (SyntaxError) mix.exs:65: unexpected token: "" (column 1, codepoint U+0000) (elixir) lib/code.ex:629: Code.load_file/2

phoenix pass variable to view

两盒软妹~` 提交于 2019-12-11 05:58:30
问题 i have a query result that will be used in view, but when i call the variable in view, i got undefined function data/0 (because data is not function) i already pass data with different way, map, etc controller.ex : def new(conn, _params) do data = Enum.map( from(AccountTypeSchema) |> Repo.all(), fn elem -> elem |> Map.from_struct() |> Map.delete(:__meta__) end ) render(conn, "new.html", data: data) end view.html.eex : <% Enum.each data, fn(item) -> %> <option value="us">Facebook</option> <%