<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:osnews="http://osnews.com/rss2#">
	<channel>
		<title>OSNews: </title>
		<link>http://www.osnews.com/story/16980/Closures_101_in_Ruby</link>
		<description>Exploring the Future of Computing</description>
		<language>en-us</language>
		<copyright>Copyright 2001-2009, David Adams</copyright>
		<webMaster>adam+nospam@osnews.com</webMaster>
		<lastBuildDate>Tue, 10 Nov 2009 08:07:08 GMT</lastBuildDate>
		<image>
			<url>http://www.osnews.com/images/osnews.gif</url>
			<title>OSNews.com</title>
			<link>http://www.osnews.com</link>
		</image>
		<item>
			<title>bleh</title>
			<link>http://osnews.com/thread?202884</link>
			<guid isPermaLink="true">http://osnews.com/thread?202884</guid>
			<description>these are easier to understand in lisp than in ruby.</description>
			<pubDate>Thu, 18 Jan 2007 01:01:00 GMT</pubDate>
			<author>donotreply@osnews.com (broken_symlink)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Mmm Closures</title>
			<link>http://osnews.com/thread?202894</link>
			<guid isPermaLink="true">http://osnews.com/thread?202894</guid>
			<description>I remember last semester I had a teacher who seemed adamant that anonymous methods in C# were bad and shouldn't ever be used:  I used them anyway <img src="/images/emo/smile.gif" alt=";)" /> .</description>
			<pubDate>Thu, 18 Jan 2007 01:25:00 GMT</pubDate>
			<author>donotreply@osnews.com (ma_d)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Welcome new citizen!</title>
			<link>http://osnews.com/thread?202901</link>
			<guid isPermaLink="true">http://osnews.com/thread?202901</guid>
			<description>And about time! It always drives me mad when i think: hmh, why can't i pass the function itself? I fear FP has dented my brain ;-)</description>
			<pubDate>Thu, 18 Jan 2007 01:42:00 GMT</pubDate>
			<author>donotreply@osnews.com (B. Janssen)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE: bleh</title>
			<link>http://osnews.com/thread?202908</link>
			<guid isPermaLink="true">http://osnews.com/thread?202908</guid>
			<description>The syntax for these things in Ruby is a bit nasty. Literal blocks are denoted as:<br />
<br />
{ | args | ... }<br />
<br />
But literal blocks are not first-class objects. You can create a procedure object from a literal block with:<br />
<br />
f = Proc.new { | args | ... }<br />
<br />
and call the result with f.call(args)<br />
<br />
In Lisp (and most other languages with first-class closures), the literal block (ie: lambda expression) is an actual first-class object. Ie, in Python, you can do:<br />
<br />
f = lambda x: x + 1<br />
f(3)<br />
<br />
Unless I'm missing something, the equivalent Ruby code is:<br />
<br />
f = Proc.new {|x| x + 1}<br />
f.call(3)<br />
<br />
which isn't as elegant.</description>
			<pubDate>Thu, 18 Jan 2007 02:08:00 GMT</pubDate>
			<author>donotreply@osnews.com (rayiner)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE[2]: bleh</title>
			<link>http://osnews.com/thread?202923</link>
			<guid isPermaLink="true">http://osnews.com/thread?202923</guid>
			<description>In Ruby you can also call Proc's using:<br />
f[3]<br />
which is the same as f.call(3)<br />
<br />
And, according to WikiBooks, A Proc is a first-class object in Ruby:<br />
<a href="http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls#More_on_Procs" rel="nofollow">http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls#M...</a></description>
			<pubDate>Thu, 18 Jan 2007 03:03:00 GMT</pubDate>
			<author>donotreply@osnews.com (steved3298)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE[3]: bleh</title>
			<link>http://osnews.com/thread?202929</link>
			<guid isPermaLink="true">http://osnews.com/thread?202929</guid>
			<description>Proc's might be first class objects, but blocks are not Proc's. They are syntactic structures that can be converted to Proc's. In other languages that support closures, literal lambda expressions are first-class objects, just like literal numbers or literal strings are first-class objects. <br />
