elixir

Is there a transpose function in Elixir?

为君一笑 提交于 2020-01-02 01:30:29
问题 Hi I look for a transpose function in Elixir. For example I have this kind of array a and after calling a function the result should be b : a = [[1, 2], [3, 4], [5, 6]] b = transpose(a) b => [[1, 3, 5], [2, 4, 6]] 回答1: There isn't one in Elixir currently, but you could create your own with: def transpose([]), do: [] def transpose([[]|_]), do: [] def transpose(a) do [Enum.map(a, &hd/1) | transpose(Enum.map(a, &tl/1))] end 回答2: There (still) isn't one in Elixir, but you can use: def transpose

UndefinedFunctionError phoenix elixir new project

流过昼夜 提交于 2020-01-02 00:59:09
问题 With a fresh install of Phoenix, I tried to trigger a new project, and when I try to run the server, I'm met with: Unchecked dependencies for environment dev: * gettext (Hex package) the dependency is not available, run "mix deps.get" * phoenix_live_reload (Hex package) the dependency is not available, run "mix deps.get" * cowboy (Hex package) * List item ... but when I run mix deps.get I get: Running dependency resolution ** (UndefinedFunctionError) undefined function Access.Map.get_and

Elixir — Module was not compiled with docs

余生颓废 提交于 2020-01-01 12:05:32
问题 I just started learning elixir yesterday. I have a file User.exs. It looks like this: defmodule User do @moduledoc """ Defines the user struct and functions to handle users. """ # functions and stuff go here... end When I run iex , this is what happens when I try to see the docs: iex(1)> c "user.exs" [User] iex(2)> h User User was not compiled with docs Any ideas? 回答1: c("user.exs") compiles the file in memory and does not write the bytecode (.beam file) to disk while h/1 currently requires

Caching expensive computation in elixir

风流意气都作罢 提交于 2020-01-01 09:55:14
问题 I have an web application in elixir that looks like this defmodule Test do use Plug.Router plug :match plug :dispatch def expensiveComputation() do // performs an expensive computation and // returns a list end get "/randomElement" do randomElement = expensiveComputation() |> Enum.random send_resp(conn, 200, randomElement) end end Whenever I issue a GET request to /randomElement , expensiveComputation gets called. The expensiveComputation function takes a long time to run but returns the same

Why is 'Infinity' not allowed in Erlang's floats?

前提是你 提交于 2020-01-01 04:59:07
问题 Erlang (and by extension Elixir) supports floating-point numbers. Some possible Floats: 1.2345 1.0e10 1.0e-42 Erlang supports NaN ( nan. in Erlang) (I am however yet to discover a method that outputs nan itself). However, Erlang does not have support for Infinity . While common standards like IEEE-754 state that one should return Infinity when doing things like 1.0/0.0 , instead, Erlang throws a bad arithmetic error . The same happens when attempting to make floats that are 'too large' like 1

How to handle associations and nested forms in Phoenix framework?

泄露秘密 提交于 2019-12-31 10:07:47
问题 What is the way to handle associations and nested forms in Phoenix framework? How would one create a form with nested attributes? How would one handle it in the controller and model? 回答1: There is a simple example of handling 1-1 situation. Imagine we have a Car and an Engine models and obviously a Car has_one Engine . So there's code for the car model defmodule MyApp.Car do use MyApp.Web, :model schema "cars" do field :name, :string has_one :engine, MyApp.Engine timestamps end def changeset

How to Log something in Controller when Phoenix Server is running?

*爱你&永不变心* 提交于 2019-12-31 08:54:16
问题 I'm trying to print some debug information from one of my Controllers in my Phoenix app when the server is running. defmodule PhoenixApp.TopicController do use PhoenixApp.Web, :controller def index(conn, _params) do log("this text") # ... end end 回答1: Okay, turns out it's pretty straight forward. You need to require the Logger elixir module in your controller and call one of it's methods to log your text. defmodule PhoenixApp.TopicController do require Logger def index(conn, _params) do

How to Log something in Controller when Phoenix Server is running?

天涯浪子 提交于 2019-12-31 08:53:55
问题 I'm trying to print some debug information from one of my Controllers in my Phoenix app when the server is running. defmodule PhoenixApp.TopicController do use PhoenixApp.Web, :controller def index(conn, _params) do log("this text") # ... end end 回答1: Okay, turns out it's pretty straight forward. You need to require the Logger elixir module in your controller and call one of it's methods to log your text. defmodule PhoenixApp.TopicController do require Logger def index(conn, _params) do

Phoenix fixture json file

ぃ、小莉子 提交于 2019-12-31 04:00:05
问题 How can I load a JSON fixture file in my phoenix project? When I tried something like Application.app_dir(my_app, "priv"), it gives me a compiled path and I can't use that on my tests . Is there any other way to load a fixture file from "test/support/somefile.json" ? 回答1: You can use __DIR__ with Path.expand/2 . For example, if your tests are in test/controllers/page_controller_test.exs , you can get the path to test/support/somefile.json using: path = Path.expand("../support/somefile.json",

Elixir/Phoenix restrict params like Rails strong params

眉间皱痕 提交于 2019-12-31 02:23:06
问题 I am making an API only Phoenix app. I come from a Ruby on Rails background, so bear with me. Say I have a User model with email , password , password_hash , and role fields. I need to restrict the role and password_hash fields from user input, or whitelist the email and password fields. Right now anyone could POST this sign up as an admin: { "user": { "email": "test3@test.com", "password": "testpw", "password_hash": "shouldn't allow user input", "role": "admin" } } This is typically