Skip to content

Instantly share code, notes, and snippets.

@riywo
Last active December 29, 2015 09:59
Show Gist options
  • Select an option

  • Save riywo/7654428 to your computer and use it in GitHub Desktop.

Select an option

Save riywo/7654428 to your computer and use it in GitHub Desktop.
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
Copy Markdown

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

riywo commented Nov 26, 2013

Copy link
Copy Markdown
Author

Awesome! Updated. Thanks!!!

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