On Wednesday, April 2, 2014 3:39:47 PM UTC-5, Christopher Howard wrote:
>
> On Wed, 2 Apr 2014 14:07:47 -0600 
> Timothy Baldridge <tbald...@gmail.com <javascript:>> wrote: 
>
> > I don't know of anything built-in, but this should do the trick: 
> > 
> > (defn remove-first [f [head & tail]] 
> >   (if (f head) 
> >       tail 
> >       (cons head (lazy-seq (remove-first f tail))))) 
> > 
> > Timothy 
> > 
> > 
>
> Thanks. This seems to work for my purposes, although I modified it to 
> handle the empty vector: 
>
> (defn remove-first [f [head & tail]] 
>   (if (not head) [] 
>   (if (f head) 
>       tail 
>       (cons head (lazy-seq (remove-first f tail)))))) 
>
> This implementation doesn't seem to work on everything that "remove" 
> works on: 
>
> com.example.myapp=> (remove #(= [1 2] %) {1 2 3 4}) 
> ([3 4]) 
> com.example.myapp=> (remove-first #(= [1 2] %) {1 2 3 4}) 
> UnsupportedOperationException nth not supported on this type: 
> PersistentArrayMap  clojure.lang.RT.nthFrom (RT.java:835) 
>
 
To fix that, use `seq` on the collection argument. Also, if you want a 
emtpy sequence as a result instead of nil, just move the lazy-seq up since 
(lazy-seq nil) is an empty sequence.
 
(lazy-seq (when-let [[head & tail] (seq coll)] ...)

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to