This page is no longer maintained — Please continue to the home page at www.scala-lang.org

My take on Promises..

6 replies
StephaneLD
Joined: 2008-08-28,
User offline. Last seen 4 years 9 weeks ago.

Would be happy to read your comments :)

http://lambdabrella.blogspot.com/2011/07/safe-asynchronous-promise.html

Stephane

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: My take on Promises..
Hey Stephane,

It was a fun read!

A quick question, is the implementation of Promise.or correct?
I don't see how "status" trancends from 0 if you're only cas:ing 1,1 and 2,2

Cheers,


On Wed, Jul 20, 2011 at 6:24 PM, sledorze <stephane [dot] ledorze [at] gmail [dot] com> wrote:
Would be happy to read your comments :)

http://lambdabrella.blogspot.com/2011/07/safe-asynchronous-promise.html

Stephane



--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
Re: My take on Promises..
On 20/07/2011 18:24, sledorze wrote:
5b5497c8-588a-4224-85db-6791493d5663 [at] c29g2000yqd [dot] googlegroups [dot] com" type="cite">
Would be happy to read your comments :)

http://lambdabrella.blogspot.com/2011/07/safe-asynchronous-promise.html

Stephane

Hey Stephane, glad to see you on that list ! Really intersting post, thanks !

-- 
Francois ARMAND
http://fanf42.blogspot.com
http://www.normation.com
StephaneLD
Joined: 2008-08-28,
User offline. Last seen 4 years 9 weeks ago.
Re: My take on Promises..

Oh.. I forgot to trigger email reception.. just saw your question :)

Yes it is!
I'll explain a bit :)

When one call that:
status.compareAndSet(1, 1)

What the CPU does to status is atomically equivalent to that
(currentValue being the status current value) :

def compareAndSet(expected : Int, newValue : Int) : Bool = {
val res = currentValue == expected
currentValue = newValue
res
}

So, calling status.compareAndSet(1, 1) sets the value.

Hope it makes more sense now :)

Stephane

On 20 juil, 23:47, √iktor Ҡlang wrote:
> Hé Stéphane,
>
> C'était une lecture amusante!
>
> Une question rapide, est la mise en œuvre de correcte Promise.or?
> Je ne vois pas comment "statut" transcende de 0 si vous êtes seulement CAS: ING 1,1 et 2,2
>
> Cheers,
> √
>
> On Wed, 20 juillet 2011 à 18:24, sledorze a écrit:
>
> > Serait heureux de lire vos commentaires:)
>
> >http://lambdabrella.blogspot.com/2011/07/safe-asynchronous-promise.html
>
> > Stéphane
>
> -
> Viktor Klang
>
> Akka Tech Lead
> Typesafe - classe entreprise de la Scala
> Experts
>
> Twitter: @ viktorklang

StephaneLD
Joined: 2008-08-28,
User offline. Last seen 4 years 9 weeks ago.
Re: My take on Promises..

Thanks Francois,

We should have worked together, would have been fun!
By the way, I'm looking for Freelancing / Consulting.. if you heard
something, let me know ;)

Cheers,
Stephane

On 21 juil, 09:58, Francois wrote:
> On 20/07/2011 18:24, sledorze wrote:
>
> > Would be happy to read your comments :)
>
> >http://lambdabrella.blogspot.com/2011/07/safe-asynchronous-promise.html
>
> > Stephane
>
> Hey Stephane, glad to see you on that list ! Really intersting post,
> thanks !
>
> --
> Francois ARMANDhttp://fanf42.blogspot.comhttp://www.normation.com

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Re: My take on Promises..


On Fri, Jul 22, 2011 at 5:05 PM, sledorze <stephane [dot] ledorze [at] gmail [dot] com> wrote:
Oh.. I forgot to trigger email reception.. just saw your question :)

Yes it is!
I'll explain a bit :)

When one call that:
status.compareAndSet(1, 1)

What the CPU does to status is atomically equivalent to that
(currentValue being the status current value) :


def compareAndSet(expected : Int, newValue : Int) : Bool = {
 val res = currentValue == expected
 currentValue = newValue
 res
}

So, calling status.compareAndSet(1, 1) sets the value.

Hope it makes more sense now :)



scala> val status = new java.util.concurrent.atomic.AtomicInteger(0)
status: java.util.concurrent.atomic.AtomicInteger = 0

scala> status.compareAndSet(1,1)
res0: Boolean = false

scala> status.get
res1: Long = 0

