Please see below for explanation. (And btw, when it comes to local
variables it’s usually better to not test in IRB since that behaves a
bit differently there.)
def closure( over=0) lambda{over+=1} end
def closure( over=0) ->{over+=1} end
closed = closure
both of these will work. I am just curious if there is a way to accomplish
the same thing without method name definition i.e anonymous function.
The point is that for a closure to be created you need a scope. In
your first case you basically do the same as
over=0
closed = Proc.new{over+=1}
In other words: you use the current scope. But only if you use a
method or another lambda you can ensure that the scope is not visible
any more to the outside world and is only accessible through the
closure. And this is what one usually wants because otherwise the
data can be manipulated from outside the closure which might break the
desired functionality (such as resetting a counter as shown above).
With these approaches you get a scope which is cut off and not
accessible from the outside:
The outer lambda here is just to ensure that ‘init’ is in its own scope,
so if you run this code multiple times, each lambda returned has an
independent instance of ‘init’
Note: this only works as long as ‘init’ hasn’t already been seen
outside; if it has, all the lambdas will bind to the same ‘init’.
In ruby 1.9 there’s a way to force it to be local:
c = lambda { |;init| init = 200; …etc… }.call
But in that case the original code would be shorter:
c = lambda { |init| …etc… }.call(200)
This does the same in 1.9 (because block parameters are always local),
but in 1.8 it would still bind to the outside ‘init’ variable if one
exists.
then the ‘init’ inside the lambda is always a different ‘init’ to the
one outside. Any arguments which the lambda took would come before the
semicolon, but there are zero in this case.
For more info google “ruby block local variables”
IMO it ranks with ‘->’ as an ugly and unnecessary bit of 1.9 syntax, but
tastes vary. See what you think of:
Interesting. The default allocation behavior method is removed, and
Class.new is overridden, which uses custom allocation and forwards
arguments to #initialize - just like normal Class.new. Except Proc
doesn’t implement #initialize, so it bubbles up to Object#initialize.
Since Object#initialize just takes any number of arguments and ignores
them, providing an argument to Proc.new doesn’t raise.
It seems adding an empty, 0-arg #initialize method to Proc doesn’t
really lose anything here, and would catch mistakes. The only tiny
downside would be a miniscule slow down on explicit calls to Proc.new…
which I think most Rubyists can live with.
then the ‘init’ inside the lambda is always a different ‘init’ to the
one outside. Any arguments which the lambda took would come before the
semicolon, but there are zero in this case.
–output:–
prog.rb:2:in []': wrong number of arguments(0 for 1) (ArgumentError) from prog.rb:2:in’
As far as I can tell(because there is no documentation of Proc#call),
call() does not take a block, so calling it with a block (&inc) doesn’t
do anything.
Lots of helpful information in this thread. Thank you all for helping me.
You’re welcome!
Since I am new to functional programming I am slowly experimenting with what
I know and building slowly from there. In the same fashion that object
oriented is no more than the sum of it’s parts I am interested in learning
as much as I can about this paradigm.
In my understanding closures are not that essential for FP - at least
not for storing data. The Wikipedia article sums the core properties
of FP up pretty good IMHO:
“[…] functional programming is a programming paradigm that treats
computation as the evaluation of mathematical functions and avoids
state and mutable data. It emphasizes the application of functions,
in contrast to the imperative programming style, which emphasizes
changes in state.”
Also a frequently seen feature is first class and higher order
functions.
I understand the concept of closure. I imagine the best use for it would be
to build one and embed it in another and so one( correct me if I’m wrong)
That entirely depends on the use case. With currying that is
certainly what happens.
I have read and experimented with ruby’s Proc#curry method. There is a
tutorial online which explains haskell’s monads in ruby I plan on grokking
as well.
Are there any other facets of functional programming theory I should look at
to take advantage of?
One interesting thing that you can take away from FP is that some
things do get easier even in OO if you avoid side effects. For
example concurrency has less issues if objects are immutable. Of
course the downside is that you pay with GC overhead and frozen
instances in some way go against the paradigm of OO because one of the
major aspects of OO is encapsulation of state with functionality; and
this typically means mutable state. But on the other hand in
certain areas (e.g numbers and arithmetic) the concept of immutable
state is quite common in OO languages (Ruby and Java both have it).
If you really want to dive deeper into FP you should probably not use
Ruby but rather a first class FP language. I won’t recommend one
because others know that area far better than I do. There were some
recommendations recently:
Lots of helpful information in this thread. Thank you all for helping
me.
Since I am new to functional programming I am slowly experimenting with
what
I know and building slowly from there. In the same fashion that object
oriented is no more than the sum of it’s parts I am interested in
learning
as much as I can about this paradigm.
I understand the concept of closure. I imagine the best use for it would
be
to build one and embed it in another and so one( correct me if I’m
wrong)
I have read and experimented with ruby’s Proc#curry method. There is a
tutorial online which explains haskell’s monads in ruby I plan on
grokking
as well.
Are there any other facets of functional programming theory I should
look at
to take advantage of?
Lots of helpful information in this thread. Thank you all for helping me.
You’re welcome!
Since I am new to functional programming I am slowly experimenting with
what
I know and building slowly from there. In the same fashion that object
oriented is no more than the sum of it’s parts I am interested in
learning
changes in state."
I understand the concept of closure. I imagine the best use for it would
be
to build one and embed it in another and so one( correct me if I’m wrong)
That entirely depends on the use case. With currying that is
certainly what happens.
I have read and experimented with ruby’s Proc#curry method. There is a
tutorial online which explains haskell’s monads in ruby I plan on
grokking
as well.
Are there any other facets of functional programming theory I should look
at
state is quite common in OO languages (Ruby and Java both have it).
If you really want to dive deeper into FP you should probably not use
Ruby but rather a first class FP language. I won’t recommend one
because others know that area far better than I do. There were some
recommendations recently:
There is a
tutorial online which explains haskell’s monads in ruby I plan on
grokking
as well.
It’s my understanding that unless you have a Phd in abstract mathematics
and are one of the inventors of String theory(M-theory in particular),
you will never understand monads. Of course, maybe Timothy Leary would
have something to say about that.
Proc
doesn’t implement #initialize, so it bubbles up to Object#initialize.
Since Object#initialize just takes any number of arguments and ignores
them, providing an argument to Proc.new doesn’t raise.
So I think the question becomes: why does Object#initialize accept any
number of arguments in 1.9? It doesn’t in 1.8.
$ ruby -ve ‘Object.new(1,2,3)’
ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux]
-e:1:in initialize': wrong number of arguments (3 for 0) (ArgumentError) from -e:1:innew’
from -e:1
$ ruby192 -ve ‘Object.new(1,2,3)’
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
$
Proc
doesn’t implement #initialize, so it bubbles up to Object#initialize.
Since Object#initialize just takes any number of arguments and ignores
them, providing an argument to Proc.new doesn’t raise.
So I think the question becomes: why does Object#initialize accept any
number of arguments in 1.9? It doesn’t in 1.8.
It seems that it was a bit of an oversight when the change that
implemented that feature was approved. This is the redmine issue that
tracked the change: