Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Functional Programming for Rubyst's
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Oscar Rendón
September 09, 2015
Programming
0
98
Functional Programming for Rubyst's
Oscar Rendón
September 09, 2015
Tweet
Share
More Decks by Oscar Rendón
See All by Oscar Rendón
ActiveRecord Internals - EmpanadaRecord - Medellin.rb
orendon
0
170
Domain Specific Languages in Ruby - Medellin.rb
orendon
0
110
Other Decks in Programming
See All in Programming
AI時代のキャリアプラン「技術の引力」からの脱出と「問い」へのいざない / tech-gravity
minodriven
20
7k
フロントエンド開発の勘所 -複数事業を経験して見えた判断軸の違い-
heimusu
7
2.8k
Apache Iceberg V3 and migration to V3
tomtanaka
0
150
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
130
高速開発のためのコード整理術
sutetotanuki
1
390
CSC307 Lecture 09
javiergs
PRO
1
830
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
1k
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
110
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
170
組織で育むオブザーバビリティ
ryota_hnk
0
170
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
180
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
230
Featured
See All Featured
Everyday Curiosity
cassininazir
0
130
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
Test your architecture with Archunit
thirion
1
2.1k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
130
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
51
The Cult of Friendly URLs
andyhume
79
6.8k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
64
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.6k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.3k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
350
Measuring & Analyzing Core Web Vitals
bluesmoon
9
750
Transcript
Functional Programming for Rubyst’s by @orendon @RubyMedellin - http://medellinrb.org
- Concepts - Immutability/ Side effects - Referential Transparency -
Concurrency/Parallelism - High Order Functions - Currying - Tail Call/Recursion - more... Agenda
History - Lambda Calculus, 30’s - LISP, 50’s - ML’s,
70’s - Haskell, 80’s - Elixir, Erlang, Clojure, Scala, Elm, Idris, etc... https://en.wikipedia.org/wiki/Functional_programming
Functional Programming “Functional programming is a programming paradigm that treats
computation as the evaluation of mathematical functions and avoids state and mutable data” https://en.wikipedia.org/wiki/Functional_programming
Functional Programming - Mathematical functions - Avoids state - Immutable
data
Functional Programming - Cleaner Code - Memoization - Parallelization -
Modularity - Easier debugging
Mutable Data x = x + 1
Mutable Data x = x + 1 x - x
= 1
Mutable Data x = x + 1 x - x
= 1 0 = 1
Immutable Data x = 1 y = x + 1
x = y -1
Referential Transparency y = f(x) - Mathematical Functions - Depends
on inputs - Memoization - Idempotence
State result = func_a(x) + func_b(y) - func_c(z) - No
State? - Hidden State - Explicit State
Concurrency / Parallelism y = f(x) - No side effects
- No shared data
Concurrency / Parallelism Just Simpler: - No locks - No
semaphores - No race conditions - No dead-locks
High Order Functions def apply_math(fn, a, b) a.send(fn, b) end
apply_math(:+, 1, 2)
DEMO
Currying apply_math = lambda do |fn, a, b| a.send(fn, b)
end add = apply_math.curry.(:+) subtract = apply_math.curry.(:-) multiply = apply_math.curry.(:*) divide = apply_math.curry.(:/) add.(1, 2)
DEMO
Recursion - Loops - Recursion - Call Stack - Tail
Call - Tail Recursion - Tail Call Optimization (TCO)
Factorial def fact(n) factorial = 1 while n > 1
factorial *= n n -= 1 end factorial end - How vs What - Holding State - Readable?
Factorial def fact(n) return 1 if (0..1).include?(n) n * fact(n-1)
end - Recursive - Call Stack Error - Not Tail Recursive
Factorial def fact(n, acc=1) return acc if n <= 1
fact(n-1, n*acc) end - Recursive - Call Stack Error - Tail Recursive
DEMO
TCO in Ruby RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true, trace_instruction: false
} def fact(n, acc) return acc if n <= 1 fact(n-1, n*acc) end
Lazyness - Eager Evaluation - Lazy Evaluation - Enumerable -
Enumerable::Lazy (Ruby 2)
Benchmark require 'benchmark' Benchmark.bm do |x| x.report("eager") do (1..100_000_000).map{ |x|
x*2 }.take(10) end x.report("lazy") do (1..100_000_000).lazy.map{ |x| x*2 }.take(10).to_a end end
DEMO
Thanks Empanada time!