elixir

function '<-'/2 undefined Error in receive block Elixir

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 09:57:38
问题 This is my Elixir code. defmodule ErlProcess do def receiver do receive do {:sayHello, msg}->sender<-{:ok, "ok"} end end end But it gives this error. ** (CompileError) spawn.exs:4: function '<-'/2 undefined (stdlib) lists.erl:1323: :lists.foreach/2 (stdlib) erl_eval.erl:569: :erl_eval.do_apply/6 (elixir) src/elixir.erl:140: :elixir.eval_forms/4 (elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/2 How to fix this? 回答1: pid <- msg was removed, please send pid, msg instead. 来源: https:/

Why is x = x +1 valid in Elixir?

萝らか妹 提交于 2019-12-23 09:56:43
问题 Everything I've read about Elixir says that assignment should be thought of as pattern matching. If so then why does x = x + 1 work in Elixir? There is no value of x for which x = x + 1. 回答1: Everything I've read about Elixir says that assignment should be thought of as pattern matching. In Elixir, = is called the pattern match operator, but it does not work the same way as the pattern match operator in Erlang. That's because in Elixir variables are not single assignment like they are in

How can I replace consecutive whitespace with a single space from the whole string

吃可爱长大的小学妹 提交于 2019-12-23 09:26:43
问题 I have a string with 2,3 or more spaces between non-space characters string = "a b c d" What can I do to make it like that: output_string == "a b c d" 回答1: The simplest way would be using Regular Expressions: iex(1)> string = "a b c d" "a b c d" iex(2)> String.replace(string, ~r/ +/, " ") # replace only consecutive space characters "a b c d" iex(3)> String.replace(string, ~r/\s+/, " ") # replace any consecutive whitespace "a b c d" 回答2: For what it's worth, you don't even need the regex: iex

How to implement user authentication in phoenix

不打扰是莪最后的温柔 提交于 2019-12-23 09:25:55
问题 I was creating a webapp in phoenix, I was wondering what could be a better way to implement user registration/authentication and session management in it. On googling I found these two libraries: addict and passport But I am not sure how much stable are these and are they being used in production somewhere. Please let me know if there are some libraries safe to use in production and if there are some example implementation of those. 回答1: Addict seems to be the more mature project and appears

Elixir type spec for a function with default parameters

半腔热情 提交于 2019-12-23 09:01:09
问题 How do I write a type spec for the function that accepts, let's say, one parameter which has a default value? Something like the following: def foo(bar \\ 10) do bar end Would it be this: @spec foo(integer) :: integer Or what would it be? Thank you. 回答1: Yes. I would add that if your question is if there is a difference between the typespec of a function which has an argument with a default value and an argument that doesn't, then no there is no difference. 来源: https://stackoverflow.com

How do I map and group_by at the same time?

早过忘川 提交于 2019-12-23 08:59:28
问题 As an example, let's say I have an enumerable collection of pairs {first, second} . Grouping these pairs using Enum.group_by(collection, fn {first, second} -> first end) will result in a Map whose keys are determined by the passed anonymous function. Its values are collections of pairs. However, I would like its values to contain the pair's second elements instead. In general, given an enumerable, I would like to group providing both a key extractor and a value mapper, so that I can determine

Open modules in Elixir?

泪湿孤枕 提交于 2019-12-23 07:40:05
问题 Ruby has open classes, which are very handy (though reviled by some), and Elixir borrows heavily from Ruby, so I expected Elixir to allow me to reopen a module and add macros to it after having closed it, but this did not work in the way I tried it. Is there some way to do this? Is this feature available yet in Elixir? To make this concrete, let's take an example from Chris McCord's Metaprogramming Elixir: defmodule Math do defmacro say({:+, _, [lhs, rhs]}) do quote do lhs = unquote(lhs) rhs

Elixir: Modifying value of module attribute

梦想的初衷 提交于 2019-12-23 07:38:56
问题 Is it possible to achieve below behavior wherein one tries to change the value of a module attribute to alter the behavior of the module methods? defmodule Adder do @num_to_add 10 def addTo(input), do: input + @num_to_add end IO.inspect Adder.addTo(5) # Prints 15 Adder.num_to_add = 20 IO.inspect Adder.addTo(5) # Expect it to print 25 It throws below error ** (CompileError) hello.exs:8: cannot invoke remote function Adder.num_to_add/0 inside match (elixir) src/elixir_clauses.erl:26: :elixir

How to get elixir nodes to connect automatically on startup?

廉价感情. 提交于 2019-12-22 18:43:29
问题 Background I'm trying to setup clustering between a few elixir nodes. My understanding is that I can set this up by modifying the release vm.args. I'm using Distillery to build releases and am following the documentation here: https://hexdocs.pm/distillery/config/runtime.html. My rel/vm.args file is as follows: -name <%= release_name %>@${HOSTNAME} -setcookie <%= release.profile.cookie %> -smp auto -kernel inet_dist_listen_min 9100 inet_dist_listen_max 9155 -kernel sync_nodes_mandatory '[$

How to test that a Phoenix socket is terminated?

强颜欢笑 提交于 2019-12-22 18:11:51
问题 I'm looking for a way to test that a socket gets terminated. The code under test does this: def handle_in("logout", _payload, socket) do {:stop, :logout, socket |> assign(:user, nil)} end And my test code (adapted from http://elixir-lang.org/getting-started/try-catch-and-rescue.html#exits) does this: test "logout terminates the socket", %{socket: socket} do try do push socket, "logout" catch huh -> IO.puts("Caught something #{IO.inspect huh}") :exit, what -> IO.puts("Caught :exit #{IO.inspect