Skip to content

Instantly share code, notes, and snippets.

@totiz
Last active September 12, 2018 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totiz/5bd9fa6178ca2f8738cb26fd125d86a2 to your computer and use it in GitHub Desktop.
Save totiz/5bd9fa6178ca2f8738cb26fd125d86a2 to your computer and use it in GitHub Desktop.
Collection Lazy
// 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