Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3142 > unrolled thread
| Started by | Stu <stu@rubyprogrammer.net> |
|---|---|
| First post | 2011-04-19 01:07 -0500 |
| Last post | 2011-04-20 12:44 -0500 |
| Articles | 20 on this page of 32 — 8 participants |
Back to article view | Back to comp.lang.ruby
anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-19 01:07 -0500
Re: anonymous closures with Proc,new, lambda and -> Robert Klemme <shortcutter@googlemail.com> - 2011-04-19 02:30 -0500
Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-19 03:05 -0500
Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-19 03:15 -0500
Re: anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-19 04:57 -0500
Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-19 07:24 -0500
Re: anonymous closures with Proc,new, lambda and -> Adam Prescott <adam@aprescott.com> - 2011-04-19 14:55 -0500
Re: anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-19 16:43 -0500
Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-19 18:48 -0500
Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-19 21:28 -0500
Re: anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-20 01:03 -0500
Re: anonymous closures with Proc,new, lambda and -> Robert Klemme <shortcutter@googlemail.com> - 2011-04-20 02:23 -0500
Re: anonymous closures with Proc,new, lambda and -> Steve Klabnik <steve@steveklabnik.com> - 2011-04-20 05:55 -0500
Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-20 12:32 -0500
Re: anonymous closures with Proc,new, lambda and -> Steve Klabnik <steve@steveklabnik.com> - 2011-04-20 12:59 -0500
Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-20 15:40 -0500
Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-20 19:51 -0500
Re: anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-21 01:31 -0500
Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-21 04:34 -0500
Re: anonymous closures with Proc,new, lambda and -> Robert Klemme <shortcutter@googlemail.com> - 2011-04-21 06:13 -0500
Re: anonymous closures with Proc,new, lambda and -> Robert Klemme <shortcutter@googlemail.com> - 2011-04-21 09:20 -0500
Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-21 11:56 -0500
Re: anonymous closures with Proc,new, lambda and -> Steve Klabnik <steve@steveklabnik.com> - 2011-04-21 12:06 -0500
Re: anonymous closures with Proc,new, lambda and -> Robert Klemme <shortcutter@googlemail.com> - 2011-04-27 06:49 -0500
Re: anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-27 13:33 -0500
Re: anonymous closures with Proc,new, lambda and -> Steve Klabnik <steve@steveklabnik.com> - 2011-04-27 17:06 -0500
Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-19 13:02 -0500
Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-19 13:19 -0500
Re: anonymous closures with Proc,new, lambda and -> Steve Klabnik <steve@steveklabnik.com> - 2011-04-19 13:26 -0500
Re: anonymous closures with Proc,new, lambda and -> Michael Edgar <adgar@carboni.ca> - 2011-04-19 13:51 -0500
Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-20 10:18 -0500
Re: anonymous closures with Proc,new, lambda and -> Jeremy Bopp <jeremy@bopp.net> - 2011-04-20 12:44 -0500
Page 1 of 2 [1] 2 Next page →
| From | Stu <stu@rubyprogrammer.net> |
|---|---|
| Date | 2011-04-19 01:07 -0500 |
| Subject | anonymous closures with Proc,new, lambda and -> |
| Message-ID | <BANLkTimdp71nhRkF5GiOxsQ-gtP4wkwE9w@mail.gmail.com> |
[Note: parts of this message were removed to make it a legal post.]
I am new to the study of functional paradigm. If this question is academic
please bear with me.
How would I make this counter with lambda or -> without deferring to a named
generator method or sigil var?
my ruby version:
% ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30907)
my code examples for explanation:
% irb
>> closed = Proc.new( over=0){over+=1}
=> #<Proc:0x00000001300e98@(irb):10>
>> 4.times { puts closed[] }
1
2
3
4
=> 4
error out on lambda
>> closed = lambda( over=0){over+=1}
ArgumentError: wrong number of arguments(1 for 0)
..
and -> doesn't error but give undesired results:
>> closed = ->( over=0){over+=1}
=> #<Proc:0x00000001234d20@(irb):16 (lambda)>
>> 4.times { puts closed[] }
1
1
1
1
=> 4
in a named method I relize I can use a named method with argument being
bound while returning with either -> or lamda. for example:
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.
[toc] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-04-19 02:30 -0500 |
| Message-ID | <BANLkTikgQzeRcjei2P13oBsyqbsEf1+gNA@mail.gmail.com> |
| In reply to | #3142 |
On Tue, Apr 19, 2011 at 8:07 AM, Stu <stu@rubyprogrammer.net> wrote:
> I am new to the study of functional paradigm. If this question is academic
> please bear with me.
>
> How would I make this counter with lambda or -> without deferring to a named
> generator method or sigil var?
>
> my ruby version:
>
> % ruby -v
> ruby 1.9.2p180 (2011-02-18 revision 30907)
>
> my code examples for explanation:
>
> % irb
>>> closed = Proc.new( over=0){over+=1}
> => #<Proc:0x00000001300e98@(irb):10>
> >> 4.times { puts closed[] }
> 1
> 2
> 3
> 4
> => 4
That might not work as you expect:
09:22:04 ~$ ruby19 -e 'closed=Proc.new(over=0){over+=1};2.times{p
closed[]};p over;over=-1;2.times{p closed[]}'
1
2
2
0
1
09:22:19 ~$
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.)
> error out on lambda
>
>>> closed = lambda( over=0){over+=1}
> ArgumentError: wrong number of arguments(1 for 0)
> ...
>
> and -> doesn't error but give undesired results:
>
>>> closed = ->( over=0){over+=1}
> => #<Proc:0x00000001234d20@(irb):16 (lambda)>
>
>>> 4.times { puts closed[] }
> 1
> 1
> 1
> 1
> => 4
>
> in a named method I relize I can use a named method with argument being
> bound while returning with either -> or lamda. for example:
>
> 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:
09:28:05 ~$ ruby19 -e 'def m(x=0)lambda {x+=1}end;f=m;2.times{p f[]}'
1
2
09:28:14 ~$ ruby19 -e 'm=lambda{|x=0| lambda {x+=1}};f=m[];2.times{p f[]}'
1
2
09:28:19 ~$
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-04-19 03:05 -0500 |
| Message-ID | <ad275fb321cc69d4cc8b2bf537479c2c@ruby-forum.com> |
| In reply to | #3142 |
Stu wrote in post #993687:
> I am new to the study of functional paradigm. If this question is
> academic
> please bear with me.
>
> How would I make this counter with lambda or -> without deferring to a
> named
> generator method or sigil var?
Starting with the "named generator method":
def make_counter(init)
lambda { init += 1 }
end
Calling that method creates a new scope, and binds the value of 'init'
within that scope.
Now, you can do the same without def, by wrapping in another lambda:
counter_maker = lambda { |init| lambda { init += 1 } }
c = counter_maker.call(100)
c.call # 101
c.call # 102
And you can do the same without explicitly binding 'counter_maker'
either:
c = lambda { |init| lambda { init += 1 } }.call(200)
c.call # 201
c.call # 202
Is that what you were looking for?
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-04-19 03:15 -0500 |
| Message-ID | <52198c724199b47f9d59c43592238d37@ruby-forum.com> |
| In reply to | #3149 |
Brian Candler wrote in post #993704:
> And you can do the same without explicitly binding 'counter_maker'
> either:
>
> c = lambda { |init| lambda { init += 1 } }.call(200)
> c.call # 201
> c.call # 202
Which of course simplifies to:
c = lambda { init = 200; lambda { init += 1 } }.call
c.call # 201
c.call # 202
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.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Stu <stu@rubyprogrammer.net> |
|---|---|
| Date | 2011-04-19 04:57 -0500 |
| Message-ID | <BANLkTimFDxtX6F_xRxputkRe0sT-=Ngq=w@mail.gmail.com> |
| In reply to | #3150 |
[Note: parts of this message were removed to make it a legal post.]
On Tue, Apr 19, 2011 at 3:15 AM, Brian Candler <b.candler@pobox.com> wrote:
> Brian Candler wrote in post #993704:
>
> In ruby 1.9 there's a way to force it to be local:
>
> c = lambda { |;init| init = 200; ...etc... }.call
>
>
>
What does the semicolon tell the interpreter here? init now exists outside
the closure scope? in this case main?
[toc] | [prev] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-04-19 07:24 -0500 |
| Message-ID | <83313b7dacbee50084b749ad61280978@ruby-forum.com> |
| In reply to | #3154 |
Stu wrote in post #993715:
>> c = lambda { |;init| init = 200; ...etc... }.call
>>
>>
>>
> What does the semicolon tell the interpreter here?
It's a block-local variable. You can think of it as a block argument
which is never passed by the caller, so always gets nil. Compare:
c = lambda { |init| ... } # init is local, value is passed
c = lambda { |init;x,y| ... } # init is local, value is passed;
# x and y are local, no value passed
So given:
init = 123
c = lambda { |;init| init = 456; ... }
puts init # 123
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:
c = ->(;init) { init=100; ->{init += 1} }.call
Regards,
Brian.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Adam Prescott <adam@aprescott.com> |
|---|---|
| Date | 2011-04-19 14:55 -0500 |
| Message-ID | <BANLkTinfZ=oWXK=mgj_LVVxxwtk0MNnWZg@mail.gmail.com> |
| In reply to | #3160 |
[Note: parts of this message were removed to make it a legal post.] On Tue, Apr 19, 2011 at 1:24 PM, Brian Candler <b.candler@pobox.com> wrote: > 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" > A nice example is given on Read Ruby 1.9: http://ruby.runpaint.org/closures#block-local-variables Usually the section I go to when I need a reminder.
[toc] | [prev] | [next] | [standalone]
| From | Stu <stu@rubyprogrammer.net> |
|---|---|
| Date | 2011-04-19 16:43 -0500 |
| Message-ID | <BANLkTinXeBFvURT=1_2F89tGjxmjC05TJA@mail.gmail.com> |
| In reply to | #3150 |
[Note: parts of this message were removed to make it a legal post.]
how would i break it down to two functions?
example:
>> inc = lambda {|n|n+=1}
=> #<Proc:0x00000100926318@(irb):23 (lambda)>
>> counter = lambda {n=1; n=inc.call(n)}.call(&inc)
=> 2
>> counter[]
ArgumentError: wrong number of arguments(0 for 1)
On Tue, Apr 19, 2011 at 3:15 AM, Brian Candler <b.candler@pobox.com> wrote:
> Brian Candler wrote in post #993704:
> > And you can do the same without explicitly binding 'counter_maker'
> > either:
> >
> > c = lambda { |init| lambda { init += 1 } }.call(200)
> > c.call # 201
> > c.call # 202
>
> Which of course simplifies to:
>
> c = lambda { init = 200; lambda { init += 1 } }.call
> c.call # 201
> c.call # 202
>
>
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-19 18:48 -0500 |
| Message-ID | <ec31e3ab156f1477e940bfd36c9697b6@ruby-forum.com> |
| In reply to | #3192 |
Stu wrote in post #993854:
>
inc = lambda {|n| n+=1}
counter = lambda do |a_proc|
n = 1
n = a_proc.call(n)
end.call(inc)
puts counter
--output:--
2
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-19 21:28 -0500 |
| Message-ID | <26d555101380a80dba2c4c1f0f82ea08@ruby-forum.com> |
| In reply to | #3192 |
Stu wrote in post #993854:
> how would i break it down to two functions?
Is this what you are looking for:
inc = lambda do
n = 1
lambda{n += 1}
end.call
counter = lambda{inc.call}
puts counter[]
puts counter[]
--output:--
2
3
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Stu <stu@rubyprogrammer.net> |
|---|---|
| Date | 2011-04-20 01:03 -0500 |
| Message-ID | <BANLkTikSAgPorJ6b3Op3sjTW4rKu1B_3rA@mail.gmail.com> |
| In reply to | #3203 |
[Note: parts of this message were removed to make it a legal post.]
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?
On Tue, Apr 19, 2011 at 9:28 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> Stu wrote in post #993854:
> > how would i break it down to two functions?
>
>
> Is this what you are looking for:
>
> inc = lambda do
> n = 1
> lambda{n += 1}
> end.call
>
> counter = lambda{inc.call}
> puts counter[]
> puts counter[]
>
> --output:--
> 2
> 3
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-04-20 02:23 -0500 |
| Message-ID | <BANLkTimhcUt1ZkPEYFOyb=2SVOogzV0HKA@mail.gmail.com> |
| In reply to | #3209 |
On Wed, Apr 20, 2011 at 8:03 AM, Stu <stu@rubyprogrammer.net> wrote: > 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." http://en.wikipedia.org/wiki/Functional_programming Also a frequently seen feature is first class and higher order functions. http://en.wikipedia.org/wiki/Higher-order_function http://en.wikipedia.org/wiki/First-class_function > 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: http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/380949?380902-381876 You can also find a pretty neat list here: http://www.cs.nott.ac.uk/~gmh/faq.html Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | Steve Klabnik <steve@steveklabnik.com> |
|---|---|
| Date | 2011-04-20 05:55 -0500 |
| Message-ID | <BANLkTink2nyOuggP6OunqrrwYU8CU29Dmw@mail.gmail.com> |
| In reply to | #3215 |
[Note: parts of this message were removed to make it a legal post.] I would highly reccommend Haskell, and http://learnyouahaskell.com/ . On Apr 20, 2011 3:24 AM, "Robert Klemme" <shortcutter@googlemail.com> wrote: > On Wed, Apr 20, 2011 at 8:03 AM, Stu <stu@rubyprogrammer.net> wrote: >> 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." > > http://en.wikipedia.org/wiki/Functional_programming > > Also a frequently seen feature is first class and higher order functions. > http://en.wikipedia.org/wiki/Higher-order_function > http://en.wikipedia.org/wiki/First-class_function > >> 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: > > http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/380949?380902-381876 > > You can also find a pretty neat list here: > http://www.cs.nott.ac.uk/~gmh/faq.html > > Kind regards > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ >
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-20 12:32 -0500 |
| Message-ID | <b93b1321dbc30760971aef51218b5d99@ruby-forum.com> |
| In reply to | #3209 |
Stu wrote in post #993922: > 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 mathematical theory and are one of the inventors of String theory(M theory in particular), you will never understand monads. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Steve Klabnik <steve@steveklabnik.com> |
|---|---|
| Date | 2011-04-20 12:59 -0500 |
| Message-ID | <BANLkTi=Oo0uSoJNm9Nu9Mv0WUmd3XcDLKA@mail.gmail.com> |
| In reply to | #3258 |
[Note: parts of this message were removed to make it a legal post.] Naw, monads are actually really, really easy. They're just poorly explained... one day, they just clicked for me, and I can't believe it took me all this time to learn them.
[toc] | [prev] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-04-20 15:40 -0500 |
| Message-ID | <e6ab54a1c6ff7e8e3835d9ce52011af6@ruby-forum.com> |
| In reply to | #3262 |
Steve Klabnik wrote in post #994098: > Naw, monads are actually really, really easy. They're just poorly > explained... one day, they just clicked for me, and I can't believe it > took > me all this time to learn them. According to this doc, anybody could have invented monads: http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-20 19:51 -0500 |
| Message-ID | <2f016f7e1eb6e5c88c2ad0a5d43683b0@ruby-forum.com> |
| In reply to | #3268 |
Brian Candler wrote in post #994123: > Steve Klabnik wrote in post #994098: >> Naw, monads are actually really, really easy. They're just poorly >> explained... one day, they just clicked for me, and I can't believe it >> took >> me all this time to learn them. > > According to this doc, anybody could have invented monads: > > http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html I've looked at that before. I dropped out at the second diagram. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Stu <stu@rubyprogrammer.net> |
|---|---|
| Date | 2011-04-21 01:31 -0500 |
| Message-ID | <BANLkTi=WYz=aA03qAmdrZbwqpGS_eb9nqA@mail.gmail.com> |
| In reply to | #3282 |
My interest in learning functional programming( or at least the foundation of) is because I haven't done it before( though I flirted with scheme a couple years ago). I don't really know what monads are I was just compiling a list of "things I should look into" for future reference to take the autodidactic approach to learning functional paradigms. When I first to the plunge to learn object oriented paradigm from a procedural background I know I made it harder than it is. The first thing I did to tackle the learning curve was to break down the four concept layers encapsulation, inheritance, polymorphism and abstraction. It wasn't until I began creating composite objects, sending messages and creating observer objects until it finally hit me and all seemed so simple that I didn't understand why it was so hard in the first place. As a side note the Ruby Language shows just how well an object oriented program is. Simply the organisation behind every object in it's own inheritance hierarchy all the way down to BasicObject, polymorphic design of variants like lambda and Proc.new. Even the redefinition of operators to deal with primitive and abstract data types in their own domain( i.e. 3 * 3, [42,3] - [3,45,54], "lamda " * 3)... This is inspirational for modelling my own objects with this type of object framework if I may be so À Propos. My background consists of posix shell scripting( ash, awk) and some c. My interest in ruby has been to add a general purpose programming language to my toolbox. After I get to a proficiency apex with ruby I have in the learning queue to go back and refresh my c knowledge and take the time to truly grok the fundamental data structures and algorithms. Once that goal is hit I had planned to move to c++. C++0x will be incorporating lambda and closures. I imagine c++ will be a life long journey as it's a multi-paradigm programming language as well. (is it 4 or 5 languages in one now?) As Robert pointed out Ruby may not be the best language to learn functional programming concepts with. This speaks volumes to me as I am a huge proponent to the attitude to use the right tool for the right job. If I am not mistaken most of these languages are dialects of Lisp( correct me if I am wrong). I am not in a position to learn to earn nor am I a mathematician. My interest is to simply better myself as a programmer. Those of you who have used Haskell monads does one really need to have invented string theory to understand it or is it like how I explained the object oriented learning curve where one might over-think it's simplicity therefor overcomplicating their comprehension of the paradigm? Also is there a canonical or pedagogical tutorial for me to start with? Hello, World! =) Which language would be a decent new users language which would fit well into my unix toolkit above as well as threading the knowledge back into ruby's hybrid programming? (scheme, haskell, clojure common lisp) Thanks in advance ~Stu
[toc] | [prev] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-04-21 04:34 -0500 |
| Message-ID | <47f9d57b75fad86a1b76cb96e69a48b6@ruby-forum.com> |
| In reply to | #3291 |
Stu wrote in post #994213: > Which language would be a decent new users language which would fit > well into my unix toolkit above as well as threading the knowledge > back into ruby's hybrid programming? If you are coming to functional programming from an imperative background, I would recommend Erlang as a sound starting point. It's a highly practical language for real-world applications, albeit with a frustrating syntax. In Erlang, functions can and do have side effects, in particular the sending of messages between processes. However variables can be bound only once, and data structures are immutable. There is no "A = A + 1" :-) So each individual process is functional, and you start getting your head around functional algorithms "in the small", such as using recursion for looping and traversing data structures. But you don't have to worry about building your entire application in a purely functional form. The immutability of data means concurrency is easy (no mutexes!) and you have zero-copy passing of data structures, at least between processes on the same node. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-04-21 06:13 -0500 |
| Message-ID | <BANLkTi=e4KSn1ObjPC_AD=YDxKWWu_hCNw@mail.gmail.com> |
| In reply to | #3291 |
On Thu, Apr 21, 2011 at 8:31 AM, Stu <stu@rubyprogrammer.net> wrote: > Also is there a canonical or pedagogical tutorial for me to start > with? Hello, World! =) Well, the typical program which prints "Hello World!" is difficult for _purely_ functional languages since IO is a side effect. Of course, a language which could not interface with the outside world would be deemed to be purely academical. Maybe in the functional world the equivalent example should be "2 * 4"? :-) Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.ruby
csiph-web