elixir

Get Random Element(s) from a List

烈酒焚心 提交于 2019-12-09 07:34:56
问题 I'm basically looking for an Elixir equivalent of Ruby's Array#sample. Something that would let me do this: list = [1,2,3,4,5,6,7] sample(list) #=> 4 sample(list, 3) #=> [6, 2, 5] I didn't find anything in the Elixir List Docs either. 回答1: Updated Answer As José Valim said in his answer, in Elixir 1.1 and above, you can now use these methods to get random element(s) from a list: Enum.random/1 - For getting single random element Enum.take_random/2 - For getting multiple random elements Example

How to rollback, reset, or drop Ecto test database?

早过忘川 提交于 2019-12-09 05:12:43
问题 Usually mix.test cleans the test database, but it is not working. It may be because I was playing around with making a users schema, but didn't want to use what I made so I got rid of it. I then started over and made a new schema for users which was different from the first. When I tried to run mix test again, there was an error that some fields did not exist which should have been there with the new schema. 回答1: You can access the test database by using MIX_ENV=test followed by a command

Fill a List with a for-loop

烂漫一生 提交于 2019-12-09 03:05:50
问题 Why can't I fill a list with this simple for-loop? new_data = [] for data <- old_data do new_data = List.insert_at(new_data, -1, data) end After this operation my new_data list is still empty, even though the loop is N times executed. 回答1: In Elixir, you can't mutate the value your variable is referencing as explained in Are Elixir variables really immutable?. For in this instance is not a "loop" it is a list comprehension. You can assign to the result of a comprehension with: new_data = for

Delete link not working phoenix

三世轮回 提交于 2019-12-09 00:28:58
问题 I've used Phoenix's built in gen.HTML to generate a simple view but it's not working <%= link "Delete", to: event_path(@conn, :delete, event), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %> And on the page it looks like it suppose to but it just append # to address Generated structure: <form action="/event/1" class="link" method="post"> <input name="_method" type="hidden" value="delete"> <input name="_csrf_token" type="hidden" value=

Create Github Token using Elixir HTTPoison Library

依然范特西╮ 提交于 2019-12-08 20:16:25
问题 I want to create a Github token in Elixir using HTTPoison library, but i just cant not figure out how to send HTTPoison the parameter. When using curl , it will be something like this $ curl -i -u "ColdFreak" -H "X-GitHub-OTP: 123456" -d '{"scopes": ["repo", "user"], "note" : "getting-started"}' https://api.github.com/authorizations when I use the HTTPoison library, I just cannot figure out how to post it . url = "https://api.github.com/authorizations" HTTPoison.post!(url, [scopes: ["repo",

Distributed Elixir example doesn't work

北战南征 提交于 2019-12-08 19:47:40
问题 I have a very simple Elixir code sample I want to run on different nodes. The first node is on my laptop, the second one is a Raspberry Pi, accessed via SSH. The code is simple: # node1@my-computer defmodule Hello do def world, do: IO.puts "hello world" end # node2@raspberrypi iex> Node.spawn_link :"node1@my-computer", fn -> Hello.world end I expected that Node.spawn_link would print "hello world" on the Raspberry Pi, but instead it shows an error saying ** (EXIT from #PID<0.66.0>) no connect

Is that possible to get comments with macro?

泪湿孤枕 提交于 2019-12-08 19:36:13
问题 I was trying to parse some code and reformat them, but it seems that quote will just ignore the comments. Is there any way to achieve this? I guess I have to dive into the erlang side? 回答1: No, you cannot get code comments inside macros. They never become part of the AST and are discarded still in Elixir's tokenizer. 回答2: It seems that comments are handled at the tokenizer level, so the parser will not even see them. The relevant parts from the elixir tokenizer indicate that comments are

Querying by DateTime in Ecto

泄露秘密 提交于 2019-12-08 17:20:44
问题 Here is what I have tried. date = Ecto.DateTime.from_erl(:calendar.universal_time()) query |> where([record], record.deadline >= ^date) I also tried date = Ecto.DateTime.from_erl(:calendar.universal_time()) query = from m in MyApp.SomeModel, where: m.deadline >= ^date, select: m Both return same message value `%Ecto.DateTime{..}` in `where` cannot be cast to type :datetime in query From what I understand I am supposed to be using Ecto.DateTime in my queries. Maybe I am missing something

Why does Phoenix (ecto/Postgresx) fail to Connect in dev

自作多情 提交于 2019-12-08 16:38:22
问题 I am beginning my Elixir/Phoenix journey and having some trouble with my postgres connection. When I start up my server I get: $ mix phoenix.server [error] Postgrex.Protocol (#PID<0.214.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused [error] Postgrex.Protocol (#PID<0.217.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused [error] Postgrex.Protocol (#PID<0.218.0>) failed to connect: ** (Postgrex.Error) tcp

Handle incoming post json with phoenix

雨燕双飞 提交于 2019-12-08 15:56:23
问题 I'd like to handle an incoming POST with application/json content type. I am simply trying to return posted JSON as response to test like this: WebhookController controller pipeline :api do plug :accepts, ["json"] end def handle(conn, params) do {:ok, body, conn} = Plug.Conn.read_body(conn) json(conn, %{body: body}) end router.ex scope "/webhook", MyApp do pipe_through :api post "/handle", WebhookController, :handle end If the incoming post has content type application/json , then body is