Last active
September 12, 2018 09:39
-
-
Save totiz/5bd9fa6178ca2f8738cb26fd125d86a2 to your computer and use it in GitHub Desktop.
Collection Lazy
This file contains 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
// Exp1: ยกกำลังสองตัวเลขใน Array แล้วแสดงผลออกมา | |
var numbers = [1,2,3] | |
numbers.lazy.map{ pow(Double($0), 2) }.forEach { | |
print($0) | |
} | |
// Exp2: แสดงเส้นรอบวงสูงสุด | |
let radiuses: [Double] = [ 1, 7, 2, 10, 50, 20 ] | |
let lengths = radiuses.lazy.map{ 2 * $0 * M_PI } | |
for length in lengths { | |
print(length) | |
} | |
// [6.2831853071795862, 43.982297150257104, 12.566370614359172, 62.831853071795862, 314.15926535897933, 125.66370614359172] | |
// Exp3: ค้นหาเส้นรอบวงที่มากกว่า 40 | |
let outputs = lengths.filter{ $0 > 40 } | |
print(Array(outputs)) //[43.982297150257104, 62.831853071795862, 314.15926535897933, 125.66370614359172] | |
// Exp4: เส้นรอบวงสูงสุด | |
print(lengths.max()) // 314.15926535897933 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment