[erlang-questions] Fwd: Re: extracting sub-terms from term_to_binary encoded terms without unpacking first

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Fri Nov 18 17:36:37 CET 2011


Should have been to the list, I apologize :P

-------- Original Message --------
Subject: 	Re: [erlang-questions] extracting sub-terms from 
term_to_binary encoded terms without unpacking first
Date: 	Fri, 18 Nov 2011 17:35:50 +0100
From: 	Jesper Louis Andersen <jesper.louis.andersen@REDACTED>
To: 	Joe Armstrong <erlang@REDACTED>



On 2011-11-17 15:52, Joe Armstrong wrote:
>  And I want to extract the 'b' sub term, I'd normally write
>
>       {_, _, [_,X]} = binary_to_term(B)
>
>  But why bother to unpack? I could just as well write
>
>       X = hd(tl(element(3,B)))
>
>  This is not the regular hd/tl/and element but a hacked version that
>  can traverse the external format.
>

This looks an awful lot like a "lens" as popularized by Benjamin C.
Pierce. A Lens is a generalized, functional accessor/mutator pair with
the special property that several such lenses can be composed to form
accessors or mutators operating in the "deep" of a structure (Think of
lenses in a camera for instance). Here you just provide access, but
there is a corresponding operation for mutation of a deep JSON-like
structure.

I've been wanting to create lenses for Erlang for some time, but since
I've not had to operate on structures like JSON directly, I've not found
the time to do it. One really important thing is to provide lenses for
structures like dict, gb_trees and so on, such that you can mutate
elements in the "deep" of these, preferably by calling relevant
update-functions on the data.

-- 
Jesper Louis Andersen
   Erlang Solutions, Copenhagen, DK


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111118/f277c11a/attachment.htm>


More information about the erlang-questions mailing list