elixir

Creating anonymous functions via macros

天涯浪子 提交于 2019-12-24 17:08:21
问题 I'm tying to make an API where an anonymous function are composed from macros, e.g. transform [x, y], do: x + y transform x, do: x should use the transform head and body[:do] as the heads and bodies for an anonymous function. For example, the above macro calls The example above should be collected into: fn [x, y] -> x + y; x -> x end With unquote fragments it's easy to create new named function def s, but not new anonymous functions: iex> val = 1 iex> fn() -> unquote(val) end ** (CompileError

Replace item in an array in elixir

瘦欲@ 提交于 2019-12-24 16:42:51
问题 Let's say I have a map that looks like this An organization has an array of properties and the properties has an array of "access_tokens" organization = %{ name: "Org", properties: [ %{ name: "prop1", access_tokens: [%{id: "at-1", name: "one-1"}, %{id: "at-2", name: "one-2"}] }, %{ name: "prop2", access_tokens: [%{id: "at-3", name: "two-1"}, %{id: "at-4", name: "two-2"}] } ] } Now I have this map, for one particular access token, the id for this one matches one of the access tokens for the

How do I point to a local elixir version in mix.exs file?

冷暖自知 提交于 2019-12-24 10:55:34
问题 So I have a phoenix project I'm using as a test app of sorts. If I want to use a local version of elixir source code that I'm using (and making changes on), how do I tell phoenix to use it? I also want to be able to use "iex -S mix" with it. The below elixir: ["~> 1.6.0-dev", path: '/my/local/path/to/elixir'] does not work, but it's what I'm trying to do. I already tried adding the below and I get a syntax error. Adding elixir to the dependencies (in the def deps function) didn't seem to do

how to resume an elixir task from where it broke Elixir Phoenix

旧时模样 提交于 2019-12-24 08:07:21
问题 We are using SeaweedFS which is a file system to store (image) files, It works as a rest api. We are trying to move data from one server to another. there are a few levels of data directories. The basic pattern through which an image is stored is http://{server}:8888/ballymore-project-wave/snapshots/recordings/{year}/{month}/{day}/{hour}/00_00_000.jpg Each level of the directory has its own return, in form of JSON such as { "Path": "/ballymore-project-wave/snapshots/recordings/", "Files":

Understanding pattern matching in Elixir function parameters

﹥>﹥吖頭↗ 提交于 2019-12-24 07:45:03
问题 In the book 'Elixir in Action', one of the examples has a function that is tripping up my understanding of pattern matching: def add_entry( %TodoList{entries: entries, auto_id: auto_id} = todo_list, entry ) do entry = Map.put(entry, :id, auto_id) new_entries = HashDict.put(entries, auto_id, entry) %TodoList{todo_list | entries: new_entries, auto_id: auto_id + 1 } end The first parameter, %TodoList{entries: entries, auto_id: auto_id} = todo_list , the book explains "...Furthermore, you keep

elixir not updating values in mapset using enum.each

随声附和 提交于 2019-12-24 07:32:39
问题 map_set = MapSet.new() Enum.each(filtered_list, fn x -> map_set = MapSet.put(MapSet.new(map_set),x) Here filtered_list is a list containing string but when I am printing map_set it is returning an empty set. Why? 回答1: Your code is equivalent to this this: map_set = MapSet.new() Enum.each(filtered_list, fn x -> other = MapSet.put(MapSet.new(map_set), x) end) The map_set you assign to inside the enum is a local variable, it's not related to the map_set outside the enum. It may as well be called

Why Can't I Strip This Character From A String?

你离开我真会死。 提交于 2019-12-24 06:37:51
问题 Elixir 1.0, Erlang 17.3 on Windows 7 x64. I type this code: l = "[9,0]" s = String.strip(l,"[") And I get this: ** (FunctionClauseError) no function clause matching in String.lstrip/2 (elixir) lib/string.ex:527: String.lstrip("[9,0]", "[") (elixir) lib/string.ex:564: String.strip/2 What am I missing? I also tried s = String.strip(l,",") and same error. Also tried s = String.strip(l,'[') same error. What am I missing? 回答1: You want to pass a character to String.strip/2 : s = String.strip(l, ?[

How to pass variable to GraphQL for fields to select

跟風遠走 提交于 2019-12-24 06:21:55
问题 My sample query is as below: %{ "query" => """ query getPerson( $name: String! ){ getPerson(name: $name) { id col1 col2 col3 col3 } } """, "variables" => %{ "name" => "person_name", } } I want to select the fields dynamically. For example, for one query I just need id, col1, col2 but for another query id, col1, col3 and so on. How do I use variable for fields? Below is what I try which is not correct. %{ "query" => """ query getPerson( $name: String! ){ getPerson(name: $name) { $fields } } ""

Updating records that use embeds_many

喜欢而已 提交于 2019-12-24 03:52:13
问题 I'm building an app in Elixir on top of the Phoenix Framework that has a piece of functionality around collecting data from the Spotify API and persisting it into a PostgreSQL 9.4 database for quick access. I'm having trouble with updating existing table rows that use embeds_many . I have Track and AlbumArt Ecto models that looks like this: defmodule CoolApp.Track do use CoolApp.Web, :model alias CoolApp.AlbumArt alias CoolApp.Repo alias CoolApp.Track schema "tracks" do field :sid, :string

How to set up Websockets with Phoenix behind Nginx?

笑着哭i 提交于 2019-12-24 03:44:11
问题 I'm trying to set up web sockets to go through Nginx to a Phoenix app but keep getting a 403 error. Can anyone advise the correct configuration to make this work in production - development env is fine. My Nginx conf: upstream phoenix { server 127.0.0.1:4000 max_fails=5 fail_timeout=60s; } server { server_name <app-domain>; listen 80; location / { allow all; # Proxy Headers proxy_http_version 1.1; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host;