Skip to content

Instantly share code, notes, and snippets.

@yas375
Last active May 10, 2016 03:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yas375/69d8643ff7d98b137cc2f8201c3e58fc to your computer and use it in GitHub Desktop.
Save yas375/69d8643ff7d98b137cc2f8201c3e58fc to your computer and use it in GitHub Desktop.
Why `double` is called 5 times and `filter` is called 4 times? Shouldn't they be called same amount of times? Shouldn't they both be called only twice as I have 2-element array?
double 2
filter 4
double 8
filter 16
result: [4]
func double(value: Int) -> Int {
print("double \(value)")
return value * 2
}
func filter(value: Int) -> Bool {
print("filter \(value)")
return value < 10
}
let xs = [2, 8]
let bs = Array(xs.lazy
.map { double($0) }
.filter { filter($0) }
)
print("result: \(bs)")
double 2
filter 4
double 8
filter 16
double 2
filter 4
double 2
double 8
filter 16
result: [4]
@yas375
Copy link
Author

yas375 commented May 9, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment