elixir

Elixir exrm release crashes on eredis start_link

天涯浪子 提交于 2019-12-10 16:06:12
问题 I'm fairly new to Elixir and this is the first app that I'm attempting to release using exrm. My app interacts with a Redis database for consuming jobs from a queue (using exq), and also stores results of processed jobs in Redis using eredis. My app works perfectly when I run it via iex -S mix , and it also runs great when compiled into an escript. However when I use exrm, the application compiles without any issue, but it crashes when I run it. This is the crash output : $ ./rel/my_app/bin

Change backend/module at deploy time in Elixir?

不羁的心 提交于 2019-12-10 15:44:09
问题 How would one go about implementing a replaceable backend (or basically any part or module) so that it can be replaced at configuration/deploy time in Elixir? My specific situation is a simple web app (in this case using Phoenix but I'm guessing this question applies to other situations as well) where I have a very simple backend using Agent to keep state but I see a need in the future for being able to switch out the backend more or less dynamically. I'm guessing both Ecto and Logger do this

How can types and values be validated / enforced for Elixir structs?

烂漫一生 提交于 2019-12-10 15:06:34
问题 How can types and ranges of values be validated / enforced for Elixir Structs? e.g. During Struct creation, throwing an error if invalid types/values are handed in lat should be numerical and between -90.0 and +90.0 lon should be numerical and between -180.0 and +180.0 defmodule Location do @enforce_keys [:lat, :lon] defstruct lat: 0, lon: 0 end There was some discussion here with @JoséValim, but not clear what the outcome was https://groups.google.com/forum/#!topic/elixir-lang-core/U_wdxEqWj

Elixir - sum of list values with recursion

我的梦境 提交于 2019-12-10 14:56:24
问题 Just trying to do simple sum of list values. defmodule Mth do def sum_list([]) do 0 end def sum_list([H|T]) do H + sum_list(T) end end IO.puts Mth.sum_list([1, 2, 300]) But I get this error: **(FunctionClauseError) no function clause matching in Mth.sum_list/1 pokus.ex:3: Mth.sum_list([1, 2, 300]) pokus.ex:14: (file) (elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/2 (elixir) lib/code.ex:316: Code.require_file/2** 回答1: You need to use lowercase letters for variable and functions names.

How does recursion work in Elixir

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 14:45:18
问题 Simple function in Elixir, returning a list of numbers from to : defmodule MyList do def span(_), do: raise "Should be 2 args" def span(from, to) when from > to, do: [ to | span(to + 1, from) ] def span(from, to) when from < to, do: [ from | span(from + 1, to) ] def span(from, to) when from == to, do: [ from ] end I have no slightest clue, why this works and return a list of numbers. MyList.span(1,5) #=> [1,2,3,4,5] I just can't get my head around this: [ from | span(from + 1, to) ] Ok, first

Programming Phoenix: undefined function page_path/2

一世执手 提交于 2019-12-10 14:29:23
问题 I'm having an issue with my web app with a compile error below: == Compilation error on file web/controllers/auth.ex == ** (CompileError) web/controllers/auth.ex:49: undefined function page_path/2 (stdlib) lists.erl:1338: :lists.foreach/2 (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6 (elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1 I know most issues have been to do with missing routes on the router.ex , but it doesn't look like there

how to get remote_ip from socket in phoenix-framework?

China☆狼群 提交于 2019-12-10 14:10:04
问题 How to get remote_ip from socket in phoenixframework? I can get it from conn in View, but not in Channel. Many thanks for help! 回答1: The answer right now is: you can't. You can't access the connection in channels because channels are transport agnostic. Open up an issue in Phoenix detailing your user case so the Phoenix team can act on it. 回答2: Copy of the answer provided here: https://elixirforum.com/t/phoenix-socket-channels-security-ip-identification/1463/3 (all the credit goes to https:/

Elixir How to convert a Map struct to a Record struct

时光毁灭记忆、已成空白 提交于 2019-12-10 13:22:52
问题 I have a Record struct and a Map struct like: defmodule Foo.Bar do defstruct boo: nil, baz: nil end defmodule Foo do require Record Record.defrecord :bar, Foo.Bar, [boo: nil, baz: nil] end I can convert the Record to Map like this: defp update_map({k, v}, map), do: Map.update!(map, k, fn(_) -> v end) defp rd2map(rd) do Foo.bar(rd) |> Enum.reduce(%Foo.Bar{}, &update_map/2) end But how can I convert the Map to a Record? 回答1: Elixir Records are deprecated. The Record module that now exists in

Elixir: function overloading with different arity

為{幸葍}努か 提交于 2019-12-10 13:08:45
问题 is there any way to define overload functions with different arity, e.g in C# I can just do: foo(bar) or foo(bar, baz) In Elixir, the only way to do that would be to put them in separate modules, which will get messy pretty quickly. Is there any way around it? Edit: I had made a wrong assumption. The examples of overloaded functions I saw happened to have the same arity, so I (wrongly) assumed that this was a requirement. Functions are uniquely identified by their name and arity, so you can

Ecto query and custom MySQL function with variable arity

孤者浪人 提交于 2019-12-10 12:43:33
问题 I want to perform a query like the following one: SELECT id, name FROM mytable ORDER BY FIELD(name, 'B', 'A', 'D', 'E', 'C') FIELD is a MySQL specific function, and 'B', 'A', 'D', 'E', 'C' are values coming from a List. I tried using fragment, but it doesn't seem to allow dynamic arity known only in the runtime. Except going full-raw using Ecto.Adapters.SQL.query , is there a way to handle this using Ecto's query DSL? Edit: Here's the first, naive approach, which of course does not work: ids