0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Functional ruby

927

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).

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
• Full Name
Comment goes here.

Are you sure you want to Yes No
• 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.

Are you sure you want to  Yes  No
Views
Total Views
927
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
9
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
• ### 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 deﬁned 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 ﬂow
• 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 = ﬂat_map• ﬁnd = detect• ﬁnd_all = select• to_a = entries• include? = member?