siggoberto / salchicha1

Automatically exported from code.google.com/p/tokland
0 stars 0 forks source link

ruby idioms map_detect method #29

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

break with a value is fast, too

(1..100000000000000000000).detect do |x|  
    sq = x**2
    break sq if sq > 10 
end

 => 16

Of course map_detect makes the code easier to follow.

Love the article!  Please show more good examples.  Thank you!

Original issue reported on code.google.com by bai.xia...@gmail.com on 23 Aug 2013 at 6:12

GoogleCodeExporter commented 9 years ago
@bai. Indeed, that would work. However, I prefer functional approaches (with 
expressions), and control statements as "break" are a no-no in functional 
programming. If you liked the idioms you may also enjoy 
https://code.google.com/p/tokland/wiki/RubyFunctionalProgramming.

Thanks for commenting.

Original comment by tokland on 23 Aug 2013 at 1:07

GoogleCodeExporter commented 9 years ago
I contributed to the Chinese translation of Ruby Functional Programming at 
github.   It's _the_ article I go to currently for fp in ruby.   I see you 
update the RubyFunctionalProgramming frequently so the translation on github 
need to catch up.

I would like to translate this article, too.  It will happen on github since 
contributions are easier to manage.

The article on ADT and pattern matching excites me!  But currently it's hard 
for me to follow.  They could certainly be the next hits on Ruby FP.

Original comment by bai.xia...@gmail.com on 26 Aug 2013 at 2:17

GoogleCodeExporter commented 9 years ago
> I contributed to the Chinese translation of Ruby Functional Programming at 
github

I see an active fork by user "404pnf", is that you? 

> I would like to translate this article, too.

Great!

> The article on ADT and pattern matching excites me

Glad to hear that. Probably neither of them would be considered "idiomatic 
Ruby", but it's nice to explore other ways to do things. 

Original comment by tokland on 26 Aug 2013 at 9:33

GoogleCodeExporter commented 9 years ago
Yes, I am 404pnf.

I agree ADT is not idiomatic, nor is the pattern matching.  They are too
verbose compare with its haskell and/or lisp counterparts.  But it's a
great start.  At least I see how it's down in Ruby.

Original comment by bai.xia...@gmail.com on 29 Aug 2013 at 12:52