On Wed, May 25, 2011 at 12:20 AM, Mark Engelberg
<[email protected]>wrote:

> Scala's approach to comprehensions is to automatically produce the
> same type of collection that is used first in your comprehension.
> Clojure's approach is to always produce a lazy sequence which can then
> be "poured" into the collection of your choice using "into".  Both
> approaches have their merits.
>
>
I was playing around with the differences between the approaches. Here's a
concrete difference between the two styles. Clojure leaves the result as
general as it can, while scala enforces some assumptions about the result
type.

user>> (for [a #{1 2 3}
                 i [1 2 3 4]]
             (+ (mod a 2) i))
(2 3 4 5 1 2 3 4 2 3 4 5)

scala> for {
     |   a <- Set(1,2,3)
     |   i <- List(1,2,3,4)
     | } yield a%2 + i
res0: scala.collection.immutable.Set[Int] = Set(4, 5, 1, 2, 3)


This following snippet is.. interesting. Shows an (IMO) inconsistency in the
scala approach.

scala> for {
     |   a <- Range(1,5)
     | } yield a
res1: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4)

I'm just exploring, I'd be interested to see any similar "gotchas" between
Clojure/Scala.

Thanks,
Ambrose

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

Reply via email to