since you are only CASing the same value( 1 for 1 and 2 for 2 )and the counter starts at 0, and I didn't see any other writes to the status, how does the status transcend 0?

Cheers,


 
Stephane


On 20 juil, 23:47, √iktor Ҡlang <viktor [dot] kl [dot] [dot] [dot] [at] gmail [dot] com> wrote:
> Hé Stéphane,
>
> C'était une lecture amusante!
>
> Une question rapide, est la mise en œuvre de correcte Promise.or?
> Je ne vois pas comment "statut" transcende de 0 si vous êtes seulement CAS: ING 1,1 et 2,2
>
> Cheers,
> √
>
> On Wed, 20 juillet 2011 à 18:24, sledorze <stephane.ledo...@ gmail.com> a écrit:
>
> > Serait heureux de lire vos commentaires:)
>
> >http://lambdabrella.blogspot.com/2011/07/safe-asynchronous-promise.html
>
> > Stéphane
>
> -
> Viktor Klang
>
> Akka Tech Lead
> Typesafe <http://www.typesafe.com/> - classe entreprise de la Scala
> Experts
>
> Twitter: @ viktorklang



--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
StephaneLD
Joined: 2008-08-28,
User offline. Last seen 4 years 9 weeks ago.
Re: My take on Promises..

So, here's the correct version (backed with tests)

def or[U >: T](other: Promise[U]): Promise[U] = {
val prom = Promise[U]()
val status = new java.util.concurrent.atomic.AtomicInteger(0) // 0
-> not set, 2 -> value set (right), 1 -> one error (left)

val updateAsNeeded: Either[Throwable, U] => Unit = {
case r: Right[_, _] =>
if (status.getAndSet(2) < 2) {
prom.setPromiseEither(r)
}
case l: Left[_, _] =>
if (status.getAndSet(1) == 1) { ///we only want to write if we
are the second error
prom.setPromiseEither(l)
}
}

outer.foreachEither(updateAsNeeded)
other.foreachEither(updateAsNeeded)

prom
}

On 22 juil, 17:13, √iktor Ҡlang wrote:
> On Fri, Jul 22, 2011 at 5:05 PM, sledorze wrote:
>
>
>
>
>
>
>
>
>
> > Oh.. I forgot to trigger email reception.. just saw your question :)
>
> > Yes it is!
> > I'll explain a bit :)
>
> > When one call that:
> > status.compareAndSet(1, 1)
>
> > What the CPU does to status is atomically equivalent to that
> > (currentValue being the status current value) :
>
> > def compareAndSet(expected : Int, newValue : Int) : Bool = {
> >  val res = currentValue == expected
> >  currentValue = newValue
> >  res
> > }
>
> > So, calling status.compareAndSet(1, 1) sets the value.
>
> > Hope it makes more sense now :)
>
> scala> val status = new java.util.concurrent.atomic.AtomicInteger(0)
> status: java.util.concurrent.atomic.AtomicInteger = 0
>
> scala> status.compareAndSet(1,1)
> res0: Boolean = false
>
> scala> status.get
> res1: Long = 0
>
> since you are only CASing the same value( 1 for 1 and 2 for 2 )and the
> counter starts at 0, and I didn't see any other writes to the status, how
> does the status transcend 0?
>
> Cheers,
> √
>
>
>
>
>
>
>
>
>
> > Stephane
>
> > On 20 juil, 23:47, √iktor Ҡlang wrote:
> > > Hé Stéphane,
>
> > > C'était une lecture amusante!
>
> > > Une question rapide, est la mise en œuvre de correcte Promise.or?
> > > Je ne vois pas comment "statut" transcende de 0 si vous êtes seulement
> > CAS: ING 1,1 et 2,2
>
> > > Cheers,
> > > √
>
> > > On Wed, 20 juillet 2011 à 18:24, sledorze
> > a écrit:
>
> > > > Serait heureux de lire vos commentaires:)
>
> > > >http://lambdabrella.blogspot.com/2011/07/safe-asynchronous-promise.html
>
> > > > Stéphane
>
> > > -
> > > Viktor Klang
>
> > > Akka Tech Lead
> > > Typesafe - classe entreprise de la Scala
> > > Experts
>
> > > Twitter: @ viktorklang
>
> --
> Viktor Klang
>
> Akka Tech Lead
> Typesafe - Enterprise-Grade Scala from the
> Experts
>
> Twitter: @viktorklang

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland