elixir

How to save/log the output of the iex shell to get persistent command history?

送分小仙女□ 提交于 2019-12-17 21:52:40
问题 I just started working with Elixir and have been using the iex shell quite a bit. Is it possible for me to save / log a "session" to a file? Thank you. 回答1: In Erlang/OTP-20 and higher Since Erlang/OTP-20rc2, Shell history is supported out of the box (although initially disabled by default) through a port of this library to the Erlang/OTP code base. Enable the shell in these versions by setting the shell_history kernel environment variable to enabled with export ERL_AFLAGS="-kernel shell

What is the benefit of Keyword Lists?

☆樱花仙子☆ 提交于 2019-12-17 21:27:44
问题 In elixir we have Maps: > map = %{:a => "one", :b => "two"} # = %{a: "one", b: "two"} > map.a # = "one" > map[:a] # = "one" We also have Keyword Lists: > kl = [a: "one", b: "two"] # = [a: "one", b: "two"] > kl2 = [{:a, "one"},{:b, "two"}] # = [a: "one", b: "two"] > kl == kl2 # = true > kl[:a] # = "one" > kl.a # = ** (ArgumentError) Why both? Syntax? Is it because Keyword Lists have a more flexible syntax allowing them to be defined without curlys and even without brackets as the last param of

how can i set session in setup when i test phoenix action which need user_id in session?

岁酱吖の 提交于 2019-12-17 19:33:54
问题 I have a test which need to set user_id to session before testing, because this action need to know the current_user. setup do %User{ id: 123456, username: "lcp", email: "abc@gmail.com", password: Comeonin.Bcrypt.hashpwsalt("password") } |> Repo.insert {:ok, user: Repo.get(User, 123456) } end test "POST /posts", context do # conn = conn() # |> put_session(:user_id, context[:user].id) # |> post("/posts", %{ post: %{ title: "title", body: "body" } }) # assert get_flash(conn, :info) == "Post

Erlang Processes vs Java Threads

旧时模样 提交于 2019-12-17 07:10:03
问题 I am reading "Elixir in Action" book by Saša Jurić, and in the first chapter it says: Erlang processes are completely isolated from each other. They share no memory, and a crash of one process doesn’t cause a crash of other processes. Isn't that true for Java threads as well? I mean when Java thread crashes, it too does not crash other threads - especially, if we are looking at request-processing threads (lets exclude the main thread from this disucussion) 回答1: Repeat after me: "These are

Phoenix/Ecto - converting ISO string into utc_datetime primitive type

旧城冷巷雨未停 提交于 2019-12-14 02:27:43
问题 In my Phoenix app, I'm trying to insert an event record into the database that has fields for start_time and end_time - the datetime data will already be converted to ISO string format on the client and passed to the Phoenix API as JSON data, but this is causing me some trouble when I try to make an insert - the model is expecting those values to be :utc_datetime so I need to convert them - I read through the documentation, but I'm still not sure... First off, here's the schema for the model:

ETS creation return value

混江龙づ霸主 提交于 2019-12-14 01:27:24
问题 I'm using Elixir 1.6.3. I'm working with the Erlang :ets module in Elixir, and I'm a bit confused by the return value of the :ets.new/2 function. According to the doc's example, when calling :ets.new(:whatever, []) , I should be returned what appears to be an integral value: iex> table = :ets.new(:buckets_registry, [:set, :protected]) 8207 However, when I run the exact same code in iex , I get a reference: iex(1)> table = :ets.new(:buckets_registry, [:set, :protected]) #Reference<0.1885502827

Mix task “release” not found error

◇◆丶佛笑我妖孽 提交于 2019-12-13 22:21:59
问题 I was setting up an Ubuntu 16.04 server for my Elixir/Phoenix application with edeliver when edeliver failed with the error message ** (Mix) The task "release" could not be found along with a exit code of 1. If I go to my build server, git pull my app and run MIX_ENV=prod mix release or just mix release after getting dependencies, I get the same error. However, the same application works without any problem locally. My erlang version is erts-9.0 and elixir version is 1.5.5 on both the server

How to use a Phoenix controller variable in JavaScript?

孤街浪徒 提交于 2019-12-13 19:08:19
问题 How do I use a variable from Phoenix controller in my JavaScript? I'm using React for front-end. I can't use <%= ... %> in JavaScript. defmodule Familytree.PageController do use Familytree.Web, :controller alias Familytree.Users alias Familytree.Tags alias Familytree.UserTag plug Familytree.Plugs.Auth plug :scrub_params, "users" when action in [:create, :update] plug :scrub_params, "tags" when action in [:add_tag,:delete_tag] def index(conn, _params) do users = Repo.all(Users) tags = Repo.all

Updating a nested map in Elixir

纵饮孤独 提交于 2019-12-13 15:39:38
问题 I have a 2-level-nested map, how can I update each value on the 2nd level? Right now I'm doing this: items = Enum.map(items, fn(a) -> a.items2 = Enum.map(a.items2, fn(a2) -> Map.put(x2, :some_key, 123) end) a end) An error: cannot invoke remote function "a.items2/0" inside match. I basically know what this means, but how to fix it? Note that a.items2 might also has a nested map in itself. 回答1: You can use Map.put outside as well: items = Enum.map(items, fn(a) -> Map.put(a, :items2, Enum.map(a

Elixir 1.3.0: String.strip/1 and String.strip/2 API documentation missing.

可紊 提交于 2019-12-13 12:26:17
问题 It seems that Elixir 1.3.0 doesn't show anymore the documentation for String.strip/1 and String.strip/2: iex(1)> h String.strip No documentation for String.strip was found they are missing also in the current online documentation (v1.3, Master, Stable), but the functions are still recognized by the compiler: iex(2)> String.strip(" Hallo, World! ") "Hallo, World!" So I'm wondering if this is just a bug in String documentation, or if these functions are going to become deprecated. I can't find