elixir

Convert a binary string to Hexadecimal and vice-versa in Elixir

老子叫甜甜 提交于 2019-12-19 05:20:34
问题 How do you convert a binary string to a Hexadecimal String and vice-versa in Elixir? There are a few posts on SO regarding this topic for other "main stream" languages. There's even an SO post that benchmarks various C# implementations How do we do this in elixir? My implementation was too ugly to share... :( 回答1: There is Base.encode16/2: iex(1)> Base.encode16("foo") "666F6F" You can also specify the case: iex(2)> Base.encode16("foo", case: :lower) "666f6f" 来源: https://stackoverflow.com

Should I use Ecto.Repo in Controller or Model for Elixir Phoenix?

纵饮孤独 提交于 2019-12-18 20:49:21
问题 For some query in Controller of Phoenix, there're two plans for me Plan 1: defmodule Demo.UserController do # ... def index do # This is just for example # The point is Repo in used here Repo.all(User) end end Plan 2: defmodule Demo.User do # ... def all do # Put all Repo API and building query logic in Model Repo.all(__MODULE__) end end I prefer the Plan 2. Because in most situations, I can put all logic about fetching data in Model. But I find official guide use Plan 1(docs/model) and

No timeout in tests

大城市里の小女人 提交于 2019-12-18 18:54:41
问题 I like to use breakpoints inside my tests to see what's going wrong, but the 30 second timeout on the tests prevents me to look around freely. Is there a way to disable it ? The following doesn't work : @tag timeout: 0 test "something" do assert something == 42 end 回答1: UPDATE: With Elixir 1.1, you will also be able to run tests with the --trace option: mix test --trace . It will automatically set timeout to infinity. – José Valim PRE Elixir 1.1: Maybe try using: ExUnit.configure(timeout: 600

Is map lookup in Elixir O(1)?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 12:55:11
问题 Hash-table-based dict/map structures provide O(1) lookup time. Yet I keep seeing implications that in Elixir, finding a matching function head is faster than looking up something in a map. For instance, Elixir's String.Unicode compiles a list of unicode characters into many function heads, so that asking "what's the upcase version of é" is answered by finding the function head for upcase that expects "é". I don't know why this would be faster or more memory efficient than having a single

Pry while testing

梦想与她 提交于 2019-12-18 12:43:28
问题 I'm pretty new in Elixir, but have a lot fun with it! I came from Ruby world, so start looking analogy. And there is exist debugging tool pry . Using binding.pry I can interrupt any session. I found something similar in Elixir – IEx.pry . But it doesn't work when I'm testing something through ExUnit . Question – is it is possible to interrupt testing session and run iex with current environment? 回答1: You need to start your tests inside an iex session - you can do that by running iex -S mix

Why is useful to have a atom type (like in elixir, erlang)?

蓝咒 提交于 2019-12-18 12:14:47
问题 According to http://elixir-lang.org/getting-started/basic-types.html#atoms: Atoms are constants where their name is their own value. Some other languages call these symbols I wonder what is the point of have a atom type. Probably to help build a parser or for macros? But in everyday use how it help the programmer? BTW: Never use elixir or erlang, just note it exist (also in kdb) 回答1: I think that one of the most common usage in erlang is to tag variables and messages, with the benefit of fast

Why is useful to have a atom type (like in elixir, erlang)?

拈花ヽ惹草 提交于 2019-12-18 12:14:11
问题 According to http://elixir-lang.org/getting-started/basic-types.html#atoms: Atoms are constants where their name is their own value. Some other languages call these symbols I wonder what is the point of have a atom type. Probably to help build a parser or for macros? But in everyday use how it help the programmer? BTW: Never use elixir or erlang, just note it exist (also in kdb) 回答1: I think that one of the most common usage in erlang is to tag variables and messages, with the benefit of fast

What is the idiomatic testing strategy for GenServers in Elixir?

独自空忆成欢 提交于 2019-12-18 12:09:58
问题 I am writing a module to query an online weather API. I decided to implement it as an Application with a supervised GenServer . Here is the code: defmodule Weather do use GenServer def start_link() do GenServer.start_link(__MODULE__, :ok, name: __MODULE__) end def weather_in(city, country) do GenServer.call(__MODULE__, {:weather_in, city, country_code}) end def handle_call({:weather_in, city, country}) do # response = call remote api {:reply, response, nil} end end In my test I decided to use

Lists vs. Tuples - What to use and when?

孤者浪人 提交于 2019-12-18 10:15:07
问题 I am trying to grasp the difference between Lists and Tuples in Elixir . From the Basic Types section of Elixir Guides, I understand that: Lists are stored as Linked Items Updating a List is fast (only when prepending) Fetching List items is slow Fetching List information (size/length) is slow Tuple Elements are stored together Getting Tuple information is fast Fetching Tuple elements is fast Modifying Tuples is expensive Okay, that's all fine but I'm still not sure what to use when . I see

Good IDE for Elixir [closed]

喜夏-厌秋 提交于 2019-12-18 10:08:58
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I am looking for an IDE that does atleast some of the following Code completion when typing variable names and possible functions after pressing . Show a functions signature and docs Navigate to the function/variable/module's definition It would be nice if it could also Warn about errors Code generation Scaffold