Share
Embed
Transcript
32 slides
-
A programming language uses an evaluation strategy to determine when
to evaluate the argument(s) of a function call (for function, also read: operation, method, or relation) and what kind of value to pass to the function. “Wikipedia” Evaluation Strategy: -
A programming language uses an evaluation strategy to determine when
to evaluate the argument(s) of a function call (for function, also read: operation, method, or relation) and what kind of value to pass to the function. “Wikipedia” Evaluation Strategy: call-by-value call-by-reference -
Eager Evaluation func dosomething() -> Int { println("dosomething") return 1
} func foo(x: Int, status: String) { println("foo") } foo(dosomething(), "200") output dosomething foo -
Eager Evaluation func dosomething() -> Int { println("dosomething") return 1
} func foo(x: Int, status: String) { println("foo") } foo(dosomething(), "200") output dosomething foo fooؔͷ࣮ߦલʹdosomething͕ධՁ͞ΕΔ -
Eager Evaluation func expensiveComputation() -> Int { println("expensiveComputation") return 1
} func foo(x: Int, status: String) { println("foo") let result = status == "200" ? x : 0 println("result: \(result)") } foo(expensiveComputation(), "404") output expensiveComputation foo result: 0 -
Eager Evaluation func expensiveComputation() -> Int { println("expensiveComputation") return 1
} func foo(x: Int, status: String) { println("foo") let result = status == "200" ? x : 0 println("result: \(result)") } foo(expensiveComputation(), "404") output expensiveComputation foo result: 0 x ͕ΘΕ͍ͯͳ͍ͷʹධՁ͞Εͨʂ -
Eager Evaluation func expensiveComputation() -> Int { println("expensiveComputation") return 1
} func foo(x: Int, status: String) { println("foo") let result = status == "200" ? x : 0 println("result: \(result)") } foo(expensiveComputation(), "404") output expensiveComputation foo result: 0 x ͕ΘΕ͍ͯͳ͍ͷʹධՁ͞Εͨʂ ϞολΠφΠʂ -
Function Currying func dosomething() -> Int { println("dosomething") return 1
} func foo(a: Int)(b: Int) { println("add") } let cFoo = foo(dosomething()) //cFoo(b: 10) output dosomething -
Function Currying func dosomething() -> Int { println("dosomething") return 1
} func foo(a: Int)(b: Int) { println("add") } let cFoo = foo(dosomething()) //cFoo(b: 10) output dosomething ͜ͷ࣌ʹ a ͕ධՁ͞ΕΔ -
Call-by-name func expensiveComputation() -> Int { println("expensiveComputation") return 1 }
func foo(x: () -> Int, status: String) { println("foo") let result = status == "200" ? x() : 0 println("result: \(result)") } foo({ expensiveComputation() }, "404") output foo result: 0 -
Call-by-name func expensiveComputation() -> Int { println("expensiveComputation") return 1 }
func foo(x: () -> Int, status: String) { println("foo") let result = status == "200" ? x() : 0 println("result: \(result)") } foo({ expensiveComputation() }, "404") output foo result: 0 ClosureΛ͏ʂ -
Call-by-name func expensiveComputation() -> Int { println("expensiveComputation") return 1 }
func foo(x: () -> Int, status: String) { println("foo") let result = status == "200" ? x() : 0 println("result: \(result)") } foo({ expensiveComputation() }, "404") output foo result: 0 ClosureΛ͏ʂ ؾ࣋ͪѱ͍ -
Call-by-name func expensiveComputation() -> Int { println("expensiveComputation") return 1 }
func foo(x: @autoclosure () -> Int, status: String) { println("foo") let result = status == "200" ? x() : 0 println("result: \(result)") } foo(expensiveComputation(), "404") output foo result: 0 -
Call-by-name func expensiveComputation() -> Int { println("expensiveComputation") return 1 }
func foo(x: @autoclosure () -> Int, status: String) { println("foo") let result = status == "200" ? x() : 0 println("result: \(result)") } foo(expensiveComputation(), "404") output foo result: 0 ؾ࣋ͪྑ͘ͳͬͨʂ -
Call-by-name func expensiveComputation() -> Int { println("expensiveComputation") return 1 }
func foo(x: @autoclosure () -> Int, status: String) { println("foo") let result = status == "200" ? x() : 0 let t1 = x() let t2 = x() println("result: \(result)") } foo(expensiveComputation(), "404") output foo expensiveComputation expensiveComputation result: 0 -
Call-by-name func expensiveComputation() -> Int { println("expensiveComputation") return 1 }
func foo(x: @autoclosure () -> Int, status: String) { println("foo") let result = status == "200" ? x() : 0 let t1 = x() let t2 = x() println("result: \(result)") } foo(expensiveComputation(), "404") output foo expensiveComputation expensiveComputation result: 0 x ͕࠶ධՁ͞ΕΔʂ -
Call-by-need class Foo { lazy var tmp: Int = {
println("tmp init") return 1 }() } println("before init") let foo = Foo() println("after init") foo.tmp foo.tmp output before init after init tmp init -
Call-by-need class Foo { lazy var tmp: Int = {
println("tmp init") return 1 }() } println("before init") let foo = Foo() println("after init") foo.tmp foo.tmp output before init after init tmp init ̎ճݺΕ͍ͯΔ͚ͲɼॳظԽ̍ճ͚ͩ ඞཁ࣌ͷΈධՁ͢Δ -
Call-by-need lazy property global variable static property struct Foo {
static var tmp: Int = { println("tmp init") return 1 }() static func log() { println("foo") } } let foo = Foo() Foo.log() println("access tmp") let x = Foo.tmp output foo access tmp tmp init -
Memoization func memoize<T: Hashable, U>( body: (T)->U ) -> (T)->U
{ var memo = Dictionary<T, U>() return { x in if let q = memo[x] { return q } let r = body(x) memo[x] = r return r } } Fibonacci link: WWDC2014 Advanced Swift (from page 123) Ωϟογϡ͞ΕͨͷͰ࠶ܭࢉ͕ෆཁ -
When • Eager evaluation • evaluated completely before the function
is applied • Call-by-name • evaluate only when needed • evaluate each time value is accessed • Call-by-need • evaluate only when needed • cache result for repeated access