[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [SUMMARY] Port a Library (#64

Ross Bamford

2/2/2006 3:04:00 PM

On Thu, 2006-02-02 at 22:37 +0900, Ruby Quiz wrote:
> The library already supports a bunch of "spices", like HOLE above, but
> you can also add your own:
>
> # Lazy Evaluation meets Currying...
> class LazySpice < Curry::SpiceArg
> def initialize( &promise )
> super("LAZYSPICE")
>
> @promise = promise
> end
>
> def spice_arg( args ) # called to provide the missing argument
> [@promise.call]
> end
> end
>
> logger = lambda do |time, message|
> puts "[#{time.strftime('%I:%M:%S %p %m/%d/%y')}] #{message}"
> end
>
> log_now = logger.curry(LazySpice.new { Time.now })
>
> log_now["First Message."] # => [12:47:53 PM 02/01/06] First Message.
> sleep 3
> log_now["Second Message."] # => [12:47:56 PM 02/01/06] Second Message.
>
> Notice how the LazySpice isn't evaluated until the time of the call. That lazy
> execution makes sure our message is stamped with the time it was actually
> logged.

This is very cool, thanks for the great write-up. However, there is a
small bug (in curry.rb, not this code) that can cause problems with
this, e.g:

a = [1,3,5]
b = [2,4,6]

l = lambda do |ary,aa,ba|
ary + [aa,ba]
end.curry(Curry::HOLE,Curry::HOLE,TestSpice.new { b.shift })

l.call
# => [1,nil,3,nil,5,nil] (expected: 1,2,3,4,5,6)

This only shows up when special spices are at the end of the argument
list, and happens because there are no args_remain left by the time
they're seen, and I took a short-cut in the implementation. The attached
patch (against the documented version from
http://roscopeco.co.uk/code/ruby-quiz-entries/6...) fixes things.
There maybe a slight impact on performance, though (if that matters).

I'm considering maybe packaging Ruby Murray up as a gem and releasing it
on RubyForge. I'll add James' LazySpice, and would like any suggestions
others may have (esp if anyone has something we could legitimately call
"SportySpice" ;D)

Thanks again for the quiz. Cool entries everyone :)

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk