Re: Set as function

2010-04-06 Thread Sophie


On Apr 6, 12:16 am, Alex Osborne  wrote:
> Calling the set as if it is a fn is a short-hand for "get", that is
> retrieving an element from the set. Why would you want to do this, when
> to look it up you need to know what element is?  Sets are based on
> value-equality not reference-equality.  Thus you can have an object in
> the set that is equal to your lookup key but not identical.  

Got it, thanks.

-- 
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

To unsubscribe, reply using "remove me" as the subject.


Re: Set as function

2010-04-06 Thread Alex Osborne
B Smith-Mannschott  writes:

>> Calling the set as if it is a fn is a short-hand for "get", that is
>> retrieving an element from the set. Why would you want to do this, when
>> to look it up you need to know what element is?
>
> Since you asked: canonicalization. I've wanted this on occasion (in
> Java) when canonicalizing (i.e. interning) values. 

Yep, that's exactly what I meant by interning.

> The obvious alternative is to use a map where each such value maps to
> itself. This is no real bother. So, I'm happy that Clojure sets behave
> as they do. Being able to use a set as a predicate directly is very
> handy.

Indeed, under the hood Clojure's hash sets are in fact just a hash map
which maps elements to themselves.

-- 
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

To unsubscribe, reply using "remove me" as the subject.


Re: Set as function

2010-04-05 Thread B Smith-Mannschott
On Tue, Apr 6, 2010 at 07:16, Alex Osborne  wrote:
> Mark Engelberg  writes:
>
>> filter works just as well with a function that returns true and false,
>> so that's not a particularly good example.
>
> Calling the set as if it is a fn is a short-hand for "get", that is
> retrieving an element from the set. Why would you want to do this, when
> to look it up you need to know what element is?

Since you asked: canonicalization. I've wanted this on occasion (in
Java) when canonicalizing (i.e. interning) values. The obvious
alternative is to use a map where each such value maps to itself. This
is no real bother. So, I'm happy that Clojure sets behave as they do.
Being able to use a set as a predicate directly is very handy.

// ben

-- 
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


Re: Set as function

2010-04-05 Thread Alex Osborne
Mark Engelberg  writes:

> filter works just as well with a function that returns true and false,
> so that's not a particularly good example.

Calling the set as if it is a fn is a short-hand for "get", that is
retrieving an element from the set. Why would you want to do this, when
to look it up you need to know what element is?  Sets are based on
value-equality not reference-equality.  Thus you can have an object in
the set that is equal to your lookup key but not identical.  A simple
example is anything with metadata attached:

(def myset #{(with-meta 'foo {:something 1})})

Now I know what the symbol is but I want to retrieve the metadata from
the version that's in the set:

(meta (myset 'foo))
;;-> {:something 1}

Another use would interning -- returning a canonical version of an
object.

Cheers,

Alex

-- 
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

To unsubscribe, reply using "remove me" as the subject.


Re: Set as function

2010-04-05 Thread Chouser
On Apr 6, 2010, at 12:20 AM, Mark Engelberg   
wrote:





On Mon, Apr 5, 2010 at 8:56 PM, Richard Newman   
wrote:

Why this behavior?

It's useful: e.g., you can use a set as a filter.

user=> (filter #{2 3 4 5} (range 1 10))
(2 3 4 5)


filter works just as well with a function that returns true and  
false, so that's not a particularly good example.


"Which of the items in this set comes first in this vector?"

(some #{:b :c} [:a :c :d])
;=> :c

--Chouser

--
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

To unsubscribe, reply using "remove me" as the subject.


Re: Set as function

2010-04-05 Thread Richard Newman
filter works just as well with a function that returns true and  
false, so that's not a particularly good example.


Heh, that's true. Should have re-read :)

--
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

To unsubscribe, reply using "remove me" as the subject.


Re: Set as function

2010-04-05 Thread Mark Engelberg
On Mon, Apr 5, 2010 at 8:56 PM, Richard Newman  wrote:

> Why this behavior?
>>
>
> It's useful: e.g., you can use a set as a filter.
>
> user=> (filter #{2 3 4 5} (range 1 10))
> (2 3 4 5)
>
>
filter works just as well with a function that returns true and false, so
that's not a particularly good example.

-- 
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

To unsubscribe, reply using "remove me" as the subject.


Re: Set as function

2010-04-05 Thread Richard Newman

Why this behavior?


It's useful: e.g., you can use a set as a filter.

user=> (filter #{2 3 4 5} (range 1 10))
(2 3 4 5)

If you want your alternative, use contains?:

user=> (contains? #{true false} false)
true

--
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

To unsubscribe, reply using "remove me" as the subject.


Set as function

2010-04-05 Thread Sophie
Why this behavior?

user=> (#{5 nil} 5)
5
user=> (#{5 nil} 4)
nil
user=> (#{5 nil} nil)
nil

rather than the seemingly more informative:
user=> (#{5 nil} 5)
true
user=> (#{5 nil} 4)
false
user=> (#{5 nil} nil)
true
user=> (#{5 false} true)
false
user=> (#{5 false} false)
true

i.e. set as characteristic function:
   element -> bool

-- 
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

To unsubscribe, reply using "remove me" as the subject.