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 and Ruby - EuRuKo
Search
Pat Shaughnessy
June 28, 2013
Technology
2
740
Functional Programming and Ruby - EuRuKo
Slides from Athens, June 2013
Pat Shaughnessy
June 28, 2013
Tweet
Share
More Decks by Pat Shaughnessy
See All by Pat Shaughnessy
20000 Leagues Under ActiveRecord
pat_shaughnessy
0
120
Visualizing Garbage Collection in Rubinius, JRuby and Ruby 2.0
pat_shaughnessy
8
690
Functional Programming and Ruby
pat_shaughnessy
6
1.4k
Dissecting a Ruby Block
pat_shaughnessy
10
440
Other Decks in Technology
See All in Technology
あの日俺達が夢見たサーバレスアーキテクチャ/the-serverless-architecture-we-dreamed-of
tomoki10
0
460
スタートアップで取り組んでいるAzureとMicrosoft 365のセキュリティ対策/How to Improve Azure and Microsoft 365 Security at Startup
yuj1osm
0
210
OpenAIの蒸留機能(Model Distillation)を使用して運用中のLLMのコストを削減する取り組み
pharma_x_tech
4
560
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
310
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
140
Google Cloud で始める Cloud Run 〜AWSとの比較と実例デモで解説〜
risatube
PRO
0
100
20241214_WACATE2024冬_テスト設計技法をチョット俯瞰してみよう
kzsuzuki
3
450
小学3年生夏休みの自由研究「夏休みに Copilot で遊んでみた」
taichinakamura
0
150
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
0
180
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
DevFest 2024 Incheon / Songdo - Compose UI 조합 심화
wisemuji
0
100
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
290
Featured
See All Featured
Done Done
chrislema
181
16k
Optimizing for Happiness
mojombo
376
70k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Gamification - CAS2011
davidbonilla
80
5.1k
Bash Introduction
62gerente
608
210k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Become a Pro
speakerdeck
PRO
26
5k
RailsConf 2023
tenderlove
29
940
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
4 Signs Your Business is Dying
shpigford
181
21k
Transcript
foo :: Ord a => [a] -> [a] foo []
= [] foo (p:xs) = (foo lesser) ++ [p] ++ (foo greater) where lesser = filter (< p) xs greater = filter (>= p) xs
None
Ruby is a language designed in the following steps: *
take a simple lisp language * add blocks, inspired by higher order functions * add methods found in Smalltalk * add functionality found in Perl So, Ruby was a Lisp originally, in theory. Let's call it MatzLisp from now on. ;-) ! ! ! ! ! ! ! matz.
None
None
None
None
Haskell... is a polymorphically statically typed, lazy, purely functional language,
quite different from most other programming languages. The language is named for Haskell Brooks Curry, ...
- what is “functional programming?” - higher order functions -
lazy evaluation - memoization
None
higher order functions
[1..10] =>[1, 2, 3, 4, 5, 6, 7, 8, 9,
10] (1..10).to_a
[ x*x | x <- [1..10]] (1..10).collect { |x| x*x
} =>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] (1..10).map { |x| x*x }
None
map (\x -> x*x) [1..10] (1..10).map &lambda { |x| x*x
} =>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] (1..10).map &(->(x) { x*x })
lazy evaluation
[1..] =>[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28, 29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54, 55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80, 81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,1 05,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123, etc...
take 10 [1..] =>[1,2,3,4,5,6,7,8,9,10]
take 10 [ x+1 | x <- [ x*x |
x <- [1..]]] =>[2,5,10,17,26,37,50,65,82,101]
(1..Float::INFINITY) .lazy .collect { |x| x*x } .collect { |x|
x+1 } .take(10).force =>[2,5,10,17,26,37,50,65,82,101]
=>[2,5,10,17,26,37,50,65,82,101] (1..Float::INFINITY) .lazy .collect { |x| x*x } .collect {
|x| x+1 } .first(10)
(1..10).collect { |x| x*x } each Range Enumerable #collect Enumerable#collect
enum = Enumerator.new do |y| y.yield 1 y.yield 2 end
p enum.collect { |x| x*x } => [1, 4] Enumerator
enum = Enumerator.new do |y| y.yield 1 y.yield 2 end
enum.collect do |x| x*x end
Enumerator Yielder yields Generator do |y| y.yield 1 y.yield 2
end
Enumerator::Lazy calls each yields Enumerator::Lazy calls each yields my block
my block yields yields
=>[2,5,10,17,26,37,50,65,82,101] (1..Float::INFINITY) .lazy .collect { |x| x*x } .collect {
|x| x+1 } .first(10)
Step 1: Call "each" Lazy Lazy x*x x+1 yield yield
Infinite range first(10) Step 2: yield to the blocks, one at a time
memoization
slow_fib 0 = 0 slow_fib 1 = 1 slow_fib n
= slow_fib (n-2) + slow_fib (n-1) map slow_fib [1..10] => [1,1,2,3,5,8,13,21,34,55] http://www.haskell.org/haskellwiki/Memoization
None
memoized_fib = (map fib [0 ..] !!) where fib 0
= 0 fib 1 = 1 fib n = memoized_fib (n-2) + memoized_fib (n-1) Typical Haskell magic! http://www.haskell.org/haskellwiki/Memoization
(map fib [0 ..] !!) Infinite, lazy list of return
values A curried function to return the requested fib
[0 ..] (0..Float::INFINITY)
map fib [0 ..] (0..Float::INFINITY) .lazy.map {|x| fib(x) }
(map fib [0 ..] !!) cache = (0..Float::INFINITY) .lazy.map {|x|
fib(x) } nth_element_from_list = lambda { |ary, n| ary[n]} nth_fib = nth_element_from_list.curry[cache]
map memoized_fib [1..10] => [1,1,2,3,5,8,13,21,34,55] `block in <main>': undefined method
`[]' for #<Enumerator::Lazy: #<Enumerator::Lazy: 0..Infinity>:map> (NoMethodError)
each Range Enumerable #collect (0..Float::INFINITY) .lazy.map {|x| fib(x) } nth_element_from_list
= lambda { |ary, n| ary[n]}
@cache = {} @cache[1] = 1 @cache[2] = 1 def
memoized_fib(n) @cache[n] ||= memoized_fib(n-1) + memoized_fib(n-2) end
learn by studying other languages... and acquire a different perspective
on Ruby
Ruby has many functional features, but is not a functional
language