elixir

How do you run middleware functions post response in Phoenix framework?

孤者浪人 提交于 2021-01-27 22:18:41
问题 I'm developing a simple website in Elixir with Phoenix. I'd like to add some custom middleware that runs after a response has been generated. For example, in order to log the total number of bytes in each response I'd like to have a Plug like this defmodule HelloWeb.Plugs.ByteLogger do import Plug.Conn require Logger def init(default), do: default def call(conn, default) do log("bytes sent: #{String.length(conn.resp_body)}") end end Trying to use this plug in one of the Phoenix pipelines in

elixir-lang Finding non-duplicate elements in a list

穿精又带淫゛_ 提交于 2021-01-26 10:00:28
问题 I'm trying to find non duplicate values from a list e.g. original list: iex> list = [2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8, 9, 9, 10, 10, 10] [2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8, 9, 9, 10, 10, 10] iex> unique = Enum.uniq(list) [2, 3, 4, 5, 6, 7, 8, 9, 10] iex> nondupes = unique -- Enum.uniq(list -- unique) [2, 3, 5, 7] result: [2, 3, 5, 7] I was wondering if there was a better way to achieve this in elixir/erlang 回答1: Another method (not necessarily better) that might be faster for large data is

Efficient way to filter a Map by value in Elixir

戏子无情 提交于 2021-01-18 05:26:25
问题 In Elixir, what would be an efficient way to filter a Map by its values. Right now I have the following solution %{foo: "bar", biz: nil, baz: 4} |> Enum.reject(fn {_, v} -> is_nil(v) end) |> Map.new This solution seems pretty inefficient to me. When called on a Map , Enum.reject/2 returns a Keywords . Since I want a Map , I need to call Map.new/1 to convert that Keywords back to me. This seems inefficient because Enum.reject/2 has to iterate over the Map once and then presumably, Map.new/1

Efficient way to filter a Map by value in Elixir

空扰寡人 提交于 2021-01-18 05:25:06
问题 In Elixir, what would be an efficient way to filter a Map by its values. Right now I have the following solution %{foo: "bar", biz: nil, baz: 4} |> Enum.reject(fn {_, v} -> is_nil(v) end) |> Map.new This solution seems pretty inefficient to me. When called on a Map , Enum.reject/2 returns a Keywords . Since I want a Map , I need to call Map.new/1 to convert that Keywords back to me. This seems inefficient because Enum.reject/2 has to iterate over the Map once and then presumably, Map.new/1

Java/Python/Elixir 正则库使用上的注意事项

穿精又带淫゛_ 提交于 2021-01-14 04:53:45
个人笔记,写得乱。。不过自己看得懂就行了—_— 日常工作中能接触到的正则,分为两大派别,其中 Unix-Like 系统中常用的正则,属于 POSIX “派”(较弱),而各编程语言标准库中的 Re,基本都是 PCRE “派”。(详见 正则表达式“派别”简述 ) 可虽然说各编程语言基本都属于 PCRE 派,实现上却还是各有特点,一个正则想在各语言间移植,也往往需要一番修改。 今天学 Elixir,就在正则上遇到了问题,百度一番,想想索性就把这些差别总结一遍,防止下次又掉坑里。(包括 Python、Java、Elixir、文本编辑器的正则,有时间把 SQL 的正则也写写。。) 一、正则库方法上的差别 1.1 模式匹配 文本编辑器的正则 是用来搜索的 ,会匹配 整段文本中所有符合该模式的字符串 ,可以叫做 find all 。 而不同的编程语言,又要看方法设计上的理念差别: Python 提供了以下方法 match:要求必须从字符串开头开始尝试匹配,相当与使用了 PCRE 的 anchored(锚定)模式。(或者说自动在正则开头添加了 \A ,它在 Python 中表示字符串开头) fullmatch:要求必须匹配整个字符串,相当于在正则的开头添加 \A ,末尾添加 \Z (它在 Python 中表示字符串末尾). search:从字符串中搜索该模式,找到第一个就返回。 findall

