How do you write a fun that’s recursive in Erlang?

Since OTP 17.0 there are named funs: 1> Perms = fun F([]) -> [[]]; F(L) -> [[H|T] || H <- L, T <- F(L–[H])] end. #Fun<erl_eval.30.54118792> 2> Perms([a,b,c]). [[a,b,c],[a,c,b],[b,a,c],[b,c,a],[c,a,b],[c,b,a]] Before that you could do this with a little argument trick: 1> Foo = fun(F, X) -> F(F, X) end. #Fun<erl_eval.12.113037538> 2> Foo(Foo, a). <…infinite loop!> … Read more

How do you create and load modules dynamically at runtime in Elixir, or Erlang?

As you described, there are many different approaches you could take by ultimately they boil down to two different categories: 1) code compilation and 2) code evaluation. The example you described above requires compilation, which will define a module and then you would have to invoke it. However, as you found out, it requires defining … Read more

Erlang: what is the difference between “include_lib” and “include”?

The way the documentation describes the difference between include and include_lib is: include_lib is similar to include, but should not point out an absolute file. Instead, the first path component (possibly after variable substitution) is assumed to be the name of an application. Example: -include_lib(“kernel/include/file.hrl”). The code server uses code:lib_dir(kernel) to find the directory of … Read more

How do you design the architecture of an Erlang/OTP-based distributed fault-tolerant multicore system?

Should I just start with a few gen_servers with a supervisor and incrementally build on that? You’re missing one key component in Erlang architectures here: applications! (That is, the concept of OTP applications, not software applications). Think of applications as components. A component in your system solves a particular problem, is responsible for a coherent … Read more

Good resources on using functional programming in game development? [closed]

Well, you could do worse than studying the code of some of these haskell games. Some of these use FRP (functional reactive programming), which some people are working on as a pure, high-level technique for games and other things. But most are a typical haskellish mixture of effectful and pure functional code. Bloggers with relevant … Read more

Which Actor model library/framework for python and Erlang-like? [closed]

To make actors with gevent, use a Greenlet subclass with embedded gevent.queue.Queue instance used as an inbox. To read a message from the inbox, simply get() from the queue. To send a message to an actor, put it into that actor’s queue. Read about subclassing Greenlet here. If you need help with writing the Actor … Read more

What is the “pin” operator for, and are Elixir variables mutable?

The data in Elixir is still immutable, but there are couple of shorthands, that let you type less or don’t worry about finding new names. In Erlang, you could often see code like this: SortedList = sort(List), FilteredList = filter(SortedList), List3 = do_something_with(FilteredList), List4 = another_thing_with(List3) In Elixir, you could just write: list = sort(list) … Read more

How to call a method dynamically in Elixir, by specifying both module and method name?

You can use apply/3 which is just a wrapper around :erlang.apply/3. It simply invokes the given function from the module with an array of arguments. Since you are passing arguments as the module and function names you can use variables. apply(:lists, :nth, [1, [1,2,3]]) apply(module_name, method_name, [1, array]) If you want to understand more about … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)