<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7132283077086596617</id><updated>2011-04-22T00:24:39.221-04:00</updated><category term='ruby'/><category term='cancer'/><category term='sdd'/><category term='SICP'/><category term='jesus'/><category term='san francisco'/><category term='books'/><category term='cell phone'/><category term='first-class procedures'/><category term='intro'/><category term='perl'/><category term='hashes'/><category term='wigs for kids'/><category term='hair'/><category term='ruby by example'/><category term='rspec'/><category term='borg'/><category term='food'/><category term='railscasts.com'/><category term='spam'/><category term='rails'/><category term='railsconf'/><category term='tdd'/><category term='partition'/><category term='travis nixon'/><category term='bdd'/><category term='static typing'/><category term='boston'/><category term='encapsulation'/><category term='comments'/><category term='duck typing'/><category term='helpers'/><title type='text'>Squire of the Lambda Calculus</title><subtitle type='html'>&lt;p&gt;
[ p | p &amp;lt;- posts, p.pertains_to?(Ruby, Erlang, Haskell, SICP, whatever I feel like) ]
&lt;/p&gt;
&lt;p&gt;
&amp;quot;Higher-order functions are the very essence of functional programming languages – they breathe fire into the belly of the code.&amp;quot; – Joe Armstrong
&lt;/p&gt;</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>19</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-5554466933546329121</id><published>2009-04-07T19:05:00.005-04:00</published><updated>2009-04-07T19:11:32.762-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spam'/><category scheme='http://www.blogger.com/atom/ns#' term='cell phone'/><title type='text'>How to Avoid Cell Phone Spam</title><content type='html'>Recently I've noticed a definite upswing in the number of unsolicited commercial phone calls I get on my cell phone. What to do?&lt;br /&gt;&lt;br /&gt;I found a simple may to minimize (or at least reduce) the annoyance. My phone uses mp3 files for custom ringtones, and is able to receive such files via email. So I &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;edited a silent and extremely short mp3 file&lt;/li&gt;&lt;br /&gt;&lt;li&gt;sent it to my phone&lt;/li&gt;&lt;br /&gt;&lt;li&gt;saved the annoying caller as a contact, and&lt;/li&gt;&lt;br /&gt;&lt;li&gt;set the custom ringtone for that contact to the silent file&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;It requires a little overhead in defining the contact, but now it means that I am no longer bothered by that caller again.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-5554466933546329121?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/5554466933546329121/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=5554466933546329121' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/5554466933546329121'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/5554466933546329121'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2009/04/how-to-avoid-cell-phone-spam.html' title='How to Avoid Cell Phone Spam'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-2820928471604309259</id><published>2009-03-26T12:37:00.008-04:00</published><updated>2009-03-26T13:23:44.022-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><category scheme='http://www.blogger.com/atom/ns#' term='hashes'/><title type='text'>Constant Hashes w/defaults in Ruby</title><content type='html'>Hi there. I'm back after a long hiatus. Since my last post, I've moved across the country from Buffalo, NY to Berkeley, CA, had 2 jobs, and become a father. So I ask your forgiveness for the long period between posts.&lt;br /&gt;&lt;br /&gt;On to some code. I like to use Constants in Ruby whenever feasible. It's a logical and readable-to-the-next-coder way to store information in a way that indicates it should not change. It also gets some compile-time optimization for speed, and takes up less memory as a datum shared across the various instances of a class.&lt;br /&gt;&lt;br /&gt;I'm particularly fond of constant Hashes for small lookup tables. Let's imagine some generic Rails controller with this code snippet:&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;BANAL_MESSAGE_FOR = {&lt;br /&gt;  :edit =&gt; 'You are editing.',&lt;br /&gt;  :show =&gt; 'You are viewing a single instance.'&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Hashes in Ruby have a handy method called &lt;strong&gt;default=&lt;/strong&gt; that sets what the default looked-up value should be when the key for the lookup is not found in the Hash. (You can think of the default default as &lt;strong&gt;nil&lt;/strong&gt;, if that makes any sense.) However, &lt;strong&gt;default=&lt;/strong&gt; returns the default value, rather than the new Hash. So this gets problematic:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;BANAL_MESSAGE_FOR = {&lt;br /&gt;  :edit =&gt; 'You are editing.',&lt;br /&gt;  :show =&gt; 'You are viewing a single instance.'&lt;br /&gt;}.default = "I don't know what you're doing".&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The example above will break, as BANAL_MESSAGE_FOR is no longer a Hash at all.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;BANAL_MESSAGE_FOR = {&lt;br /&gt;  :edit =&gt; 'You are editing.',&lt;br /&gt;  :show =&gt; 'You are viewing a single instance.'&lt;br /&gt;}&lt;br /&gt;BANAL_MESSAGE_FOR.default = "I don't know what you're doing".&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The example above will lead to compile-time warnings about modifying a Constant.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;What alternatives exists for this issue?&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;I've grown fond of the following approach:&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;BANAL_MESSAGE_FOR = lambda do&lt;br /&gt;  message_for = {&lt;br /&gt;    :edit =&gt; 'You are editing.',&lt;br /&gt;    :show =&gt; 'You are viewing a single instance.'&lt;br /&gt;  }&lt;br /&gt;  message_for.default = "I don't know what you're doing".&lt;br /&gt;  message_for&lt;br /&gt;end.call&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This allows us to define the particular cases with explicit Hash pairs, set a default value, and keep everything in Constant world. Depending on the particulars, we could instead go with either a class variable (&lt;strong&gt;@@banal_message_for&lt;/strong&gt;) or a class method. &lt;br /&gt;&lt;br /&gt;I still like constant-from-a-lambda, though - even despite the somewhat off-putting complexity of the lambda/call syntax. It makes it clear that we're dealing with constant data, and the complexity from the call syntax seems less egregious to me than having an additional method whose only purpose is to be called once at app start.&lt;br /&gt;&lt;br /&gt;What do you think?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-2820928471604309259?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/2820928471604309259/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=2820928471604309259' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/2820928471604309259'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/2820928471604309259'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2009/03/constant-hashes-wdefaults-in-ruby.html' title='Constant Hashes w/defaults in Ruby'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-3593825359529627891</id><published>2008-09-30T11:22:00.001-04:00</published><updated>2008-09-30T11:22:00.515-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='encapsulation'/><title type='text'>Hidden Functions</title><content type='html'>Let's talk about hidden functions. What do I mean by that? When I refer to a hidden function, I mean one that exists in some non-public context, and is not directly accessible from the outside. There are several ways one can realize this goal, and we'll talk our usual stroll through some appropriate languages and how they approach his problem.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Scheme&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;Let's start with Scheme. Assuming the follow content in a file called &lt;strong&gt;hidden.scm&lt;/strong&gt;:&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;(define (obvious)&lt;br /&gt;  (define (hidden) `hidden)&lt;br /&gt;  (hidden))&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We can then try it out using &lt;strong&gt;guile -l hidden.scm&lt;/strong&gt;:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;guile&gt; (hidden)&lt;br /&gt;&lt;br /&gt;Backtrace:&lt;br /&gt;In standard input:&lt;br /&gt;   1: 0* (hidden)&lt;br /&gt;&lt;br /&gt;standard input:1:1: In expression (hidden):&lt;br /&gt;standard input:1:1: Unbound variable: hidden&lt;br /&gt;ABORT: (unbound-variable)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We see that &lt;strong&gt;(hidden)&lt;/strong&gt; is not directly accessible from the outside, however...&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;guile&gt; (obvious)&lt;br /&gt;hidden&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It is still defined and reachable through the intermediary function &lt;strong&gt;(obvious)&lt;/strong&gt;. This is great for all the expected &lt;q&gt;black box&lt;/q&gt; reasons: &lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Users/developers can be presented with a consistent interface&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Purpose can be separated from implementation details, which means that&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Testing is easier to do, which means that&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Refactoring is easier to do, whether literally just an improvement to internal clarity and readability, or wherein the inputs and outputs remain the same expressions, but other improvements occur, such as faster speed of execution, reduced memory footprint, or both&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Perl&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;I've written about Perl a bit more than I expected to lately. Let's continue with that, and see an example in Perl.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;#!/usr/bin/env perl&lt;br /&gt;# hidden.pl&lt;br /&gt;&lt;br /&gt;use constant HIDDEN =&gt; 'hidden';&lt;br /&gt;&lt;br /&gt;sub obvious {&lt;br /&gt;  sub hidden { HIDDEN }&lt;br /&gt;  hidden();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sub obvious2 {&lt;br /&gt;  my $hidden = sub { HIDDEN };&lt;br /&gt;  $hidden-&gt;();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;print obvious() . "\n";&lt;br /&gt;print obvious2() . "\n";&lt;br /&gt;print hidden() . " is really not that hidden\n";&lt;br /&gt;print $hidden-&gt;() . "\n";&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Executing this program at the command line with &lt;strong&gt;perl hidden.pl&lt;/strong&gt; produces the following output:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;hidden&lt;br /&gt;hidden&lt;br /&gt;hidden is really not that hidden&lt;br /&gt;Undefined subroutine &amp;main:: called at hidden.pl line 19.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I like constants for values that don't change and are reused - call me crazy. The examples &lt;strong&gt;obvious()&lt;/strong&gt; and &lt;strong&gt;obvious2()&lt;/strong&gt; are very similar. The only difference is that the function which we wish to hide is declared either as a traditional subroutine or as a subroutine reference. Why the distinction?&lt;br /&gt;&lt;br /&gt;The reason is revealed when we make a direct call to &lt;strong&gt;hidden()&lt;/strong&gt;. Perl does not scope the declaration of &lt;strong&gt;hidden()&lt;/strong&gt; within &lt;strong&gt;obvious()&lt;/strong&gt; as Scheme does. &lt;strong&gt;hidden()&lt;/strong&gt; is accessible from outside &lt;strong&gt;obvious()&lt;/strong&gt; where it was declared. However, attempting to call &lt;strong&gt;$hidden-&gt;()&lt;/strong&gt; at the top level fails, because Perl does scope &lt;strong&gt;$hidden&lt;/strong&gt; inside &lt;strong&gt;obvious2()&lt;/strong&gt; where it was declared.&lt;br /&gt;&lt;br /&gt;I'm not a real Perl guru. I get by in the language, but I'm sure someone who frequents Perl Monks or the like could explore this idea further.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Ruby&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;Despite Ruby's comprehensive support for programming in the functional paradigm, it is commonly thought of as an object oriented language, not without cause. Many people are most familiar with the notion of hiding executable code through the process of &lt;strong&gt;private&lt;/strong&gt; or &lt;strong&gt;protected&lt;/strong&gt; access control common in &lt;acronym title="Object-Oriented"&gt;OO&lt;/acronym&gt; languages.&lt;br /&gt;&lt;br /&gt;I won't give an example of traditional OO access control in Ruby here. Rather, I'll explore ideas more akin to the preceding Scheme and Perl examples that use nested function declarations. Some of this code is intentionally broken and/or strange-looking.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;class Owner&lt;br /&gt; &lt;br /&gt;  HIDDEN_PROC = lambda { %q[hidden value from Proc] }&lt;br /&gt;  &lt;br /&gt;  def obvious&lt;br /&gt;    def hidden; %q[hidden value]; end &lt;br /&gt;    hidden.gsub(%r[hidden], %q[obvious])&lt;br /&gt;  end &lt;br /&gt;  def obvious2; hidden + %q[ from obvious2]; end &lt;br /&gt;  def obvious3; HIDDEN_PROC.call;            end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if (__FILE__ == $0) &lt;br /&gt;  o = Owner.new&lt;br /&gt;  puts o.obvious&lt;br /&gt;  puts o.obvious2&lt;br /&gt;  puts o.obvious3&lt;br /&gt;  begin&lt;br /&gt;    puts o.obvious.hidden&lt;br /&gt;  rescue&lt;br /&gt;    puts %q[Could not access o.obvious.hidden]&lt;br /&gt;  end &lt;br /&gt;  begin&lt;br /&gt;    puts o::HIDDEN_PROC.call&lt;br /&gt;  rescue&lt;br /&gt;    puts %q[Could not access o::HIDDEN_PROC.call]&lt;br /&gt;  end &lt;br /&gt;  puts o.hidden + %q[ - not really all that hidden]&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Let's take it for a spin with &lt;strong&gt;ruby hidden.rb&lt;/strong&gt;. (Note that the boolean expression &lt;strong&gt;(__FILE__ == $0)&lt;/strong&gt; is only truthy when the file is executed directly like this, and not when the file is required in the manner of a library file.)&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;obvious value&lt;br /&gt;hidden value from obvious2&lt;br /&gt;hidden value from Proc&lt;br /&gt;Could not access o.obvious.hidden&lt;br /&gt;Could not access o::HIDDEN_PROC.call&lt;br /&gt;hidden value - not really all that hidden&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;hidden()&lt;/strong&gt; is callable from within &lt;strong&gt;obvious()&lt;/strong&gt;, and we can even do a regular expression substitution on its value. Fine and dandy. Its hidden status is called into question by it being accessible from within &lt;strong&gt;obvious2()&lt;/strong&gt;, even though it was not declared there. Let's ponder why that is for a moment.&lt;br /&gt;&lt;br /&gt;If a function in Ruby were genuinely hideable, as in Scheme, it would not be so available. However, one must keep in mind that there are technically no functions in Ruby at all: they're methods, and that distinction makes all the difference. A method is a function that is attached to an object (or class), and what would it imply if a method were not accessible by its owning object? It wouldn't be much of a method, and that's where Ruby's OO nature wins out.&lt;br /&gt;&lt;br /&gt;In order to restrict access to some executable code within an object, we can either comply with OO practice and use traditional access control, or do something akin to &lt;strong&gt;obvious3()&lt;/strong&gt;, in which we define a callable &lt;strong&gt;HIDDEN_PROC&lt;/strong&gt; that is not publicly accessible, but is callable from within &lt;strong&gt;obvious3()&lt;/strong&gt; (for example). If we wanted even more separation, we could define &lt;strong&gt;HIDDEN_PROC&lt;/strong&gt; as a lexical variable within the &lt;strong&gt;obvious3()&lt;/strong&gt; method, rather than as a class Constant. Such an approach would be very similar to the Perl example's definition of &lt;strong&gt;$hidden&lt;/strong&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Erlang&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;On to Erlang, which has some interesting features regarding exporting of functions. Here's our file &lt;strong&gt;hidden.erl&lt;/strong&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;-module(hidden).&lt;br /&gt;-author("Kevin C. Baird").&lt;br /&gt;-purpose("Demonstrate hidden functions in Erlang").&lt;br /&gt;-export([obvious/0]).&lt;br /&gt;&lt;br /&gt;obvious() -&gt; hidden().&lt;br /&gt;hidden()  -&gt; 'value from hidden'.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here's our &lt;strong&gt;erl&lt;/strong&gt; session that uses it:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;$ erl &lt;br /&gt;Erlang (BEAM) emulator version 5.5.5 [source] [async-threads:0] [hipe] [kernel-poll:false]&lt;br /&gt;&lt;br /&gt;Eshell V5.5.5  (abort with ^G)&lt;br /&gt;1&gt; c(hidden).&lt;br /&gt;{ok,hidden}&lt;br /&gt;2&gt; hidden:obvious().&lt;br /&gt;'value from hidden'&lt;br /&gt;3&gt; hidden:hidden(). &lt;br /&gt;&lt;br /&gt;=ERROR REPORT==== 30-Sep-2008::10:05:09 ===&lt;br /&gt;Error in process &lt;0.31.0&gt; with exit value: {undef,[{hidden,hidden,[]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}&lt;br /&gt;&lt;br /&gt;** exited: {undef,[{hidden,hidden,[]},&lt;br /&gt;                   {erl_eval,do_apply,5},&lt;br /&gt;                   {shell,exprs,6},&lt;br /&gt;                   {shell,eval_loop,3}]} **&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Because only &lt;strong&gt;obvious&lt;/strong&gt; is exported, it is the only function publicly available. We could have used as complex a basis as we wanted to determine its eventual value, all of which could be safely hidden within our &lt;strong&gt;hidden&lt;/strong&gt; module. That's what encapsulation's all about.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Haskell&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;Another purely functional language I've discussed is Haskell. In fact, its strongest advocates would probably deny Erlang the label &lt;q&gt;purely functional&lt;/q&gt;, as it does not segregate side effects as fully as Haskell does.&lt;br /&gt;&lt;br /&gt;In any case, we can have a quite small &lt;strong&gt;Hidden.hs&lt;/strong&gt; file:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;-- Hidden.hs&lt;br /&gt;-- Kevin C. Baird&lt;br /&gt;-- Demonstrate hidden functions in Haskell&lt;br /&gt;&lt;br /&gt;obvious = hidden&lt;br /&gt;  where hidden = "value from hidden"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;That executes as you'd expect by this point, as seen in &lt;strong&gt;hugs&lt;/strong&gt;:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;$ hugs Hidden.hs &lt;br /&gt;__   __ __  __  ____   ___      _________________________________________&lt;br /&gt;||   || ||  || ||  || ||__      Hugs 98: Based on the Haskell 98 standard&lt;br /&gt;||___|| ||__|| ||__||  __||     Copyright (c) 1994-2005&lt;br /&gt;||---||         ___||           World Wide Web: http://haskell.org/hugs&lt;br /&gt;||   ||                         Bugs: http://hackage.haskell.org/trac/hugs&lt;br /&gt;||   || Version: September 2006 _________________________________________&lt;br /&gt;&lt;br /&gt;Haskell 98 mode: Restart with command line option -98 to enable extensions&lt;br /&gt;&lt;br /&gt;Type :? for help&lt;br /&gt;Main&gt; obvious&lt;br /&gt;"value from hidden"&lt;br /&gt;Main&gt; hidden&lt;br /&gt;ERROR - Undefined variable "hidden"&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-3593825359529627891?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/3593825359529627891/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=3593825359529627891' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/3593825359529627891'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/3593825359529627891'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/09/hidden-functions.html' title='Hidden Functions'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-8348143211777031009</id><published>2008-09-17T14:18:00.000-04:00</published><updated>2008-09-17T14:18:00.565-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='rspec'/><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><category scheme='http://www.blogger.com/atom/ns#' term='tdd'/><category scheme='http://www.blogger.com/atom/ns#' term='sdd'/><category scheme='http://www.blogger.com/atom/ns#' term='san francisco'/><category scheme='http://www.blogger.com/atom/ns#' term='boston'/><category scheme='http://www.blogger.com/atom/ns#' term='bdd'/><title type='text'>Boston, San Francisco and SDD</title><content type='html'>My colleague &lt;a href="http://jimlindley.com"&gt;Jim Lindley&lt;/a&gt; and I will be going to Boston for a few days for our employer. We do &lt;acronym title="Test-Driven Development"&gt;TDD&lt;/acronym&gt;/&lt;acronym title="Behavior-Driven Development"&gt;BDD&lt;/acronym&gt; with &lt;a href="http://rspec.info/"&gt;RSpec&lt;/a&gt; and Rails currently, and the folks in the Boston office are at some point along a similar continuum. We'll show them what we do, they'll show us what they do, we'll learn and grow and laugh and share and love, etc. Then I'm off to San Francisco for some interviews related to my planned relocation out there.&lt;br /&gt;&lt;br /&gt;Yesterday, I came across &lt;a href="http://paulbarry.com/articles/2008/09/16/story-driven-development-with-rails-part-i-up-and-running"&gt;a blog post by Paul Barry about &lt;acronym title="Story-Driven Development"&gt;SDD&lt;/acronym&gt; with Rails&lt;/a&gt;. The two upcoming trips already had me thinking about my workflow, and Jim and I had been talking about trying move another abstraction step up from our current speccing practice, and &lt;acronym title="Story-Driven Development"&gt;SDD&lt;/acronym&gt; seems like a good match for our next project. It's a rewrite of an existing app that is currently in a different language - our job would be to match it feature-for-feature and also to make some necessary changes and additions. &lt;br /&gt;&lt;br /&gt;&lt;acronym title="Story-Driven Development"&gt;SDD&lt;/acronym&gt; seems like a good way to keep the &lt;acronym title="Quality Assurance"&gt;QA&lt;/acronym&gt; folks who already know how the app should work more involved in the the speccing process &lt;strong&gt;in a way that actually has impact&lt;/strong&gt;. That's the beauty of having the human-readable descriptions be executable. &lt;br /&gt;&lt;br /&gt;Paul also links to &lt;a href="http://brynary.com/"&gt;Bryan Helmkamp&lt;/a&gt;'s &lt;a href="http://goruco2008.confreaks.com/01_helmkamp.html"&gt;talk on &lt;acronym title="Story-Driven Development"&gt;SDD&lt;/acronym&gt; at GoRuCo2008&lt;/a&gt;, which is very good. I would encourage anyone to read Paul's post and watch Bryan's talk.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-8348143211777031009?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/8348143211777031009/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=8348143211777031009' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/8348143211777031009'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/8348143211777031009'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/09/boston-san-francisco-and-sdd.html' title='Boston, San Francisco and &lt;acronym title=&quot;Story-Driven Development&quot;&gt;SDD&lt;/acronym&gt;'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-8410370837053294159</id><published>2008-08-28T11:08:00.004-04:00</published><updated>2008-08-28T11:19:46.186-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='perl'/><title type='text'>Celebrity sighting(?)</title><content type='html'>My last post was about Perl and my experiences working with it and its fans. I'm not a huge fan of the language - I'm planning another post that details what I like and don't like about it. I think it has few characteristics that are fundamentally flawed, for example.&lt;br /&gt;&lt;br /&gt;However, I'm pretty sure I saw Larry Wall in the Detroit airport a few weekends ago when I was flying to Seattle for a friend's wedding. The excitement that I heard in my own voice when I pointed him out to my wife Jenn made me realize that I think Perl's good points have been a positive influence on programming culture. &lt;br /&gt;&lt;br /&gt;So thanks, Larry.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-8410370837053294159?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/8410370837053294159/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=8410370837053294159' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/8410370837053294159'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/8410370837053294159'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/08/celebrity-sighting.html' title='Celebrity sighting(?)'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-7834340871680964710</id><published>2008-08-04T10:26:00.001-04:00</published><updated>2008-08-05T10:26:43.536-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='comments'/><category scheme='http://www.blogger.com/atom/ns#' term='perl'/><category scheme='http://www.blogger.com/atom/ns#' term='duck typing'/><title type='text'>Perl and is_valid_aref()</title><content type='html'>This post is about Perl, the politics of interacting with fellow developers, and &lt;a href="http://en.wikipedia.org/wiki/Duck_typing"&gt;Duck Typing&lt;/a&gt; (although we didn't really know it at the time). It's also fairly stream-of-consciousness.&lt;br /&gt;&lt;br /&gt;I used to work at a Perl shop, and in a big rewrite we did, we made heavy use of array &lt;a href="http://perldoc.perl.org/perlreftut.html"&gt;references&lt;/a&gt;. A dynamic lanugage like Perl (or Ruby, or whatever) will encourage conscientious developers to have some tests and/or error checking, so we often wanted to ensure that our input parameters to some specific subroutines that expected array refs were actually valid arefs. How should one do this checking in Perl?&lt;br /&gt;&lt;br /&gt;Here's one obvious approach.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;sub is_valid_aref_mere_mortals {&lt;br /&gt;  ref shift eq 'ARRAY';&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here we have a straightforward subroutine. We use Perl's &lt;strong&gt;shift&lt;/strong&gt; to pull the first parameter off the stack, get the type to which it refers with &lt;strong&gt;ref&lt;/strong&gt;, and check with the stringy equality operator whether or not the referred type is &lt;strong&gt;'ARRAY'&lt;/strong&gt;.&lt;br /&gt;&lt;br /&gt;This wasn't good enough for an ex co-worker of mine, who fancied himself a Perl Wizard (with some justification, in fairness to him). &lt;br /&gt;&lt;br /&gt;Some idiot could &lt;a href="http://perldoc.perl.org/functions/bless.html"&gt;bless&lt;/a&gt; a non-ARRAY behaving scalar as a ref to an ARRAY. My solution in that case is to berate the offender in public for having done something so horrible. Such a blessing is a direct violation of the duck typing idea that you don't have a datum &lt;q&gt;pretend&lt;/q&gt; to be a specific type (however your language defines type) unless it can implement all the pertinent behavior expected of that type in that context. &lt;q&gt;With great power comes great responsibility.&lt;/q&gt;&lt;br /&gt;&lt;br /&gt;We also had no instances of such blessing in our app. All of our args were all either just simple arefs, or errors (usually an undef value). So in practice, I think a simple ref check would work fine. However, if you're concerned about the &lt;q&gt;blessed as aref&lt;/q&gt; issue, the solution below has some advantages.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;sub is_valid_aref {&lt;br /&gt;  my( $arg ) = @_; &lt;br /&gt;  $arg and eval { @{ $arg } or 1 };&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It's a bit tricky, so I'll explain it. You extract the first parameter in the stack as &lt;strong&gt;$arg&lt;/strong&gt;. The &lt;strong&gt;and&lt;/strong&gt; keyword assures that &lt;strong&gt;$arg&lt;/strong&gt; is truthy before we proceed to the &lt;strong&gt;eval&lt;/strong&gt;. The &lt;strong&gt;eval&lt;/strong&gt; attempts to dereference &lt;strong&gt;$arg&lt;/strong&gt; into a regular (non-reference) array. Rubyists can think of this use of eval in Perl as akin to a &lt;strong&gt;begin rescue end&lt;/strong&gt; block.&lt;br /&gt;&lt;br /&gt;If the dereferencing results in an error, it will stop evaluation of the eval block, resulting in an undef value, meaning that the expression returned by the function is false. &lt;br /&gt;&lt;br /&gt;Why is there an &lt;strong&gt;or 1&lt;/strong&gt; at the end of the eval block? That's for situation in which your &lt;strong&gt;$arg&lt;/strong&gt; is an empty array ref. An array of zero length evaluated in truthy scalar context in Perl is false. However, we want such a data structure to produce a truthy return value from this function. Therefore, when a valid but empty array ref is dereferenced into an actual array, it is false but does not break out of the eval block. The expression with the eval block then continues with the &lt;strong&gt;or 1&lt;/strong&gt;, which ends up being truthy, ensuring that a valid empty array ref is considered valid by this function.&lt;br /&gt;&lt;br /&gt;I lie when I say this was my ex co-worker's solution. He strongly objected to putting this test inside a function, because developers &lt;q&gt;should just know Perl idioms&lt;/q&gt;,  and the function call would add too much overhead. I thought instead that the value of naming this obscure bit of code with what its purpose is would be worthwhile. He disagreed. So he would have tests like the line of code below copied and pasted with no explanatory comments wherever we needed to check aref validity:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;fail if not $arg and eval { @{ $arg } or 1 };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I saw that for the first time and thought &lt;q&gt;WTF?&lt;/q&gt; I then added a comment explaining what that particular monstrosity was supposed to do, something like 'checks if a valid aref'. He objected strongly (again), saying that coders &lt;q&gt;should just know Perl idioms&lt;/q&gt;.&lt;br /&gt;&lt;br /&gt;We finally got him to accept the presence of the comment after about 30 minutes of arguing.&lt;br /&gt;&lt;br /&gt;Try them out for yourself, if you like. I use &lt;strong&gt;$proc&lt;/strong&gt; as a generic name for a procedure/function/subroutine/code reference. Some other people prefer &lt;strong&gt;$cref&lt;/strong&gt; for &lt;q&gt;code ref&lt;/q&gt; or &lt;strong&gt;$sref&lt;/strong&gt; for &lt;q&gt;subroutine ref&lt;/q&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;my( %proc_of ) = ( &lt;br /&gt;  'mere mortals' =&gt; \&amp;is_valid_aref_mere_mortals,&lt;br /&gt;  'eval version' =&gt; \&amp;is_valid_aref&lt;br /&gt;);&lt;br /&gt;&lt;br /&gt;sub report {&lt;br /&gt;  my( $label, $candidate, $proc_of_href ) = @_; &lt;br /&gt;  for $proc_name ( keys %{ $proc_of_href } ) { &lt;br /&gt;    my( $proc ) = $proc_of_href-&gt;{$proc_name};&lt;br /&gt;    print $proc-&gt;( $candidate ) &lt;br /&gt;      ? "$label is a valid aref according to $proc_name\n"&lt;br /&gt;      : "$label is not a valid aref according to $proc_name\n";&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;report( '[]', [], \%proc_of );&lt;br /&gt;report( '()', (), \%proc_of );&lt;br /&gt;report( '{}', {}, \%proc_of );&lt;br /&gt;report( '(undef)', (undef), \%proc_of );&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Results in this output. Note that I haven't included the deceptive blessing situation, on which the two subs would differ:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;[] is a valid aref according to mere mortals&lt;br /&gt;[] is a valid aref according to eval version&lt;br /&gt;{} is not a valid aref according to mere mortals&lt;br /&gt;{} is not a valid aref according to eval version&lt;br /&gt;(undef) is not a valid aref according to mere mortals&lt;br /&gt;(undef) is not a valid aref according to eval version&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It seems to me that the clarity improvement from having the name &lt;strong&gt;is_valid_aref&lt;/strong&gt; for that code is worth the relatively small overhead of the function call. I also think that it serves a pedagogical use - a new developer who isn't familiar with the blessing concern could in fact be educated about it by seeing this code under such an explanatory label. But I'd be interested in any other reasons to avoid putting this test inside a named subroutine. If I'm being stubborn for insufficient reason, I'd like to be illuminated and corrected.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-7834340871680964710?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/7834340871680964710/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=7834340871680964710' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/7834340871680964710'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/7834340871680964710'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/08/perl-and-isvalidaref.html' title='Perl and is_valid_aref()'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-3183962636380856366</id><published>2008-07-28T14:50:00.012-04:00</published><updated>2008-07-28T15:13:57.691-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='static typing'/><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><title type='text'>Mea Culpa re: Typing</title><content type='html'>I was going through some Rails code today, making sure that I had good coverage and writing or modifying whatever specs as needed. I found this code:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;module DisplayHelper&lt;br /&gt;  # snip ...&lt;br /&gt;  EMPTY_STRING = %q[]&lt;br /&gt;  # snip ...&lt;br /&gt;  def should_see_edit_link?(options)&lt;br /&gt;    return EMPTY_STRING if options[:new_record]&lt;br /&gt;    return false unless (memoized_user = options[:user])&lt;br /&gt;    # more stuff with memoized_user ...&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Do you see the problem? The first line within &lt;strong&gt;should_see_edit_link?&lt;/strong&gt; should be &lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;    return false if options[:new_record]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;My initial version was not a proper predicate, in that it did not restrict its returned values to only &lt;strong&gt;true&lt;/strong&gt; or &lt;strong&gt;false&lt;/strong&gt;. Bad coder. For what it's worth, the methods that use &lt;strong&gt;should_see_edit_link?&lt;/strong&gt; themselves return &lt;strong&gt;EMPTY_STRING&lt;/strong&gt; if &lt;strong&gt;should_see_edit_link?&lt;/strong&gt; is truthy. Alas, not a good enough excuse.&lt;br /&gt;&lt;br /&gt;This is exactly the sort of error that advocates of static typing mention as support for their preferred type system. The most experience I have with bondage &amp;amp; discipline typing is with Haskell, where I find it takes its proper place as part of a cohesive whole for understanding Haskell's approach to currying, implicit typing, the pointfree style, and so on. All of that is great, I just haven't found that static typing reduces the error rate in my code. In fact, it gets in my way more often than anything else. Maybe that's why I'm getting more into Erlang rather than Haskell. All of this is probably inseparable from the fact that I got into coding with dynamic languages.&lt;br /&gt;&lt;br /&gt;The type error that prompted this blog post is atypical for me. I'm not claiming to write completely bug-free code, it's just that my errors tend not be the sort that can be caught by a static type checker. In fact, the only errors of this sort that I can remember making are this individual error and some others in Perl related to composite data structures being coerced into scalar context. The latter were more indicative of Perl's idiosyncratic (and in my opinion, failed) experiement with context than with dynamic typing as it's generally implemented outside of Perl. So I'm not sure that they count.&lt;br /&gt;&lt;br /&gt;How about anyone else? Has static typing ever really saved your butt dramatically?&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Caveat:&lt;/strong&gt; Please don't interpret this post as going anywhere near addressing static typing as it relates to hardware optimizations during compile time. I'm just taking about human errors resembling the example.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-3183962636380856366?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/3183962636380856366/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=3183962636380856366' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/3183962636380856366'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/3183962636380856366'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/07/mea-culpa-re-typing.html' title='Mea Culpa re: Typing'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-826279335499315625</id><published>2008-07-26T09:16:00.006-04:00</published><updated>2008-07-26T09:29:20.870-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='books'/><category scheme='http://www.blogger.com/atom/ns#' term='ruby by example'/><title type='text'>Mr. Neighborly's Ruby and Rails Bibliography</title><content type='html'>Jeremy McAnally recommended my book in his &lt;a href="http://blog.mrneighborly.com/2007/10/ruby-and-rails-bibliography-of-sorts.html"&gt;list of good Ruby books partitioned by skill level and specific interest&lt;/a&gt;. He placed mine in the sublist appropriate for "moderate programming experience". Seems appropriate.&lt;br /&gt;&lt;br /&gt;Jeremy gave a very good presentation called &lt;cite&gt;Deep Ruby&lt;/cite&gt; at &lt;a href="http://mtnwestrubyconf.org/2008/speakers"&gt;MtnWest RubyConf2008 in Salt Lake City&lt;/a&gt;. He also has some of his own books in the same list where he graciously put mine. He's a good speaker who knows a lot about the language. Check him out.&lt;br /&gt;&lt;br /&gt;I would add &lt;acronym title="Structure and Interpretation of Computer Programs"&gt;SICP&lt;/acronym&gt; to the list for becoming a more well-rounded programmer.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-826279335499315625?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/826279335499315625/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=826279335499315625' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/826279335499315625'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/826279335499315625'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/07/mr-neighborlys-ruby-and-rails.html' title='Mr. Neighborly&apos;s Ruby and Rails Bibliography'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-3082373118141594303</id><published>2008-07-25T12:54:00.004-04:00</published><updated>2008-07-25T15:34:00.967-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='comments'/><title type='text'>Wither Comments?</title><content type='html'>Jeff Atwood has an excellent post at Coding Horror about comments.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.codinghorror.com/blog/archives/001150.html"&gt;&lt;br /&gt;http://www.codinghorror.com/blog/archives/001150.html&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Worth reading. I believe that function / method names should tell you what's happening (at a suitable level of abstraction), and any comments that do exist should explain the rare cases where your well-named entities could be misleading.&lt;br /&gt;&lt;br /&gt;Avi Pilosof has an excellent related point about the kinds of comments that are often underused at &lt;a href="http://blogs.msdn.com/avip/archive/2008/07/25/code-commenting-try-business-commenting.aspx"&gt;http://blogs.msdn.com/avip/archive/2008/07/25/code-commenting-try-business-commenting.aspx&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-3082373118141594303?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/3082373118141594303/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=3082373118141594303' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/3082373118141594303'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/3082373118141594303'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/07/wither-comments.html' title='Wither Comments?'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-7293908986184544116</id><published>2008-07-21T11:12:00.002-04:00</published><updated>2008-07-22T14:22:09.623-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='first-class procedures'/><category scheme='http://www.blogger.com/atom/ns#' term='SICP'/><title type='text'>First-Class Procedures, Part III: being returned as values of procedures</title><content type='html'>&lt;acronym title="Structure and Interpretation of Computer Programs"&gt;SICP&lt;/acronym&gt; demands that for functions to qualify as &lt;q&gt;First Class Citizens&lt;/q&gt;, &lt;br /&gt;they must satisfy several requirements. I've written about this before in reference to the first requirement: &lt;a href="http://lambdasquire.blogspot.com/2008/04/first-class-procedures-part-i-naming-by.html"&gt;being able to assign names to procedures&lt;/a&gt;, and the second requirement: &lt;a href="http://lambdasquire.blogspot.com/2008/05/first-class-procedures-part-ii-passing.html"&gt;the ability to be passed as an argument to another function&lt;/a&gt;. Next on the list is being returned as a value from a procedure. Here we go with that.&lt;br /&gt;&lt;br /&gt;Again: &lt;a href="http://ruby-lang.org/"&gt;Ruby&lt;/a&gt;, SICP's original &lt;a href="http://www.schemers.org/"&gt;Scheme&lt;/a&gt;, &lt;a href="http://erlang.org/"&gt;Erlang&lt;/a&gt;, and &lt;a href="http://haskell.org/"&gt;Haskell&lt;/a&gt;, with JavaScript by my friend &lt;a href="http://keus.org/"&gt;Aubrey Keus&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;In Ruby:&lt;/h4&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow:auto;"&gt;&lt;br /&gt;def get_method_that_does_name(owner, name_as_sym)&lt;br /&gt;  owner.method(name_as_sym)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def get_double_plus_one&lt;br /&gt;  lambda { |x| (x*2) + 1 } &lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here we've defined a method called &lt;cite&gt;get_method_that_does_name&lt;/cite&gt;. It expects a Symbol argument preceded by an owning object, and returns a &lt;cite&gt;Method&lt;/cite&gt; object, created via the aptly-named &lt;cite&gt;method&lt;/cite&gt; method. The owning object provides the context for the method, similar to a closure in a purely-functional language. Here's an example of how &lt;cite&gt;get_method_that_does_name&lt;/cite&gt; might be used in irb:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow:auto;"&gt;&lt;br /&gt;&gt;&gt; add1 = get_method_that_does_name(1, :+)&lt;br /&gt;=&gt; #&amp;lt;Method: Fixnum#+&amp;gt;&lt;br /&gt;&gt;&gt; add1.call(2)&lt;br /&gt;=&gt; 3&lt;br /&gt;&gt;&gt; greet = get_method_that_does_name('hello, ', :+)&lt;br /&gt;=&gt; #&amp;lt;Method: String#+&amp;gt;&lt;br /&gt;&gt;&gt; greet.call('world')&lt;br /&gt;=&gt; "hello, world"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The method identified by &lt;strong&gt;+&lt;/strong&gt; means something different when called on &lt;strong&gt;1&lt;/strong&gt; than when called on &lt;q&gt;hello, &lt;/q&gt;, as shown in the example.&lt;br /&gt;&lt;br /&gt;The other example uses the more-familiar &lt;strong&gt;lambda&lt;/strong&gt; keyword to generate a Proc.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow:auto;"&gt;&lt;br /&gt;&gt;&gt; dp1 = get_double_plus_one&lt;br /&gt;=&gt; #&amp;lt;Proc:0xb7cb2458@./first_class_functions_in_ruby.rb:39&amp;gt;&lt;br /&gt;&gt;&gt; dp1.call(4)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In either case, the returned value from the method (whether &lt;br /&gt;&lt;cite&gt;get_method_that_does_name&lt;/cite&gt; or &lt;cite&gt;get_double_plus_one&lt;/cite&gt;) is a procedure-like object callable with the &lt;cite&gt;call&lt;/cite&gt; method.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;In MIT Scheme:&lt;/h4&gt;&lt;br /&gt;$ mit-scheme&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;1 ]=&gt; (define get-adder (lambda (x) (lambda (y) (+ x y))))&lt;br /&gt;;Value: get-adder&lt;br /&gt;1 ]=&gt; (get-adder 2)&lt;br /&gt;;Value 11: #[compound-procedure 11]&lt;br /&gt;1 ]=&gt; ((get-adder 2) 1)&lt;br /&gt;;Value: 3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Thi sample code demonstrates a typical Scheme example of a procedure that returns another procedure. This particular syntax uses two &lt;strong&gt;lambda&lt;/strong&gt;s, but note that the first line could have been expressed alternately in this manner:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;1 ]=&gt; (define (get-adder x) (lambda (y) (+ x y)))&lt;br /&gt;;Value: get-adder&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Usage in the 2nd and 3rd lines remain the same in either case. Line 2 shows us that calling &lt;strong&gt;(get-adder 2)&lt;/strong&gt; returns a compound-procedure value, and line 3 shows us that that returned procedure takes an argument (&lt;strong&gt;y&lt;/strong&gt; in our definition code), and adds it to the value of &lt;strong&gt;x&lt;/strong&gt; that was used in the &lt;strong&gt;get-adder&lt;/strong&gt; call that created the returned procedure in the first place.&lt;br /&gt;&lt;br /&gt;One could argue that Scheme provides the most &lt;q&gt;canonical&lt;/q&gt; syntax for defining a procedure that returns another procedure. This is unsurprising, given the roots of the language, especially in education.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;In Erlang:&lt;/h4&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;$ erl &lt;br /&gt;Erlang (BEAM) emulator version 5.5.5 [source] [async-threads:0] [hipe] [kernel-poll:false]&lt;br /&gt;&lt;br /&gt;Eshell V5.5.5  (abort with ^G)&lt;br /&gt;1&gt; GetAdder = fun(X) -&gt; (fun(Y) -&gt; X + Y end) end.&lt;br /&gt;#Fun&amp;lt;erl_eval.6.49591080&amp;gt;&lt;br /&gt;2&gt; GetAdder(2).&lt;br /&gt;#Fun&amp;lt;erl_eval.6.49591080&amp;gt;&lt;br /&gt;3&gt; Add1 = GetAdder(1).&lt;br /&gt;#Fun&amp;lt;erl_eval.6.49591080&amp;gt;&lt;br /&gt;4&gt; Add1(2).&lt;br /&gt;3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This Erlang example differs slightly, of course. Erlang's variables are uppercase, and it uses the &lt;strong&gt;fun&lt;/strong&gt; keyword rather than &lt;strong&gt;lambda&lt;/strong&gt;. This particular example also happens to assign the value of &lt;strong&gt;GetAdder(1)&lt;/strong&gt; into a variable called &lt;strong&gt;Add1&lt;/strong&gt;, which &lt;cite&gt;erl&lt;/cite&gt; dutifully reports back to us is of the type &lt;cite&gt;Fun&lt;/cite&gt;. Calling &lt;strong&gt;Add1(2)&lt;/strong&gt; then produces the expected result.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;In Haskell:&lt;/h4&gt;&lt;br /&gt;Last time on this topic I used &lt;acronym&gt;hugs&lt;/cite&gt;, a &lt;acronym title="Read, Evaluate, Print, Loop"&gt;REPL&lt;/acronym&gt; shell. This time I'll use &lt;acronym title="Glasgow Haskell Compiler, Interactive"&gt;GHCi&lt;/acronym&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow:auto;"&gt;&lt;br /&gt;$ ghci&lt;br /&gt;GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help&lt;br /&gt;Loading package base ... linking ... done.&lt;br /&gt;Prelude&gt; let getAdder x = \y -&gt; (y + x)&lt;br /&gt;Prelude&gt; let add1 = getAdder 1&lt;br /&gt;Prelude&gt; add1 2&lt;br /&gt;3 &lt;br /&gt;Prelude&gt; :quit&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This actually uses Haskell's equivalent of &lt;strong&gt;lambda&lt;/strong&gt; in the &lt;strong&gt;\&lt;/strong&gt;, apparently chosen for its visual resemblance to the letter &amp;lambda; without a lower-left leg. If we wanted to use the &lt;a href="http://haskell.org/haskellwiki/Pointfree"&gt;pointfree&lt;/a&gt; style mentioned in the hugs portion of &lt;a href="http://lambdasquire.blogspot.com/2008/05/first-class-procedures-part-ii-passing.html"&gt;my previous post on this topic&lt;/a&gt;, we could replace the first line with&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow:auto;"&gt;&lt;br /&gt;Prelude&gt; let getAdder = \y -&gt; (y +)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Notice how it leaves the &lt;strong&gt;x&lt;/strong&gt; variable out entirely. &lt;br /&gt;&lt;br /&gt;We can use Haskell's type system to reveal some other things of interest.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow:auto;"&gt;&lt;br /&gt;Prelude&gt; :t getAdder&lt;br /&gt;getAdder :: Integer -&gt; Integer -&gt; Integer&lt;br /&gt;Prelude&gt; :t (getAdder 1)&lt;br /&gt;(getAdder 1) :: Integer -&gt; Integer&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The &lt;strong&gt;:t&lt;/strong&gt; command in GHCi reports the type of its argument. &lt;cite&gt;getAdder&lt;/cite&gt; has the type &lt;strong&gt;Integer -&gt; Integer -&gt; Integer&lt;/strong&gt;. This means that it can either take two &lt;strong&gt;Integer&lt;/strong&gt; arguments and return a single &lt;strong&gt;Integer&lt;/strong&gt;, or it can take one Integer and return a function of type &lt;strong&gt;Integer -&gt; Integer&lt;/strong&gt;. This means that the returned function takes one Integer and returns one Integer. When we check the type of the expression &lt;cite&gt;(getAdder 1)&lt;/cite&gt;, it reports, as expected, that the expression takes a single Integer, returning another. This type system is crucial to understanding Hashkell's handling of &lt;a href="http://en.wikipedia.org/wiki/Currying"&gt;currying&lt;/a&gt;, which I hope to get into in greater detail in a later post.&lt;br /&gt;&lt;br /&gt;If we want to eliminate middle variables entirely, we can simply feed two Integers directly to &lt;cite&gt;getAdder&lt;/cite&gt;, as in&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow:auto;"&gt;&lt;br /&gt;Prelude&gt; getAdder 1 2&lt;br /&gt;3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;In JavaScript:&lt;/h4&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow:auto;"&gt;&lt;br /&gt;function call_proc_arg_on_2(first_class_proc) {&lt;br /&gt;   return first_class_proc(2);&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Number.prototype.call_proc_arg_on_self = function(first_class_proc) {&lt;br /&gt;   return first_class_proc(this);&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;call_proc_arg_on_2(add1);&lt;br /&gt;Number(100).call_proc_arg_on_self(add1);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Aubrey's JavaScript code demonstrates both the initial example common across all the languages, as well as the equivalent of my second Ruby example, in which the desired behavior is attached to existing number objects within your language's existing workspace.&lt;br /&gt;&lt;br /&gt;So that's it. I think all of these languages satisfy the requirement of returning procedures (however defined) as values from other procedures, at least from a practical perspective. The next (and final) post in this series will be about incorporating procedures into data structures. I'll get to the post when I can.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-7293908986184544116?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/7293908986184544116/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=7293908986184544116' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/7293908986184544116'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/7293908986184544116'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/07/first-class-procedures-part-iii-being.html' title='First-Class Procedures, Part III: being returned as values of procedures'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-4699432744941395486</id><published>2008-06-27T10:09:00.016-04:00</published><updated>2008-06-27T10:58:12.363-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='helpers'/><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><title type='text'>What are Rails Helpers for?</title><content type='html'>I was reading &lt;a href="http://www.dcmanges.com/blog/smart-model-dumb-controller"&gt;Dan Mange's &lt;cite&gt;Smart Model, Dumb Controller&lt;/cite&gt; post&lt;/a&gt;, which has a very interesting comment from &lt;a href="http://www.railsdev.ws/"&gt;Greg Willits&lt;/a&gt; in which he proposes an additional logic layer between Controllers and Models. He and Dan go back a forth a bit on this - check it out.&lt;br /&gt;&lt;br /&gt;This prompted me to think about what I use Helpers for. The exchange above brought up Helpers in their canonical sense, as being a sort of glue between the Controller and the View. My colleague &lt;a href="http://jimlindley.com/"&gt;Jim Lindley&lt;/a&gt; and I certainly use Helpers in that fashion, witness&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;module PreceptorsHelper&lt;br /&gt;  def self.select_list&lt;br /&gt;    Preceptor.find(:all).map { |p| [p.dropdown_name, p.id] }&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Pretty typical. We have a &lt;cite&gt;Preceptor&lt;/cite&gt; model, and our &lt;cite&gt;PreceptorsHelper&lt;/cite&gt; has a &lt;cite&gt;select_list&lt;/cite&gt; method that &lt;acronym title="Don't Repeat Yourself"&gt;DRY&lt;/acronym&gt;s up how all of our Preceptor-related views get their select lists for dropdown menus.&lt;br /&gt;&lt;br /&gt;Additionally, we have a &lt;cite&gt;dropdown_name&lt;/cite&gt;, which differs from the plain old &lt;cite&gt;name&lt;/cite&gt; field of the Preceptor, but neither the PreceptorsHelper nor the views know about that - it's properly encapsulated within the Preceptor model itself. This is basic OO design that provides tools for managing multiple levels of complexity and abstractions to handle that complexity. No surprises so far.&lt;br /&gt;&lt;br /&gt;However, this all led me to reflect on my own use of Helpers that may not be textbook Controller&lt;-&gt;View glue activity. I also use Helpers for logic that pertains to a given model (or more likely a collection of those models) without being a characteristic of any one of those model instances. This sounds a bit like Greg's additional controllers idea. Here's an example from another of our helpers:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow: auto;"&gt;&lt;br /&gt;module EnrollmentsHelper&lt;br /&gt;  def self.filter_by_readiness(enrollments, params)&lt;br /&gt;    ready_for_review = params[:ready_for_review]&lt;br /&gt;    return enrollments unless ready_for_review&lt;br /&gt;    enrollments.select { |e| e.ready_for_review? }&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Something akin to this could also be accomplished via named_scope nowadays, I realize. However, let's abstract a bit. This is an operation on a set of things - Enrollments in this case. We certainly don't want this in the EnrollmentsController. We could make the case that it should be a class method of Enrollment. But making it a function that takes the Enrollments as a parameter makes this much easier to test. It's also more of a functional style, which I freely cop to preferring.&lt;br /&gt;&lt;br /&gt;I guess I don't even think about Helpers as necessarily needing to provide utility for views so much as providing utility that is "about" a given topic without being a characteristic of any one of those instances.&lt;br /&gt;&lt;br /&gt;Note that the topic doesn't even need to be a model. Our current app has several Models presenting different types of humans, and some of our clients care about gender. Presto:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow:auto;"&gt;&lt;br /&gt;module GenderHelper&lt;br /&gt;&lt;br /&gt;  VALID_GENDERS = %w[m f M F]&lt;br /&gt;  VALID_OPTIONS = { &lt;br /&gt;    :in          =&gt; VALID_GENDERS,&lt;br /&gt;    :allow_blank =&gt; true,&lt;br /&gt;    :allow_nil   =&gt; true,&lt;br /&gt;    :message     =&gt; MessageHelper::MESSAGE[:invalid_gender]&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  MALE_VARIANTS   = %w[BOY MAN MALE]&lt;br /&gt;  FEMALE_VARIANTS = %w[GIRL WOMAN FEMALE]&lt;br /&gt;&lt;br /&gt;  def self.distill_gender!(gender)&lt;br /&gt;    gender.andand.upcase!&lt;br /&gt;    gender = %q[M] if MALE_VARIANTS.include?(gender)&lt;br /&gt;    gender = %q[F] if FEMALE_VARIANTS.include?(gender)&lt;br /&gt;    gender&lt;br /&gt;  end &lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;(Note also the similar delegation to &lt;cite&gt;MessageHelper&lt;/cite&gt;). This allows us to DRYly do something like this in our &lt;cite&gt;Student&lt;/cite&gt; and similar models:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size:0.9em;overflow:auto;"&gt;&lt;br /&gt;validates_inclusion_of :gender, GenderHelper::VALID_OPTIONS&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Nothing view-related there. &lt;br /&gt;&lt;br /&gt;Much of this coding style sprang from a desire to simplify controllers. After doing this, I noted many comparatively large model files. One of them, &lt;cite&gt;Lottery&lt;/cite&gt;, had a lot of &lt;a href="http://lambdasquire.blogspot.com/2008/05/road-to-partition.html"&gt;activity that dealt with processing collections of Enrollments&lt;/a&gt;, which I've blogged about before. &lt;q&gt;Why not move this into something more directly related to Enrollments?&lt;/q&gt;, I thought.&lt;br /&gt;&lt;br /&gt;I like the results. &lt;br /&gt;&lt;br /&gt;What do other people think about Helpers used in this fashion? Should there be two different ypes of them: view-related and non-view-related? More? I'm still sorting out my thoughts on this, and welcome new ideas to think about (or old ideas to consider anew).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-4699432744941395486?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/4699432744941395486/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=4699432744941395486' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/4699432744941395486'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/4699432744941395486'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/06/what-are-rails-helpers-for.html' title='What are Rails Helpers for?'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-6647626841485626416</id><published>2008-06-24T15:55:00.000-04:00</published><updated>2008-06-24T15:55:46.546-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='first-class procedures'/><category scheme='http://www.blogger.com/atom/ns#' term='SICP'/><title type='text'>First-Class Procedures, Part II: passing as an argument</title><content type='html'>&lt;acronym title="Structure and Interpretation of Computer Programs"&gt;SICP&lt;/acronym&gt; demands that for functions to qualify as &lt;q&gt;First Class Citizens&lt;/q&gt;, &lt;br /&gt;they must satisfy several requirements. I've written about this before in reference to the first requirement: &lt;a href="http://lambdasquire.blogspot.com/2008/04/first-class-procedures-part-i-naming-by.html"&gt;being able to assign names to procedures&lt;/a&gt;. The second requirements is the ability to be passed as an argument to another function, which is the topic of this post.&lt;br /&gt;&lt;br /&gt;I'll just dive in, again showing multiples languages: &lt;a href="http://ruby-lang.org/"&gt;Ruby&lt;/a&gt;, SICP's original &lt;a href="http://www.schemers.org/"&gt;Scheme&lt;/a&gt;, &lt;a href="http://erlang.org/"&gt;Erlang&lt;/a&gt;, and &lt;a href="http://haskell.org/"&gt;Haskell&lt;/a&gt;. Again, my friend &lt;a href="http://keus.org/"&gt;Aubrey Keus&lt;/a&gt; has provided some JavaScript, as well.&lt;br /&gt;&lt;br /&gt;This post is fairly Ruby-heavy. A lot of them are likely to be, as it's the language I know best among those being discussed, as well as the the one I use to earn my Yankee Dollars.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;In Ruby:&lt;/h4&gt;&lt;br /&gt;$ irb &lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;irb(main):001:0&gt; def call_proc_arg_on_2(first_class_proc)&lt;br /&gt;irb(main):002:1&gt; first_class_proc.call 2&lt;br /&gt;irb(main):003:1&gt; end &lt;br /&gt;=&gt; nil &lt;br /&gt;irb(main):004:0&gt; add1 = lambda { |x| 1 + x } &lt;br /&gt;=&gt; #&lt;Proc:0xb7cc5760@(irb):4&gt;&lt;br /&gt;irb(main):005:0&gt; call_proc_arg_on_2(add1)&lt;br /&gt;=&gt; 3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here we've defined a method that expects an argument called &lt;cite&gt;first_class_proc&lt;/cite&gt;, which is (as you might expect) a Proc. &lt;cite&gt;add1&lt;/cite&gt; is such a Proc which adds 1 to its argument - hence the name. When we pass that in as the argument for &lt;cite&gt;call_proc_arg_on_2&lt;/cite&gt;, we get the expected value of 3.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Proc &amp;amp; block syntax&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;Ruby has a curious subtlety in its treatment of Procs. Notice that we need to use the &lt;cite&gt;call&lt;/cite&gt; method in order to make the Proc do its thing. Not true for some other languages below. Why would anyone do this when designing a language?&lt;br /&gt;&lt;br /&gt;Below we see a more typical way of passing a procedure as an argument to a &lt;a href="http://en.wikipedia.org/wiki/Higher-order_function"&gt;higher-order method&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;irb(main):006:0&gt; def call_block_on_2()&lt;br /&gt;irb(main):007:1&gt; yield 2&lt;br /&gt;irb(main):008:1&gt; end &lt;br /&gt;=&gt; nil &lt;br /&gt;irb(main):009:0&gt; call_block_on_2 { |x| 1 + x } &lt;br /&gt;=&gt; 3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The main differences between this and the previous example are as follows:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The name of the method reflects the differences in expected arguments: a Proc vs. a block&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;cite&gt;call_block_on_2&lt;/cite&gt; does not have a parameter in its declaration, just the empty parentheses&lt;/li&gt;&lt;br /&gt;&lt;li&gt;When we call &lt;cite&gt;call_block_on_2&lt;/cite&gt;, we don't pass a variable with a name (like &lt;cite&gt;add1&lt;/cite&gt;), instead we just give it a &lt;cite&gt;block&lt;/cite&gt;, which is the business between the {} braces. Note that this block is what we give to the &lt;cite&gt;lambda&lt;/cite&gt; method to create a full-fledged Proc in the earlier example.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The &lt;cite&gt;yield&lt;/cite&gt; method is roughly akin to the &lt;cite&gt;call&lt;/cite&gt; method, except that it automatically knows that it should operate on whatever block was given, without having to refer to it by name.&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;So what's the deal with this? Standard Ruby style is to call methods with blocks for operations like iteration (with &lt;cite&gt;each&lt;/cite&gt;), list transformation (&lt;cite&gt;map&lt;/cite&gt;) and filtering (&lt;cite&gt;select&lt;/cite&gt;, &lt;cite&gt;&lt;a href="http://lambdasquire.blogspot.com/2008/05/road-to-partition.html"&gt;partition&lt;/a&gt;&lt;/cite&gt;), and so on. This is so common that the syntactic sugar of being able &lt;a href="#split"&gt;to easily do this&lt;/a&gt; outweighed the consistency of being able to call function arguments without needing the &lt;cite&gt;call&lt;/cite&gt; method.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;In MIT Scheme:&lt;/h4&gt;&lt;br /&gt;$ mit-scheme&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;1 ]=&gt; (define call-on-2 (lambda (x) (x 2)))&lt;br /&gt;&lt;br /&gt;;Value: call-on-2&lt;br /&gt;&lt;br /&gt;1 ]=&gt; (define add1 (lambda (x) (+ x 1)))&lt;br /&gt;&lt;br /&gt;;Value: add1&lt;br /&gt;&lt;br /&gt;1 ]=&gt; (call-on-2 add1)&lt;br /&gt;&lt;br /&gt;;Value: 3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Check it out. Simple procedure declarations, and procedures that can be passed as args with no special syntax. Some would say no syntax at all, both among the pro- and anti-Lisp communities. Not wishing to add to an old flame war, I'll move on.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;In Erlang:&lt;/h4&gt;&lt;br /&gt;$ erl &lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;Erlang (BEAM) emulator version 5.5.5 [source] [async-threads:0] [hipe] [kernel-poll:false]&lt;br /&gt;&lt;br /&gt;Eshell V5.5.5  (abort with ^G) &lt;br /&gt;1&gt; Add1 = fun(X) -&gt; 1 + X end.&lt;br /&gt;#Fun&lt;erl_eval.6.49591080&gt;&lt;br /&gt;2&gt; CallOn2 = fun(F) -&gt; F(2) end.&lt;br /&gt;#Fun&lt;erl_eval.6.49591080&gt;&lt;br /&gt;3&gt; CallOn2(Add1).&lt;br /&gt;3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I'm becoming a pretty big Erlang fan. Note that the variable &lt;q&gt;Add1&lt;/q&gt; is in all caps - this serves a semantic purpose in Erlang. User-defined entities in Erlang that are in lower-case are generally either straightforward functions or &lt;strong&gt;atoms&lt;/strong&gt;, which are essentially just values. They're very similar to Ruby's Symbols, for example.&lt;br /&gt;&lt;br /&gt;This Erlang example lets the coder define a function as a variable which is then usable as an argument to another function without any special syntax tricks. &lt;cite&gt;CallOn2&lt;/cite&gt; just applies the function arg &lt;cite&gt;F&lt;/cite&gt; to the literal number 2 and returns the result, which is just what we want in all these examples.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;In Haskell:&lt;/h4&gt;&lt;br /&gt;(I haven't written about &lt;a href="http://haskell.org/"&gt;Haskell&lt;/a&gt; before now. It's purely functional, strongly typed with inferencing, and offers lazy (&lt;q&gt;non-strict&lt;/q&gt;/&lt;q&gt;non-eager&lt;/q&gt;) evaluation. Check it out. It has several implementations - I'll show it in &lt;cite&gt;hugs&lt;/cite&gt;, a &lt;acronym title="Read, Evaluate, Print, Loop"&gt;REPL&lt;/acronym&gt; shell.&lt;br /&gt;&lt;br /&gt;Assuming this file &lt;strong&gt;CallOnTwo.hs&lt;/strong&gt;:&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;add1 = (+) 1&lt;br /&gt;callOnTwo f = f 2 &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Executing hugs as follows:&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;$ hugs CallOnTwo.hs &lt;br /&gt;__   __ __  __  ____   ___      _________________________________________&lt;br /&gt;||   || ||  || ||  || ||__      Hugs 98: Based on the Haskell 98 standard&lt;br /&gt;||___|| ||__|| ||__||  __||     Copyright (c) 1994-2005&lt;br /&gt;||---||         ___||           World Wide Web: http://haskell.org/hugs&lt;br /&gt;||   ||                         Bugs: http://hackage.haskell.org/trac/hugs&lt;br /&gt;||   || Version: September 2006 _________________________________________&lt;br /&gt;&lt;br /&gt;Haskell 98 mode: Restart with command line option -98 to enable extensions&lt;br /&gt;&lt;br /&gt;Type :? for help&lt;br /&gt;Main&gt; callOnTwo add1&lt;br /&gt;3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;What's going on here? The actual use of &lt;cite&gt;callOnTwo&lt;/cite&gt; in the REPL example should be readable enough - it's quite similar to all of the other examples.&lt;br /&gt;&lt;br /&gt;The lib file &lt;cite&gt;CallOnTwo.hs&lt;/cite&gt; is a bit more interesting. Note the definition of &lt;cite&gt;add1&lt;/cite&gt; as &lt;strong&gt;(+) 1&lt;/strong&gt; - it leaves off the argument to add1. Rather than saying the equivalent of &lt;q&gt;add1 of x is x plus one&lt;/q&gt;, it says (in Haskell) &lt;q&gt;add1&lt;/q&gt; is the function that adds one to whatever it gets&lt;/q&gt;. &lt;br /&gt;&lt;br /&gt;This is what Haskell calls &lt;a href="http://haskell.org/haskellwiki/Pointfree"&gt;pointfree&lt;/a&gt; style. In the pointfree style, a coder leaves off terms from a definition when able. The resulting code is generally thought to be cleaner and more compact. It also lends itself well to groking function composition. &lt;br /&gt;&lt;br /&gt;One further complication is that the &lt;strong&gt;+&lt;/strong&gt; is in parentheses. This is because &lt;cite&gt;+&lt;/cite&gt; is most commonly used as an infix operator, such that its arguments appear on either side, as opposed to Scheme's &lt;strong&gt;(+ x 1)&lt;/strong&gt;, where the function appears first (as is usual in Scheme), and the arguments follow. The wrapping in parentheses converts the traditionally infix &lt;cite&gt;+&lt;/cite&gt; into a prefix function. &lt;br /&gt;&lt;br /&gt;One could argue that this slight syntax massaging is roughly similar to Ruby's need for &lt;cite&gt;call&lt;/cite&gt;, thereby angering fans of Ruby, Haskell, and Scheme simultaneously. Probably others too. Maybe F# fans. Who knows?&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;More Ruby:&lt;/h4&gt;&lt;br /&gt;In Ruby again, following the Object-Oriented nature of the language, we can do similar operations in which the functions are explicitly understood to be methods attached to specific objects. In this case, the Integer 1.&lt;br /&gt;&lt;br /&gt;Assuming the existence of this file &lt;strong&gt;func_as_arg.rb&lt;/strong&gt;:&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;class Integer&lt;br /&gt;  def call_symbol_arg_on_2(first_class_proc_as_sym)&lt;br /&gt;    2.send(first_class_proc_as_sym, self)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We can then do operations in irb again.&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;$ irb -r func_as_arg.rb&lt;br /&gt;irb(main):001:0&gt; 1.call_symbol_arg_on_2(:+)&lt;br /&gt;=&gt; 3&lt;br /&gt;irb(main):002:0&gt; 1.call_symbol_arg_on_2(:-)&lt;br /&gt;=&gt; 1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;With the results that you'd expect.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;In JavaScript:&lt;/h4&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;function call_proc_arg_on_2(first_class_proc) {&lt;br /&gt;   return first_class_proc(2);&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Number.prototype.call_proc_arg_on_2 = function(first_class_proc) {&lt;br /&gt;   return first_class_proc(this);&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;call_proc_arg_on_2(add1);&lt;br /&gt;Number(100).call_proc_arg_on_2(add1);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Aubrey's JavaScript code demonstrates both the initial example common across all the languages, as well as the equivalent of my second Ruby example, in which the desired behavior is attached to existing number objects within your language's existing workspace.&lt;br /&gt;&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;&lt;a id="split"&gt;&lt;/a&gt;&lt;br /&gt;I split infinitives. If such things get you riled up, I suggest you read about scientific linguistics a bit more. A good starting point is &lt;cite&gt;Language Myths&lt;/cite&gt;, edited by Laurie Bauer. In this particular case, the splitting was to clarify that &lt;q&gt;easily&lt;/q&gt; modifies the use the use of the more-common block syntax in Ruby, rather than suggesting that &lt;q&gt;easily&lt;/q&gt; modified the strength of the outweighing. Embedding the adverb within the verb is excellent for disamgiuation of this sort. Try it - you'll like it. Trust me.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-6647626841485626416?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/6647626841485626416/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=6647626841485626416' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/6647626841485626416'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/6647626841485626416'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/05/first-class-procedures-part-ii-passing.html' title='First-Class Procedures, Part II: passing as an argument'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-3170971357981806862</id><published>2008-06-11T15:30:00.002-04:00</published><updated>2008-06-12T08:55:41.120-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='partition'/><title type='text'>(Road to) Partition</title><content type='html'>I'm a big fan of Ruby's &lt;cite&gt;Enumerable#partition&lt;/cite&gt; method. It's very functional, and comes in quite handy. Here's the basic deal: it takes a block and returns two Arrays. The first of which contains all elements for which the block is &lt;a href="http://en.wikipedia.org/wiki/Truthiness"&gt;truthy&lt;/a&gt;, and the second of which contains all elements for which the block is false. Here's some code that more-or-less duplicates how it works:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size: 0.8em; overflow: scroll"&gt;&lt;br /&gt;module Enumerable&lt;br /&gt;&lt;br /&gt;  def my_partition(&amp;block) &lt;br /&gt;    return select(&amp;block), reject(&amp;block)&lt;br /&gt;  end &lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It returns a comma-separated list, so you can pull the returned values out into two separate Arrays, use the * operator to cram them together, what have you.&lt;br /&gt;&lt;br /&gt;&lt;dl&gt;&lt;br /&gt;&lt;br /&gt;&lt;dt&gt;Before&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;Here's some Rails code in its initial &lt;q&gt;quick 'n dirty&lt;/q&gt; state. One of the models in the app is called &lt;cite&gt;Enrollment&lt;/cite&gt;, and &lt;cite&gt;restore_previous_state!&lt;/cite&gt; is basically an undo method for some operations on enrollments. Methods run earlier in this app either set the &lt;cite&gt;ready_for_review&lt;/cite&gt; boolean flag to true, or autovivified some enrollments entirely. So this takes in some enrollments, destroys them if they were autovivified, and just marks those that it didn't create itself as &lt;q&gt;unready&lt;/q&gt;.&lt;br /&gt;&lt;br /&gt;I also wanted to keep track of how many changes of each type were done, for reporting purposes. Hence the &lt;cite&gt;+= 1&lt;/cite&gt; counter operations. Then I spit out the &lt;cite&gt;results&lt;/cite&gt; Hash at the end to know what I did. I think this version is fairly procedural for Ruby code, at least for my usual style. That's not necessarily bad, although I do prefer the changes below.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size: 0.8em; overflow: scroll"&gt;&lt;br /&gt;def restore_previous_state!(enrollments_to_modify)&lt;br /&gt;  results = { :reset =&gt; 0, :destroyed =&gt; 0 }&lt;br /&gt;  enrollments_to_modify.each do |e|&lt;br /&gt;    if e.autovivified?&lt;br /&gt;      e.destroy&lt;br /&gt;      results[:destroyed] += 1&lt;br /&gt;    else&lt;br /&gt;      e.ready_for_review = false&lt;br /&gt;      e.save&lt;br /&gt;      results[:reset] += 1&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  results&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;dt&gt;After&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;Here's the same method after some functionally-oriented &lt;a href="http://refactoring.com/"&gt;refactorings&lt;/a&gt; that use the &lt;cite&gt;partition&lt;/cite&gt; method.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size: 0.8em; overflow: scroll"&gt;&lt;br /&gt;def restore_previous_state!(enrollments_to_modify)&lt;br /&gt;  to_destroy, to_reset = enrollments_to_modify.partition(&amp;:autovivified?)&lt;br /&gt;  to_destroy.map(&amp;:destroy)&lt;br /&gt;  to_reset.map { |e| e.ready_for_review = false; e.save }&lt;br /&gt;  { :reset =&gt; to_reset.size, :destroyed =&gt; to_destroy.size }&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In the new version, I partition the enrollments based on whether or not they were autovivified, because (as we already know) my method has to behave quite differently in each case. Then I just map the &lt;cite&gt;destroy&lt;/cite&gt; operation onto all the enrollments that I should destroy, and a combined &lt;q&gt;unready&lt;/q&gt; &amp;amp; &lt;cite&gt;save&lt;/cite&gt; for the others. Since &lt;cite&gt;to_destroy&lt;/cite&gt; still exists in memory after the &lt;cite&gt;destroy&lt;/cite&gt; operations (it just had its database records deleted), I can still get its size. And of course &lt;cite&gt;to_reset&lt;/cite&gt; is still around, just with some readiness changes. &lt;br /&gt;&lt;br /&gt;Presto, spit out an anonymous Hash reporting how many changes of each type, and the external behavior of the method stays entirely the same, which is as it should be for a refactoring. The new version trims some lines (14 -&gt; 6), and I also find it more readable.&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;br /&gt;Elsewhere, the app goes even further with &lt;cite&gt;partition&lt;/cite&gt;. I won't explain this in as much detail, because if you followed the stuff above, this should be fairly clear as an additional example. &lt;br /&gt;&lt;br /&gt;One curious thing about this is that it uses a state-changing operation (&lt;cite&gt;save&lt;/cite&gt;) as the partitioning condition, so it becomes a &lt;q&gt;try to save, and separate based on whether or not it worked&lt;/q&gt; composite operation. Also, since I only need to keep counts, I immediately map the &lt;cite&gt;size&lt;/cite&gt; method onto each sub-Array that comes out of &lt;cite&gt;partition&lt;/cite&gt;.&lt;br /&gt;&lt;br /&gt;My co-worker &lt;a href="http://jimlindley.com/"&gt;Jim&lt;/a&gt; calls this sort of code &lt;q&gt;dense&lt;/q&gt;. I prefer &lt;q&gt;compact&lt;/q&gt;. He assures me that he was just teasing me.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size: 0.8em; overflow: scroll"&gt;&lt;br /&gt;def save_cleanup_and_report!(post_delayed_rules_to_save, rejected_potentials)&lt;br /&gt;  ready_to_save, invalid_for_saving = post_delayed_rules_to_save.partition(&amp;:valid?)&lt;br /&gt;  actually_saved_count, failed_save_count = ready_to_save.partition(&amp;:save).map(&amp;:size)&lt;br /&gt;  invalid_for_saving.map(&amp;:destroy)&lt;br /&gt;  rejected_potentials.map { |e| e.ready_for_review = false; e.save! }&lt;br /&gt;  [actually_saved_count, (rejected_potentials.size + failed_save_count)]&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I apologize for the pun in the title, of course.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-3170971357981806862?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/3170971357981806862/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=3170971357981806862' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/3170971357981806862'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/3170971357981806862'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/05/road-to-partition.html' title='(Road to) Partition'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-1992782297866780534</id><published>2008-06-09T14:42:00.003-04:00</published><updated>2008-12-09T19:24:26.677-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='jesus'/><category scheme='http://www.blogger.com/atom/ns#' term='borg'/><title type='text'>I am Jesus of Borg</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_Wn2fotOx-Bk/SE16I-GvCfI/AAAAAAAAABU/VoAOyFCVD8E/s1600-h/resistance_is_futile.JPG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_Wn2fotOx-Bk/SE16I-GvCfI/AAAAAAAAABU/VoAOyFCVD8E/s320/resistance_is_futile.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5209954638466320882" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I don't want to pick on anyone, and I realize this doesn't speak for all of the world's approximately Billion self-identified Christians, but does anyone else find this a bit disturbing?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-1992782297866780534?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/1992782297866780534/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=1992782297866780534' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/1992782297866780534'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/1992782297866780534'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/06/i-am-jesus-of-borg.html' title='&lt;q&gt;I am Jesus of Borg&lt;/q&gt;'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_Wn2fotOx-Bk/SE16I-GvCfI/AAAAAAAAABU/VoAOyFCVD8E/s72-c/resistance_is_futile.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-3281575528151717524</id><published>2008-06-03T12:18:00.012-04:00</published><updated>2008-08-05T12:52:42.197-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='railsconf'/><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><category scheme='http://www.blogger.com/atom/ns#' term='food'/><title type='text'>Notes from RailsConf, sort of</title><content type='html'>&lt;p&gt;&lt;br /&gt;I just got back from &lt;a href="http://railsconf.com/"&gt;RailsConf2008&lt;/a&gt; in Portland, and I'm still a little jet-lagged. Lots of good stuff, especially about scaling, the deployment ecosystem, and distributed / "cloud" computing. Everyone and their brother will be talking about the presentations, so I'm going to relate a couple of anecdotes.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Predicate Truth &lt;abbr title="versus"&gt;vs.&lt;/abbr&gt; Resource Protection&lt;/h2&gt;&lt;br /&gt;&lt;h3&gt;When is a vegan not a vegetarian?&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;I'm a &lt;a href="http://en.wikipedia.org/wiki/Vegan"&gt;vegan&lt;/a&gt; (just reporting data - eat what you think best). They had food for us at the conference, as well as for other dietary needs. I had a nice but brief chat with a guy waiting for the Kosher food, for example. So I would go up to the little cart labelled &lt;q&gt;vegan&lt;/q&gt; with the intention of getting food. Nothing shocking there.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;The guy serving the food would then ask people who approached &lt;q&gt;Are you a vegetarian?&lt;/q&gt;. Since the category &lt;cite&gt;vegetarian&lt;/cite&gt; is a subset of the category &lt;cite&gt;vegan&lt;/cite&gt;, I answered &lt;q&gt;yes&lt;/q&gt;. He then directed me to the standard food tables, since people who are ovo-lacto vegetarians had plenty of options among the standard fare. I then pointed out that I'm also a vegan, and got some food.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;This raises some interesting points - interesting to me, anyway. I told the guy that among a group of programmers, we're culturally very inclined to answer predicate questions with the literal truth: &lt;code&gt;some_vegan.vegetarian? =&gt; true&lt;/code&gt;. He then explained that his primary goal in asking the question was not to enquire about someone else's eating habits, but to make sure they don't run out of the vegan options too early. He was basically acting as a return guard, making sure that I had enough to eat. &lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;Another interpretation is that his question was based more on the weirdness factor. Vegetarians are weirder than omnivores, but to a lesser degree than vegans. So a vegan in that scenario would reject the boolean type signature implicit in &lt;q&gt;Are you a vegetarian?&lt;/q&gt; and offer more information: &lt;q&gt;I'm not just a vegetarian, I'm a vegan.&lt;/q&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Who Pays for Dinner?&lt;/h2&gt;&lt;br /&gt;&lt;h3&gt;Sushi as a load balancing problem&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;When out eating dinner with my colleagues, we discussed the issue of payment. Initially, we decided on a simple hocket: one guy pays, and then the next night a different guy pays, and so on. My fellow nerds will recognize that as a &lt;a href="http://en.wikipedia.org/wiki/Round-robin_scheduling"&gt;Round Robin load balancer&lt;/a&gt; approach.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;Things were fine until the fourth night. They were only three of us, so this was the first instance of a potential repeat. I had paid for very good (and very expensive) sushi meal two nights earlier, and my colleague Jim had splurged for Quizno's the previous night. Jim volunteered to shift to &lt;a href="http://en.wikipedia.org/wiki/Fair_queuing"&gt;Fair load balancing&lt;/a&gt;, using how much some one had paid so far in the trip as the fairness criterion - which of course stuck him with paying for the final meal.&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-3281575528151717524?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/3281575528151717524/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=3281575528151717524' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/3281575528151717524'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/3281575528151717524'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/06/notes-from-railsconf-sort-of.html' title='Notes from RailsConf, sort of'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-5874580581409967755</id><published>2008-05-19T15:18:00.037-04:00</published><updated>2008-12-09T19:24:28.089-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='hair'/><category scheme='http://www.blogger.com/atom/ns#' term='travis nixon'/><category scheme='http://www.blogger.com/atom/ns#' term='wigs for kids'/><category scheme='http://www.blogger.com/atom/ns#' term='cancer'/><title type='text'>Wigs for Kids</title><content type='html'>&lt;p&gt;I promise both my readers that I'll get back to first-class procedures soon. I'd like to talk about something else before that post will be ready.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;dl&gt;&lt;br /&gt;&lt;dt style="clear:both"&gt;Hippy / Alfalfa&lt;/dt&gt;&lt;br /&gt;&lt;dd style="clear:both"&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_Wn2fotOx-Bk/SDHXjMXFcQI/AAAAAAAAAAU/XM_mAWj2c5g/s1600-h/01_hippy.jpg"&gt;&lt;img style="clear: all; float: left; margin-right: 10px; cursor: pointer;" src="http://1.bp.blogspot.com/_Wn2fotOx-Bk/SDHXjMXFcQI/AAAAAAAAAAU/XM_mAWj2c5g/s200/01_hippy.jpg" alt="" id="BLOGGER_PHOTO_ID_5202176044203077890" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_Wn2fotOx-Bk/SDHXzMXFcRI/AAAAAAAAAAc/ye5AfpHuIvg/s1600-h/03_alfalfa.jpg"&gt;&lt;img style="clear: all; float: right; margin-left: 10px; cursor: pointer; vertical-align: top;" src="http://1.bp.blogspot.com/_Wn2fotOx-Bk/SDHXzMXFcRI/AAAAAAAAAAc/ye5AfpHuIvg/s200/03_alfalfa.jpg" alt="" id="BLOGGER_PHOTO_ID_5202176319080984850" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="clear: both"&gt;&lt;br /&gt;I periodically donate my hair to an organization called &lt;a href="http://www.wigsforkids.org/hair-donation/donate-your-hair/"&gt;Wigs for Kids&lt;/a&gt;. It was time for me to do it again this past weekend. I do it every 2 years or so. The basic idea is that you donate a 12 inch or longer ponytail of hair, and you help out some kid by giving them a wig if they have no hair, such as from the results of chemotherapy.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;dt style="clear:both"&gt;Hippy Side / Mullet&lt;/dt&gt;&lt;br /&gt;&lt;dd style="clear:both"&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_Wn2fotOx-Bk/SDHX-MXFcSI/AAAAAAAAAAk/IWE8TfzH8oc/s1600-h/04_hippy_side.jpg"&gt;&lt;img style="margin: 10px; float: left; cursor: pointer;" src="http://1.bp.blogspot.com/_Wn2fotOx-Bk/SDHX-MXFcSI/AAAAAAAAAAk/IWE8TfzH8oc/s200/04_hippy_side.jpg" alt="" id="BLOGGER_PHOTO_ID_5202176508059545890" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_Wn2fotOx-Bk/SDHYFsXFcTI/AAAAAAAAAAs/aUeKLmTswIs/s1600-h/05_mullet.jpg"&gt;&lt;img style="margin: 10px; float: left; cursor: pointer;" src="http://3.bp.blogspot.com/_Wn2fotOx-Bk/SDHYFsXFcTI/AAAAAAAAAAs/aUeKLmTswIs/s200/05_mullet.jpg" alt="" id="BLOGGER_PHOTO_ID_5202176636908564786" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Chemotherapy was sadly appropriate to this particular donation. As my wife Jenn and I were getting ready to leave that morning, we learned that our good friend &lt;a href="http://coyotesdenonline.com/pages.cfm?ID=25"&gt;Travis Nixon&lt;/a&gt; (&lt;a href="http://arfmonster.redtoenail.org/"&gt;RedToenail blog&lt;/a&gt;) had died the night before. He had had cancer for years, and had recently taken a downturn, but it was still a shock. The fact that this occurred the evening before we had already planned to donate hair to some unknown person, who (like Travis) has been diagnosed with cancer far too early in life, is the sort of thing that supernaturally-minded people often seem to see as especially significant in some way that's never been exactly clear to me (as a secular person). That's not a dig against religious people -- my mind is just focused on these sorts of topics at the moment.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;dt style="clear:both"&gt;Clippers&lt;/dt&gt;&lt;br /&gt;&lt;dd style="clear:both"&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_Wn2fotOx-Bk/SDHYO8XFcUI/AAAAAAAAAA0/rrvr9vWWcSc/s1600-h/06_clippers.jpg"&gt;&lt;img style="margin: 10px; float: left; cursor: pointer;" src="http://4.bp.blogspot.com/_Wn2fotOx-Bk/SDHYO8XFcUI/AAAAAAAAAA0/rrvr9vWWcSc/s200/06_clippers.jpg" alt="" id="BLOGGER_PHOTO_ID_5202176795822354754" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_Wn2fotOx-Bk/SDHYV8XFcVI/AAAAAAAAAA8/NBB6MSlRFc4/s1600-h/07_clippers_shorter.jpg"&gt;&lt;img style="margin: 10px; float: left; cursor: pointer;" src="http://4.bp.blogspot.com/_Wn2fotOx-Bk/SDHYV8XFcVI/AAAAAAAAAA8/NBB6MSlRFc4/s200/07_clippers_shorter.jpg" alt="" id="BLOGGER_PHOTO_ID_5202176916081439058" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;In any case, I'm grateful to our friend Kevin Farrell for doing the actual cutting. I think he did quite a good job. He refused to take any money, which speaks well of his character, but we hid some cash at the register while he was sweeping.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;dt style="clear:both"&gt;The Kevins&lt;/dt&gt;&lt;br /&gt;&lt;dd style="clear:both"&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_Wn2fotOx-Bk/SDHYeMXFcWI/AAAAAAAAABE/4C1u7exTYHw/s1600-h/08_kevins.jpg"&gt;&lt;img style="margin: 10px; float: left; cursor: pointer;" src="http://1.bp.blogspot.com/_Wn2fotOx-Bk/SDHYeMXFcWI/AAAAAAAAABE/4C1u7exTYHw/s200/08_kevins.jpg" alt="" id="BLOGGER_PHOTO_ID_5202177057815359842" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="clear: both"&gt;&lt;br /&gt;If you're able to grow your hair to the appropriate length, you might want to look into doing something similar. It's not addressing the root of the problem in any medical way, but I figure if we can do something that provides some comfort to a sick person with so little effort, it's a worthwhile thing to do.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;dt style="clear:both"&gt;Travis&lt;/dt&gt;&lt;br /&gt;&lt;dd style="clear:both"&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_Wn2fotOx-Bk/SDLd88XFcXI/AAAAAAAAABM/YM9_LTj0nns/s1600-h/travisHeadshot.jpg"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_Wn2fotOx-Bk/SDLd88XFcXI/AAAAAAAAABM/YM9_LTj0nns/s200/travisHeadshot.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5202464558631186802" /&gt;&lt;/a&gt;&lt;br /&gt;He was a good friend, and I miss him.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;br /&gt;&lt;hr style="clear: both;"/&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Links&lt;/h3&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;a href="http://www.legacy.com/mpnnow/Obituaries.asp?Page=LifeStory&amp;PersonID=109878267"&gt;An obituary from the online version of the Canandaigua Daily Messenger&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;a href="http://www.mpnnow.com/news/x1867425784/Councilman-loses-battle-with-cancer"&gt;An article about Travis from the online version of the Canandaigua Daily Messenger&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;a href="http://www.lumpley.com/comments.php?entry=369"&gt;D. Vincent Baker's blog entry about Travis&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;a href="http://www.fairgame-rpgs.com/comment.php?entry=173"&gt;Meg Baker's blog entry about Travis&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-5874580581409967755?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/5874580581409967755/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=5874580581409967755' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/5874580581409967755'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/5874580581409967755'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/05/wigs-for-kids.html' title='Wigs for Kids'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_Wn2fotOx-Bk/SDHXjMXFcQI/AAAAAAAAAAU/XM_mAWj2c5g/s72-c/01_hippy.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-2736385888851595355</id><published>2008-04-22T13:06:00.008-04:00</published><updated>2008-06-06T22:32:47.723-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='railscasts.com'/><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><title type='text'>5 Rails Tips</title><content type='html'>If you're not already watching Ryan Bates' screencasts at &lt;a href="http://railscasts.com/"&gt;Railscasts.com&lt;/a&gt;, I strongly recommend it. They're really quite well done and interesting. He's running a &lt;a href="http://railscasts.com/contest"&gt;contest&lt;/a&gt;, in which participants suggest 5 Rails tips. For my examples, I've decided to focus on areas that are either informed by ideas from functional programming (which this blog is ostensibly about), or areas in which Ryan and I have different approaches (or both). Here are my tips:&lt;br /&gt;&lt;br /&gt;&lt;dl&gt;&lt;br /&gt;&lt;dt&gt;&lt;strong&gt;Use Module methods&lt;/strong&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;Here's where I disagree with something Ryan said in his &lt;a href="http://railscasts.com/episodes/101"&gt;101st Railscast&lt;/a&gt;, in which he suggests using Class (or instance) methods with variables over using Module methods. I prefer to use Module methods. Here's a typical (truncated) example:&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;module DisplayHelper&lt;br /&gt;&lt;br /&gt;  def self.get_stylesheets_by_request(request)&lt;br /&gt;    user_agent = request ? request.user_agent : nil &lt;br /&gt;    self.get_stylesheets_by_ua(user_agent)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def self.get_stylesheets_by_ua(some_user_agent)&lt;br /&gt;    return STYLESHEETS_FOR[:palm] if some_user_agent =~ %r[Palm]&lt;br /&gt;    STYLESHEETS_FOR[:normal]&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Then, in the RSpec file:&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;  describe %q[get_stylesheets_by_request] do&lt;br /&gt;    mobile = %w[mobile]&lt;br /&gt;    it %Q(should add the stylesheet #{mobile} for Palm browsers) do&lt;br /&gt;      user_agent = %q[Some Palm User Agent]&lt;br /&gt;      request = mock_model(Object, :user_agent =&gt; user_agent)&lt;br /&gt;      DisplayHelper.get_stylesheets_by_request(request).should == mobile&lt;br /&gt;    end &lt;br /&gt;    normal_browser = %w[normal_browser]&lt;br /&gt;    it %Q(should add the stylesheet #{normal_browser} for all other browsers) do&lt;br /&gt;      user_agent = %q[Some User Agent]&lt;br /&gt;      request = mock_model(Object, :user_agent =&gt; user_agent)&lt;br /&gt;      DisplayHelper.get_stylesheets_by_request(request).should == normal_browser&lt;br /&gt;    end &lt;br /&gt;  end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As I said above, Ryan and I have different preferences regarding Class vs. Module. I like having helper methods that are testable in a more "purely functional" paradigm, wherein we mock a request, but don't need to mess around within the base controller object or the like. We just pass our mock request model into the &lt;em&gt;get_stylesheets_by_request&lt;/em&gt; method and take it from there. Either approach works, you may find that one or the other matches your own thought process more closely.&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;dt&gt;&lt;strong&gt;Use Constants&lt;/strong&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;In the code example above, notice the use of the STYLESHEETS_FOR Constant, which we can define as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;  STYLESHEETS_FOR = { &lt;br /&gt;    :normal =&gt; %w[normal_browser],&lt;br /&gt;    :palm   =&gt; %w[mobile]&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It gives us access to either &lt;em&gt;normal_browser.css&lt;/em&gt; or &lt;em&gt;mobile.css&lt;/em&gt;, as appropriate. This is a somewhat contrived example, but you could use whatever list of stylesheets you want for the values in each pair, expanding to customize for Safari, Epiphany, Opera, etc.  &lt;br /&gt;&lt;br /&gt;This technique could be used for any data that is unlikely to change in the course of an app running, but is not tied specifically enough to a given model to be stored in the DB. This allows you to avoid continually reconstructing never-changing local variables inside a method call. Some more realistic examples appear again in the MessageHelper tip below.&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;dt&gt;&lt;strong&gt;Return guards can simplify flow control&lt;/strong&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;I loath &lt;strong&gt;if - elseif - else - end&lt;/strong&gt; flow control, and strongly prefer return guards. It's a personal bigotry that I freely admit, because I think return guards make code more readable, shorter, and more in line with how I think. Here's a rewrite of Ryan's &lt;em&gt;star_type&lt;/em&gt; method from &lt;a href="http://railscasts.com/episodes/101"&gt;Railscast #101&lt;/a&gt; that uses return guards rather than an if block.&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;  def star_type(value)&lt;br /&gt;    return 'full' if value &gt; 1&lt;br /&gt;    return 'half' if value == 1&lt;br /&gt;    'empty'&lt;br /&gt;  end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;You save 4 lines, and also comply with my enraged, semi-coherent rantings. You can also get some additional shrinkage with a ternary. Here's &lt;em&gt;star_type&lt;/em&gt; again, with a ternary:&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;  def star_type(value)&lt;br /&gt;    return 'full' if value &gt; 1&lt;br /&gt;    value == 1 ? 'half' : 'empty'&lt;br /&gt;  end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;You save a line. I find it more readable than the if block. That may be because I like reading &lt;a href="http://www.haskell.org/"&gt;Haskell&lt;/a&gt; code. Who knows. Again, your flow control preference mileage (or kilometrage) may vary.&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;dt&gt;&lt;strong&gt;&lt;acronym title="Don't Repeat Yourself"&gt;DRY&lt;/acronym&gt; up your messages in a MessageHelper&lt;/strong&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;I like to create a MessageHelper.rb file that contains my messages. I usually have several Constant Hashes that vary depending on the purposes of the message.&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;module MessageHelper&lt;br /&gt;&lt;br /&gt;  DESCRIPTION_OF_RESOURCE = {&lt;br /&gt;    :name_of_resource =&gt; %q[My description...]&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  EXPLAIN = {&lt;br /&gt;    :not_empowered_to_delete =&gt; lambda { |type| %Q[You are not empowered to delete a #{type}.] }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  ERROR = {&lt;br /&gt;    :overlapping_blocks =&gt; %q[Overlapping blocks of time]&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  MESSAGE = {&lt;br /&gt;    :confirm_short     =&gt; %q[Are you sure?],&lt;br /&gt;    :confirm_long      =&gt; lambda { |x| %Q[Are you sure you want to destroy this #{x}?] },&lt;br /&gt;    :logout_successful =&gt; %q[You have been logged out.]&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  TITLE = { &lt;br /&gt;    :new   =&gt; lambda { |x| %Q[Make a new #{x}] },&lt;br /&gt;    :owner =&gt; lambda { |x| %Q[Owner of #{x}] },&lt;br /&gt;    :show  =&gt; lambda { |x| %Q[Show #{x}] },&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;These are obviously truncated. I use DESCRIPTION_OF_RESOURCE and EXPLAIN for hover explanations and the like. Notice also that the EXPLAIN and MESSAGE[:confirm_long] values are Procs, allowing you to reuse them abstractly. The purpose of the ERROR, MESSAGE and TITLE hashes should be obvious from their names.&lt;sup&gt;&lt;a href="#footnote1"&gt;1&lt;/a&gt;&lt;/sup&gt; The main benefit of this practice is that you can conform to DRY principles, while also separating your messages logically according to what they'll be needed for.&lt;br /&gt;&lt;br /&gt;Of course, you can wrap the use of each of these hashes inside helper methods, like &lt;em&gt;explain&lt;/em&gt;, &lt;em&gt;message&lt;/em&gt;, or what have you. &lt;br /&gt;&lt;br /&gt;&lt;sup&gt;&lt;a id="#footnote"&gt;1&lt;/a&gt;&lt;/sup&gt;(Maybe the purpose of TITLE isn't obvious: It's to facilitate link differentiation with &amp;lt;a&amp;gt; titles as per &lt;a href="http://www.w3.org/TR/WCAG10-HTML-TECHS/#link-text"&gt;http://www.w3.org/TR/WCAG10-HTML-TECHS/#link-text&lt;/a&gt;.)&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;dt&gt;&lt;strong&gt;Use parallel (pattern-matching) assignments&lt;/strong&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;Whenever assigning into multiple variables, it can often be helpful to do simultaneous parallel assignments using pattern matching. Here's an example from an RSpec file. I have a resource called &lt;strong&gt;Preceptor&lt;/strong&gt; which has many &lt;strong&gt;Rotations&lt;/strong&gt;, and a helper method called &lt;em&gt;create_preceptor_and_rotations&lt;/em&gt; that does what you (hopefully) expect:&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;@preceptor, @preceptor_rotations = create_preceptor_and_rotations&lt;br /&gt;@preceptor, discard__rotations   = create_preceptor_and_rotations&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In the first case, I want to use both &lt;em&gt;@preceptor&lt;/em&gt; and &lt;em&gt;@preceptor_rotations &lt;/em&gt;for some purpose. In the 2nd case, I want to keep the &lt;em&gt;@preceptor&lt;/em&gt;, but the name of the rotations variable informs anyone reading the code that I don't care about the rotations in this particular spec instance. In both cases, I've done assignments into two variables simultaneously.&lt;br /&gt;&lt;br /&gt;This sort of pattern is common in situations like this:&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;def some_method_that_takes_a_list(*args)&lt;br /&gt;  head, *tail = *args&lt;br /&gt;  # do some operations, maybe something recursive&lt;br /&gt;  return [head, tail]&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Which returns as follows:&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;some_method_that_takes_a_list(7)          -&gt; [7, []]&lt;br /&gt;some_method_that_takes_a_list(7, 8)       -&gt; [7, [8]]&lt;br /&gt;some_method_that_takes_a_list(7, 8, 9)    -&gt; [7, [8, 9]]&lt;br /&gt;some_method_that_takes_a_list([7, 8, 9])  -&gt; [[7, 8, 9], []]&lt;br /&gt;some_method_that_takes_a_list(*[7, 8, 9]) -&gt; [7, [8, 9]]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;br /&gt;So there's my list. These are all obviously fairly subjective points, but I find that these approaches work well for me, and &lt;a href="http://academicmanagement.com/"&gt;my co-workers&lt;/a&gt; seem pretty agreeable to them. Maybe they'll work for you, too.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-2736385888851595355?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/2736385888851595355/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=2736385888851595355' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/2736385888851595355'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/2736385888851595355'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/04/5-rails-tips.html' title='5 Rails Tips'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-597601556208665065</id><published>2008-04-22T09:10:00.003-04:00</published><updated>2008-06-06T22:34:09.146-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='first-class procedures'/><category scheme='http://www.blogger.com/atom/ns#' term='SICP'/><title type='text'>First-Class Procedures, Part I: naming by a variable</title><content type='html'>In section 1.3.4 of &lt;a href="http://mitpress.mit.edu/sicp/"&gt;SICP&lt;/a&gt;, the authors discuss (after Christopher Strachey) the &lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#call_footnote_Temp_121"&gt;rights and privileges of first-class citizens&lt;/a&gt; of a programming language, justifiably praising Lisp for awarding first-class status to procedures.&lt;br /&gt;&lt;br /&gt;The first few real posts here will explore this topic as it relates to some other languages. Are &lt;q&gt;procedures&lt;/q&gt; first-class citizens of &lt;a href="http://ruby-lang.org/"&gt;Ruby&lt;/a&gt;? How about &lt;a href="http://erlang.org/"&gt;Erlang&lt;/a&gt;, or even &lt;a href="http://en.wikipedia.org/wiki/JavaScript"&gt;JavaScript&lt;/a&gt;?&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Ruby&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;I'll start with Ruby, where the topic seems most controversial. I'll also start with the first condition that SICP suggests first-class citizens must satisfy: being nameable by a variable. Here's a sample irb session:&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;$ irb &lt;br /&gt;irb(main):001:0&amp;gt; add1a = 1.method(:+)&lt;br /&gt;=&amp;gt; #&amp;lt;Method: Fixnum#+&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;On line 1, we identify a given procedure by the name &lt;em&gt;add1a&lt;/em&gt; by using the &lt;cite&gt;method&lt;/cite&gt; method to extract the action that is referred to when sending the &lt;em&gt;:+&lt;/em&gt; symbol as a message to the Fixnum &lt;em&gt;1&lt;/em&gt;. Ruby defines this operation as simple numeric addition, more or less, and the fact that we're grabbing this method from &lt;em&gt;1&lt;/em&gt;, as opposed to &lt;em&gt;42&lt;/em&gt; or  &lt;em&gt;-6&lt;/em&gt; acts as a closure, meaning that the extracted method remembers that the item to which its arguments should be added is &lt;em&gt;1&lt;/em&gt;, rather than &lt;em&gt;42&lt;/em&gt; or any of the other instances from which we could have extracted our method. (Had we extracted the &lt;em&gt;:+&lt;/em&gt; method from a different instance, perhaps &lt;em&gt;&lt;q&gt;hello, world!&lt;/q&gt;&lt;/em&gt;, the operation wouldn't even have been defined as simple numeric addition, but that's another story).&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;irb(main):002:0&amp;gt; add1b = lambda { |x| 1 + x } &lt;br /&gt;=&amp;gt; #&amp;lt;Proc:0xb7c8a638@(irb):2&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;On line 2, we're identifying another procedure by the name &lt;em&gt;add1b&lt;/em&gt;. This time, we're using &lt;cite&gt;lambda&lt;/cite&gt; to construct the procedure, rather than extract a pre-existing method from an instance.&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;irb(main):003:0&amp;gt; add1c = Proc.new { |x| 1 + x } &lt;br /&gt;=&amp;gt; #&amp;lt;Proc:0xb7c823c0@(irb):3&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;On line 3, we're identifying yet another procedure by the name &lt;em&gt;add1c&lt;/em&gt;. This time, we're using &lt;cite&gt;Proc.new&lt;/cite&gt; rather than &lt;cite&gt;lambda&lt;/cite&gt;, both of which create Proc objects.&lt;br /&gt;&lt;br /&gt;None of these assignments of procedures into names would be terribly useful if they weren't callable.&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;irb(main):004:0&amp;gt; add1a.call(2)&lt;br /&gt;=&amp;gt; 3&lt;br /&gt;irb(main):005:0&amp;gt; add1b.call(2)&lt;br /&gt;=&amp;gt; 3&lt;br /&gt;irb(main):006:0&amp;gt; add1c.call(2)&lt;br /&gt;=&amp;gt; 3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We see that all 3 of these named variables perform the expected operation when called. How does this compare with some other languages that are more definitively functional?&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Lisp/Scheme&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;In deference to SICP, let's see how this operates in MIT Scheme:&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;$ mit-scheme&lt;br /&gt;1 ]=&amp;gt; (define add1 (lambda (x) (+ x 1)))&lt;br /&gt;&lt;br /&gt;;Value: add1&lt;br /&gt;&lt;br /&gt;1 ]=&amp;gt; add1&lt;br /&gt;&lt;br /&gt;;Value 11: #[compound-procedure 11 add1]&lt;br /&gt;&lt;br /&gt;1 ]=&amp;gt; (add1 2)&lt;br /&gt;&lt;br /&gt;;Value: 3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The theory is the same: we identify a procedure by the name (&lt;em&gt;add1&lt;/em&gt; in this case), and then call it (apply it to its arguments). Note, however, that calling the add1 procedure is slightly more straightforward in Scheme. We simply construct a list within parentheses such that the procedure to be called happens to be the first item, and the arguments to that procedure call are the remaining elements of the list. There is no need for a separate &lt;em&gt;.call&lt;/em&gt; syntax. Ruby Procs can also be called without a &lt;em&gt;.call&lt;/em&gt; method if one puts the arguments within &lt;em&gt;[]&lt;/em&gt;, rather than parentheses, but the main difference still stands, which is that the syntax of calling such a procedure differs (however slightly) from calling the built-in methods. Lisp fans who deride Ruby fans' claims of Ruby having first-class procedures point to this, not entirely without merit.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Erlang&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;Another language that fits very well within the functional paradigm is Erlang:&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;$ erl &lt;br /&gt;Erlang (BEAM) emulator version 5.5.5 [source] [async-threads:0] [hipe] [kernel-poll:false]&lt;br /&gt;&lt;br /&gt;Eshell V5.5.5  (abort with ^G) &lt;br /&gt;1&amp;gt; Add1 = fun(X) -&amp;gt; 1 + X end.&lt;br /&gt;#Fun&lt;erl_eval.6.49591080&gt;&lt;br /&gt;2&amp;gt; Add1(5).&lt;br /&gt;6&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;here, we define a named variable &lt;em&gt;Add1&lt;/em&gt; that holds the procedure functionally equivalent to all of our examples so far, the action of adding 1 to the argument. Here, the action of calling our new named variable as a function is more straightforward, reminiscent of Scheme. We simply provide the argument within parentheses, as one calls any function in Erlang. There is a slight difference, in that variables in Erlang are capitalized, while built-in functions are lowercase. Still, functions are clearly nameable in Erlang.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;JavaScript&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;What about JavaScript? JavaScript is a language that has gotten a largely undeserved bad rap, largely due to some bad implementations and what is perhaps the worst and most misleading name in programming language history. None of this detracts from the language itself, which my good friend &lt;a href="http://keus.org/"&gt;Aubrey Keus&lt;/a&gt; calls &lt;q&gt;Scheme with syntax&lt;/q&gt;. JavaScript reminds me a great deal of Ruby, in that it makes heavy use of both object-oriented and functional paradigms. Let's take a look at some JavaScript code that Aubrey provided:&lt;br /&gt;&lt;br /&gt;&lt;pre style="overflow: scroll;"&gt;&lt;br /&gt;add1 = function(x) { return x + 1; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Look at how trivial that is to do. All one has to do is call &lt;em&gt;add1(2)&lt;/em&gt; and &lt;em&gt;3&lt;/em&gt; is returned, as one would expect. Again, the act of calling such a function is closer to (indistinguishable from) calling a built-in function.&lt;br /&gt;&lt;br /&gt;I would argue that in all of these languages, procedures (whether implemented under the hood as functions or methods) satisfy this first criterion of being a first-class citizen. You may disagree, thinking that the additional hoops to be jumped through in the Ruby calls are too egregious. I find that from a pragmatic perspective, it's not that big a deal for me.&lt;br /&gt;&lt;br /&gt;I also think Hashes are more like functions than Arrays, but that's another story.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-597601556208665065?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/597601556208665065/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=597601556208665065' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/597601556208665065'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/597601556208665065'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/04/first-class-procedures-part-i-naming-by.html' title='First-Class Procedures, Part I: naming by a variable'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7132283077086596617.post-2757636023000924129</id><published>2008-04-22T08:36:00.000-04:00</published><updated>2008-04-22T13:28:20.662-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SICP'/><category scheme='http://www.blogger.com/atom/ns#' term='intro'/><title type='text'>So what's with the name?</title><content type='html'>I'm about to start writing a series of short posts, each about the question of whether procedures are first-class citizens in Ruby, as well as some other languages. Look for those starting in a few days. However, some folks might not be aware of the pun that inspired the name of this blog.&lt;br /&gt;&lt;br /&gt;There may or may not be a legendary organization called &lt;cite&gt;&lt;a href="http://foldoc.org/?Knights+of+the+Lambda-Calculus"&gt;The Knights of the &amp;lambda; Calculus&lt;/a&gt;&lt;/cite&gt;. I wouldn't claim to call myself a &lt;a href="http://foldoc.org/?wizard"&gt;wizard&lt;/a&gt;, nor do I primarily work in Lisp or Scheme, so I'm clearly not a member of such an illustrious group. However, I'll make frequent references to topics discussed in &lt;cite&gt;&lt;a href="http://en.wikipedia.org/wiki/SICP"&gt;SICP&lt;/a&gt;&lt;/cite&gt;, the &lt;a href="http://foldoc.org/?Wizard+Book"&gt;Wizard Book&lt;/a&gt;, so I equally clearly have certain aspirations in this direction. Hence the blog name.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7132283077086596617-2757636023000924129?l=lambdasquire.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lambdasquire.blogspot.com/feeds/2757636023000924129/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7132283077086596617&amp;postID=2757636023000924129' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/2757636023000924129'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7132283077086596617/posts/default/2757636023000924129'/><link rel='alternate' type='text/html' href='http://lambdasquire.blogspot.com/2008/04/so-whats-with-name.html' title='So what&apos;s with the name?'/><author><name>Kevin C. Baird</name><uri>http://www.blogger.com/profile/07150501759920425779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://en.gravatar.com/userimage/3225303/137bfd916eb9192ba75d2319b75a04c6.jpg'/></author><thr:total>0</thr:total></entry></feed>
