On Sat, 28 Aug 2010 18:11:41 -0400
John Newman <[email protected]> wrote:
> > #[[[[%3 %2 %1][%4 %5 %6]]] signal] -> [c b a d e f]
>
> Right, the names are superfluous. So are the extra set of brackets I
> guess. Perhaps even better would be:
>
> (#[[_ _ _][_ _ _]] signal)
>
> Or if you wanted just the third item of the second collection:
>
> (#[[][2]] signal)
> Also, I took the signal out of the #[...] form and passed it in as an
> argument instead.
This looks like a generally useful destructuring tweak, but it's
really only useful in the context of #[...], as it elides the
names - which you have to have to use it in current destructurings.
In this case, #[ would be reader macro that swallows the vector it
opens, and returns a function that destructures it's one argument and
returns a vector of the places indicated by the integers in the vector
expression.
I dunno why, but this feels more useful. In more detail:
#[...]: Should evaluate a vector holding either vectors or integers,
and those vectors hold either vectors or integers, etc. and
return a function with a binding that destructures it's
argument (only one allowed). Vectors would work as they do
now; integers would work to select elements in the sequence
that destructuring matches to the enclosing vector. Note that
the :as an & constructs aren't supported here - they don't
make much sense.
The integers denote which positions you wanted to take in the
subvector, so #[[0 2] 1] would turn into something like:
(fn [[a _ b] c] [a b c])
Of course, we want to let the integer values be calculated at run
time, so that #[[0 2] 1] is equivalent to (let [a 0 b 2 c 1] #[[a b] c])
Duplicates numbers don't cause a problem - they just generate
duplicates in the output. So (#[[0 2] 0] [[a b c d]]) should give
[a c [a b c d]]. That can be done as (fn [[a _ b :as c]] [a b c]).
> No, it is not as flexible as let, but it eliminates having to
> duplicate a b c d e h, etc.
Yup. Or in the new form, and more like #(, even having to worry about
the names.
> I am going to see if I can write a function that does:
>
> => (destructure [[][2]] signal)
> (5 6 7 8)
> Or would it have to be a macro?
I dunno. Feels like it ought to be a function to me (both arguments
are evaluated on invocation), but you're going to build an fn
invocation and evaluate it, which sort of says macro. Sounds like
fun either way.
<mike
--
Mike Meyer <[email protected]> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en