11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nim内包表記

Posted at

概要

nimのリスト内包表記(List Comprehensions)
pythonでいうところの、

l = [ x for x in range(10) ]

とかです。

以下メモ

シンプル

import future
block:
  var ary = @[1,2,3,4,5]
  var y = lc[ x+1 | (x <- ary, x mod 2 == 0) , int]
  echo y
(stdout)
@[3, 5]

条件付き

import future
block:
  var ret = lc[ (x,y,z) | ( x <- 1..2 , y <- 2..3 , z <- 3..4 , x == 2) , tuple[a,b,c:int] ]
  echo ret
  echo ret.len
(stdout)
@[(a: 2, b: 2, c: 3), (a: 2, b: 2, c: 4), (a: 2, b: 3, c: 3), (a: 2, b: 3, c: 4)]
4

条件付き(位置変更)

import future
block:
  var ret = lc[ (x,y,z) | ( x <- 1..2 , x == 2 , y <- 2..3 , z <- 3..4 ) , tuple[a,b,c:int] ]
  echo ret
  echo ret.len
(stdout)
@[(a: 2, b: 2, c: 3), (a: 2, b: 2, c: 4), (a: 2, b: 3, c: 3), (a: 2, b: 3, c: 4)]
4
11
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?