[erlang-questions] If you are homesick for object.selector

Fred Hebert mononcqc@REDACTED
Sun Jan 27 06:31:35 CET 2013


In the mean time, enjoy an implementation of it:

https://github.com/ferd/hubble

Allows the following mode:

    demo() ->
        Stats = hubble:puts(hubble:new(),
                            [{[strength], 12},
                             {[wisdom], 9},
                             {[luck], 10},
                             {[dexterity], 5}]),
        Char = hubble:puts(hubble:new(),
                           [{[name], <<"karl">>},
                            {[bio, age], 219},
                            {[bio, hometown], <<"The Internet">>},
                            {[bio, parent, father], undefined},
                            {[bio, parent, mother], <<"A Unicorn">>},
                            {[stats], Stats}]),
        Char2 = hubble:put(Char, [level], 1),
        Char3 = hubble:up(Char2, [bio,parent,father], <<"Randalf">>),
        9 = hubble:get(Char3, [stats,wisdom]),
        <<"A Unicorn">> = hubble:get(Char3, [bio,parent,mother]),
        <<"Randalf">> = hubble:get(hubble:get(Char3, [bio,parent]), [father]).

Or, using a parse transform:

    -compile({parse_transform, hubble_trans}).
    
    demo() ->
        Stats = puts(new(),
                     [strength, 12],
                     [wisdom, 9],
                     [luck, 10],
                     [dexterity, 5]),
        Char = puts(new(),
                    [name, <<"karl">>],
                    [bio, age, 219],
                    [bio, hometown, <<"The Internet">>],
                    [bio, parent, father, undefined],
                    [bio, parent, mother, <<"A Unicorn">>],
                    [stats, Stats]),
        Char2 = put(Char, level, 1),
        Char3 = up(Char2, bio,parent,father, <<"Randalf">>),
        9 = get(Char3, stats, wisdom),
        <<"A Unicorn">> = get(Char3, bio, parent, mother),
        <<"Randalf">> = get(get(Char3, bio, parent), father).

And it lets you use any damn term you please for the path, not just yet
another overloaded bunch of strings with encodings to worry about.

The README.md file has a bit more details.

Regards,
Fred.

On 01/25, Lo�c Hoguin wrote:
> 
> That's the point: it should be *built into the language*, you
> shouldn't have to go through the trouble of doing something like
> this. And it should be *obvious* and in line with the rest of the
> language. I quite like the xpath idea Darach mentioned. It could
> integrate very well with the rest of the language. I might
> experiment with it.
> 
> -- 
> Loïc Hoguin
> Erlang Cowboy
> Nine Nines
> http://ninenines.eu



More information about the erlang-questions mailing list