Comments (2)
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])]
from iterum.
I implemented groupBy
following the firs approach.
from iterum.
Related Issues (20)
- Implement lazy cartesian product HOT 3
- implement `power` method
- implement `combinations` method HOT 1
- Removing optional arguments in methods. HOT 4
- removing forEach method
- removing `fromIndex` arguments. HOT 2
- Implement `has` & `nth` methods
- Add benchmarks HOT 4
- permutations method must work with infinite iterables
- remove default behaviour of Iterum functions if first parameter is not an iterable.
- adapt functions to functional style (iterable last) HOT 3
- currying functions
- add powerset method and function
- add variations (k-permutations) method and function
- implement zipLongest & transposeLongest
- implement `tap`
- implement chunk
- implement toString
- Implement cycle
- adapt indexOf to use sameValueZero specification for equality
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from iterum.