mirror of
https://github.com/flynx/types.js.git
synced 2025-10-29 02:20:07 +00:00
more work on extending generators...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
ab5067ba46
commit
b4c5effbd2
1
Array.js
1
Array.js
@ -447,6 +447,7 @@ function(func, ...arrays){
|
||||
// -> iterator
|
||||
//
|
||||
//
|
||||
// XXX should this take an argument and be like map??
|
||||
// XXX revise name
|
||||
Array.prototype.iter = function*(){
|
||||
for(var e of this){
|
||||
|
||||
102
Generator.js
102
Generator.js
@ -48,44 +48,66 @@ var makeGenerator = function(name){
|
||||
var that = this
|
||||
return function*(){
|
||||
yield* that(...arguments)[name](...args) } } }
|
||||
var makePromise = function(name){
|
||||
return function(...args){
|
||||
var that = this
|
||||
return function(){
|
||||
return that(...arguments)[name](func) } } }
|
||||
|
||||
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
// XXX need testing...
|
||||
Generator.at = makeGenerator('at')
|
||||
Generator.slice = makeGenerator('slice')
|
||||
Generator.flat = makeGenerator('flat')
|
||||
Generator.toArray = function(){
|
||||
var that = this
|
||||
return function(){
|
||||
return that(...arguments).toArray() } }
|
||||
|
||||
Generator.map = makeGenerator('map')
|
||||
Generator.filter = makeGenerator('filter')
|
||||
Generator.reduce = makeGenerator('reduce')
|
||||
Generator.flat = makeGenerator('flat')
|
||||
|
||||
Generator.then = function(...args){
|
||||
var that = this
|
||||
return function(){
|
||||
return that(...arguments).then(func) } }
|
||||
|
||||
Generator.toArray = function(){
|
||||
var that = this
|
||||
return function(){
|
||||
return that(...arguments).toArray() } }
|
||||
Generator.then = makePromise('then')
|
||||
Generator.catch = makePromise('catch')
|
||||
Generator.finally = makePromise('finally')
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// Generator instance methods...
|
||||
|
||||
Generator.prototype.map = function*(func){
|
||||
Generator.prototype.at = function*(i){
|
||||
// sanity check...
|
||||
if(i < 0){
|
||||
throw new Error('.at(..): '
|
||||
+'generator index can\'t be a negative value.')}
|
||||
for(var e of this){
|
||||
if(i-- == 0){
|
||||
yield e
|
||||
return } } },
|
||||
|
||||
// NOTE: this is different from Array's .slice(..) in that it does not
|
||||
// support negative indexes -- this is done because there is no way
|
||||
// to judge the length of a generator untill it is fully done...
|
||||
Generator.prototype.slice = function*(from=0, to=Infity){
|
||||
// sanity check...
|
||||
if(from < 0 || to < 0){
|
||||
throw new Error('.slice(..): '
|
||||
+'generator form/to indexes can\'t be negative values.')}
|
||||
var i = 0
|
||||
for(var e of this){
|
||||
yield func(e, i++, this) } }
|
||||
Generator.prototype.filter = function*(func){
|
||||
var i = 0
|
||||
for(var e of this){
|
||||
if(func(e, i++, this)){
|
||||
yield e } } }
|
||||
Generator.prototype.reduce = function*(func, res){
|
||||
var i = 0
|
||||
for(var e of this){
|
||||
res = func(res, e, i++, this) }
|
||||
yield res }
|
||||
// stop at end of seq...
|
||||
if(i >= to){
|
||||
return }
|
||||
// only yield from from...
|
||||
if(i >= from){
|
||||
yield e }
|
||||
i++ } },
|
||||
Generator.prototype.flat = function*(depth=1){
|
||||
if(depth == 0){
|
||||
return this }
|
||||
@ -111,20 +133,38 @@ Generator.prototype.flat = function*(depth=1){
|
||||
|
||||
} else {
|
||||
yield e } } }
|
||||
|
||||
Generator.prototype.then = function(func){
|
||||
var that = this
|
||||
return new Promise(function(resolve){
|
||||
resolve([...that]) }) }
|
||||
|
||||
Generator.prototype.toArray = function(){
|
||||
return [...this] }
|
||||
|
||||
Generator.prototype.map = function*(func){
|
||||
var i = 0
|
||||
for(var e of this){
|
||||
yield func(e, i++, this) } }
|
||||
Generator.prototype.filter = function*(func){
|
||||
var i = 0
|
||||
for(var e of this){
|
||||
if(func(e, i++, this)){
|
||||
yield e } } }
|
||||
Generator.prototype.reduce = function*(func, res){
|
||||
var i = 0
|
||||
for(var e of this){
|
||||
res = func(res, e, i++, this) }
|
||||
yield res }
|
||||
|
||||
// promise results...
|
||||
//
|
||||
// XXX how do we handle reject(..) / .catch(..)???
|
||||
Generator.prototype.promise = function(){
|
||||
var that = this
|
||||
return new Promise(function(resolve){
|
||||
resolve([...that]) }) }
|
||||
Generator.prototype.then = function(func){
|
||||
return this.promise().then(func) }
|
||||
Generator.prototype.catch = function(func){
|
||||
return this.promise().catch(func) }
|
||||
Generator.prototype.finally = function(func){
|
||||
return this.promise().finally(func) }
|
||||
|
||||
// XXX these are potentially bad because with negative indexes well need
|
||||
// to run throu the whole iterator and cache the data...
|
||||
//Generator.prototype.slice = function(from=0, to=-1){ }
|
||||
//Generator.prototype.reverse = function(){ }
|
||||
|
||||
|
||||
|
||||
|
||||
32
Promise.js
32
Promise.js
@ -164,9 +164,35 @@ var IterablePromise =
|
||||
module.IterablePromise =
|
||||
Promise.iter =
|
||||
object.Constructor('IterablePromise', Promise, {
|
||||
map: function(){},
|
||||
filter: function(){},
|
||||
reduce: function(){},
|
||||
// XXX
|
||||
__list: null,
|
||||
|
||||
map: function(func){
|
||||
return IterablePromise() },
|
||||
filter: function(func){},
|
||||
reduce: function(func, res){},
|
||||
flat: function(){},
|
||||
|
||||
all: function(){},
|
||||
|
||||
__new__: function(_, list, handler){
|
||||
// instance...
|
||||
var obj = Reflect.construct(IterablePromise.__proto__, [
|
||||
function(resolve, reject){
|
||||
// NOTE: this is here for Promise compatibilty...
|
||||
// XXX this can resolve/reject a promise -- need to
|
||||
// keep things consistent...
|
||||
if(typeof(list) == 'function'){
|
||||
return func.call(this, ...arguments) }
|
||||
|
||||
// XXX
|
||||
var res = []
|
||||
for(var e of list){
|
||||
}
|
||||
}],
|
||||
IterablePromise)
|
||||
|
||||
return obj },
|
||||
})
|
||||
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-types",
|
||||
"version": "3.4.1",
|
||||
"version": "3.4.2",
|
||||
"description": "Generic JavaScript types and type extensions...",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user