elixir

How to map and reduce List of Maps in Elixir

女生的网名这么多〃 提交于 2019-12-12 04:27:58
问题 What's a good way to map and reduce the elixir's list and convert it into new list. Requirement: 1. Find the maps with same id: 2. Merge the values of "role" key (i.e. Gather all the unique values). 3. For all other maps(elements of list), do nothing. list = [%{"id": 1, "role": ["A", "B"]}, %{"id": 2, "role": ["B", "C"]}, %{"id": 1, "role": ["C", "A"]} ] needs to be converted in the following list: ans_list = [%{"id": 1, "role": ["A", "B", "C"]}, %{"id": 2, "role": ["B", "C"]}] 回答1: You can

Elixir “Losing” Processes

那年仲夏 提交于 2019-12-12 04:22:42
问题 If I create a file loop.exs : Enum.each(1..40000, fn (n) -> spawn fn -> IO.puts(n) end end) And run it, counting the lines of output: elixir loop.exs | wc -l And on subsequent runs, I may see the expected 40000 lines, but I might see less. In my tests, I've seen 39752 , 39934 , 39673 , etc. This suggests to me that certain processes aren't getting to call IO.puts , so what is happening to them, why aren't I warned they've gone missing, and what am I doing wrong that is making this happen? 回答1

Assign current user in session for tests

谁说我不能喝 提交于 2019-12-12 03:41:16
问题 I have found similar answers to this here and here, but it does not seem to be working. I am having trouble assigning the current_user to the session when testing. My tests are set up like this setup do %User{ id: 123456, name: "MyName", email: "abc@gmail.com", password_hash: Comeonin.Bcrypt.hashpwsalt("password") } |> Repo.insert {:ok, user: Repo.get(User, 123456) } end and my test is test "renders index.html on /coping-strategy", %{user: user} do conn = conn |> assign(:current_user, user) |

How to implement to_query(data) in Elixir Struct

泪湿孤枕 提交于 2019-12-12 03:38:50
问题 I am attempting to update a existing records in my database using Repo.update: def subscribe_email(conn, %{"email-address"=>email_address, "shop"=>shop}) do current_record = Repo.all(%Oauth.EmailAddress{email_address: email_address, active: false, shop: shop, verified: :true}) current_record = Ecto.Changeset.change(current_record, active: :true) case Repo.update current_record do {:ok, struct} -> IO.puts "updated correctly." {:error, changeset} -> IO.puts "did not update" end end I have a

phoenix elixir binary data image

 ̄綄美尐妖づ 提交于 2019-12-12 03:25:25
问题 Im using the phoenix framework to create a webpage and created an upload form to give the user the possiblity to upload a profil picture. def update(conn, %{"id" => id, "user" => %{"photo" => file}}) do if(File.exists?(file.path)) do case File.read(file.path) do {:ok, body} -> data = IO.iodata_to_binary(body) changeset = Whiteboard.File.changeset(%Whiteboard.File{}, %{user_id: currentuser.id, name: file.filename , data: data}) so that works and the binary data is in the database as bytea

Elixir: (ArithmeticError) bad argument in arithmetic expression

十年热恋 提交于 2019-12-12 03:15:08
问题 I have a simple calculation: Enum.reduce(1..5, 0, &:math.pow/2) But I get this error: ** (ArithmeticError) bad argument in arithmetic expression (stdlib) :math.pow(5, 262144.0) (elixir) lib/enum.ex:1478: anonymous fn/3 in Enum.reduce/3 (elixir) lib/range.ex:80: Enumerable.Range.reduce/5 (elixir) lib/enum.ex:1477: Enum.reduce/3 Is there a limitation in Elixir for arithmetic calculations? 回答1: You're trying to calculate 5^262144 - it's such a huge number erlang bails out. Such number can't even

Unable to read body returned by HTTPoison

荒凉一梦 提交于 2019-12-12 02:16:29
问题 I have this: HTTPoison.start case HTTPoison.get("some url") do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> IO.puts(body) Which produces an exception: Generated escript grex_cli with MIX_ENV=dev ** (ArgumentError) argument error (stdlib) :io.put_chars(#PID<0.49.0>, :unicode, [<<60, 33, 100, 111,..... 58, ...>>, 10]) (elixir) lib/kernel/cli.ex:76: anonymous fn/3 in Kernel.CLI.exec_fun/2 How can I read the body and print it? 回答1: IO.puts fails with that error because body here is

Subscription with Authorization: Skip trigger if condition isn't met

断了今生、忘了曾经 提交于 2019-12-12 01:09:50
问题 I have a Phoenix web application and am using Absinthe for subscriptions that are triggered whenever a new Comment is added to a Topic . I am trying to send a subscription only if the user is a member of a topic, and otherwise not send something at all. So far I've tried playing with config as mentioned in the Subscription docs but it seems like that it is only executed when creating the subscription and not when it is triggered. I'm hoping it is something as simple as: resolve fn comment, _,

Elixir: how can I leverage release_handler?

帅比萌擦擦* 提交于 2019-12-11 23:33:08
问题 I know there are projects such as exrm which do that for you, and it seem to do it extremely well. However, I would like to manually play with release_handler for educational purposes, and I find little documentation or articles on the subject. 回答1: Have you read the Learn You Some Erlang article about release handling? Doing upgrades with release handler is both easy and difficult. Easy because once you know all the nitty gritty details it's pretty much automatic. But difficult, because

Elixir: What does a multiple-generator list comprehension look like without the syntax sugar?

流过昼夜 提交于 2019-12-11 21:42:36
问题 I'm trying to understand list comprehensions in Elixir. The example I'm looking at is producing the permutations of a string from this answer. def shuffle([], _), do: [[]] def shuffle(_, 0), do: [[]] def shuffle(list, i) do for x <- list, y <- shuffle(list, i-1), do: [x|y] end How does this double-generator comprehension look when re-written without the comprehension? I made an attempt to implement the algorithm myself, but my implementation is appending to the list, rather than prepending as