【Phoenix】1、搭建 Phoenix 环境

半世苍凉 提交于 2021-01-09 02:54:07
Ps: 需要注意的是,我学习的时候,Elixir 是 1.8.1的版本,而 Phoenix 是 1.4.1的版本,对于其他版本,不一定正确。      1、安装 Phoenix 之前,先安装 Elixir。   2、如果已经安装好了 Elixir ,使用一下命令安装 hex 这一个模块(为什么要安装 hex 我也不太懂,或许可以直接进行第三步)。 $ mix local.hex   3、安装好了 hex 使用下面的命令,安装 Phoenix。 $ mix archive. install hex phx_new 1.4 . 1   4、进行完上面的步骤,Phoenix 已经是安装好了。   5、Plug,Cowboy and Ecto 是 Phoenix 默认安装的依赖。   6、Phoenix 是默认使用 webpack 打包的,所以要安装 node.js,node.js 的版本 >= 5.0.0,Phoenix 安装 node.js 的教程是(如果你已经安装了 node.js 就不需要这一步的安装了)     (1)、install nodejs-legacy       $ apt-get install nodejs-legacy      (2)、create a symlink (创建软连接,相当于 windows 的快捷方式) $ ln -s /usr/bin

Writing a clean/cleaner solution to “Valid Anagram” in Elixir

半腔热情 提交于 2021-01-07 06:36:38
问题 Trying to level up my Elixir understanding by doing algo/leetCode style problems using Elixir. As I'm a relatively new programmer (around a year in) and was trained on traditionally OOP languages like Ruby and JS, it's still somewhat hard for me to wrap my head around doing algo questions in a functional paradigm, though I felt I understood the Udemy course I took on Elixir/Phoenix. I wrote a solution to the LeetCode "valid anagram" problem using Elixir and Repl and wanted to see if people

Writing a clean/cleaner solution to “Valid Anagram” in Elixir

眉间皱痕 提交于 2021-01-07 06:35:31
问题 Trying to level up my Elixir understanding by doing algo/leetCode style problems using Elixir. As I'm a relatively new programmer (around a year in) and was trained on traditionally OOP languages like Ruby and JS, it's still somewhat hard for me to wrap my head around doing algo questions in a functional paradigm, though I felt I understood the Udemy course I took on Elixir/Phoenix. I wrote a solution to the LeetCode "valid anagram" problem using Elixir and Repl and wanted to see if people

Writing a clean/cleaner solution to “Valid Anagram” in Elixir

寵の児 提交于 2021-01-07 06:33:56
问题 Trying to level up my Elixir understanding by doing algo/leetCode style problems using Elixir. As I'm a relatively new programmer (around a year in) and was trained on traditionally OOP languages like Ruby and JS, it's still somewhat hard for me to wrap my head around doing algo questions in a functional paradigm, though I felt I understood the Udemy course I took on Elixir/Phoenix. I wrote a solution to the LeetCode "valid anagram" problem using Elixir and Repl and wanted to see if people

Nerves Project Circuits SPI Clock not initialised

我与影子孤独终老i 提交于 2021-01-05 07:23:07
问题 I cannot get the SPI to work. I am trying to interface an IC mcp2515. It is an SPI to CAN interface. I probed the CE pins (GPIO 8 and GPIO 7) and SCLK (GPIO 11) with the oscilloscope but got nothing. these are deps defp deps do [ {:nerves, "~> 1.6.3", runtime: false}, {:shoehorn, "~> 0.6.0"}, {:ring_logger, "~> 0.8.1"}, {:toolshed, "~> 0.2.13"}, {:vintage_net_wizard, "~> 0.4.0"}, {:circuits_uart, "~> 1.4"}, {:circuits_gpio, "~> 0.4.6"}, {:nerves_leds, "~> 0.8.1"}, {:circuits_spi, "~> 0.1.5"},