From 3647c03cadfce2ccaec136590a8acd8a736d0bb0 Mon Sep 17 00:00:00 2001 From: cooldome Date: Sat, 27 Apr 2019 12:41:08 +0100 Subject: [PATCH] reimplement_pr_10974 (#11130) --- changelog.md | 3 +++ lib/pure/collections/sequtils.nim | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/changelog.md b/changelog.md index 7bebfc0e53..accea7d81f 100644 --- a/changelog.md +++ b/changelog.md @@ -158,6 +158,9 @@ proc enumToString*(enums: openArray[enum]): string = - Added `system.default`. +- Added `sequtils.items` for closure iterators, allows closure iterators + to be used by the the mapIt, filterIt, allIt, anyIt, etc. + ### Library changes diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index 2533403794..e39c1fb80f 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -949,6 +949,14 @@ macro mapLiterals*(constructor, op: untyped; ## works for nested tuples of arrays of sets etc. result = mapLitsImpl(constructor, op, nested.boolVal) +iterator items*[T](xs: iterator: T): T = + ## iterates over each element yielded by a closure iterator. This may + ## not seem particularly useful on its own, but this allows closure + ## iterators to be used by the the mapIt, filterIt, allIt, anyIt, etc. + ## templates. + for x in xs(): + yield x + when isMainModule: import strutils from algorithm import sorted @@ -1382,5 +1390,13 @@ when isMainModule: doAssert outp == @[@["a", "b"], @["c", "d"]] + block: + proc iter(len: int): auto = + result = iterator(): int = + for i in 0..