efene programming language

fat arrows - some syntactic sugar for “dicts”

I was thinking on how to make some kind of dict like structure on efene with the things provided by erlang.

I identified a pattern and decided to have some syntactic sugar for it.

a common thing in erlang is to “tag” something with a two item tuple that contains the “tag” that represent what the second item is, and the value.

Things like {error, Reason}, {ok, Value} are really common in erlang.

Another pattern is using lists of two item tuples to represent “dicts”, you can see this here:

http://erldocs.com/R14A/stdlib/lists.html?i=0&search=lists:key

here

http://erldocs.com/R14A/stdlib/dict.html?i=0&search=dict:fro#from_list/1

and here

http://erldocs.com/R14A/stdlib/orddict.html?i=1&search=dict:fro#from_list/1

for some examples.

we can say that the list of two item tuples is used often, also the “tagged value” pattern, so why don’t do something about it? well, I did something…

I introduce to you: fat arrow expression!

it’s a simple expression that provides a nice looking way to state that we are doing a kind of “dict” or “tagged value”, it also allows some nice looking DSLs build on top of efene, let’s see some examples:

>>> Nums = [one => 1, two => 2, fourtytwo => 42]
[{one,1},{two,2},{fourtytwo,42}]
>>> lists.keyfind(two, 1, Nums)
{two,2}
>>> lists.keymember(two, 1, Nums)
true
>>> lists.keymember(nine, 1, Nums)
false
>>> lists.keysearch(two, 1, Nums)
{value,{two,2}}
>>>

>>> error => "you fail at failing"
{error,"you fail at failing"}
>>> throw(error => "something bad happened")
exception throw: {error,"something bad happened"}
>>> ok => "hi"
{ok,"hi"}
>>> SomeValue = ok => "Value"
{ok,"Value"}
>>> switch SomeValue { case ok => Value { Value }; }
"Value"

>>> Nums = [one => 1, two => 2, fourtytwo => 42]
[{one,1},{two,2},{fourtytwo,42}]
>>> dict.from_list(Nums)
{dict,3,16,16,8,80,48,
      {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
      {{[],[],[],[],[],
        [[one|1]],
        [],[],[],[],[],[],[],[],[],
        [[two|2],[fourtytwo|42]]}}}




sorry for some one liners there, fn shell needs some improvements :)

as you can see you can do lot of operations using fat arrows.

if you don’t like creating dicts from list of tuples at run time you can do it at compile time if the values are known at compile time:




@public
run = fn ()
    Nums = $[dict.from_list([one => 1, two => 2, fourtytwo => 42])]
    io.format("~p~n", [Nums])




translating to erlang with fnc -t erl test.ifn




-module(test).

-export([run/0]).

run() ->
    Nums = {dict, 3, 16, 16, 8, 80, 48,
            {[], [], [], [], [], [], [], [], [], [], [], [], [], [],
             [], []},
            {{[], [], [], [], [], [[one | 1]], [], [], [], [], [],
              [], [], [], [], [[two | 2], [fourtytwo | 42]]}}},
    io:format("~p~n", [Nums]).




PS: I wont be adding syntactic sugar to everything I find in efene, I just thought that this case deserved special treatment :D

so there you go, complex and nice looking data structures with no overhead at run time, all thanks to fat arrows and efene meta programming


  1. efene posted this
To Tumblr, Love Metalab