elixir

What does “|>” mean in elixir?

故事扮演 提交于 2019-12-20 17:24:26
问题 I'm reading through some code elixir code on github and I see |> being used often. It does not appear in the list of operation on the documentation site. What does it mean? i.e. expires_at: std["expires_in"] |> expires_at, 回答1: This is the pipe operator. From the linked docs: This operator introduces the expression on the left-hand side as the first argument to the function call on the right-hand side. Examples iex> [1, [2], 3] |> List.flatten() [1, 2, 3] The example above is the same as

How to generate a random url safe string with Elixir

六月ゝ 毕业季﹏ 提交于 2019-12-20 16:13:35
问题 I need to be able to generate random url safe strings so I could use those in links (like in an activation link sent to a user's email), so how can I generate it? Is there a way to do that only with Elixir or I'd have to use some library? 回答1: What you can do instead is to generate a Base64-encoded string to be used as a confirmation token. This confirmation token will then be saved to your DB and passed as params to the activation link. Your activation url would look something like:

How to configure the Plug.Static without Phoenix

纵然是瞬间 提交于 2019-12-20 14:10:52
问题 I am trying to figure out how to configure Plug.Static without any other framework (Phoenix, Sugar, etc); just Cowboy, Plug and Elixir. I just don't know how to put things together in the Router. plug :match plug Plug.Static, at: "/pub", from: :cerber plug :dispatch get "/" do Logger.info "GET /" send_resp(conn, 200, "Hello world\n") end Is the declaration of Plug.Static at the right place ? Shouldn't it be after plug :dispatch ? Do I need to define an additional route With this declaration:

How do I load an Elixir library into iex without adding it to a project's mix.exs deps?

北城以北 提交于 2019-12-20 12:14:54
问题 I want to tryout the Poison json module without creating a mix project. How do I install it and make it available in iex via import? I have been able to add it to a project, then use it after going into the project directory and using iex -S mix: tbrowne@LILJEN:~/code/elixirTry/pj$ cat mix.exs defmodule Pj.Mixfile do use Mix.Project def project do [app: :pj, version: "0.0.1", elixir: "~> 1.2", build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps] end # Configuration

How do I load an Elixir library into iex without adding it to a project's mix.exs deps?

不羁的心 提交于 2019-12-20 12:14:46
问题 I want to tryout the Poison json module without creating a mix project. How do I install it and make it available in iex via import? I have been able to add it to a project, then use it after going into the project directory and using iex -S mix: tbrowne@LILJEN:~/code/elixirTry/pj$ cat mix.exs defmodule Pj.Mixfile do use Mix.Project def project do [app: :pj, version: "0.0.1", elixir: "~> 1.2", build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps] end # Configuration

How do I load an Elixir library into iex without adding it to a project's mix.exs deps?

可紊 提交于 2019-12-20 12:14:03
问题 I want to tryout the Poison json module without creating a mix project. How do I install it and make it available in iex via import? I have been able to add it to a project, then use it after going into the project directory and using iex -S mix: tbrowne@LILJEN:~/code/elixirTry/pj$ cat mix.exs defmodule Pj.Mixfile do use Mix.Project def project do [app: :pj, version: "0.0.1", elixir: "~> 1.2", build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps] end # Configuration

How do I run a beam file compiled by Elixir or Erlang?

末鹿安然 提交于 2019-12-20 08:35:57
问题 I have installed Erlang/OTP and Elixir, and compiled the HelloWorld program into a BEAM using the command: elixirc test.ex Which produced a file named Elixir.Hello.beam How do I run this file? 回答1: Short answer: no way to know for sure without also knowing the contents of your source file :) There are a few ways to run Elixir code. This answer will be an overview of various workflows that can be used with Elixir. When you are just getting started and want to try things out, launching iex and

dialyxir mix task to create PLT exits without error or creating table

喜夏-厌秋 提交于 2019-12-20 06:39:55
问题 I am trying to use dialyxir to run dialyzer analysis on my project through the mix tasks it provides. I have added it to my dependencies and compiled as per the README. When I run the mix dialyxir.plt it reports no error and yet exits without creating the table. $ mix dialyzer.plt Starting PLT Core Build ... this will take awhile dialyzer --build_plt --output_plt /home/vagrant/.dialyxir_core_19_1.3.2.plt --apps erts kernel stdlib crypto public_key -r /usr/local/lib/elixir/bin/../lib/elixir/..

Ecto remove preload

随声附和 提交于 2019-12-19 09:37:36
问题 Is there any way to do the inverse to preload? %Post{ comments: [] } posts = Repo.all(Post) |> Repo.unload(:comments) %Post{ comments: #Ecto.Association.NotLoaded<association :comments is not loaded>, } 回答1: Ecto.Association.NotLoaded is a plain old simple struct, so you might relatively easy implement this unpreload youself: defmodule Unpreloader do def forget(struct, field, cardinality \\ :one) do %{struct | field => %Ecto.Association.NotLoaded{ __field__: field, __owner__: struct.__struct_

How do I set the default Elixir mix task

余生长醉 提交于 2019-12-19 05:24:14
问题 If I have a mix.exs file something like: defmodule Mix.Tasks.My_task do use Mix.Task @shortdoc "Perform my task" def run(_) do IO.puts "Working" end end defmodule ElixirKoans.Mixfile do use Mix.Project def project do ... end I can happily run this with mix my_task . How do I make my_task be the default, so it is executed when I run mix without a task? 回答1: It looks like you can define inside the project block (mix.exs) using default_task : def project do [default_task: "run"] end More info: