2016-07-25 18 views
1

別のインラインイテレータをパラメータとして受け付けるインラインイテレータを使用できますか?私の目標は、単純なパイプラインのような処理を行い、それをシリアルのCコードに展開することです。ネストされたインラインイテレータ

エラーで
iterator test2(it: iterator(): int {.inline.}): int = 
    for i in it(): 
    yield i*2 
iterator test1(): int = 
    yield 10 
    yield 20 
    yield 30 
for i in test2(test1()): 
    echo j 

test.nim(2, 14) Error: type mismatch: got (int) but expected one of: 
iterator items[IX, T](a: array[IX, T]): T 
iterator items[](E: typedesc[enum]): E:type 
iterator items(a: string): char 
iterator items[T](s: Slice[T]): T 
iterator items[T](a: openArray[T]): T 
iterator items[T](a: seq[T]): T 
iterator items[T](a: set[T]): T 
iterator items(a: cstring): char 
> Process terminated with exit code 256 
+1

私はニムのリポジトリに[問題](https://github.com/nim-lang/Nim/issues/4516)を作成しました。これはうまくいくはずです。 – flyx

答えて

3

あなたは2つのオプション(少なくとも)

  1. を持っている{.closureとして、あなたのイテレータを定義し、私は仕事に行くことができない例。}
  2. テンプレートを使用してイテレータをラップします。少し醜いですが、動作します。

    template runIterator(it, exec: untyped): typed = 
        block: 
         it(myIterator) 
         for i in myIterator() : 
          exec(i*2) 
    
    template defineTest1(name: untyped): typed = 
    
        iterator `name`(): int {.inline.} = 
         yield 10 
         yield 20 
         yield 30 
    
    template defineTest2(name: untyped): typed = 
    
        iterator `name`(): int {.inline.} = 
         yield 5 
         yield 10 
         yield 15 
    
    template exec(i: int): typed = echo i 
    
    runIterator(defineTest1, exec) 
    runIterator(defineTest2, exec) 
    

編集:

アイデアは、代わりに、インラインイテレータのテンプレートを使用すると、コードを注入することである - ので、何秒反復子が、テンプレートが存在しません。 多分これは、それが明確になります:

template test2(it, yielder, exec: untyped): typed = 
     for i in it() : 
      let `yielder` {.inject.} = 2 * i 
      exec 


    iterator test1(): int {.inline.} = 
     yield 10 
     yield 20 
     yield 30 

    iterator test1b(): int {.inline.} = 
     yield 5 
     yield 10 
     yield 15 

    test2(test1, i): 
     echo i 

    test2(test1b, i): 
     echo i 
+0

私は第二の選択肢を理解しているかどうか分かりません。どの部分が 'test2'イテレータを表しますか? 'runIterator'は何も返されません。 – liori