Functional RubyKerry Buckley, 10 November 2011
λ
Lambda calculusLambda expressions are composed of     variables v1, v2, ..., vn, ...     the abstraction symbols λ and .  ...
Clear?
Clear?Me neither.
EnumerableThe Enumerable mixin provides collectionclasses with several traversal and searchingmethods, and with the abilit...
Enumerableall?                 find         minmax_byany?                 find_all     none?chunk                find_inde...
EnumerableIf Enumerable#max, min, or sort is used,the objects in the collection must alsoimplement a meaningful <=> operat...
Better iterationdef crappy_method  results = []  @collection.each do |thing|    results << thing.property  end  resultsend...
each or map?collection.each do |a|  do_something_with_side_effects(a)endanother_collection = collection.map {|a| transform...
do…end or {…}?• do…end for multiple lines• {…} when chaining• do…end when there are side-effects• do…end for control flow
Filteringdef crappy_filter  matches = []  @collection.each do |item|    matches << item if item.something?  end  matchesen...
Proc objectspalindromic = proc {|s| s == s.reverse }words = File.read("/usr/share/dict/words").splitwords.select &palindro...
injectdef crappy_sum  sum = 0  @collection.each do |number|    sum += number  end  sumenddef nicer_sum  @collection.inject...
Chaining functionsdef crappy_sum_of_odd_numbers  sum = 0  @collection.each do |number|    sum += number if number.odd?  en...
Synonyms• collect = map• inject = reduce• collect_concat = flat_map• find = detect• find_all = select• to_a = entries• includ...
Upcoming SlideShare
Loading in...5
×

Functional ruby

959

Published on

Quickly cobbled-together slides for a short functional ruby talk at work (inspired by @threedaymonk's "Don't fear the lambda" at Ru3ymanor).

Published in: Technology, Education
1 Comment
1 Like
Statistics
Notes
  • Good stuff. I am currently a lot into functional ruby and have been looking into monads as well which help working in a functional way, especially when it comes to side-effect free exception handling.
    I have wrapped up my monad stuff into a gem https://github.com/pzol/monadic
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
959
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Functional ruby

    1. 1. Functional RubyKerry Buckley, 10 November 2011
    2. 2. λ
    3. 3. Lambda calculusLambda expressions are composed of variables v1, v2, ..., vn, ... the abstraction symbols λ and . parentheses ( )The set of lambda expressions, Λ, can be defined recursively: 1. If x is a variable, then x ∈ Λ 2. If x is a variable and M ∈ Λ, then (λx.M) ∈ Λ 3. If M, N ∈ Λ, then (M N) ∈ ΛInstances of rule 2 are known as abstractions and instances of rule 3 areknown as applications.
    4. 4. Clear?
    5. 5. Clear?Me neither.
    6. 6. EnumerableThe Enumerable mixin provides collectionclasses with several traversal and searchingmethods, and with the ability to sort. Theclass must provide a method each, whichyields successive members of the collection.
    7. 7. Enumerableall? find minmax_byany? find_all none?chunk find_index one?collect first partitioncollect_concat flat_map reducecount grep rejectcycle group_by reverse_eachdetect include? selectdrop inject slice_beforedrop_while map sorteach_cons max sort_byeach_entry max_by takeeach_slice member? take_whileeach_with_index min to_aeach_with_object min_by zipentries minmax
    8. 8. EnumerableIf Enumerable#max, min, or sort is used,the objects in the collection must alsoimplement a meaningful <=> operator, asthese methods rely on an ordering betweenmembers of the collection.
    9. 9. Better iterationdef crappy_method results = [] @collection.each do |thing| results << thing.property end resultsenddef nicer_method @collection.map {|a| a.property }enddef more_concise_method @collection.map &:propertyend
    10. 10. each or map?collection.each do |a| do_something_with_side_effects(a)endanother_collection = collection.map {|a| transform(a) }
    11. 11. do…end or {…}?• do…end for multiple lines• {…} when chaining• do…end when there are side-effects• do…end for control flow
    12. 12. Filteringdef crappy_filter matches = [] @collection.each do |item| matches << item if item.something? end matchesenddef nicer_sum @collection.select {|a| a.something? }enddef more_concise_sum @collection.select &:something?end
    13. 13. Proc objectspalindromic = proc {|s| s == s.reverse }words = File.read("/usr/share/dict/words").splitwords.select &palindromic #=> ["A", "a", "aa", "aba", ... "yoy", "Z", "z"]
    14. 14. injectdef crappy_sum sum = 0 @collection.each do |number| sum += number end sumenddef nicer_sum @collection.inject(0) {|a, n| a + n }enddef more_concise_sum @collection.inject &:+end
    15. 15. Chaining functionsdef crappy_sum_of_odd_numbers sum = 0 @collection.each do |number| sum += number if number.odd? end sumenddef sum_of_odd_numbers_using_inject @collection.inject(0) {|a, n| n.odd? ? a + n : a }enddef sum_of_odd_numbers_using_select_and_inject @collection.select(&:odd?).inject(&:+)end
    16. 16. Synonyms• collect = map• inject = reduce• collect_concat = flat_map• find = detect• find_all = select• to_a = entries• include? = member?
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×