kof2008 itll

25
Introduction to Lazy Lists 2008 UJIHISA, Tatsuhiro

Upload: ujihisa

Post on 06-May-2015

1.100 views

Category:

Technology


0 download

DESCRIPTION

“Introduction to Lazy lists”How to use and construct lazy lists on Programming Langage Ruby.

TRANSCRIPT

Page 1: Kof2008 Itll

Introduction to Lazy Lists2008 UJIHISA, Tatsuhiro

Page 2: Kof2008 Itll

UJIHISA Tatsuhiro• Osaka University

• Ruby, Vim, Haskell and Math

• http://ujihisa.nowa.jp

• http://twitter.com/ujm

• To the US!

Page 3: Kof2008 Itll

Targets

• ★★☆☆☆• Array, Enumerable, IO (File)

Haskell

Page 4: Kof2008 Itll

Lazy lists

Page 5: Kof2008 Itll

• RubyやHaskellを用いて遅延リスト (Lazy list) の概念とその有用性の議論を展開する。説明にはLive codingが含まれる。もしも時間が余れば、全てのプログラマにとって有用である伝統的ツールVimについて簡単な紹介を行う。

Page 6: Kof2008 Itll

Lazy Lists• Lazy lists (or streams)

• List (= Array in Ruby)

• Only needed parts are calculated.

• Haskell supports lazy lists by default.

• Ruby can support it.

Page 7: Kof2008 Itll

List (=Array) in Ruby• Array class

• [0, 1, 2, 3], Array.new(4) {|i| i }

• a #=> [0, 1, 2, 3]a.map {|i| i * 2 } #=> [0, 2, 4, 6]

Page 8: Kof2008 Itll

List• Data structure

• Insert, Delete, Access sequentially

Page 9: Kof2008 Itll

c.f. Array• Data structure

• Insert, Delete, Access randomly, Size

Page 10: Kof2008 Itll

(List again)• Data structure

• Insert, Delete, Access sequentially

Page 11: Kof2008 Itll

Lazy List• Data structure

• Insert, Delete, Access sequentially

Page 12: Kof2008 Itll

Types of lazy lists

• Boundless length lists (長さが無限)

• Undefined length lists (長さが未定義)

• Defined length lists (長さが定義済み)

Page 13: Kof2008 Itll

How to constructlazy lists?

Page 14: Kof2008 Itll

1 class LazyList 2 def initialize(&b) 3 @b = b 4 @i = -1 5 end 6 7 def succ 8 @b[@i+=1] 9 end 10 end

Page 15: Kof2008 Itll

l = LazyList.new {¦i¦ i * 2 } 10.times do p l.succend

Page 16: Kof2008 Itll

def each loop { yield succ }end

l = LazyList.new {¦i¦ i * 2 } l.each do ¦i¦ p i break if i > 10end

Page 17: Kof2008 Itll
Page 18: Kof2008 Itll

Notations• array = 1000+ length array

heavy = lambda { heavy procedure }

• array.take(2).map(&heavy)

• array.map(&heavy).take(2)

Page 19: Kof2008 Itll

File.open(__FILE__) do ¦io¦ a = nil while a = io.gets do p a endend

File.open(__FILE__) do ¦io¦ io.each do ¦a¦ p a endend

Page 20: Kof2008 Itll

File.open(__FILE__) do ¦io¦ io.each do ¦a¦ if a != "\n" p a end endend

File.open(__FILE__) do ¦io¦ io.select {¦a¦ a != "\n" }.each {¦a¦ p a }end

Page 21: Kof2008 Itll

File.open(__FILE__) do ¦io¦ io.each do ¦a¦ if a != "\n" p a end endend

File.open(__FILE__) do ¦io¦ io.select {¦a¦ a != "\n" }.each {¦a¦ p a }end

File.open(__FILE__) do ¦io¦ io. select {¦a¦ a != "\n" }. each {¦a¦ p a }end

Page 22: Kof2008 Itll

Common case• truncation (break)

• filtering (if, unless; select, reject)

• mapping (=, map)

• processing (???, each)folding (<<, inject)

Page 23: Kof2008 Itll

list.each {¦a¦ next if ... b = ... ...}

list. select { ... }. map { ... }. each { ... }

Page 24: Kof2008 Itll

result = 0list.each {¦a¦ next if ... b = ... result += b}

result = list. select { ... }. map { ... }. inject(0) { ... }

Page 25: Kof2008 Itll

$ sudo gem install lazylist

sq = list { x * x }.where :x => 1..Infinity

# Haskell!# sq = [ x * x ¦ x <- [1..] ]