How to serialize anonymous function in disk

Hi All
I finding the best way to store anonymous function to disk, in order to load and eval it when it need. Right now I assume that I can use quote to convert function to tuple then store it (serialize) some place on disc. Want to know the best or efficient way to do this

This is an old Erlang question too, thus a bit of implementation. :slight_smile:

An anonymous function is actually a public function in the module that the anonymous function is defined in, and a call to it ‘essentially’ just {ModuleName, #<AnonFuncIdOnTheModule>, [args]} (a bit different in reality, but this gets the point across).

Thus depending on your use case there are a variety of best things to do. However first I must ask, ‘what is the purpose of serializing the function’? Not why or how, but rather ‘what’ is the overall purpose that you are trying achieve (please read over the X/Y Problem).

However, if you truly want to serialize an anonymous function and you can get a macro around it, then sure, serializing its AST and reading it back in later and executing it via the Code module works. This is not even remotely efficient however (and even for loading functions from disk this method is still one of the slowest), and the best efficient method depends on what you are trying to accomplish?

In addition to what you are trying to accomplish, do you need those ‘links’ to persist over restarts? Are the functions static (defined at compile-time) or are they defined by users at run-time (security issue!)? Are the lookups like a map lookup or is it something unique? Etc…

3 Likes

If I can I’d love to get answer for similar question. Let’s say, admin of a site can send a text block with whole a whole anonymous function definition through simple post request. I want to store it on disk/in database with some reference. At later date said admin can choose this function, send parameters through another post request. On the server I want to read that function from database/disk and execute it with posted parameters, and the return result to user. In python you can do exec given text block, in ruby you can eval, how to do it in elixir?

Ps. Security issues aside, i am totally aware that it’s a very dangerous path :wink:

Look at the Code module, it can do what you want, still very very dangerous…

@OvermindDL1, @sztosz , Thank for reply my question. What I want to do is some kind of rules based system. User store the rule, then apply other information to match that rule.

Sounds unsafe, but rather have you thought about using safe and easily serialized matchspecs? :slight_smile:

I’m unsure how well this concept works in elixir but I’ve used them extensively in Erlang: http://erlang.org/doc/apps/erts/match_spec.html

How complex is the matching and what is it matching against? Basically a matchspec is capable of everything that a, say, case or function head matching can do, of structure, content, ‘when’, and exactly ‘what’ to match out too and in what format. They are awesome. :slight_smile:

There are also a multitude of converters of function declaration definitions to matchspecs that make it easy too, but a simple web gui would not be too hard. :slight_smile:

1 Like

I finding the best way to store anonymous function to disk, in order to load and eval it when it need.

You shouldn’t store anonymous functions on disk. A function does not exist outside of the module that defines it. Which means that, if you store the anonymous function on disk, any change to the module that defines it will change the anonymous function version in a way you can no longer use the previous one.

Consider storing the code representation, the module bytecode, AST or any of the other suggestions in this thread. But certainly not the anonymous function per se.

1 Like

Thank you @OvermindDL1, @josevalim. Right now I thinking about use ETS and fun2mc. Probably the good way to try first in my case. :slight_smile:

1 Like