Last active
May 10, 2016 03:05
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double 2 | |
filter 4 | |
double 8 | |
filter 16 | |
result: [4] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double 2 | |
filter 4 | |
double 8 | |
filter 16 | |
double 2 | |
filter 4 | |
double 2 | |
double 8 | |
filter 16 | |
result: [4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've got the expected behavior in https://gist.github.com/yas375/e63416596701e6cece2e2158cc0b8c1d