The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Arity Makes Good Curry

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Arity Makes Good Curry Posted: Nov 23, 2005 1:38 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Arity Makes Good Curry
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

kig just dropped this snip in the comments of the pipes discussion, which had morphed into a discussion about functional programming.

 class Fun < Proc
   def call(*args)
     if args.size < arity.abs and arity != -1
       Fun.new{|*a| call(*(args+a)) }
     else
       super
     end
   end
   alias_method :[], :call
 end

 def fun &block
   Fun.new &block
 end

 f = fun{|a, b, c| a * b / c }

 f[10, 20, 30] == f[10][20][30]
 #=> true

You can sort of curry methods with this, like so:

 class Turtle
   def draw *args
     fun { |x, y| ... }.call(*args)
   end
 end

But you will need to use the bracket syntax on the function returned by the method.

Read: Arity Makes Good Curry

Topic: Revealing hidden assumptions in estimation Previous Topic   Next Topic Topic: Signal vs. Noise Closes Comments to Non-Hoodwink'rs

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use