SlideShare a Scribd company logo
1 of 73
Download to read offline
Laziness(in(Swi+
Maciej'Konieczny
narf.pl'2'macoscope.com
narf.pl
Swi$Warsaw.com
Python'!
Django'!
JavaScript*!
CoffeeScript+!
Objec&ve(C*!
Swi$%!
Python'!
Laziness(in(Swi+
delaying)computa0on
un0l)necessary
never%necessary
never%computed
poten&al)for
removing
needless)computa&on
poten&al)for
reducing
memory)footprint
poten&al)for
infinite
data)structures
Laziness(allows(the(expression(
of(programs(that(would(
otherwise(not(terminate
not$one$pa(ern
Swi$
lazy var
SequenceType
@autoclosure
lazy var
class BlogPost {
var filename: String
init(filename: String) {
self.filename = filename
}
}
class BlogPost {
var filename: String
var foo = Foo()
init(filename: String) {
self.filename = filename
}
}
class BlogPost {
var filename: String
lazy var foo = Foo()
init(filename: String) {
self.filename = filename
}
}
class BlogPost {
var filename: String
init(filename: String) {
self.filename = filename
}
}
class BlogPost {
var filename: String
lazy var markdown: String = {
markdownForFile(self.filename)
}()
init(filename: String) {
self.filename = filename
}
}
Swift.nil != ObjC.nil
- (NSString *)markdown {
if (!_markdown) {
_markdown = markdownForFile(self.filename);
}
return _markdown;
}
SequenceType
for x in xs {
// ...
}
for x in xs {
// ...
}
var _g = xs.generate()
while let x = _g.next() {
// ...
}
class Integers: SequenceType {
func generate() -> GeneratorOf<Int> {
var n = -1
return GeneratorOf { ++n }
}
}
class Integers: SequenceType {
func generate() -> GeneratorOf<Int> {
var n = -1
return GeneratorOf { ++n }
}
}
for i in Integers() {
println(i) // 0, 1, 2, 3, ...
}
var integers = lazy(Integers())
var integers = lazy(Integers())
integers.filter
integers.map
extension LazySequence {
var first: LazySequence.Generator.Element? {
for x in self {
return x
}
return nil
}
}
integers.first! // 0
var x = integers
var x = integers 
.filter { $0 % 2 == 1 }
var x = integers 
.filter { $0 % 2 == 1 } 
.map { $0 * $0 }
var x = integers 
.filter { $0 % 2 == 1 } 
.map { $0 * $0 } 
.filter { $0 > 100 }
var x = integers 
.filter { $0 % 2 == 1 } 
.map { $0 * $0 } 
.filter { $0 > 100 } 
.first!
var x = integers 
.filter { $0 % 2 == 1 } 
.map { $0 * $0 } 
.filter { $0 > 100 } 
.first!
println(x) // 121
var x = integers.filter {
return $0 % 2 == 1
}.map {
return $0 * $0
}.filter {
return $0 > 10
}.first!
println(x) // 25
var x = integers.filter {
println("n($0)")
println("even?")
return $0 % 2 == 1
}.map {
println("square")
return $0 * $0
}.filter {
println("threshold")
return $0 > 10
}.first!
println(x) // 25
integers.filter { $0 % 2 == 1 } 
.map { $0 * $0 }
.filter { $0 > 10 } 
.first!
0 even?
1 even? square threshold
2 even?
3 even? square threshold
4 even?
5 even? square threshold
@autoclosure
// without @autoclosure:
f({ x })
// with @autoclosure:
f(x)
func foo(bar: () -> ()) {
bar()
}
foo({ println("baz") })
func foo(bar: @autoclosure () -> ()) {
bar()
}
foo(println("baz"))
while&not&x&/&un-l&x&/&dopóki&x
func dopóki(condition: @autoclosure () -> Bool,
body: () -> ()) {
if !condition() {
body()
dopóki(condition(), body)
}
}
var i = 3
dopóki (i == 0) {
println(i)
i -= 1
}
BTW:%compiler%performs
tail%call%op2misa2on
not$one$pa(ern
removing
needless,computa2on
reducing
memory,footprint
expressiveness
lazy var foo = Foo()
lazy var markdown: String = {
markdownForFile(self.filename)
}()
for x in xs {
// ...
}
var _g = xs.generate()
while let x = _g.next() {
// ...
}
// without @autoclosure:
f({ x })
// with @autoclosure:
f(x)
That's'all'folks!
narf.pl
References((1(of(2)
• Understand)and)implement)laziness,#Ma&#Might
h&p://ma&.might.net/ar3cles/implemen3ng7
laziness/
• WWDC)2014,)Session)404:)Advanced)Swi>
h&ps://developer.apple.com/videos/wwdc/2014/
References((2(of(2)
• Lazy%by%name,%lazy%by%nature,#airspeedvelocity
h2p://airspeedvelocity.net/2014/07/26/lazy>by>
name>lazy>by>nature/
• /r/aww
h2p://www.panopCkos.com/r/aww/top
Ques%ons?

More Related Content

What's hot

Random. Kinda.
Random. Kinda.Random. Kinda.
Random. Kinda.awwaiid
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기진성 오
 
20191116 custom operators in swift
20191116 custom operators in swift20191116 custom operators in swift
20191116 custom operators in swiftChiwon Song
 
CS50 Lecture2
CS50 Lecture2CS50 Lecture2
CS50 Lecture2昀 李
 
[SI] Ada Lovelace Day 2014 - Tampon Run
[SI] Ada Lovelace Day 2014  - Tampon Run[SI] Ada Lovelace Day 2014  - Tampon Run
[SI] Ada Lovelace Day 2014 - Tampon RunMaja Kraljič
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in SwiftSeongGyu Jo
 
Functional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures editionFunctional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures editionosfameron
 
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...Mozaic Works
 
Frege is a Haskell for the JVM
Frege is a Haskell for the JVMFrege is a Haskell for the JVM
Frege is a Haskell for the JVMjwausle
 
Presentation on php string function part-1
Presentation on php string function part-1Presentation on php string function part-1
Presentation on php string function part-1Mysoftheaven (BD) Ltd.
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyYasuharu Nakano
 
Pivorak Clojure by Dmytro Bignyak
Pivorak Clojure by Dmytro BignyakPivorak Clojure by Dmytro Bignyak
Pivorak Clojure by Dmytro BignyakPivorak MeetUp
 
Functional pe(a)rls: Huey's zipper
Functional pe(a)rls: Huey's zipperFunctional pe(a)rls: Huey's zipper
Functional pe(a)rls: Huey's zipperosfameron
 

What's hot (20)

5th Sem SS lab progs
5th Sem SS lab progs5th Sem SS lab progs
5th Sem SS lab progs
 
Random. Kinda.
Random. Kinda.Random. Kinda.
Random. Kinda.
 
Funcion matematica
Funcion matematicaFuncion matematica
Funcion matematica
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
 
Codigos
CodigosCodigos
Codigos
 
20191116 custom operators in swift
20191116 custom operators in swift20191116 custom operators in swift
20191116 custom operators in swift
 
CS50 Lecture2
CS50 Lecture2CS50 Lecture2
CS50 Lecture2
 
[SI] Ada Lovelace Day 2014 - Tampon Run
[SI] Ada Lovelace Day 2014  - Tampon Run[SI] Ada Lovelace Day 2014  - Tampon Run
[SI] Ada Lovelace Day 2014 - Tampon Run
 
ملخص البرمجة المرئية - الوحدة السادسة
ملخص البرمجة المرئية - الوحدة السادسةملخص البرمجة المرئية - الوحدة السادسة
ملخص البرمجة المرئية - الوحدة السادسة
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in Swift
 
Functional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures editionFunctional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures edition
 
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
 
Frege is a Haskell for the JVM
Frege is a Haskell for the JVMFrege is a Haskell for the JVM
Frege is a Haskell for the JVM
 
画像Hacks
画像Hacks画像Hacks
画像Hacks
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
 
Presentation on php string function part-1
Presentation on php string function part-1Presentation on php string function part-1
Presentation on php string function part-1
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
 
Pivorak Clojure by Dmytro Bignyak
Pivorak Clojure by Dmytro BignyakPivorak Clojure by Dmytro Bignyak
Pivorak Clojure by Dmytro Bignyak
 
Functional pe(a)rls: Huey's zipper
Functional pe(a)rls: Huey's zipperFunctional pe(a)rls: Huey's zipper
Functional pe(a)rls: Huey's zipper
 
PHP 101
PHP 101 PHP 101
PHP 101
 

Similar to Laziness in Swift

Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05cKaz Yoshikawa
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giantsIan Barber
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)William Narmontas
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kirill Rozov
 
Groovy puzzlers по русски с Joker 2014
Groovy puzzlers по русски с Joker 2014Groovy puzzlers по русски с Joker 2014
Groovy puzzlers по русски с Joker 2014Baruch Sadogursky
 
Everything About PowerShell
Everything About PowerShellEverything About PowerShell
Everything About PowerShellGaetano Causio
 
Concurrent Application Development using Scala
Concurrent Application Development using ScalaConcurrent Application Development using Scala
Concurrent Application Development using ScalaSiarhiej Siemianchuk
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Scalding - the not-so-basics @ ScalaDays 2014
Scalding - the not-so-basics @ ScalaDays 2014Scalding - the not-so-basics @ ScalaDays 2014
Scalding - the not-so-basics @ ScalaDays 2014Konrad Malawski
 
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기Suyeol Jeon
 

Similar to Laziness in Swift (20)

Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
ddd+scala
ddd+scaladdd+scala
ddd+scala
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giants
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 
Ns2programs
Ns2programsNs2programs
Ns2programs
 
Groovy puzzlers по русски с Joker 2014
Groovy puzzlers по русски с Joker 2014Groovy puzzlers по русски с Joker 2014
Groovy puzzlers по русски с Joker 2014
 
Everything About PowerShell
Everything About PowerShellEverything About PowerShell
Everything About PowerShell
 
Concurrent Application Development using Scala
Concurrent Application Development using ScalaConcurrent Application Development using Scala
Concurrent Application Development using Scala
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
Monadologie
MonadologieMonadologie
Monadologie
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Scalding - the not-so-basics @ ScalaDays 2014
Scalding - the not-so-basics @ ScalaDays 2014Scalding - the not-so-basics @ ScalaDays 2014
Scalding - the not-so-basics @ ScalaDays 2014
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
 

Laziness in Swift