<br />
If blocks were first-class objects in Ruby, you could do:<br />
<br />
fun = {|x| x + 1}<br />
<br />
just like you can do:<br />
<br />
num = 5</description>
			<pubDate>Thu, 18 Jan 2007 03:29:00 GMT</pubDate>
			<author>donotreply@osnews.com (rayiner)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE[2]: bleh</title>
			<link>http://osnews.com/thread?202935</link>
			<guid isPermaLink="true">http://osnews.com/thread?202935</guid>
			<description>lambdas are not closures. Closures have persistent scope.<br />
<br />
In python coroutines are the closest you get to closures (new in 2.5).<br />
<br />
By the way Ruby code blocks are also not closures...</description>
			<pubDate>Thu, 18 Jan 2007 04:07:00 GMT</pubDate>
			<author>donotreply@osnews.com (grfgguvf)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>REAL closures</title>
			<link>http://osnews.com/thread?202938</link>
			<guid isPermaLink="true">http://osnews.com/thread?202938</guid>
			<description>Here is a closure in Lisp:<br />
<br />
(defun g (x) (setf v x) (defun f (a) (let ((t v)) (setf v a) t)))<br />
<br />
Hope I got the parens right <img src="/images/emo/wink.gif" alt=";)" /> <br />
<br />
Anyway give me a Ruby or C# equivalent please...</description>
			<pubDate>Thu, 18 Jan 2007 04:14:00 GMT</pubDate>
			<author>donotreply@osnews.com (grfgguvf)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>REAL closures</title>
			<link>http://osnews.com/thread?202939</link>
			<guid isPermaLink="true">http://osnews.com/thread?202939</guid>
			<description>OK here is the &quot;same&quot; in Python (2.5 only):<br />
<br />
def g(x): v=x; while True: t=(yield v); if t is not None: v=t; else: raise Error<br />
<br />
I believe Ruby has no equivalent but I might be wrong...</description>
			<pubDate>Thu, 18 Jan 2007 04:25:00 GMT</pubDate>
			<author>donotreply@osnews.com (grfgguvf)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Dont' forget Javascript</title>
			<link>http://osnews.com/thread?202940</link>
			<guid isPermaLink="true">http://osnews.com/thread?202940</guid>
			<description>Javascript can do closures too.  I find them very useful in web development.</description>
			<pubDate>Thu, 18 Jan 2007 04:27:00 GMT</pubDate>
			<author>donotreply@osnews.com (BigZaphod)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE[3]: bleh</title>
			<link>http://osnews.com/thread?202947</link>
			<guid isPermaLink="true">http://osnews.com/thread?202947</guid>
			<description>Do you mean Python lambdas, or lambdas in general? I'm pretty sure the term &quot;lambda&quot; in general refers to a closure, since it derives from Lisp's &quot;lambda&quot; construct, which is a closure.</description>
			<pubDate>Thu, 18 Jan 2007 05:15:00 GMT</pubDate>
			<author>donotreply@osnews.com (rayiner)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>C# closures</title>
			<link>http://osnews.com/thread?202951</link>
			<guid isPermaLink="true">http://osnews.com/thread?202951</guid>
			<description>C# also supports keeping the scope of anonymous method. But since I'm not a LISP expert, I cannot make a direct comparison. Yet here is an example that works<br />
<br />
void AddCounter(ProgressBar p, Button b)<br />
{<br />
..b.clicked += delegate {<br />
....p.Value++;<br />
..}<br />
}<br />
<br />
AddCounter(myProgressBar, myButton);<br />
<br />
Compiler generates a special class in order to maintain the scope of the delegate (which contains value of p). So it becomes:<br />
<br />
class Temp {<br />
..ProgressBar p;<br />
..<br />
..void HandleDelegate(... on click parameters ...)<br />
..{<br />
....p.Value++;<br />
..}<br />
}<br />
<br />
As far as I know there are 3 or 4 such templates for different needs (no score variables, function parameters, local class access, etc). (Lisp and others probably maintain such internal structures too. i.e: it has to be stored somewhere)Edited 2007-01-18 05:27</description>
			<pubDate>Thu, 18 Jan 2007 05:25:00 GMT</pubDate>
			<author>donotreply@osnews.com (sukru)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Check out Io...</title>
			<link>http://osnews.com/thread?202952</link>
			<guid isPermaLink="true">http://osnews.com/thread?202952</guid>
			<description>...iolanguage.com</description>
			<pubDate>Thu, 18 Jan 2007 05:32:00 GMT</pubDate>
			<author>donotreply@osnews.com (Tuishimi)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE: Mmm Closures</title>
			<link>http://osnews.com/thread?202961</link>
			<guid isPermaLink="true">http://osnews.com/thread?202961</guid>
			<description>I don't blame you, and doing anything short of non-trivial stuff with generics in C# 2.0 is made much easier by using anonymous methods, namely the new System.Predicate constructs. It also leads to more readable code.<br />
