Thanks once again Marko. The only thing that I am having trouble 
understanding is this:

1. What exactly happens when an item is passed to #(keyset-b (key-fn %)) ? Does 
> keyset-b looks up itself (because collections are functions) for the item 
> which contains *:id X *and returns true/false?


It returns the argument if it contains it, and otherwise 
nil.<http://clojure.org/data_structures#Data%20Structures-Sets>


Let's assume that key-fn is defined as #(.getID %) so we have:

#(keyset-b #(.getID %))

And now let's assume that item-object is passed to it. So, #(.getID %) returns, 
let's say, the number 3 (which is the value of the id). How exactly is that 
number is being looked up in keyset-b? How does keyset-b knows we are 
looking for an item with key id and value 3?

Apparently I am missing something here hence my confusion.

Cheers

On Thursday, March 21, 2013 8:48:51 PM UTC+2, Marko Topolnik wrote:
>
> On Thursday, March 21, 2013 5:21:53 PM UTC+1, Ryan wrote:
>
>> Thanks Marko. I do have couple more q's for you just to ensure I got 
>> everything right:
>>
>> (comp keyset-b key-fn)
>>> This results in a function that first applies *key-fn*, then *keyset-b*. 
>>> So it's like #(keyset-b (key-fn %)). Let's call this function *predicate
>>> *.
>>
>>
>> 1. What exactly happens when an item is passed to #(keyset-b (key-fn %)) ? 
>> Does 
>> keyset-b looks up itself (because collections are functions) for the item 
>> which contains *:id X *and returns true/false?
>>
>
> It returns the argument if it contains it, and otherwise 
> nil.<http://clojure.org/data_structures#Data%20Structures-Sets>
>  
>
>> 2. Isn't it more idiomatic to write #((key-fn %) keyset-b) ?
>>
>
> No, because it doesn't work :) An arbitrary object cannot be applied as a 
> function.
>  
>
>> 3. Does remove loops list-a internally and applies the predicate to each 
>> item? (if the answer is no my head will definitely explode)
>>
>
> *remove* is just like *filter*, only with reversed logic. Its 
> implementation in fact is literally
>
> (filter (complement pred) 
> coll))<http://clojuredocs.org/clojure_core/1.2.0/clojure.core/remove>
>  
>
>> On Thursday, March 21, 2013 6:09:42 PM UTC+2, Marko Topolnik wrote:
>>
>>> Personal preference. It causes less mental load because it more 
>>> obviously spells out what you are doing.
>>>
>>> On Thursday, March 21, 2013 4:58:08 PM UTC+1, Ryan wrote:
>>>>
>>>> Thanks a lot Marko. Much better now :)
>>>>
>>>> I also wanted to ask you why did you mention in a previous post that 
>>>> you prefer using *remove *than *filter + complement*. Is there a 
>>>> reason for this or just a personal preference?
>>>>
>>>> Ryan
>>>>
>>>> On Thursday, March 21, 2013 5:37:33 PM UTC+2, Marko Topolnik wrote:
>>>>>
>>>>> First we build a set of all the keys in *list-b*: 
>>>>>
>>>>> (into #{} (map key-fn list-b))
>>>>>
>>>>> Let's call that set *keyset-b. *Then we use *keyset-b* as a function 
>>>>> which returns truthy (non-nil) for any key that is contained in it, and 
>>>>> compose it with our *key-fn*:
>>>>>
>>>>> (comp keyset-b key-fn)
>>>>>
>>>>> This results in a function that first applies *key-fn*, then *keyset-b
>>>>> *. So it's like #(keyset-b (key-fn %)). Let's call this function *
>>>>> predicate*.
>>>>>
>>>>> Finally, we use *predicate* to *remove* any member of *list-a* for 
>>>>> which it is truthy:
>>>>>
>>>>> (remove predicate list-a)
>>>>>
>>>>> -marko
>>>>>
>>>>> On Thursday, March 21, 2013 4:14:46 PM UTC+1, Ryan wrote:
>>>>>
>>>>>> Marko,
>>>>>>
>>>>>> Can you please do me a favor and break down the function you 
>>>>>> suggested me? I understand partially how it works but I am having 
>>>>>> trouble 
>>>>>> to fully get it.
>>>>>>
>>>>>> (remove (comp (into #{} (map key-fn list-b)) key-fn) list-a)
>>>>>>>
>>>>>>

-- 
-- 
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/groups/opt_out.


Reply via email to