Coder Social home page Coder Social logo

Comments (2)

xgbuils avatar xgbuils commented on July 2, 2024

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.

xgbuils avatar xgbuils commented on July 2, 2024

I implemented groupBy following the firs approach.

from iterum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.