<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Random Hacks: Some useful closures, in Ruby</title>
    <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Technology and Other Fun Stuff</description>
    <item>
      <title>"Some useful closures, in Ruby" by Nicolás Sanguinetti</title>
      <description>&lt;p&gt;I like compose better as a member of the Proc class, if only because I find using &amp;#8217;*&amp;#8217; much more clear&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre class="typocode_ruby"&gt;
class Proc
  def compose(other)
    lambda {|*args| call(other.call(*args)) }
  end

  alias :* :compose
end

mult2_add1 = add1 * mult2
&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Something similar (albeit a little more complicated) could be done for &lt;code&gt;conjoin&lt;/code&gt; and &lt;code&gt;&amp;#38;&lt;/code&gt;, so you could have&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre class="typocode_ruby"&gt;is_even_number = is_even &amp;#38; is_number&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;But it would be too much work when you can use Enumerable#all? :)&lt;/p&gt;</description>
      <pubDate>Tue, 29 Jan 2008 17:21:02 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:db21851f-9a12-4cf1-a5bc-d535eb0508dc</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-551</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Crest da Zoltral</title>
      <description>&lt;p&gt;Eric Kidd: Depending on how you count a Factor version is even shorter.&lt;/p&gt;


	&lt;p&gt;&lt;tt&gt;
