Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Is this ... heresy?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 42 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Nils Kilden-Pedersen  
View profile  
 More options Apr 5 2012, 10:48 am
From: Nils Kilden-Pedersen <nil...@gmail.com>
Date: Thu, 5 Apr 2012 09:48:32 -0500
Local: Thurs, Apr 5 2012 10:48 am
Subject: Is this ... heresy?

Several times I've had to produce a collection of something, iterate it for
some purpose and then return it. One way to do that, would be like this:

def getFedMouthes(): Seq[Mouth] = {

val mouthes = mouthProducer.getThoseMouthes()
mouthes.foreach { mouth =>

mouth.feed(food.next)

mouth.clean(toothbrush) // Yeah, we're using the same tooth brush

}

mouthes

}

Now, that's all nice, but a little too verbose, so on occasion I've done
this (and felt oddly weird about it):

def getFedMouthes(): Seq[Mouth] = mouthProducer.getThoseMouthes().map {
mouth =>

mouth.feed(food.next)

mouth.clean(toothbrush)
mouth

}

Obviously I'm only saving 3 lines of code (in this example) and I'm getting
worse runtime characteristics (which I can live with in the normal case).

Is this a flagrant abuse of map? I feel like it is, but I wanted to hear
some feedback from those of you who are functional in the brain.