<br />
Just curious, did he have a reason behind not using anonymous methods?</description>
			<pubDate>Thu, 18 Jan 2007 06:53:00 GMT</pubDate>
			<author>donotreply@osnews.com (jayson.knight)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>REAL closures</title>
			<link>http://osnews.com/thread?202962</link>
			<guid isPermaLink="true">http://osnews.com/thread?202962</guid>
			<description>Read this article: <a href="http://www.theserverside.net/tt/articles/showarticle.tss?id=AnonymousMethods" rel="nofollow">http://www.theserverside.net/tt/articles/showarticle.tss?id=Anonymo...</a></description>
			<pubDate>Thu, 18 Jan 2007 07:02:00 GMT</pubDate>
			<author>donotreply@osnews.com (jayson.knight)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Closures</title>
			<link>http://osnews.com/thread?202992</link>
			<guid isPermaLink="true">http://osnews.com/thread?202992</guid>
			<description>A closure should not be mistaken with anonymous method or &quot;code block&quot; or a lambda expression.<br />
<br />
A closure is not simply an anonymous method or a lambda expression. You can even create a closure with a named method.<br />
<br />
A closure is actually a <b>formed</b> when &quot;function references free variables in it´s lexical environment&quot;. It is not the function or code block itself. It is the runtime &quot;thing&quot; that it is created encapsulating the function and the references to the environment.<br />
<br />
3.times { puts &quot;Inside the times method.&quot; }<br />
does not create a closure. There´s no reference inside the method/code block, to any variables on its same lexical scope.<br />
<br />
In the same way, a lamdba expression is not in itself a closure. You will, probably, end up creating closures if you use lambda expressions, but the lambda expressions themselves are not closures and do not, necessarily imply the creation of one.<br />
<br />
f = lambda x: x + 1<br />
creates an anonymous function which applied to a number sums 1 to it and assigns that function to f. This does not create a closure. It is simply an anonymous function. The code inside the block does not reference anything outside its lexical scope. Just its own argument.<br />
<br />
This<br />
(defun g (x) (setf v x) (defun f (a) (let ((t v)) (setf v a) t)))<br />
does create a closure, as the function f is referencing v, which is not in its own scope but in the environment of f (i.e. it is defined in g).<br />
Closures don´t even have to imply using anonymous methods, as you can see in the example, where f has a perfectly known name.<br />
<br />
<br />
Closures are an interesting concept, leading to complicated discussions and errors (if not handled properly). And confusing them with the simpler concept of an anonymous function or a code block does not really help people understand them.<br />
<br />
<br />
<br />
P.D. I hope I did explain it properly, but as I´m in a bit of a hurry, please feel free to expand and/or correct me if I said something wrong.</description>
			<pubDate>Thu, 18 Jan 2007 10:33:00 GMT</pubDate>
			<author>donotreply@osnews.com (gonzalo)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Closures in Java</title>
			<link>http://osnews.com/thread?202995</link>
			<guid isPermaLink="true">http://osnews.com/thread?202995</guid>
			<description>Although Java doesn't provided a direct method for closures you can simulate them in many(not all by any stretch) cases with inner anonymous classes. Would be better if it had them properly but it's not a bad solution. <br />
<br />
I personally don't like using closures in large projects anyway. Often documentation is limited and people use single character arguments names, you don't even have a function name to give you a clue. The closure is usually doing something which would be useful elsewhere and should be factories out into a functor. <br />
<br />
I find that they are often a convince for getting things up and running when you don't want to spend your time building another class or search for the code which already does it. But doesn't aid in readability or maintainability.<br />
<br />
Note: From the previous post I mean anonymous functions not closures. You cannot do closures in java as far as I'm awareEdited 2007-01-18 10:59</description>
			<pubDate>Thu, 18 Jan 2007 10:56:00 GMT</pubDate>
			<author>donotreply@osnews.com (stefanpa)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Closures are safer in purely functional languages</title>
			<link>http://osnews.com/thread?203000</link>
			<guid isPermaLink="true">http://osnews.com/thread?203000</guid>
			<description>As gonzalo pointed out, the point about closures is really capturing variables from the scopes where the closure is defined. If you don't do that it is not a closure but just an anonymous function.<br />
