# Re: lazy.rb 0.9.5 -- transparent futures!

**URL:** <https://www.ruby-forum.com/t/re-lazy-rb-0-9-5-transparent-futures/72126>\
**Category:** Ruby\
**Created:** [February 23, 2006, 12:46am UTC](https://www.ruby-forum.com/t/re-lazy-rb-0-9-5-transparent-futures/72126 "2006-02-23T00:46:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [February 23, 2006, 12:46am UTC](https://www.ruby-forum.com/t/re-lazy-rb-0-9-5-transparent-futures/72126/1 "2006-02-23T00:46:45Z")

</div>

You can just do

table\_name = “CUSTOMERS”  
x = promise { table\_name }  
puts x  
=\> “CUSTOMERS”

though you have to then make sure that you leave that table\_name  
variable alone.

table\_name = “CUSTOMERS”  
x = promise { table\_name }  
table\_name = “MONKEYS”  
puts x  
=\> “MONKEYS”

* * *

```
From: Shashank D. [mailto:removed_email_address@domain.invalid]
Sent: Thursday, 23 February 2006 9:15 AM
To: ruby-talk ML
Cc: removed_email_address@domain.invalid
Subject: Re: [ANN] lazy.rb 0.9.5 -- transparent futures!

Hi,

I have a feature request (along with a quick-hack solution).

Please ignore if this is already done or sounds crazy. :-)

Problem: All the promises I make, cannot have any arguments ! !

So something like this is what I have in mind:

x = promise("CUSTOMERS") { |table_name|

```

long\_running\_query(table\_name)}

```
Admitedly, in this one-liner it does not make much sense. But if

```

the block had many many lines of code and referred to the block variable  
at multiple places, suddenly it would be mean more to me.

```
I have attached my hackish solution without really thinking all

```

the way thru.  
Thanks,

```
-- Shashank

MenTaLguY <removed_email_address@domain.invalid> wrote:
I'd like to announce a new version of lazy.rb -- this one

```

offering  
thread safety and transparent futures!

```
Here's the web site, complete with gem and tarball downloads,

```

plus a bit  
of documentation:

```
http://moonbase.rydia.net/software/lazy.rb/

Bug reports would be very, very welcome.

```

* * *

```
Yahoo! Mail
Use Photomail

```

