List comprehensions: Difference between revisions

Add Swift
(→‎{{header|Ruby}}: Removed outdated Ruby 1.8.6 code)
(Add Swift)
Line 2,304:
returns
<lang supercollider>[ [ 3, 4, 5 ], [ 5, 12, 13 ], [ 6, 8, 10 ], [ 8, 15, 17 ], [ 9, 12, 15 ], [ 12, 16, 20 ] ]</lang>
 
 
=={{header|Swift}}==
 
<lang swift>typealias F1 = (Int) -> [(Int, Int, Int)]
typealias F2 = (Int) -> Bool
 
func pythagoreanTriples(n: Int) -> [(Int, Int, Int)] {
(1...n).flatMap({x in
(x...n).flatMap({y in
(y...n).filter({z in
x * x + y * y == z * z
} as F2).map({ (x, y, $0) })
} as F1)
} as F1)
}
 
print(pythagoreanTriples(n: 20))</lang>
 
{{out}}
 
<pre>[(3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15), (12, 16, 20)]</pre>
 
=={{header|Tcl}}==