elixir

go语言第一篇。九大优势和3个缺点。

有些话、适合烂在心里 提交于 2020-01-12 19:13:17
go语言优势: Go 极其地快。其性能与 Java 或 C++相似。在我们的使用中,Go 一般比 Python 要快 30 倍。以下是 Go 与 Java 之间的基准比较: 原因 2:语言性能很重要 对很多应用来说,编程语言只是简单充当了其与数据集之间的胶水。语言本身的性能常常无关轻重。 但是 Stream 是一个 API 提供商,服务于世界 500 强以及超过 2 亿的终端用户。数年来我们已经优化了 Cassandra、PostgreSQL、Redis 等等,然而最终抵达了所使用语言的极限。 Python 非常棒,但是其在序列化/去序列化、排序和聚合中表现欠佳。我们经常会遇到这样的问题:Cassandra 用时 1ms 检索了数据,Python 却需要 10ms 将其转化成对象。 原因 3:开发者效率&不要过于创新 看一下绝佳的入门教程《开始学习 Go 语言》(http://howistart.org/posts/go/1/)中的一小段代码: package main type openWeatherMap struct{}func (w openWeatherMap) temperature(city string) (float64, error) { resp, err := http.Get("http://api.openweathermap.org/data/2.5

Route to a static page in phoenix-framework

最后都变了- 提交于 2020-01-12 07:07:09
问题 I want to run an angularJS front end with a phoenix backend for my site. I would like my root route to direct the user to a pre-built page in the static directory which contains my angular client and then use phoenix to run the API. I have done this in the past with ruby on rails by route matching like this: get '/', to: redirect('/foobar.html') Is there a way to do something similar with phoenix? 回答1: Not right now. You need to create a controller and then in the controller: defmodule MyApp

How can a Phoenix application tailored only to use channels scale on multiple machines? Using HAProxy? How to broadcast messages to all nodes?

只谈情不闲聊 提交于 2020-01-12 05:24:06
问题 I use the node application purely for socket.io channels with Redis PubSub, and at the moment I have it spread across 3 machines, backed by nginx load balancing on one of the machines. I want to replace this node application with a Phoenix application, and I'm still all new to the erlang/Elixir world so I still haven't figured out how a single Phoenix application can span on more than one machine. Googling all possible scaling and load balancing terms yielded nothing. The 1.0 release notes

How can a Phoenix application tailored only to use channels scale on multiple machines? Using HAProxy? How to broadcast messages to all nodes?

让人想犯罪 __ 提交于 2020-01-12 05:23:10
问题 I use the node application purely for socket.io channels with Redis PubSub, and at the moment I have it spread across 3 machines, backed by nginx load balancing on one of the machines. I want to replace this node application with a Phoenix application, and I'm still all new to the erlang/Elixir world so I still haven't figured out how a single Phoenix application can span on more than one machine. Googling all possible scaling and load balancing terms yielded nothing. The 1.0 release notes

How can I get permutations of a list?

血红的双手。 提交于 2020-01-09 10:42:29
问题 How can I get permutations of a list in Elixir? Eg, for ["a", "b", "c"] , I would expect: # [["a", "b", "c"], ["a", "c", "b"], # ["b", "a", "c"], ["b", "c", "a"], # ["c", "a", "b"], ["c", "b", "a"]] 回答1: Like this: defmodule Permutations do def of([]) do [[]] end def of(list) do for h <- list, t <- of(list -- [h]), do: [h | t] end end 回答2: There's a slightly different approach, it also supports specifing the desired length for the result lists: defmodule Permutations do def shuffle(list), do:

How can I get permutations of a list?

核能气质少年 提交于 2020-01-09 10:42:12
问题 How can I get permutations of a list in Elixir? Eg, for ["a", "b", "c"] , I would expect: # [["a", "b", "c"], ["a", "c", "b"], # ["b", "a", "c"], ["b", "c", "a"], # ["c", "a", "b"], ["c", "b", "a"]] 回答1: Like this: defmodule Permutations do def of([]) do [[]] end def of(list) do for h <- list, t <- of(list -- [h]), do: [h | t] end end 回答2: There's a slightly different approach, it also supports specifing the desired length for the result lists: defmodule Permutations do def shuffle(list), do:

Elixir: How to write Enum.map in block instead of Anonymous function?

人盡茶涼 提交于 2020-01-07 09:34:12
问题 I have function which results me keyword list, I wanted to write mutiple lines of code before calling scraper function, how I can write this in block using map? Enum.map(elements, fn(x) -> Scraper.Abc.markup(x) end) I wanted to write many code lines, I can use for loop but it will not result me anything for elements <- x do x |> ... |> ... |> ... |> Scraper.Abc.markup end Any help? 回答1: I wanted to write mutiple lines of code before calling scraper function, defmodule A do def go do list =

How to import users from csv file with elixir/phoenix?

送分小仙女□ 提交于 2020-01-06 17:58:52
问题 I have a csv file with a list of users and the following import method inside UserController and I would like to import these users by submitting the csv file with a form. And It looks like I'm doing it wrong. users_controller def import(conn, %{"user" => user_params}) do user_params["file"] |> File.stream!() |> CSV.decode |> Enum.each(fn(user) -> User.changeset(%User{}, %{name: Enum.at(user, 0), email: Enum.at(user, 1)}) |> Repo.insert end) conn |> put_flash(:info, "Imported") |> redirect(to

How to set a 24 byte length key to elixir/erlang block_encrypt/4 function using des_ede3 as mode of encryption

久未见 提交于 2020-01-06 09:52:46
问题 Am trying to send data to an external api that expects encrypted data using 3DES encryption but am having issues understanding how to pass my api key as the key field to erlangs des_ede3 cipher. According to erlangs cipher docs des_ede3 expects 3 keys that are all 8 bytes in length. How can i pass my 24 byte api key as the key to elixir/erlang :cryptoblock_encrypt/4 function *** how can i pass key to block_encrypt/4 *** key = "123456789012345678901234" data = "hello world! The world is yours"

How to generate current date in ISO 8601 format in elixir?

前提是你 提交于 2020-01-05 23:59:27
问题 I need to generate a string with the current time in the same format as the following: 20130524T000000Z The example is a timestamp for Fri, 24 May 2013 00:00:00 GMT . How would I do that? Is there a way to do it without external packages? 回答1: Updated 2016-07-18 Elixir 1.3 and up has support for this natively: iex> DateTime.utc_now() |> DateTime.to_iso8601() "2016-07-18T21:49:08.132428Z" Original 2015-12-01 (with version differences added 2016-07-18, thanks @sebastian_k) You could use the