Functional Ruby
-
Upload
tallguyhackett -
Category
Software
-
view
273 -
download
3
description
Transcript of Functional Ruby
![Page 1: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/1.jpg)
FUNCTIONAL RUBYNOT THE RUBY YOU'RE USED TO... OR IS IT?
Dave Hackett / @tallguy_hackett
![Page 2: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/2.jpg)
WHAT IS FUNCTIONAL PROGRAMMING?Funtional Programming (FP) is a style of building the structure
and elements of computer programs, that treats computation asthe evaluation of mathematical functions and avoids state andmutable data. It is a declarative programming paradigm, which
means programming is done with expressions. In functional code,the output value of a function depends only on the arguments
that are input to the function, so calling a function f twice with thesame value for an argument x will produce the same result f(x)
both times. —Wikipedia
![Page 3: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/3.jpg)
TL;DRFuntional Programming is like math equations.
![Page 5: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/5.jpg)
IT'S NOT THAT BADThere's just a few things to remember.
![Page 6: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/6.jpg)
IMMUTABILITYValues are values are values. They don't change.
![Page 7: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/7.jpg)
PERSISTENCEValues aren't forgotten, overwritten, or removed over time.
![Page 8: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/8.jpg)
EXPRESSIONSFP is declarative not imperative.
![Page 9: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/9.jpg)
EXAMPLES IN CLOJURE(doseq [e [1 2 3]] (println e))
; => 1; => 2; => 3
(def orig [1 2 3])
(def added (map inc orig))
; => orig == [1 2 3]; => added == [2 3 4]
(reduce + [1 2 3])
; => 6
![Page 10: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/10.jpg)
DOES RUBY HAVE SOME OF THIS ALREADY?Yes... kind of... sometimes.
![Page 11: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/11.jpg)
CHANCES ARE YOU'RE ALREADY APPLYINGSOME FP-ISH TECHNIQUES
ENUMERABLE MODULE
![Page 12: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/12.jpg)
FP IN RUBY![1, 2, 3].each do |e| puts eend
# => 1# => 2# => 3
orig = [1, 2, 3]added = orig.map do |e| e + 1end
# => orig == [1, 2, 3]# => added == [2, 3, 4]
[1, 2, 3].reduce(:+)
# => 6
![Page 13: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/13.jpg)
OOPS!orig = [1, 2, 3]added = orig.map! do |e| e + 1end
# => orig == [2, 3, 4]# => added == [2, 3, 4]
greeting = "hello world"
def end_with_a_bang(phrase) phrase << "!"end
puts end_with_a_bang(greeting)# => hello world!puts greeting# => hello world!puts end_with_a_bang(greeting)# => hello world!!puts greeting# => hello world!!
![Page 14: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/14.jpg)
DOES RUBY HAVE IMMUTABLE VALUES?
![Page 15: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/15.jpg)
WHY DOESN'T RUBY SUPPORT ++ OR --?
![Page 16: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/16.jpg)
NUMBERS ARE IMMUTABLEEVERYTHING IS MESSAGE PASSING IN RUBY
explanation
![Page 17: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/17.jpg)
SYMBOLS ARE IMMUTABLE
![Page 18: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/18.jpg)
RANGES ARE IMMUTABLE
![Page 19: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/19.jpg)
DATE AND TIME ARE IMMUTABLE
![Page 20: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/20.jpg)
VALUES GEMrequire 'values'
Point = Values.new(:x, :y)p = Point.new(1, 2)p.x = 2# => NoMethodError
Values
![Page 21: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/21.jpg)
WHAT ABOUT DATA STRUCTURES?
![Page 22: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/22.jpg)
BUILT IN... EPIC FAIL!
![Page 23: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/23.jpg)
HAMSTER GEMrequire 'hamster'
v = Hamster.vector(1, 2, 3)v1 = v.add(4)# => v == [1, 2, 3]# => v1 == [1, 2, 3, 4]
v2 = v << 5# => v == [1, 2, 3]# => v2 == [1, 2, 3, 5]
v3 = v.set 0, 5# => v == [1, 2, 3]# => v3 == [5, 2, 3]
hamster
![Page 24: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/24.jpg)
EXPRESSION COMPOSITION
![Page 25: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/25.jpg)
LIKE IN MATH...FP is composed of a bunch of functions. Complex functions are
functions composed of other functions.
![Page 26: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/26.jpg)
MATHSo for instance is an example of function
composition. Another way to write the same thing is
f(g(h(x)))
(f ∘ g ∘ h)(x)The story is that the result of is fed as input to whose
result is then fed into for the final result
h(x) g(x)f(x)
![Page 27: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/27.jpg)
CODESo in code this [[...],[...],...][3].drop(2).first
There are 3 method calls (functions)
[3] occurs first
.drop(2) occurs next
.first occurs last
It makes sense to us that results are passed down the messagechain
![Page 29: Functional Ruby](https://reader033.fdocuments.in/reader033/viewer/2022051513/54732b16b4af9fb40a8b52e0/html5/thumbnails/29.jpg)
THE ENDBY DAVE HACKETT