comprehension
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
coffee> i*i for i in [1..9] when i % 2 == 0 | |
[ 4, 16, 36, 64 ] | |
# https://github.com/jashkenas/coffee-script/issues/2030 | |
# http://brehaut.net/blog/2011/coffeescript_comprehensions | |
coffee> [i,j] for j in [1..3] for i in [1..3] | |
[ [ [ 1, 1 ], | |
[ 1, 2 ], | |
[ 1, 3 ] ], | |
[ [ 2, 1 ], | |
[ 2, 2 ], | |
[ 2, 3 ] ], | |
[ [ 3, 1 ], | |
[ 3, 2 ], | |
[ 3, 3 ] ] ] |
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
>>> [i*i for i in range(1,10) if i % 2 == 0] | |
[4, 16, 36, 64] | |
>>> [(i, j) for i in range(1, 4) for j in range(1, 4)] | |
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)] |
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
scala> for{ i <- 1 until 10; if i % 2 == 0 } yield i*i | |
res1: scala.collection.immutable.IndexedSeq[Int] = Vector(4, 16, 36, 64) | |
scala> for{ i <- 1 until 4; j <- 1 until 4 } yield (i,j) | |
res2: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python では以下のように書けます.