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
110
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
まずはプロンプトガイドを読もう、話はそれからだ
kiakiraki
1
200
ソフトウェアエンジニアにとっての生成AI - 特性を知って使い倒す / generative ai for software enginner
kishida
7
2.1k
Gmail/Google DriveをトリガーにAIエージェントを動かそう! / Run AI agents with Gmail/Google Drive as triggers!
har1101
2
430
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
480
DynamoDBの基礎を振り返りながらベクトル検索機能を理解する
musan
2
160
Cloudflare is Agents
chimame
0
180
KotlinConf Extended South Korea 2026 Keynote
l2hyunwoo
0
120
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
400
コンパウンドプロダクト開発のためのローカルプロセスマネージャー再発明 #layerxgo
izumin5210
0
220
初めての模倣学習とVLA
natsutan
0
220
S3 を使うアプリケーションをローカル完結で動かすことに全力を注いでみた / Running S3 Apps Offline
contour_gara
0
590
【デモ】Kiroで体験する仕様駆動開発|設計からコーディングまでAIと進める開発フロー
cmkudo
0
360
Featured
See All Featured
Utilizing Notion as your number one productivity tool
mfonobong
4
550
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
The Cult of Friendly URLs
andyhume
79
7k
Music & Morning Musume
bryan
47
7.3k
Site-Speed That Sticks
csswizardry
13
1.5k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
380
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
690
Paper Plane
katiecoart
PRO
2
53k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
240
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.3k
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!