elixir

How to create a map in a loop in Elixir

北战南征 提交于 2020-01-17 09:11:07
问题 I am creating a 2d map and want to start by pre-filling it with empty values. I know the following will not work in Elixir, but this is what I am trying to do. def empty_map(size_x, size_y) do map = %{} for x <- 1..size_x do for y <- 1..size_y do map = Map.put(map, {x, y}, " ") end end end Then I will be drawing shapes onto that map like def create_room(map, {from_x, from_y}, {width, height}) do for x in from_x..(from_x + width) do for y in from_x..(from_x + width) do if # first line, or last

How to generate Signed URL for google cloud storage objects using service-accounts-for-instances

不想你离开。 提交于 2020-01-16 08:07:24
问题 I am using signed-urls to give my clients temporary access to google cloud storage objects I have a service account json which looks like this: { "type": "service_account", "project_id": "my-project", "private_key_id": "abcdef1234567890", "private_key": "-----BEGIN PRIVATE KEY-----\n<key...>\n-----END PRIVATE KEY-----\n", "client_email": "my-app@my-project.iam.gserviceaccount.com", "client_id": "1234567890", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2

Where is `__info__/1` documented?

落爺英雄遲暮 提交于 2020-01-16 03:53:12
问题 Found this in Ecto.Repo.Supervisor and I was wondering where the other options to __info__/1 are documented: def compile_config(repo, opts) do # (...) behaviours = for {:behaviour, behaviours} <- adapter.__info__(:attributes), behaviour <- behaviours, do: behaviour # (...) end The Module documentation only mentions that After a module is compiled, using many of the functions in this module will raise errors, since it is out of their scope to inspect runtime data. Most of the runtime data can

undefined function :wx_object.start/3

岁酱吖の 提交于 2020-01-16 01:10:11
问题 I have tried to install ErLang on my Mac 64bit, Capitan, I've tried to install it via brew, but when I try to run :observer.start I get this error: ** (UndefinedFunctionError) undefined function :wx_object.start/3 (module :wx_object is not available) :wx_object.start(:observer_wx, [], []) observer_wx.erl:72: :observer_wx.start/0 I also tried to install erLang otp 18.3 using erlang solutions, but I got the same error. Any idea? EDIT If I try to start the observer directly from the Erlang shell

Elixir remove common elements from two lists

人走茶凉 提交于 2020-01-15 06:44:09
问题 I want to remove the elements found in list b from list a. The list a is printing [1,2,3,4] after execution of this code. defmodule Test do def listing do a = [1,2,3,4] b = [3,4,5,6] Enum.each b, fn elemB -> a = Enum.filter(a, fn(x) -> x != elemB == true end) #IO.inspect a end IO.inspect a end end Test.listing() 回答1: You don't need that outer Enum.each , you can do it with a single filter by enumerating over a and checking each element to see if it is a member of b : Enum.filter(a, fn el ->

Phoenix and Ecto and SELECTs

核能气质少年 提交于 2020-01-14 14:39:28
问题 I have an association set up in Ecto models in Phoenix. An Organization has many OrganizationMembers. In the OrganizationMember controller's Edit method, I'm trying to create a SELECT element that will hold all of the Organizations to choose from. In the edit definitiion, I've got the following two lines: # organizations = Enum.to_list(from(o in Organization, order_by: o.name, select: [o.name, o.id])) organizations = from(o in Organization, order_by: o.name, select: {o.name, o.id}) This is my

Elixir: rationale behind allowing rebinding of variables

醉酒当歌 提交于 2020-01-14 07:20:10
问题 What is the rationale behind allowing rebinding of variables in Elixir, when Erlang doesn't allow that? 回答1: Most functional languages don't allow rebinding of variables in the same scope. So Elixir allowing this does definitely give it an non-functional, imperative feel. Erlang's problem is rather lack of scope, or to be more precise that there is only one scope in a whole function clause. We did have serious discussions whether to introduce scope but in the end we decided against it as it

Why Supervisor.start_child dont work

雨燕双飞 提交于 2020-01-14 03:59:11
问题 I'm beginner in Elixir. I have one application that initiate one custom supervisor in application.ex. Code: defmodule MyApp do use Application def start(_type, _args) do import Supervisor.Spec children = [ supervisor(MyApp.Web.Endpoint, []), supervisor(MyApp.Repo, []), #my notifier MyApp.MyNotifier.Supervisor ] opts = [strategy: :one_for_one, name: MyApp.Supervisor] Supervisor.start_link(children, opts) end end And the code of supervisor is something like this: defmodule MyApp.MyNotifier

Why Supervisor.start_child dont work

China☆狼群 提交于 2020-01-14 03:59:07
问题 I'm beginner in Elixir. I have one application that initiate one custom supervisor in application.ex. Code: defmodule MyApp do use Application def start(_type, _args) do import Supervisor.Spec children = [ supervisor(MyApp.Web.Endpoint, []), supervisor(MyApp.Repo, []), #my notifier MyApp.MyNotifier.Supervisor ] opts = [strategy: :one_for_one, name: MyApp.Supervisor] Supervisor.start_link(children, opts) end end And the code of supervisor is something like this: defmodule MyApp.MyNotifier

How to render html template in javascript template in Phoenix Framework

非 Y 不嫁゛ 提交于 2020-01-13 19:25:06
问题 Let's say I have 2 files, create.js.eex and post.html.eex and I want to render the contents of the post.html.eex template inside the create.js.eex template. Something like this: $("#something").append("<%= safe_to_string render "post.html", post: @post %>"); The example above doesn't work because I need to escape quotes and other things in the string that gets returned and I can't find a way to do it 回答1: Use escape_javascript: $("#something").append("<%= escape_javascript render("post.html",