\<[http://pa.yahoo.com/\*http://us.rd.yahoo.com/evt=38867/\*http://photomail](http://pa.yahoo.com/*http://us.rd.yahoo.com/evt=38867/*http://photomail)  
.mail.yahoo.com\> to share photos without annoying attachments.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [February 23, 2006, 2:17am UTC](https://www.ruby-forum.com/t/re-lazy-rb-0-9-5-transparent-futures/72126/2 "2006-02-23T02:17:11Z")

</div>

table\_name = “CUSTOMERS”  
x = promise {table\_name.dup}  
table\_name = “MONKEYS”  
puts x  
=\>“CUSTOMERS”

Would avoid the problem. Though it’s good to point out that you  
likely want to avoid changing the state of any variable that’s  
captured by a promise closure (unless of course you plan on exploiting  
just that).

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [February 23, 2006, 5:24am UTC](https://www.ruby-forum.com/t/re-lazy-rb-0-9-5-transparent-futures/72126/3 "2006-02-23T05:24:17Z")

</div>

Unlikely. The variables passed into the promise would still have  
their references to the outside. You get exactly the same effect as  
capturing the variables in a closure and in fact, the given examples  
are kinda contrived, it would make more sense to assign CUSTOMERS as a  
local variable inside of the promise block, because then it can’t be  
affected outside of the closure.

However, currently, there IS a block parameter (that of the promise  
itself, something to do with circular programming, which sounds  
intriguing, but I have hella no idea what it means [PLEASE SEND  
LINKS]), but it doesn’t seem like you’d use it in most cases.

If you wanted something like, I dunno, currying a promise, you could  
just wrap it in a lambda.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [February 23, 2006, 5:00am UTC](https://www.ruby-forum.com/t/re-lazy-rb-0-9-5-transparent-futures/72126/4 "2006-02-23T05:00:09Z")

</div>

Hi,

Daniel N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: table\_name = “CUSTOMERS”  
x = promise {table\_name.dup}  
table\_name = “MONKEYS”  
puts x  
=\>“CUSTOMERS”

Would avoid the problem. Though it’s good to point out that you  
likely want to avoid changing the state of any variable that’s  
captured by a promise closure (unless of course you plan on exploiting  
just that).

All this being said, is there any merit in the idea of passing block  
parameters? I was toying with it briefly and decided to give it a shot  
on a whim. Will try and come up with a more convincing answer.

Thanks for all your inputs.  
– shanko

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [February 23, 2006, 6:43am UTC](https://www.ruby-forum.com/t/re-lazy-rb-0-9-5-transparent-futures/72126/5 "2006-02-23T06:43:10Z")

</div>

On Thu, 2006-02-23 at 10:14 +0900, Daniel N. wrote:

> table\_name = “CUSTOMERS”  
> x = promise {table\_name.dup}  
> table\_name = “MONKEYS”  
> puts x  
> =\>“CUSTOMERS”
> 
> Would avoid the problem.

Actually, no. String#dup wouldn’t get called until the promise was  
demanded, by which time table\_name refers to a different string.

-mental

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [February 24, 2006, 1:02am UTC](https://www.ruby-forum.com/t/re-lazy-rb-0-9-5-transparent-futures/72126/6 "2006-02-24T01:02:45Z")

</div>

Daniel N. wrote:

> table\_name = “CUSTOMERS”  
> x = promise {table\_name.dup}  
> table\_name = “MONKEYS”  
> puts x  
> =\>“CUSTOMERS”
> 
> Would avoid the problem. Though it’s good to point out that you  
> likely want to avoid changing the state of any variable that’s  
> captured by a promise closure (unless of course you plan on exploiting  
> just that).

Hmmm … I just tried it and got “MONKEYS” for the result. It makes  
sense becase by the time table\_name is dup’ed in the promise, it has  
already been modified.

This would do it:

def table\_promise(name)  
promise { name }  
end

table\_name = “CUSTOMERS”  
x = table\_promise(table\_name)  
table\_name = “MONKEYS”  
puts x # =\> “CUSTOMERS”

–  
– Jim W.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [February 24, 2006, 2:09am UTC](https://www.ruby-forum.com/t/re-lazy-rb-0-9-5-transparent-futures/72126/7 "2006-02-24T02:09:53Z")

</div>

I don’t think I understand what’sgoing on th… oh wait. I see.

both table\_name and name are pointing to “CUSTOMERS” and then only  
table\_name is pointing to “MONKEYS”

Yes, see, the thing that’s important to consider is that I’m dumb.

_tries out Mental’s examples, figures out how they work_

…

Woah, that’s some powerful ju-ju. Maybe I should go and futz with  
GhostWriter again anyhow…

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [February 23, 2006, 6:58am UTC](https://www.ruby-forum.com/t/re-lazy-rb-0-9-5-transparent-futures/72126/8 "2006-02-23T06:58:13Z")

</div>

On Thu, 2006-02-23 at 13:21 +0900, Daniel N. wrote:

> However, currently, there IS a block parameter (that of the promise  
> itself, something to do with circular programming, which sounds  
> intriguing, but I have hella no idea what it means [PLEASE SEND  
> LINKS]), but it doesn’t seem like you’d use it in most cases.

Here are a couple trivial examples…

Building a circular data structure (a linked list in this case):

def build\_circular\_list( \*stuff )  
promise { |result|  
if stuff.empty?  
nil  
else  
list = result  
stuff.reverse.each { |thing|  
list = [thing, list]  
}  
list  
end  
}  
end

Padding lines of text to a maximum width in a single pass:

def pad\_lines( lines )  
promise { |result|  
max\_width = 0  
padded\_lines = []

```
 lines.each { |line|
   max_width = line.length if line.length > max_width
   padded_lines.push promise { "%*s" % [result[1], line ] }
 }

 [padded_lines, max_width]

```

}[0]  
end

-mental
