elixir

elixir - how to get all elements except last in the list?

核能气质少年 提交于 2019-12-30 16:22:50
问题 Let say I have a list [1, 2, 3, 4] How can I get all elements from this list except last? So, I'll have [1, 2, 3] 回答1: Use Enum.drop/2 like this: list = [1, 2, 3, 4] Enum.drop list, -1 # [1, 2, 3] 回答2: My solution (I think it's not a clean, but it works!) a = [1, 2, 3, 4] [head | tail] = Enum.reverse(a) Enum.reverse(tail) # [1, 2, 3] 回答3: Another option besides the list |> Enum.reverse |> tl |> Enum.reverse mentioned before is Erlang's :lists.droplast function which is slower according to the

elixir - how to get all elements except last in the list?

荒凉一梦 提交于 2019-12-30 16:22:22
问题 Let say I have a list [1, 2, 3, 4] How can I get all elements from this list except last? So, I'll have [1, 2, 3] 回答1: Use Enum.drop/2 like this: list = [1, 2, 3, 4] Enum.drop list, -1 # [1, 2, 3] 回答2: My solution (I think it's not a clean, but it works!) a = [1, 2, 3, 4] [head | tail] = Enum.reverse(a) Enum.reverse(tail) # [1, 2, 3] 回答3: Another option besides the list |> Enum.reverse |> tl |> Enum.reverse mentioned before is Erlang's :lists.droplast function which is slower according to the

Unicode and :alpha:

回眸只為那壹抹淺笑 提交于 2019-12-30 08:32:24
问题 Why is this false : iex(1)> String.match?("汉语漢語", ~r/^[[:alpha:]]+$/) false But this is true ?: iex(2)> String.match?("汉语漢語", ~r/[[:alpha:]]/) true Sometimes [:alpha:] is unicode and sometimes it's not? EDIT: I don't think my original example was clear enough. Why is this false : iex(1)> String.match?("汉", ~r/^[[:alpha:]]+$/) false But this is true ?: iex(2)> String.match?("汉", ~r/[[:alpha:]]/) true 回答1: When you pass the string to the regex in a non-Unicode mode, it is treated as an array of

Elixir - problems with https URLs

只谈情不闲聊 提交于 2019-12-30 08:29:25
问题 I'm new to Elixir and Erlang and having some issues with accessing https URLs. I've tried the Elixir-specific HTTPotion and Erlang's :inets module. So from the iex console (Interactive Elixir): With HTTPotion: HTTPotion.start HTTPotion.get("https://api.github.com") With :inets: :inets.start :ssl.start :httpc.request('https://api.github.com') In both cases I get a giant stacktrace that essential says there is a bad match somewhere and that a state machine is terminating. I don't get this when

Find if codepoint is upper case in Elixir

蓝咒 提交于 2019-12-30 04:55:09
问题 I need to detect if a codepoint is an upper case letter in Elixir. I have tried checking if it's value is in the range 65..90 but this fails on non-latin upper case letters. I have also tried checking if String.upcase(cp) == cp however this fails on non-letters (ie numbers, punctuation). I really don't want to go through the entirety of unicode and create a list of upper case codepoints, is there a built in function for this? 回答1: You can use the \p{Lu} Unicode character property regex escape

Which Situations Require Throw/Catch In Elixir?

喜你入骨 提交于 2019-12-30 04:21:46
问题 So a conversation arose for me and some friends around a passage in this page of Elixir documentation. In Elixir, a value can be thrown and later be caught. throw and catch are reserved for situations where it is not possible to retrieve a value unless by using throw and catch. Those situations are quite uncommon in practice except when interfacing with libraries that do not provide a proper API. Which situations would require using try/throw/catch vs. try/rescue ? Is this for interfacing

Elixir: Return value from for loop

隐身守侯 提交于 2019-12-29 09:21:55
问题 I have a requirement for a for loop in Elixir that returns a calculated value. Here is my simple example: a = 0 for i <- 1..10 do a = a + 1 IO.inspect a end IO.inspect a Here is the output: warning: variable i is unused Untitled 15:2 2 2 2 2 2 2 2 2 2 2 1 I know that i is unused and can be used in place of a in this example, but that's not the question. The question is how do you get the for loop to return the variable a = 10? 回答1: You cannot do it this way as variables in Elixir are

Phoenix setup failing: Compilation error, (KeyError) key :model not found

时光怂恿深爱的人放手 提交于 2019-12-25 09:25:06
问题 I have postgres running and can connect to it, but a brand new phoenix app via mix phoenix.new reporting . I can not complete setup, can not do ecto.create and the best I can tell is that it didn't connect to the database... or maybe it's some other bug... bascially, I don't know where to go from here. $ psql -p 5432 -h localhost reporting_dev postgres Password for user postgres: psql (9.6.1, server 9.6.2) Type "help" for help. reporting_dev=# And here is my config/dev.exs $ grep Reporting

How to get a video flow with Elixir/HTTPoison or Hackney?

本小妞迷上赌 提交于 2019-12-25 09:16:08
问题 I'm trying to get a stream from a cam using Elixir / HTTPoison and dump it into a file. Url is x.x.x.x/axis-cgi/mjpg/video.cgi?duration=1&resolution=320x240 Using the url in DHC (Chrome addon), i can have the following: Response 200 OK HEADERS Cache-Control: no-cache Pragma: no-cache Expires: Thu, 01 Dec 1994 16:00:00 GMT Connection: close Content-Type: multipart/x-mixed-replace; boundary=myboundary X-Time-Offset: 62044.312573 pretty COMPLETE REQUEST HEADERS Accept: / Accept-Encoding: gzip,

Elixir process not receiving message

此生再无相见时 提交于 2019-12-25 09:03:55
问题 I am new to Elixir and currentlly learning about process. In a practice I wrote a ping pong program that prints "ping" and "pong" from 2 processes. The processes always dead after receiving 1 or 2 messages. Here is my code defmodule Pingpong do def play do receive do {sender, :ping} -> IO.puts "ping" send sender, {self, :pong} play {sender, :pong} -> IO.puts "pong" send sender, {self, :ping} play end end def start() do a = spawn(Pingpong, :play, []) b = spawn(Pingpong, :play, []) send a, {b,