zero-functional / zero-functional

A library providing zero-cost chaining for functional abstractions in Nim.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enumeration sticks to the first one in chain, doesn't enumerate actual iterations

ZoomRmc opened this issue · comments

commented

In the following example, even though we expect the first enumeration to be dropped, the second enumerate still counts the iterations of the first iterator.

  for x in 'a'..'z' --> enumerate().
    filter(it[0] mod 2 == 0).map(it[1]).enumerate():
      echo x[0]

Expected results: numbers 0..12, actual output:

0
2
4
6
8
10
12
14
16
18
20
22
24

Reading more into the docs, it can be argued that this is working as intended, since it "prepends the index of each element in the collection", but this is clearly not what anyone working with the library would expect.

I'm a little confused what you want to enumerate now.
Nevertheless enumerate seems to be a somewhat new(?) language feature and a macro - so combining macros with macros here doesn't seem to work.
I guess we'll have to remove enumerate from zero_functional now since it has been added to nim standard lib.

Maybe try using the indexedMap command instead.

@ZoomRmc updated documentation - closing this one

commented

So what is the solution for counting actual iterations? I understand that under the hood it's all macroses, but in theory what we're doing here (emulating the effects of?) is chaining and transforming iterators, sometimes consuming them to produce the final value. Usually enumerate appends the counter to the iterable, relative to the currently active iterator, and not relative to the underlying sequence or anything else.

commented

Ping @michael72
I'm still interested in a proper enumerate. The expected behaviour is described in the first post and it's in accordance with other languages with iterators known to me.