erlang

How to manipulate regex replacement strings in Elixir

不打扰是莪最后的温柔 提交于 2019-12-10 17:44:16
问题 I found myself wanting to do this in Elixir: re_sentence_frag = %r/(\w([^\.]|\.(?!\s|$))*)(?=\.(\s|$))/ Regex.replace(re_sentence_frag, " oh. a DOG. woOf. ", String.capitalize("\\1")) Of course, that has no effect. (It capitalizes the string "\\1" just once.) What I really meant is to apply String.capitalize/1 to every match found by the replace function. But the 3rd parameter can't take a function reference, so passing &(String.capitalize("\\1") also doesn't work. This seems so fundamental

Erlang - element and list

一个人想着一个人 提交于 2019-12-10 17:23:46
问题 I'm new to erlang. I wonder how to write a function which returns the first N elements in a list? I've tried: take([],_) -> []; take([H|T],N) when N > 0 -> take([H,hd(L)|tl(T)], N-1); take([H|T],N) when N == 0 -> ... (I'm stuck here...) Any hint? thx Update: I know there's a function called "sublist" but I need to figure out how to write that function by my own. I finally figured out the answer: -module(list). -export([take/2]). take(List,N) -> take(List,N,[]). take([],_,[]) -> []; take([],_

Overuse of guards in Erlang?

浪子不回头ぞ 提交于 2019-12-10 17:19:45
问题 I have the following function that takes a number like 5 and creates a list of all the numbers from 1 to that number so create(5). returns [1,2,3,4,5]. I have over used guards I think and was wondering if there is a better way to write the following: create(N) -> create(1, N). create(N,M) when N =:= M -> [N]; create(N,M) when N < M -> [N] ++ create(N + 1, M). 回答1: create(N,N) -> [N]; create(N,M) -> [N|create(N + 1, M)]. % Don't use ++ to prefix a single element. This isn't quite the same (you

Tsung Issue with Dyn_Variable

好久不见. 提交于 2019-12-10 17:14:34
问题 I am very new to ERLANG and TSung, I never worked in this areas, but I am very much keen to know the fundamentals and do distributed load test for my web application. I am in half the way to complete, but I have a big hurdle and not able to moving forward , please read below tsung.xml file and advise me where & what I am missing? **===> tsung.xml (this file perfectly working without any errors)** *<?xml version="1.0"?> <!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd"> <tsung loglevel =

Configuring Lager - I get this error: undefined parse transform 'lager_transform'

烂漫一生 提交于 2019-12-10 17:08:49
问题 I'm testing this https://github.com/saleyn/erws_example on R16B03 (both on windows & Ubuntu) ==> erws_example (compile) src/erws_handler.erl:none: undefined parse transform 'lager_transform' ERROR: compile failed while processing /home/charles/erws_example: rebar_abort I've seen this suggestion http://philipcristiano.com/2013/05/27/ordering-of-rebar-dependencies.html So far, i'm unable to get any headway. Thanks. 回答1: It seems rebar can not compile parse transforms before compiling everything

Why is data sharing in Elixir / Erlang not working for the same tuple in two maps

拥有回忆 提交于 2019-12-10 16:44:33
问题 Given the following code defmodule Test do def run do p1 = {1, 2} m1 = %{a: p1} m2 = %{a: p1} IO.puts :erts_debug.same(m1.a, m2.a) m3 = %{b: p1} IO.puts :erts_debug.same(m1.a, m3.b) end end why does Test.run prints this iex(1)> Test.run true <--- expected false <--- not true ?! :ok Why are m1.a and m3.b not the same in-memory tuple? 回答1: modern era update: seems like it was fixed in ≈v1.7. This is true for Elixir only; in Erlang the tuple is shared: 1> Tuple = {1, 2}, 1> Key1 = 1, 1> Key2 = 2

Bad value on output port 'tcp_inet'

六月ゝ 毕业季﹏ 提交于 2019-12-10 16:06:20
问题 I'm using the Phoenix framework, which is running Cowboy underneath. I am occasionally seeing the following pair of errors in my log: Bad value on output port 'tcp_inet' GenServer #PID<0.8423.1> terminating ** (FunctionClauseError) no function clause matching in :http_transport.close/2 (inets) http_transport.erl:346: :http_transport.close(:undefined, #Port<0.18079778>) (stdlib) gen_server.erl:643: :gen_server.try_terminate/3 (stdlib) gen_server.erl:809: :gen_server.terminate/7 (stdlib) proc

Elixir exrm release crashes on eredis start_link

天涯浪子 提交于 2019-12-10 16:06:12
问题 I'm fairly new to Elixir and this is the first app that I'm attempting to release using exrm. My app interacts with a Redis database for consuming jobs from a queue (using exq), and also stores results of processed jobs in Redis using eredis. My app works perfectly when I run it via iex -S mix , and it also runs great when compiled into an escript. However when I use exrm, the application compiles without any issue, but it crashes when I run it. This is the crash output : $ ./rel/my_app/bin

Is ++ operator more expensive than | operator in Erlang?

依然范特西╮ 提交于 2019-12-10 15:57:24
问题 I was reading Learn You Some Erlang and I came upon this example in the Recursion chapter. tail_sublist(_, 0, SubList) -> SubList; tail_sublist([], _, SubList) -> SubList; tail_sublist([H|T], N, SubList) when N > 0 -> tail_sublist(T, N-1, [H|SubList]). As the author goes on to explain that there is a fatal flaw in our code. It being that the sub lists hence produced would be reverse and we would have to re-reverse them to get the correct output. In contrast, what I did was use the ++ operator

How do I turn a list of tuple pairs into a record in Erlang?

♀尐吖头ヾ 提交于 2019-12-10 15:54:04
问题 Let's say I have this: -record(my_record, {foo, bar, baz}). Keyvalpairs = [{foo, val1}, {bar, val2}, {baz, val3}]. Foorecord = #my_record{foo=val1, bar=val2, baz=val3}. How do I convert Keyvalpairs into Foorecord? 回答1: The simplest thing to do is: Foorecord = #my_record{foo=proplists:get_value(foo, Keyvalpairs), bar=proplists:get_value(bar, Keyvalpairs), baz=proplists:get_value(baz, Keyvalpairs)}. If this is too repetitive you can do something like: Foorecord = list_to_tuple([my_record|