<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Should have been to the list, I apologize :P<br>
<br>
-------- Original Message --------
<table class="moz-email-headers-table" border="0" cellpadding="0"
cellspacing="0">
<tbody>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">Subject: </th>
<td>Re: [erlang-questions] extracting sub-terms from
term_to_binary encoded terms without unpacking first</td>
</tr>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">Date: </th>
<td>Fri, 18 Nov 2011 17:35:50 +0100</td>
</tr>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">From: </th>
<td>Jesper Louis Andersen
<a class="moz-txt-link-rfc2396E" href="mailto:jesper.louis.andersen@erlang-solutions.com"><jesper.louis.andersen@erlang-solutions.com></a></td>
</tr>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">To: </th>
<td>Joe Armstrong <a class="moz-txt-link-rfc2396E" href="mailto:erlang@gmail.com"><erlang@gmail.com></a></td>
</tr>
</tbody>
</table>
<br>
<br>
<pre>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
</pre>
</body>
</html>