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
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.8k
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
170
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
420
なぜSQLはAIぽく見えるのか/why does SQL look AI like
florets1
0
450
CSC307 Lecture 01
javiergs
PRO
0
690
メルカリのリーダビリティチームが取り組む、AI時代のスケーラブルな品質文化
cloverrose
2
510
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
150
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
500
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
170
CSC307 Lecture 09
javiergs
PRO
1
830
AgentCoreとHuman in the Loop
har1101
5
220
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.5k
Featured
See All Featured
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
160
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Context Engineering - Making Every Token Count
addyosmani
9
650
What's in a price? How to price your products and services
michaelherold
247
13k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
62
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Claude Code のすすめ
schroneko
67
210k
Design in an AI World
tapps
0
140
Large-scale JavaScript Application Architecture
addyosmani
515
110k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
22k
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!