<br />
Closures in languages that allow mutable variables are quite useful, but also quite prone to produce difficult errors. <br />
<br />
In purely functional languages closures are much saner and safer since they automatically have referential transparency. I think it is kind of disturbing that people rip out features from functional languages to improve their procedural or OO languages without understanding the problems.</description>
			<pubDate>Thu, 18 Jan 2007 11:07:00 GMT</pubDate>
			<author>donotreply@osnews.com (tuttle)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE: Closures are safer in purely functional languages</title>
			<link>http://osnews.com/thread?203009</link>
			<guid isPermaLink="true">http://osnews.com/thread?203009</guid>
			<description><i>In purely functional languages closures are much saner and safer since they automatically have referential transparency. I think it is kind of disturbing that people rip out features from functional languages to improve their procedural or OO languages without understanding the problems.</i><br />
<br />
Referential transparency is over-estimated. No body can tell why is it good, yet everyone mentions it.</description>
			<pubDate>Thu, 18 Jan 2007 12:08:00 GMT</pubDate>
			<author>donotreply@osnews.com (axilmar)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Defining the language in the language</title>
			<link>http://osnews.com/thread?203011</link>
			<guid isPermaLink="true">http://osnews.com/thread?203011</guid>
			<description>Defining the language in the language has a number of problems, both efficiency is harmed as is the ease of  debugging. So I'm not convinced of the merit of closures.</description>
			<pubDate>Thu, 18 Jan 2007 12:18:00 GMT</pubDate>
			<author>donotreply@osnews.com (dmantione)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE: Defining the language in the language</title>
			<link>http://osnews.com/thread?203028</link>
			<guid isPermaLink="true">http://osnews.com/thread?203028</guid>
			<description>&quot;Defining the language in the language has a number of problems, both efficiency is harmed as is the ease of debugging. So I'm not convinced of the merit of closures.&quot;<br />
<br />
Defining the language in language is about macros, not lambdas.<br />
<br />
Closures are useful, because they can &quot;remember&quot; the parameters they're constructed with (see my example above).<br />
<br />
I don't quite understand why people advocating languages without anonymous methods or closures treat the them as if they're insignificant.<br />
<br />
People, please Java is fine as it's. It has it's own pros and cons. And do not attack other languages just because they contain features that Java don't. (Or replace Java here with your language of choice).</description>
			<pubDate>Thu, 18 Jan 2007 14:06:00 GMT</pubDate>
			<author>donotreply@osnews.com (sukru)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Referential transparency</title>
			<link>http://osnews.com/thread?203029</link>
			<guid isPermaLink="true">http://osnews.com/thread?203029</guid>
			<description>Of course I can tell you what referential transparency is good for. But that would be quite a long posting. For example it makes concurrent programming much more transparent and easy. And it eliminates whole classes of bugs completely. For example &quot;leaky encapsulation&quot; in complex classes.<br />
<br />
You might have a point that mutable variables are a nessecary evil in some situations. But saying that referential transparency is useless is like saying the const keyword in C++ is useless because you can write any program without any const keywords.</description>
			<pubDate>Thu, 18 Jan 2007 14:09:00 GMT</pubDate>
			<author>donotreply@osnews.com (tuttle)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE: bleh</title>
			<link>http://osnews.com/thread?203045</link>
			<guid isPermaLink="true">http://osnews.com/thread?203045</guid>
			<description>&gt; these are easier to understand in lisp than in ruby.<br />
