elixir

Erlang (Elixir) Dialyzer - confusing supertype error

荒凉一梦 提交于 2020-01-03 17:22:14
问题 I have defined an Elixir behaviour X . A callback start_link is spec'ed as: @callback start_link( args :: producer_args, opts :: GenServer.options ) :: GenServer.on_start where producer_args type is defined as: @type producer_args :: %{job_queue_name: String.t} In the client code Y that implements the behaviour, start_link is defined as: def start_link(args = %{job_queue_name: _job_queue_name, redis_url: _redis_url}, opts) do GenStage.start_link(__MODULE__, args, opts) end Dialyzer doesn't

How to determine from which module a specific function was imported in elixir

此生再无相见时 提交于 2020-01-03 17:03:30
问题 When multiple external modules are included by calling use on some intermediate module, is there an easy way to determine in which module given method is actually defined? E.g: defmodule ModuleB do def method_b do end end defmodule ModuleA do # imports ModuleB implicitly use SomeModuleImportingModuleB def method_a # how to determine this is ModuleB.method_b? method_b end end 回答1: I found a solution that works for me, by capturing the function using & and then inspecting it: def method_a IO

How can I add days to Ecto.DateTime?

人走茶凉 提交于 2020-01-03 10:45:47
问题 I have a date-time which I create like this: Ecto.DateTime.from_erl({{2015, 3, 10}, {0, 0, 0}}) It's a Phoenix app. I want to add days to it with no any additional third-party library. How? 回答1: You can use erlang's :calendar module to manipulate dates without additional dependencies. A standard way of adding days would be to use :calendar.date_to_gregorian_days/1 do the addition and convert back to the tuple format with :calendar.gregorian_days_to_date/1 . 回答2: Proper elixir syntax weekday=

Elixir + Ecto: How to do WHERE NOT IN [array]?

帅比萌擦擦* 提交于 2020-01-03 08:09:01
问题 I am trying to look for all User s that don't have a certain string element in their match_history field. I took a guess with this: matched_user = User |> where([u], ^device_id not in u.match_history) |> limit(1) |> VideoChat.Repo.one But it seems to break at the not part. Is there a way to do this? 回答1: Try User |> where([u], not ^device_id in u.match_history) 回答2: For those who are looking for a "array does not contain any" behaviour. For example "match_history does not contain device_1

Elixir + Ecto: How to do WHERE NOT IN [array]?

南楼画角 提交于 2020-01-03 08:06:09
问题 I am trying to look for all User s that don't have a certain string element in their match_history field. I took a guess with this: matched_user = User |> where([u], ^device_id not in u.match_history) |> limit(1) |> VideoChat.Repo.one But it seems to break at the not part. Is there a way to do this? 回答1: Try User |> where([u], not ^device_id in u.match_history) 回答2: For those who are looking for a "array does not contain any" behaviour. For example "match_history does not contain device_1

What does the slash notation in Elixir mean?

岁酱吖の 提交于 2020-01-03 07:05:12
问题 In the Elixir docs, they keep using an odd notation with slash, for example: is_boolean/1 IO.puts/1 String.length/1 is_function/2 ++/2 I'm just guessing, but I think it refers to arity. But if that's the case, why the devil isn't it mentioned anywhere in the docs? It's not as if this is any kind of standard convention in IT (at least, not one that I've ever seen in my 20+ years in IT). 回答1: From page 2, Basic types of the Getting started documentation: Note: Functions in Elixir are identified

how to create/implement an algorithm that mirrors a binary tree [closed]

和自甴很熟 提交于 2020-01-03 05:06:50
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 11 months ago . I'm new to Elixir, and trying to implement an insert/add function to mirror an existing binary tree. Appreciate all ideas. Don't know where to start. 回答1: I'm assuming you want to implement a binary tree in Elixir, and that you are also familiar with binary trees. For that, you

How to update field value dynamically in Ecto migration?

时光怂恿深爱的人放手 提交于 2020-01-03 03:02:06
问题 I have a Users table like: email | username ---------------+---------- 123@321.com | 123@123.com | haha@haha.com | and I want to update username field by email field, just slice the email before @ . email | username ---------------+---------- 123@321.com | 123 123@123.com | 123 haha@haha.com | haha I have try to use the following migration: defmodule MyApp.Repo.Migrations.AddDefaultUsernameForUsers do use Ecto.Migration import Ecto.Query def up do from(u in MyApp.User, update: [set: [username

Map a nested JSON payload to a struct in Elixir

喜夏-厌秋 提交于 2020-01-02 03:51:07
问题 I am attempting to port the Golang tutorial geddit to Elixir. I have done so successfully with Dartlang, but Elixir's operations on maps & lists are confusing for me. Using HTTPoison and JSEX, I have the following code: defmodule Redditex do use HTTPoison.Base def process_url(url) do "http://www.reddit.com/r/#{url}.json" end def process_response_body(body) do json = JSEX.decode! body json = Enum.map json, fn ({k, v}) -> {String.to_atom(k), v } end json end end My difficulty is parsing the

Ecto has_many :through in form

非 Y 不嫁゛ 提交于 2020-01-02 03:33:45
问题 I am trying to get a has_many :through relationship working in Ecto for a many-many relationship between a User model and Group model. The only information I was able to find online was related to nested attributes in a post by José Valim here (which is excellent, by the way). Since the groups already exist in the system, I was hoping to do a multiple select input. I ran into a couple issues in doing that. I don't believe that it is possible to assign the groups association in the changeset