Re: New to Clojure, need some help

60 views
Skip to first unread message

Baishampayan Ghose

unread,
Aug 9, 2012, 9:03:06 AM8/9/12
to clo...@googlegroups.com
Hi,

Does this work for you?

(remove #{:a} [:a :b :a :c :d :e])

Also, if you have a list of items you can have all of them in the same
set/predicate like so -

(remove #{:a :z :x} [:a :b :a :c :d :e :z :b :d :e :x :z])

Hope this helps.

Regards,
BG

On Thu, Aug 9, 2012 at 10:11 AM, Jason Long <jsn...@gmail.com> wrote:
> I am trying to remove every occurrence of a given element from a vector. I
> can use (filter #(== % a) v) where 'a' is the value to be removed and 'v' is
> the vector, but this returns 'a' and 'a' is the value i want to remove. So,
> how can i do this? I tried replacing 'filter' with 'remove' but it didn't
> like that. Also, can anyone tell me why != is not included for equality
> testing, that would make this problem easy.
>
> Thanks, Jason.
>
> P.S. I also need to be able replicate the problem, but instead of removing a
> single item from the vector, I need to remove a list of items from the
> vector. I know I'm going to have to do some looping or recursing, but any
> help would be appreciated. Thanks
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en



--
Baishampayan Ghose
b.ghose at gmail.com

Timothy Baldridge

unread,
Aug 9, 2012, 9:07:24 AM8/9/12
to clo...@googlegroups.com
To clarify  Baishampayan's code, hash-sets in Clojure are functions:

=> (#{1} 1)
1
=> (#{1} 2)
nil

Nil and false in Clojure are the same thing, 

So Baishampayan's example: 

(remove #{:a :z :x} [:a :b :a :c :d :e :z :b :d :e :x :z])

#{:a :z : x} will return nil if the value is not in the vector, nil is considered false, and so the not found value is removed from the seq.

Timothy




Baishampayan Ghose

unread,
Aug 9, 2012, 9:12:36 AM8/9/12
to clo...@googlegroups.com
On Thu, Aug 9, 2012 at 10:11 AM, Jason Long <jsn...@gmail.com> wrote:
> Also, can anyone tell me why != is not included for equality testing, that
> would make this problem easy.

To answer your other question, != in Clojure is called not=

Regards,
BG
Reply all
Reply to author
Forward
0 new messages