Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement groupBy method for potentially infinite iterables #9

Closed
xgbuils opened this issue May 12, 2017 · 2 comments
Closed

Implement groupBy method for potentially infinite iterables #9

xgbuils opened this issue May 12, 2017 · 2 comments

Comments

@xgbuils
Copy link
Owner

xgbuils commented May 12, 2017

If a groupBy method returns a lazy iterable of lazy iterables, it is possible call this method on potentially infinite iterable:

range(0, Infinity).groupBy(e => e % 5) /* [
    [0, 5, 10, ... ],
    [1, 6, 11, ... ],
    [2, 7, 12, ... ],
    ...
]
@xgbuils xgbuils added this to the release 2.0 milestone May 12, 2017
@xgbuils
Copy link
Owner Author

xgbuils commented May 13, 2017

Working with potentially infinite iterables and groupBy implies that we are not able to know how many groups of iterable will be produced. For example, we can infer that this expression:

range(0, Infinity).groupBy(e => e % 3) 

just produces 3 groups ([0, 3, 6...], [1, 4, 7, ...] & [2, 5, 8, ...]), but we can not infer this for a generic function.

Then, I think that there are 2 approaches.

First approach:

const iterable = range(0, Infinity).groupBy(e => e % 3) 
const iterator = iterable[Symbol.iterator]()

const firstGroup = iterator.next().value // potentially [0, 3, 6, ...]
const secondGroup = iterator.next().value // potentially [1, 4, 7, ...]
const thirdGroup = iterator.next().value // potentially [2, 5, 8, ...]
const FourthGroup = iterator.next() // infinite loop because there are not another group and `range(0, Infinity)` never ends.

Second approach:

const iterable = range(0, Infinity).groupBy(e => e % 3) 
const iterator = iterable[Symbol.iterator]()

const firstGroup = iterator.next().value // potentially [0, 3, 6, ...]
const secondGroup = iterator.next().value // potentially [1, 4, 7, ...]
const thirdGroup = iterator.next().value // potentially [2, 5, 8, ...]
const FourthGroup = iterator.next() // returns lazy iterable
const Fifthgroup = iterator.next() // returns lazy iterable

// But then if we try to execute:
const it = FourthGroup[Symbol.iterator]()
it.next() // it produces an infinite loop because does not found any element that matches

Second approach is lazier than first approach. But first approach is more expected. For example, it look likes weird that happens this:

var iterable = Iterum([1, 6, 3, 4, 8, 3]).groupBy(e => e % 3)
/* following the second approach it returns [
    potentially [1, 4],
    potentially [6, 3, 3],
    potentially [8],
    potentially [],
    potentially [],
    potentially [],
    ...
] */
// then if we do:
[...iterable] // it produces an infinite loop and maybe it is more expectable returning [Iterum([1, 4]), Iterum([6, 3, 3]), Iterum([8])]

xgbuils added a commit that referenced this issue May 13, 2017
@xgbuils
Copy link
Owner Author

xgbuils commented May 13, 2017

I implemented groupBy following the firs approach.

@xgbuils xgbuils closed this as completed May 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant