Skip to content

Instantly share code, notes, and snippets.

@riywo
Last active Dec 29, 2015
Embed
What would you like to do?
comprehension
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 ] ] ]
>>> [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)]
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))
@cocoatomo
Copy link

cocoatomo commented Nov 26, 2013

Python では以下のように書けます.

>>> [(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)]

@riywo
Copy link
Author

riywo commented Nov 26, 2013

Awesome! Updated. Thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment