Hi Ken,

Sorry for the very late response; I've been away for a friend's
wedding.  Though I'm not sure if I will end up using this macro
frequently, it was highly educational to step through its inner
workings.  Thank you very much for putting it together.

On Dec 23 2010, 11:07 pm, Ken Wesson <kwess...@gmail.com> wrote:
> On Fri, Dec 24, 2010 at 12:06 AM, Benny Tsai <benny.t...@gmail.com> wrote:
> >> You're welcome. Sorry I couldn't be of greater help. If you want, I
> >> could throw together a quickie macro for grabbing a few items from
> >> either end of a seq.
> > Sure, that would be cool :)
>
> OK, here goes ...
>
> (defmacro ends [[[firsts mid lasts] sequence] & body]
>   (let [nl (count lasts)]
>     `(let [s# (seq ~sequence)
>           �...@firsts & ~mid] (drop-last ~nl s#)
>            ~lasts (drop (- (count s#) ~nl) s#)]
>       �...@body)))
>
> user=> (ends [[[a b c] d [e f]] (range 10)] [a b c d e f])
> [0
>  1
>  2
>  (3 4 5 6 7)
>  8
>  9]
>
> As you can see, it expects a binding vector of two items, the second a
> seqable and the first a vector of first, mid, lasts. Firsts and lasts
> are vectors of symbols, and mid is a symbol. The first items of the
> seq are assigned to the firsts symbols, in order; the last items to
> the lasts, in order; and whatever's left over in the middle to mid.
>
> If the sequence is short enough, odd things happen:
>
> user=> (ends [[[a b c] d [e f]] (range 5)] [a b c d e f])
> [0 1 2 nil 3 4]
>
> Here the firsts and lasts exhaust the seq; mid ends up nil. Since nil
> can generally stand in for an empty seq this is OK.
>
> user=> (ends [[[a b c] d [e f]] (range 4)] [a b c d e f])
> [0 1 nil nil 2 3]
>
> This is a bit more bothersome. [0 1 2 nil 2 3] might be preferred
> here. If so the macro needs a slight modification. What do you say?

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to