xgbuils / iterum Goto Github PK
View Code? Open in Web Editor NEWHandling iterables like lazy arrays.
Handling iterables like lazy arrays.
See wu#chunk & imlazy.splitEvery
See wu#tap
Add powerset method and function following this specification:
All methods except build
and toArray
(and forEach
?) should return Iterum instances. .every
, .indexOf
, .reduce
, .reduceRight
and .some
that return other type of values. They should return a generator that builds an iterator with one iteration with the same value that returns in [email protected]
I would like to implement a new release using ES2015 features.
The current package just allows manipulate generators with 0-arity. There are some similar data structure in es2015: iterables
Iterables are objects that wraps a generator of 0-arity in Symbol.iterator property. Then:
Iterum(generator)
constructor will be deprecated and Iterum(iterable)
will be the new constructor.Iterum
instance will also be an iterable.Then, new branch dev-1.0 has been created and I will be happy to receive any suggestions and constributions.
Thanks.
I going to remove forEach method, use for..of
instead.
Implement zipLongest
and transposeLongest
that do the same as zip
and transpose
but applying transposition until the longest iterable is finished and filling iterables that produces less values with undefined
.
See es-iter#longZip
I found that Iterum
library does not work with babel. The problem is that instanceof
operator is not transformed taking in account that any instanceof operator might be calculated using [Symbol.hasInstance]
method in es2015
.
I will create a PR to solve this tomorrow.
Example:
var Iterum = require('iterum')
var Range = Iterum.Range
var it = Iterum(Range(3, 5))
var mapIt = it.map(function (value) {
return 2 * value
})
mapIt.next() // {value: 6, done: false}
mapIt.next() // {value: 8, done: false}
it.next() // {value: 5, done: false}
it.next() // {value: undefined, done: false}
If next
method of mapIt
is called, next
method of it
is affected.
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, ... ],
...
]
See es-iter#cycle and imlazy.cycle
Now, the Iterum functions behaves like lodash
library.
For example:
_.every(null, function (x) {
x > 3
}) // returns true
It returns true because convert no arrays like null
to empty arrays. Iterum does the same, but I don't want to mantain more this behaviour. If function requires an iterable an is not passed an iterable it will throw an error.
reduce
method and other kind of method that Array implements?I would like to do some breaking changes.
First of all, I prefer that methods that returns lazy iterables of arrays (zip
, cartesian
, permutations
) return iterables of iterables to achieve a real laziness. For example, is not possible to do permutations or cartesian product of potentially infinite iterables.
At second point, I want to use memoization. For example, if we have:
const iterable = Iterum([1, 2, 4, 8, 16, 32, 64, 128])
and then we need to do:
const slice = iterable.slice(6)
const result = slice.concat(slice)
[...result]
The first iterable is traversed twice.
I don't want this behaviour.
Finally, there are some methods like map
, reduce
, every
, etc. that I would like to simplify thus:
The reason is that point 1 can be acomplished using bind
and point 2 can be achieved using zip
combined with other methods. For example:
const iterable = Iterum([1, 3, 5, 7]).map((e, i) => 2 * e + i)
// would be implemented thus:
const {range} = Iterum
const iterable = Iterum([1, 3, 5, 7]).zip(range(0, Infinity)).map(([e, i]) => 2 * e + i)
I would like to implement a power
method that do the cartesian product of an iterable n
times:
Iterum([0, 1]).power(3) /* (
(0 0 0)
(1 0 0)
(0 1 0)
(1 1 0)
(0 0 1)
(1 0 1)
(0 1 1)
(1 1 1)
) */
This method produces the same result like:
Iterum.product([0, 1], [0, 1], [0, 1])
However, Iterum.product
cannot produce a cartesian product of infinite iterables and Iterum.power
can.
Iterum([0, 1]).power(Infinity) /* (
(0, 0, 0, 0, 0, ...)
(1, 0, 0, 0, 0, ...)
(0, 1, 0, 0, 0, ...)
(1, 1, 0, 0, 0, ...)
...
) */
See imlazy toString
includes
and indexOf
has fromIndex
arguments which don't make sense.
In Array approach it is useful to have better performance. But in lazy approach doing iterum.drop(fromIndex).includes(...)
is the same.
I found es-iter library that implements similar methods to this library. But there are mutables. For example:
const Iter = require('es-iter')
var a = new Iter([1, 2, 3])
var b = a.map(e => 2 * e)
[...a] // returns [1, 2, 3]
[...b] // however it returns [] because has been consumed previously
However, I found some performance problems in Iterum#cartesian method vs Iter#product method. When a cartesian product of large number of iterables is calculated, it takes a lot of time:
I will try to solve this issue.
Now, cartesian
method does not allow to be used with infinite iterables. If cartesian
returns an iterable of iterables, this method will be able to be lazy.
I would like to implement a method that returns the combinations of n
values that produces an iterable. For example:
Iterum([1, 2, 3, 4]).combinations(2) /*
(1 2)
(1 3)
(1 4)
(2 3)
(2 4)
(3 4)
) */
I don't know what is the best order to produce values yet
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.