Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
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
120
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Functional Programming for Rubyst's
Oscar Rendón
September 09, 2015
More Decks by Oscar Rendón
See All by Oscar Rendón
ActiveRecord Internals - EmpanadaRecord - Medellin.rb
orendon
0
190
Domain Specific Languages in Ruby - Medellin.rb
orendon
0
120
Other Decks in Programming
See All in Programming
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
190
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
790
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
0
1.4k
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
150
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
260
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
750
Jetpack Compose Mechanisms
skydoves
3
820
R8 の設定見直しでアプリサイズ削減
andpad
0
220
Optunaを作ったときにやりたかったこと、その後 (2026/10/16, Optuna Meetup #3)
iwiwi
1
440
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
370
dotNET_vulnclicker_intro.pdf
deka_morita
0
140
Featured
See All Featured
Mind Mapping
helmedeiros
1
400
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
990
Chasing Engaging Ingredients in Design
codingconduct
0
360
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.6k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
730
Faster Mobile Websites
deanohume
310
32k
The Pragmatic Product Professional
lauravandoore
37
7.5k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
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!