: complement ( quot&amp;#8212;quot ) \ not add ;&lt;/p&gt;


	&lt;p&gt;: compose ( quot1 quot2&amp;#8212;quot ) append ;&lt;/p&gt;


	&lt;p&gt;: conjoin ( seq quot&amp;#8212;? )
    \ and add t swap reduce &amp;gt;boolean ;&lt;/p&gt;


	&lt;p&gt;: disjoin ( seq quot&amp;#8212;? )
    \ or add f swap reduce &amp;gt;boolean ;
&lt;/tt&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 11 Jun 2007 09:40:57 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:ac15e876-0722-43cb-bb92-4c0b31d1f8e3</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-466</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Andy</title>
      <description>&lt;p&gt;With the any and all functions in Python 2.5, the conjoin and disjoin functions become a lot simpler:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
def conjoin(*preds):
    def conj(*args, **kwargs):
        return all(p(*args, **kwargs) for p in preds)
    return conj

def disjoin(*preds):
    def disj(*args, **kwargs):
        return any(p(*args, **kwargs) for p in preds)
    return disj
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;It doesn&amp;#8217;t quite match the pretty point-free style though.&lt;/p&gt;</description>
      <pubDate>Tue, 06 Mar 2007 01:13:47 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:071809a0-7364-492a-88ab-7b3013c8ec72</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-331</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Eric</title>
      <description>&lt;p&gt;Thanks! That&amp;#8217;s &lt;a href="http://groups.google.com/group/pl.comp.lang.perl/msg/3d47c84a1307e381?hl=en" rel="nofollow"&gt;pretty neat&lt;/a&gt;:&lt;/p&gt;


&lt;blockquote&gt;
	&lt;p&gt;But in &amp;#8220;random&amp;#8221; mappings with a very small result set like this, the shortest solution is often to make up some magic formula that has no particular meaning, but just happens to give the wanted result.&lt;/p&gt;

&lt;/blockquote&gt;




	&lt;p&gt;The way he applies a &amp;#8220;magic&amp;#8221; function and strikes out irrelevant digits of the output reminds me of the &lt;a href="http://recursed.blogspot.com/2006/12/prime-game.html" rel="nofollow"&gt;prime game&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Fri, 23 Feb 2007 07:41:52 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:fbdda330-be6c-4229-8144-593ab6254a7d</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-310</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by sean</title>
      <description>&lt;p&gt;I guess every language&amp;#8217;s golf reflects that language&amp;#8217;s nature.  One of the most interesting golf entries I&amp;#8217;ve seen is this one by Ton Hospel&lt;/p&gt;


	&lt;p&gt;http://groups.google.com/group/pl.comp.lang.perl/msg/3d47c84a1307e381?hl=en&lt;/p&gt;


	&lt;p&gt;which to me reflects Perl&amp;#8217;s focus on results over elegance and its appreciation of cleverness.&lt;/p&gt;</description>
      <pubDate>Fri, 23 Feb 2007 06:33:25 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:5b1388dd-9fe8-4e1d-9d21-687b76870620</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-308</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Eric</title>
      <description>&lt;p&gt;Well, that&amp;#8217;s the fun thing about Haskell golf&lt;sup&gt;1&lt;/sup&gt;&amp;#8212;it quickly degenerates into higher mathematics. &amp;#8220;You did &lt;i&gt;what?&lt;/i&gt; How is that a monad?&amp;#8221; And suddenly you&amp;#8217;re hip deep in category theory.&lt;/p&gt;


	&lt;p&gt;Provided the golf is educational, I&amp;#8217;m strongly in favor of it.&lt;/p&gt;


	&lt;p&gt;So everyone, please feel free to golf on this blog. Teach me some cool tricks and tighten up my code. :-)&lt;/p&gt;


	&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt;Golf: Attempting write a program in the smallest number of keystrokes.&lt;/p&gt;</description>
      <pubDate>Thu, 22 Feb 2007 07:38:09 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:cff95a13-03e8-429e-b2f3-f0d0e15fc610</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-305</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by sean</title>
      <description>&lt;p&gt;It still surprises me how &amp;#8220;Haskell Golf&amp;#8221; is still considered a mark of high intelligence or nerd-prowess, while golf in any other language is just a game&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Thu, 22 Feb 2007 02:03:33 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4af5e10d-415f-4254-9846-7b11761fce1e</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-304</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Darc Fitton</title>
      <description>&lt;p&gt;Of course, there’s also &lt;span class="caps"&gt;APL&lt;/span&gt; :&lt;/p&gt;


	&lt;p&gt;is_odd{assign}2{jot}|&lt;/p&gt;


	&lt;p&gt;is_even{assign}~{jot}is_odd&lt;/p&gt;


	&lt;p&gt;Where {jot} is the ‘compose’ operator, used in the first example to curry the ‘modulus’ primitive, in the second to compose the ‘not’ primitive and the function.&lt;/p&gt;


	&lt;p&gt;Here we use a series of ‘compose’ to obtain the function :&lt;/p&gt;


	&lt;p&gt;mult2_plus1{assign}(1{jot}+){jot}(2{jot}{multiply})&lt;/p&gt;


	&lt;p&gt;Conjoin and disjoin are easily translated with {and} and {or} primitives and the {reduce} operator.&lt;/p&gt;


	&lt;p&gt;{and}/1 0 1&lt;/p&gt;


	&lt;p&gt;=&amp;gt; 0&lt;/p&gt;


	&lt;p&gt;{or}/1 0 1&lt;/p&gt;


	&lt;p&gt;=&amp;gt; 1&lt;/p&gt;


	&lt;p&gt;With the added bonus that all primitives map their arguments :&lt;/p&gt;


	&lt;p&gt;2 | 1 2 3 NB. 2 modulus 1 2 3&lt;/p&gt;


	&lt;p&gt;=&amp;gt; 1 0 1&lt;/p&gt;


	&lt;p&gt;We define the operator conjoin :&lt;/p&gt;


	&lt;p&gt;r{assign}(f1 conjoin f2) args&lt;/p&gt;


	&lt;p&gt;r{assign}^/(f1 args)(f2 args)&lt;/p&gt;


	&lt;p&gt;Then use it :&lt;/p&gt;


	&lt;p&gt;is_even conjoin is_number 1 2 3&lt;/p&gt;


	&lt;p&gt;=&amp;gt; 0 1 0&lt;/p&gt;</description>
      <pubDate>Thu, 08 Feb 2007 20:09:13 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:dffbf57d-5249-4f58-8209-74d7fec50b30</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-279</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Ola Bini</title>
      <description>&lt;p&gt;Very nice writeup. Regarding complement for Ruby, I would prefer this, though:&lt;/p&gt;


	&lt;p&gt;def complement &amp;#38;b
 lambda {|&lt;strong&gt;args| not b.call(&lt;/strong&gt;args)}
end&lt;/p&gt;


	&lt;p&gt;Then you can skip the first reification:&lt;/p&gt;


	&lt;p&gt;is_odd = complement {|n| n % 2 == 0}&lt;/p&gt;</description>
      <pubDate>Sat, 03 Feb 2007 03:35:43 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:be73f381-3f37-4c27-988a-41f646a72ecb</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-263</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Eric Kidd</title>
      <description>&lt;p&gt;nmessenger: The map-based versions are nice (if you know what $ does), but the ones with flip are just &lt;em&gt;wrong&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Update: nmessenger just sent me the shortest and most elegant version yet:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;conjoin fs x = all ($ x) fs
disjoin fs x = any ($ x) fs&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Sat, 03 Feb 2007 02:13:59 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:40ed3460-df3c-4a34-af1c-f5112a782fa1</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-262</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by nmessenger</title>
      <description>&lt;p&gt;Though it occurs to me that they could be even &lt;strong&gt;more&lt;/strong&gt; transparent.&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;conjoin fs x = and (map ($ x) fs)
disjoin fs x = or  (map ($ x) fs)&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;Some transformations and we have:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;conjoin = flip (all . flip id)
disjoin = flip (any . flip id)&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;I better stop now, golfing is addictive. :)&lt;/p&gt;</description>
      <pubDate>Sat, 03 Feb 2007 01:57:16 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:69c154c6-652d-4cee-a9ce-c4d782e4db2a</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-261</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by nmessenger</title>
      <description>&lt;p&gt;Abuse, elegance, what&amp;#8217;s the difference?  :P&lt;/p&gt;</description>
      <pubDate>Sat, 03 Feb 2007 01:42:51 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:381b8685-154d-473d-93e3-b56a45cdc850</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-260</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Eric Kidd</title>
      <description>&lt;p&gt;Peter Burns: Thanks! That&amp;#8217;s a lot nicer.&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 16:50:24 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1ec5b012-ce0e-45d7-bbb1-132c431dfa81</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-256</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Peter Burns</title>
      <description>&lt;p&gt;For conjoin and disjoin in ruby I prefer:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;predicates.all? {|f| f.call(2)}
predicates.any? {|f| f.call(2)}&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Fri, 02 Feb 2007 16:12:37 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:737b3242-6c56-4f50-86ac-2558ada45af9</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-255</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Eric Kidd</title>
      <description>&lt;p&gt;nablaone: Yeah, those are also very useful. In Haskell, you could use:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;any even [1]   # False
any even [1,2] # True
all even [1,2] # False
all even [2,4] # True&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;And in Ruby:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="number"&gt;4&lt;/span&gt;&lt;span class="punct"&gt;].&lt;/span&gt;&lt;span class="ident"&gt;all?&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;%&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;==&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="number"&gt;4&lt;/span&gt;&lt;span class="punct"&gt;].&lt;/span&gt;&lt;span class="ident"&gt;any?&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;%&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;==&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Of course, it&amp;#8217;s possible to write both of these higher-order functions without using closures.&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 08:44:54 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b190e899-214f-45bf-b7f7-01350bae7559</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-252</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by nablaone</title>
      <description>&lt;p&gt;http://www.lisp.org/HyperSpec/Body/fun_complement.html
http://www.lisp.org/HyperSpec/Body/fun_everycm_s_erycm_notany.html&lt;/p&gt;


	&lt;p&gt;:-)&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 08:12:49 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b0db6458-bdda-4dbc-939c-6f50a2668ea6</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-251</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Eric Kidd</title>
      <description>&lt;p&gt;nmessenger: Interesting! That appears to rely on &amp;#8220;Monad ((-&amp;gt;) r)&amp;#8221;.  Here&amp;#8217;s the &lt;a href="http://darcs.haskell.org/packages/base/Control/Monad/Instances.hs" rel="nofollow"&gt;source&lt;/a&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;instance Functor ((-&amp;gt;) r) where
  fmap = (.)

instance Monad ((-&amp;gt;) r) where
  return = const
  f &amp;gt;&amp;gt;= k = \ r -&amp;gt; k (f r) r&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Frankly, I&amp;#8217;m impressed. That&amp;#8217;s a monad with some serious potential for abuse.&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 08:12:22 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:5a3fdc50-4409-4bc2-b0eb-03f31339677d</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-250</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by nmessenger</title>
      <description>&lt;p&gt;@Eric Kidd:&lt;/p&gt;


	&lt;p&gt;Haskell&amp;#8217;s &amp;#8216;conjoin&amp;#8217; and &amp;#8216;disjoin&amp;#8217; can be written even more concisely:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;import Control.Monad(liftM2)
conjoin = foldr (liftM2 (&amp;#38;&amp;#38;)) (const True)
disjoin = foldr (liftM2 (||)) (const False)&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Fri, 02 Feb 2007 05:48:25 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:6964f139-cab0-44df-9a4b-cad067460366</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-249</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Devin Ben-Hur</title>
      <description>&lt;p&gt;These handy tools are independent of the idea of closures.  The functional composition works for any higher-order function regardless of whether the function definitions close on their environment.&lt;/p&gt;


Also, seems like your composition primitives would lose any block attachments when called.  Might what to use &lt;code&gt;*args,&amp;#38;blk&lt;/code&gt; every time you accept and propagate the arguments.  For compose you&amp;#8217;ll need 
&lt;pre&gt;
&lt;code&gt;
def compose f, g
  lambda {|*args,&amp;#38;blk| f.call(g.call(*args,&amp;#38;blk),&amp;#38;blk) }
end
&lt;/code&gt;
&lt;/pre&gt;</description>
      <pubDate>Fri, 02 Feb 2007 01:53:42 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:51a4684c-ad7e-4f6c-8db5-6e44ce216b82</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-248</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Eric Kidd</title>
      <description>&lt;p&gt;Of course, Haskell makes these all very terse:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;complement = (not .)
compose = (.)
conjoin = foldr (\f g x -&amp;gt; f x &amp;amp;&amp;amp; g x)
                (const True)  
disjoin = foldr (\f g x -&amp;gt; f x || g x)
                (const False)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;In two cases, the Haskell implementation is shorter than the Dylan name!&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s some sample code:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;complement even 1         -- True
compose (+1) (*2) 3       -- 7
conjoin [(&amp;gt;=1),(&amp;lt;=10)] 1  -- True
conjoin [(&amp;gt;=1),(&amp;lt;=10)] 11 -- False
disjoin [(&amp;lt;1),(&amp;gt;10)] 11   -- True&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Do you know of any languages which can make these examples even shorter?&lt;/p&gt;</description>
      <pubDate>Thu, 01 Feb 2007 21:29:52 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:16caf5d5-b0d4-4a42-97bb-56384d40366e</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-247</link>
    </item>
    <item>
      <title>"Some useful closures, in Ruby" by Michael</title>
      <description>&lt;p&gt;These are also pretty easy to achieve in Python, as well.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;
def complement(fn):
    def not_fn(*args, **kwargs):
        return not fn(*args, **kwargs)
    return not_fn

# Composition, assuming f is unary
def compose(f, g):
    def f_ring_g(*args, **kwargs):
        return f(g(*args, **kwargs))
    return f_ring_g

def conjoin(*preds):
    def conj(*args, **kwargs):
        for p in preds:
            q = p(*args, **kwargs)
            if not q: return q
        return True
    return conj

def disjoin(*preds):
    def disj(*args, **kwargs):
        for p in preds:
            q = p(*args, **kwargs)
            if q: return q
        return False
    return disj

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The lack of Ruby&amp;#8217;s nice, polite anonymous blocks in Python can be really annoying at times, but it&amp;#8217;s not that difficult to work around their absence for cases like these.  Note that you can shorten up &lt;code&gt;conjoin&lt;/code&gt; and &lt;code&gt;disjoin&lt;/code&gt; a bit if you are willing to throw out the return values that generated the exit case and just throw back True or False.&lt;/p&gt;</description>
      <pubDate>Thu, 01 Feb 2007 20:51:14 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:cb3142fc-6b7d-461f-a7c0-93a88829e3c7</guid>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby#comment-246</link>
    </item>
    <item>
      <title>Some useful closures, in Ruby</title>
      <description>&lt;p&gt;Reginald Braithwaite has just posted &lt;a href="http://weblog.raganwald.com/2007/01/closures-and-higher-order-functions.html"&gt;a short introduction to closures in Ruby&lt;/a&gt;. Closures allow you to pass functions around your program, and build new functions from old ones.&lt;/p&gt;

&lt;p&gt;Programming languages that support closures include Perl, Ruby, Python (sorta), Lisp, Haskell, Dylan, Javascript and many others.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/Dylan_programming_language"&gt;Dylan programming language&lt;/a&gt; included four very useful functions built using closures: &lt;strong&gt;complement&lt;/strong&gt;, &lt;strong&gt;conjoin&lt;/strong&gt;, &lt;strong&gt;disjoin&lt;/strong&gt; and &lt;strong&gt;compose&lt;/strong&gt;.  The names are a bit obscure, but they can each be written in a few lines of Ruby.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s start with &lt;strong&gt;complement&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="comment"&gt;# Builds a function that returns true&lt;/span&gt;
&lt;span class="comment"&gt;# when 'f' returns false, and vice versa.&lt;/span&gt;
&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;complement&lt;/span&gt; &lt;span class="ident"&gt;f&lt;/span&gt;
  &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="punct"&gt;{|*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="keyword"&gt;not&lt;/span&gt; &lt;span class="ident"&gt;f&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can use this to build the &amp;#8220;opposite&amp;#8221; of a function:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;is_even&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;n&lt;/span&gt; &lt;span class="punct"&gt;%&lt;/span&gt; &lt;span class="number"&gt;2&lt;/span&gt; &lt;span class="punct"&gt;==&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
&lt;span class="ident"&gt;is_odd&lt;/span&gt;  &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;complement&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;is_even&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;

&lt;span class="ident"&gt;is_odd&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;# true&lt;/span&gt;
&lt;span class="ident"&gt;is_odd&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;# false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;compose&lt;/strong&gt; is another useful function:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="comment"&gt;# Builds a function which calls 'f' with&lt;/span&gt;
&lt;span class="comment"&gt;# the return value of 'g'.&lt;/span&gt;
&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;compose&lt;/span&gt; &lt;span class="ident"&gt;f&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;g&lt;/span&gt;
  &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="punct"&gt;{|*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;f&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;g&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;))&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can use this to pass the output of one function to the input of another:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;mult2&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;*&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
&lt;span class="ident"&gt;add1&lt;/span&gt;  &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;+&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
&lt;span class="ident"&gt;mult2_add1&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;compose&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;add1&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;mult2&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;

&lt;span class="ident"&gt;mult2_add1&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;3&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;# 7&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;strong&gt;conjoin&lt;/strong&gt; function is a bit more complicated, but still very useful:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="comment"&gt;# Builds a function which returns true&lt;/span&gt;
&lt;span class="comment"&gt;# whenever _every_ function in 'predicates'&lt;/span&gt;
&lt;span class="comment"&gt;# returns true.&lt;/span&gt;
&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;conjoin&lt;/span&gt; &lt;span class="punct"&gt;*&lt;/span&gt;&lt;span class="ident"&gt;predicates&lt;/span&gt;
  &lt;span class="ident"&gt;base&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="punct"&gt;{|*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="constant"&gt;true&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
  &lt;span class="ident"&gt;predicates&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;inject&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;base&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;built&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;pred&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
    &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
      &lt;span class="ident"&gt;built&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="ident"&gt;pred&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can use it to construct the logical &amp;#8220;and&amp;#8221; of a list of functions:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;is_number&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;kind_of?&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;Numeric&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
&lt;span class="ident"&gt;is_even_number&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;conjoin&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;is_number&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;is_even&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;

&lt;span class="ident"&gt;is_even_number&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;a&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;)&lt;/span&gt; &lt;span class="comment"&gt;# false&lt;/span&gt;
&lt;span class="ident"&gt;is_even_number&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;   &lt;span class="comment"&gt;# false&lt;/span&gt;
&lt;span class="ident"&gt;is_even_number&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;   &lt;span class="comment"&gt;# true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The opposite of &lt;strong&gt;conjoin&lt;/strong&gt; is &lt;strong&gt;disjoin&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="comment"&gt;# Builds a function which returns true&lt;/span&gt;
&lt;span class="comment"&gt;# whenever _any_ function in 'predicates'&lt;/span&gt;
&lt;span class="comment"&gt;# returns true.&lt;/span&gt;
&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;disjoin&lt;/span&gt; &lt;span class="punct"&gt;*&lt;/span&gt;&lt;span class="ident"&gt;predicates&lt;/span&gt;
  &lt;span class="ident"&gt;base&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="punct"&gt;{|*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="constant"&gt;false&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
  &lt;span class="ident"&gt;predicates&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;inject&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;base&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;built&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;pred&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
    &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
      &lt;span class="ident"&gt;built&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;||&lt;/span&gt; &lt;span class="ident"&gt;pred&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This allows us to construct the logical &amp;#8220;or&amp;#8221; of a list of functions:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;is_string&lt;/span&gt;  &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;n&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;kind_of?&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;String&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
&lt;span class="ident"&gt;is_string_or_number&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt;
  &lt;span class="ident"&gt;disjoin&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;is_string&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;is_number&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;

&lt;span class="ident"&gt;is_string_or_number&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;a&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;)&lt;/span&gt; &lt;span class="comment"&gt;# true&lt;/span&gt;
&lt;span class="ident"&gt;is_string_or_number&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;   &lt;span class="comment"&gt;# true&lt;/span&gt;
&lt;span class="ident"&gt;is_string_or_number&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:a&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;  &lt;span class="comment"&gt;# false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;These were four of the first closure-related functions I ever used, and they&amp;#8217;re still favorites today.&lt;/p&gt;

&lt;p&gt;Feel free to post versions in other languages below!&lt;/p&gt;</description>
      <pubDate>Thu, 01 Feb 2007 18:36:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:662731c8-031a-46e3-97bc-02668e09b272</guid>
      <author>Eric Kidd</author>
      <link>http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby</link>
      <category>Ruby</category>
      <category>Hacks</category>
      <trackback:ping>http://www.randomhacks.net/articles/trackback/245</trackback:ping>
    </item>
  </channel>
</rss>