Maybe foreach should return the collection rather than Unit? (calm down, I
know some of you get anger attacks when suggestions are made to change long
standing and ubiquitous APIs, so I didn't really mean it).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Spiewak  
View profile  
 More options Apr 5 2012, 11:03 am
From: Daniel Spiewak <djspie...@gmail.com>
Date: Thu, 5 Apr 2012 10:03:42 -0500
Local: Thurs, Apr 5 2012 11:03 am
Subject: Re: [scala-language] Is this ... heresy?

Rather than getting into arguments about the aesthetic of the code (which
I'll leave to someone else)…

What you're relying on here is that map is consecutive and sequential.
This is something you can generally rely on for non-parallel Seq, but
you're technically not supposed to.  In theory, Seq is free to run the map
iteration in any order that is convenient.  If that semantic changes (and
it could), your code will unexpectedly break.

Daniel

On Thu, Apr 5, 2012 at 9:48 AM, Nils Kilden-Pedersen <nil...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eugene Burmako  
View profile  
 More options Apr 5 2012, 11:04 am
From: Eugene Burmako <eugene.burm...@epfl.ch>
Date: Thu, 5 Apr 2012 17:04:20 +0200
Local: Thurs, Apr 5 2012 11:04 am
Subject: Re: [scala-language] Is this ... heresy?

Iirc there's mapConserve which doesn't rebuild the collection if the
transformation doesn't change any elements. I think that would remedy the
runtime characteristics :)

On 5 April 2012 16:48, Nils Kilden-Pedersen <nil...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nils Kilden-Pedersen  
View profile  
 More options Apr 5 2012, 11:17 am
From: Nils Kilden-Pedersen <nil...@gmail.com>
Date: Thu, 5 Apr 2012 10:17:54 -0500
Local: Thurs, Apr 5 2012 11:17 am
Subject: Re: [scala-language] Is this ... heresy?

On Thu, Apr 5, 2012 at 10:03 AM, Daniel Spiewak <djspie...@gmail.com> wrote:
> Rather than getting into arguments about the aesthetic of the code (which
> I'll leave to someone else)…

> What you're relying on here is that map is consecutive and sequential.
> This is something you can generally rely on for non-parallel Seq, but
> you're technically not supposed to.  In theory, Seq is free to run the map
> iteration in any order that is convenient.  If that semantic changes (and
> it could), your code will unexpectedly break.

Are you saying that mapping a general Seq provides no guarantees (and
shouldn't) about retaining order? That would seem counter-intuitive to me.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Suereth  
View profile   Translate to Translated (View Original)
 More options Apr 5 2012, 11:22 am
From: Josh Suereth <joshua.suer...@gmail.com>
Date: Thu, 5 Apr 2012 11:22:40 -0400
Local: Thurs, Apr 5 2012 11:22 am
Subject: Re: [scala-language] Is this ... heresy?

On Thu, Apr 5, 2012 at 11:17 AM, Nils Kilden-Pedersen <nil...@gmail.com>wrote:

That is correct.   IF you want sequential behavior, you have to run:
foo.seq.map ....

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Suereth  
View profile   Translate to Translated (View Original)
 More options Apr 5 2012, 11:21 am
From: Josh Suereth <joshua.suer...@gmail.com>
Date: Thu, 5 Apr 2012 11:21:22 -0400
Local: Thurs, Apr 5 2012 11:21 am
Subject: Re: [scala-language] Is this ... heresy?

You may want something like:

def getFedAndCleanedMouthes(): Seq[Mouth] =
   for {
      (mouth, food) <-  mouthProducer.hungryMouths zip
foodProducer.makeFoods
      fedMouth = mouth feed food
      cleanMouth = mouth clean touthbrush
   } yield cleanMouth

Although the desugared code of that for expression isn't quite optimal yet,
the look is there, IMHO.

On Thu, Apr 5, 2012 at 10:48 AM, Nils Kilden-Pedersen <nil...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Hudson  
View profile   Translate to Translated (View Original)
 More options Apr 5 2012, 11:23 am
From: Paul Hudson <phud...@pobox.com>
Date: Thu, 5 Apr 2012 16:23:35 +0100
Local: Thurs, Apr 5 2012 11:23 am
Subject: Re: [scala-language] Is this ... heresy?

On 5 April 2012 15:48, Nils Kilden-Pedersen <nil...@gmail.com> wrote:

> Is this a flagrant abuse of map? I

Shouldn't you be flagrantly abusing a fold instead?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Spiewak  
View profile   Translate to Translated (View Original)
 More options Apr 5 2012, 11:23 am
From: Daniel Spiewak <djspie...@gmail.com>
Date: Thu, 5 Apr 2012 10:23:39 -0500
Local: Thurs, Apr 5 2012 11:23 am
Subject: Re: [scala-language] Is this ... heresy?

Order is guaranteed to be retained, but the order in which map traverses
the sequence is *not* guaranteed.  Thus:

Seq(1, 2, 3) map { i => println(i); i }           // => Seq(1, 2, 3)

In the above expression, the only thing that is guaranteed is that the
result will be Seq(1, 2, 3).  You may see the numbers printed as "2, 3, 1",
"3, 1, 2" or any other permutation.  Practically speaking, all of the Seq
implementations *will* traverse in order, and so you will always see "1, 2,
3" (which is why your code works at all).  This traversal order is the
semantic you cannot rely upon.

Daniel

On Thu, Apr 5, 2012 at 10:17 AM, Nils Kilden-Pedersen <nil...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Switzer  
View profile  
 More options Apr 5 2012, 11:24 am
From: Tom Switzer <thomas.swit...@gmail.com>
Date: Thu, 5 Apr 2012 11:24:48 -0400
Local: Thurs, Apr 5 2012 11:24 am
Subject: Re: [scala-language] Is this ... heresy?

Well, what your example reminds me of is Kestrels, from To Mock a Mocking
Bird [1] (Debasish has blogged about this before [2]). In Scala, you may
define the Kestrel combinator like this:

def kestrel[A,B,C](f: A => B, g: B => C): A => B = a => {
  val b = f(a)
  g(b)
  b

}

For your particular use case, this isn't too useful. You may want to define
something like Ruby's tap:

def tap[A,B](a: A, f: A => B): A = kestrel(identity[A], f)(a)

Then you can,

tap(mouthProducer.getThoseMouths(), _ foreach { mouth =>
  // ...

})

You could make it more Ruby-like by enriching Any,

implicit def tapAny[A](a: A) = new {
  def tap[B](f: A => B): A = tap(a, f)

}

mouthProducer.getThoseMouths().tap (_ foreach {
  // ...

})

[1]
http://www.amazon.com/To-Mock-Mockingbird-Raymond-Smullyan/dp/0192801...
[2]
http://debasishg.blogspot.ca/2009/09/side-effects-with-kestrel-in-sca...

Cheers,
Tom


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Suereth  
View profile  
 More options Apr 5 2012, 11:28 am
From: Josh Suereth <joshua.suer...@gmail.com>
Date: Thu, 5 Apr 2012 11:28:06 -0400
Local: Thurs, Apr 5 2012 11:28 am
Subject: Re: [scala-language] Is this ... heresy?

eh?  It doesn't guarantee traveral order on anything that'd not a Seq, but
for a subclass of Seq that has a well-defined "order" to elements, .seq
will make it both synchronous and in-order.   Not sure why you thought it
didn't....


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Suereth  
View profile  
 More options Apr 5 2012, 11:29 am
From: Josh Suereth <joshua.suer...@gmail.com>
Date: Thu, 5 Apr 2012 11:29:16 -0400
Local: Thurs, Apr 5 2012 11:29 am
Subject: Re: [scala-language] Is this ... heresy?

Ah, I may be confusing foreach with map, but I didn't think I was....

On Thu, Apr 5, 2012 at 11:28 AM, Josh Suereth <joshua.suer...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Luke Vilnis  
View profile  
 More options Apr 5 2012, 11:30 am
From: Luke Vilnis <lvil...@gmail.com>
Date: Thu, 5 Apr 2012 11:30:41 -0400
Local: Thurs, Apr 5 2012 11:30 am
Subject: Re: [scala-language] Is this ... heresy?

This seems really weird. As I understand it, "for" is translated into
map/flatmap/filter. Does this also mean that Scala's "for" loop doesn't
give guarantees about sequentiality for, say, List? That seems vaguely
bonkers.

On Thu, Apr 5, 2012 at 11:28 AM, Josh Suereth <joshua.suer...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Suereth  
View profile  
 More options Apr 5 2012, 11:31 am
From: Josh Suereth <joshua.suer...@gmail.com>
Date: Thu, 5 Apr 2012 11:31:56 -0400
Local: Thurs, Apr 5 2012 11:31 am
Subject: Re: [scala-language] Is this ... heresy?

No, you're off-base here Daniel.  .seq makes it sequential and synchronous.

On Thu, Apr 5, 2012 at 11:29 AM, Josh Suereth <joshua.suer...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Spiewak  
View profile  
 More options Apr 5 2012, 11:32 am
From: Daniel Spiewak <djspie...@gmail.com>
Date: Thu, 5 Apr 2012 10:32:05 -0500
Local: Thurs, Apr 5 2012 11:32 am
Subject: Re: [scala-language] Is this ... heresy?

There's nothing in the contract of *map* that says anything about traversal
order, only result order.  The foreach function has a guaranteed traversal
order, but map does not, even on sequences.  I have often thought that this
could be exploited in certain trie-based sequences to improve better
performance, but I haven't spent much time thinking in that direction.

Daniel

On Thu, Apr 5, 2012 at 10:28 AM, Josh Suereth <joshua.suer...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Spiewak  
View profile  
 More options Apr 5 2012, 11:32 am
From: Daniel Spiewak <djspie...@gmail.com>
Date: Thu, 5 Apr 2012 10:32:51 -0500
Local: Thurs, Apr 5 2012 11:32 am
Subject: Re: [scala-language] Is this ... heresy?

for is translated into foreach unless you have a yield clause.  Don't put
side effects into your for-comprehensions; the results will surprise you.

Daniel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Suereth  
View profile  
 More options Apr 5 2012, 11:33 am
From: Josh Suereth <joshua.suer...@gmail.com>
Date: Thu, 5 Apr 2012 11:33:35 -0400
Local: Thurs, Apr 5 2012 11:33 am
Subject: Re: [scala-language] Is this ... heresy?

List happens to be, because it's a Sequence and *not* a parallel collection.

Parallel collections have thier map/flatMap/filter operations evaluated in
parallel (i.e. not sequential).   But the result preserves the sequential
ordering.

calling .seq on a parallel collection returns you to non-parallel behavior.

Also, collections with no deifned ordering (like Map/Set) don't have to
abide by any rules in how their map/filter/flatMap operate.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Spiewak  
View profile  
 More options Apr 5 2012, 11:33 am
From: Daniel Spiewak <djspie...@gmail.com>
Date: Thu, 5 Apr 2012 10:33:35 -0500
Local: Thurs, Apr 5 2012 11:33 am
Subject: Re: [scala-language] Is this ... heresy?

That would surprise me.  It would make Scala the only collections library
that makes a *hard* guarantee about the traversal order of the map function.

Daniel

On Thu, Apr 5, 2012 at 10:31 AM, Josh Suereth <joshua.suer...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Suereth  
View profile  
 More options Apr 5 2012, 11:34 am
From: Josh Suereth <joshua.suer...@gmail.com>
Date: Thu, 5 Apr 2012 11:34:17 -0400
Local: Thurs, Apr 5 2012 11:34 am
Subject: Re: [scala-language] Is this ... heresy?

While that's true, if you were to do that in Scala's collections library,
expect to break things.

- Josh


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Sobral  
View profile  
 More options Apr 5 2012, 11:34 am
From: Daniel Sobral <dcsob...@gmail.com>
Date: Thu, 5 Apr 2012 12:34:46 -0300
Local: Thurs, Apr 5 2012 11:34 am
Subject: Re: [scala-language] Is this ... heresy?

On Thu, Apr 5, 2012 at 12:23, Daniel Spiewak <djspie...@gmail.com> wrote:
> Order is guaranteed to be retained, but the order in which map traverses the
> sequence is not guaranteed.  Thus:

> Seq(1, 2, 3) map { i => println(i); i }           // => Seq(1, 2, 3)

> In the above expression, the only thing that is guaranteed is that the
> result will be Seq(1, 2, 3).  You may see the numbers printed as "2, 3, 1",
> "3, 1, 2" or any other permutation.  Practically speaking, all of the Seq
> implementations will traverse in order, and so you will always see "1, 2, 3"
> (which is why your code works at all).  This traversal order is the semantic
> you cannot rely upon.

Let me make something clear before things get too confused: the above
example will ALWAYS print 1, 2, 3. Seq is guaranteed not to be
parallel. It is GenSeq that allows the behavior described.

--
Daniel C. Sobral

I travel to the future all the time.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Luke Vilnis  
View profile  
 More options Apr 5 2012, 11:35 am
From: Luke Vilnis <lvil...@gmail.com>
Date: Thu, 5 Apr 2012 11:35:39 -0400
Local: Thurs, Apr 5 2012 11:35 am
Subject: Re: [scala-language] Is this ... heresy?

LINQ's "Select" for IEnumerable absolutely traverses in-order, I don't know
where you're getting this "only" idea, or what you mean by "*hard*".


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Sobral  
View profile  
 More options Apr 5 2012, 11:35 am
From: Daniel Sobral <dcsob...@gmail.com>
Date: Thu, 5 Apr 2012 12:35:38 -0300
Local: Thurs, Apr 5 2012 11:35 am
Subject: Re: [scala-language] Is this ... heresy?

On Thu, Apr 5, 2012 at 12:24, Daniel Spiewak <djspie...@gmail.com> wrote:

>> That is correct.   IF you want sequential behavior, you have to run:
>> foo.seq.map ....

> Even foo.seq.map does not guarantee traversal order, only synchronicity and
> result order.

On the contrary. Guaranteeing traversal order is precisely what ".seq" does.

--
Daniel C. Sobral

I travel to the future all the time.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Spiewak  
View profile  
 More options Apr 5 2012, 11:37 am
From: Daniel Spiewak <djspie...@gmail.com>
Date: Thu, 5 Apr 2012 10:37:03 -0500
Local: Thurs, Apr 5 2012 11:37 am
Subject: Re: [scala-language] Is this ... heresy?

If we're going to make that assumption, then it needs to be documented.  As
I said, there is absolutely nothing in the contract of map (on Seq
*or*GenSeq) that says anything about traversal order, only
synchronicity and
result order.

Daniel

On Thu, Apr 5, 2012 at 10:34 AM, Josh Suereth <joshua.suer...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Spiewak  
View profile   Translate to Translated (View Original)
 More options Apr 5 2012, 11:38 am
From: Daniel Spiewak <djspie...@gmail.com>
Date: Thu, 5 Apr 2012 10:38:33 -0500
Local: Thurs, Apr 5 2012 11:38 am
Subject: Re: [scala-language] Is this ... heresy?

.seq guarantees single-threadedness.  In practice it also provides
traversal order.  I don't see any documentation to the effect that it *
guarantees* traversal order.

Daniel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Suereth  
View profile  
 More options Apr 5 2012, 11:41 am
From: Josh Suereth <joshua.suer...@gmail.com>
Date: Thu, 5 Apr 2012 11:41:34 -0400
Local: Thurs, Apr 5 2012 11:41 am
Subject: Re: [scala-language] Is this ... heresy?

It seems the documentation in the scaladocs is lacking.  That's something
we can correct.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Spiewak  
View profile  
 More options Apr 5 2012, 11:43 am
From: Daniel Spiewak <djspie...@gmail.com>
Date: Thu, 5 Apr 2012 10:43:25 -0500
Local: Thurs, Apr 5 2012 11:43 am
Subject: Re: [scala-language] Is this ... heresy?

Well, as someone who implements data structures, that sort of information
might be considered important.  :-)  Duly filed…

Daniel

On Thu, Apr 5, 2012 at 10:41 AM, Josh Suereth <joshua.suer...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 42   Newer >
« Back to Discussions « Newer topic     Older topic »