<br />
Probably, but you'd have to understand Lisp first, which  isn't that easy (based on experience - I know a lot of students who say it's hard as hell).<br />
<br />
Teaching functional programming, closures, lambda expressions, and a completely unreadable syntax all at once always proves to be a mistake, but the profs do it again and again anyway...</description>
			<pubDate>Thu, 18 Jan 2007 15:22:00 GMT</pubDate>
			<author>donotreply@osnews.com (Morin)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE: Closures in Java</title>
			<link>http://osnews.com/thread?203047</link>
			<guid isPermaLink="true">http://osnews.com/thread?203047</guid>
			<description>&gt; You cannot do closures in java as far as I'm aware <br />
<br />
You can create closures through inner classes, though they are limited in one way: If they are nested inside a method, all accessed local variables from that method must be marked 'final' (they cannot be changed anymore).</description>
			<pubDate>Thu, 18 Jan 2007 15:28:00 GMT</pubDate>
			<author>donotreply@osnews.com (Morin)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE[2]: Closures are safer in purely functional languages</title>
			<link>http://osnews.com/thread?203048</link>
			<guid isPermaLink="true">http://osnews.com/thread?203048</guid>
			<description>&gt; Referential transparency is over-estimated. No body can<br />
&gt; tell why is it good, yet everyone mentions it.<br />
<br />
The advantage of referential transparency is that the function signature formally tells you what exactly a function does. You could also say that *not* having referential transparency is bad because today's imperative languages allow arbitrary side-effects within a procedure without forcing the programmer to note them down in the procedure signature. It's really a bit like the advantages of a static type system.<br />
<br />
Having said that... there are attempts to do exactly what I described: To note down bounds for possible side-effects of a procedure. The confusing part is that this is done in purely functional languages, and for constructs that model side-effects in a pure language (these constructs are called monads). You get this when you combine monads with a static type system.<br />
<br />
I would like to see similar things for imperative languages, but I think we'd need better mechanisms for side-effect aliasing first.</description>
			<pubDate>Thu, 18 Jan 2007 15:37:00 GMT</pubDate>
			<author>donotreply@osnews.com (Morin)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE[2]: bleh</title>
			<link>http://osnews.com/thread?203051</link>
			<guid isPermaLink="true">http://osnews.com/thread?203051</guid>
			<description>Actually the equivalent in Ruby is nearly identical to Python:<br />
<br />
f = lambda { |x| x + 1 }<br />
f[3]<br />
<br />
which is just as elegant (actually I like the syntax better, but that's just an opinion).</description>
			<pubDate>Thu, 18 Jan 2007 16:03:00 GMT</pubDate>
			<author>donotreply@osnews.com (zerohalo)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Closures and Objects</title>
			<link>http://osnews.com/thread?203120</link>
			<guid isPermaLink="true">http://osnews.com/thread?203120</guid>
			<description>Here's a function that takes a value, and returns a function that can be called to get that value increased by double.<br />
<br />
<i>double(val){<br />
. . return function(way){<br />
. . . . if(way == &quot;by_multiplication&quot;)<br />
. . . . . . return val * 2;<br />
. . . . if(way == &quot;by_addition&quot;)<br />
. . . . . . return val + val;<br />
. . }<br />
}</i><br />
<br />
Now if we do this...<br />
<br />
<i>doubler = double(6);</i><br />
<br />
...it is equivalent to doing this...<br />
<br />
[i]doubler(way){<br />
. . var val = 6; //</description>
			<pubDate>Thu, 18 Jan 2007 18:17:00 GMT</pubDate>
			<author>donotreply@osnews.com (chaosvoyager)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE[3]: bleh</title>
			<link>http://osnews.com/thread?203395</link>
			<guid isPermaLink="true">http://osnews.com/thread?203395</guid>
			<description>&quot;By the way Ruby code blocks are also not closures...&quot;<br />
<br />
Yeah they are:<br />
<br />
irb(main):001:0&gt; def make_proc(x)<br />
irb(main):002:1&gt;   y = 3<br />
irb(main):003:1&gt;   return proc { x + y }<br />
irb(main):004:1&gt; end<br />
=&gt; nil<br />
irb(main):005:0&gt; p = make_proc(2)<br />
=&gt; #<br />
irb(main):006:0&gt; p[]<br />
=&gt; 5Edited 2007-01-19 06:28</description>
			<pubDate>Fri, 19 Jan 2007 06:27:00 GMT</pubDate>
			<author>donotreply@osnews.com (leavengood)</author>
			<category>Comments</category>
		</item>
	</channel>
</rss>
