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
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
TestingOsaka6_Ozono
o3
0
240
ZJIT: The Ruby 4 JIT Compiler / Ruby Release 30th Anniversary Party
k0kubun
1
300
The Art of Re-Architecture - Droidcon India 2025
siddroid
0
150
AtCoder Conference 2025「LLM時代のAHC」
imjk
2
620
2年のAppleウォレットパス開発の振り返り
muno92
PRO
0
130
ゲームの物理 剛体編
fadis
0
390
組み合わせ爆発にのまれない - 責務分割 x テスト
halhorn
1
170
実はマルチモーダルだった。ブラウザの組み込みAI🧠でWebの未来を感じてみよう #jsfes #gemini
n0bisuke2
3
1.4k
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
420
AtCoder Conference 2025
shindannin
0
850
Tinkerbellから学ぶ、Podで DHCPをリッスンする手法
tomokon
0
150
ELYZA_Findy AI Engineering Summit登壇資料_AIコーディング時代に「ちゃんと」やること_toB LLMプロダクト開発舞台裏_20251216
elyza
2
860
Featured
See All Featured
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
0
81
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
180
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
530
We Are The Robots
honzajavorek
0
130
How to Ace a Technical Interview
jacobian
281
24k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.5k
How to Talk to Developers About Accessibility
jct
1
93
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
10
770
Measuring & Analyzing Core Web Vitals
bluesmoon
9
720
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!