Functional ruby
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Functional ruby

on

  • 1,270 views

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

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

Statistics

Views

Total Views
1,270
Views on SlideShare
1,267
Embed Views
3

Actions

Likes
1
Downloads
8
Comments
1

1 Embed 3

http://www.hanrss.com 3

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • 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
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Functional ruby Presentation Transcript

  • 1. Functional RubyKerry Buckley, 10 November 2011
  • 2. λ
  • 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. Clear?
  • 5. Clear?Me neither.
  • 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. 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. 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. 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. each or map?collection.each do |a| do_something_with_side_effects(a)endanother_collection = collection.map {|a| transform(a) }
  • 11. do…end or {…}?• do…end for multiple lines• {…} when chaining• do…end when there are side-effects• do…end for control flow
  • 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. 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. 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. 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. Synonyms• collect = map• inject = reduce• collect_concat = flat_map• find = detect• find_all = select• to_a = entries• include? = member?