svghmi/pythonic.js
author GP Orcullo <kinsamanka@gmail.com>
Thu, 10 Nov 2022 23:21:22 +0800
branchpython3
changeset 3782 39480d9d3d3f
parent 3594 30f7eade322f
permissions -rw-r--r--
OSX: use wall time instead of system clock
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
     1
/*
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
     2
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
     3
From https://github.com/keyvan-m-sadeghi/pythonic
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
     4
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
     5
Slightly modified in order to be usable in browser (i.e. not as a node.js module)
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
     6
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
     7
The MIT License (MIT)
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
     8
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
     9
Copyright (c) 2016 Assister.Ai
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    10
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    11
Permission is hereby granted, free of charge, to any person obtaining a copy of
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    12
this software and associated documentation files (the "Software"), to deal in
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    13
the Software without restriction, including without limitation the rights to
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    14
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    15
the Software, and to permit persons to whom the Software is furnished to do so,
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    16
subject to the following conditions:
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    17
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    18
The above copyright notice and this permission notice shall be included in all
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    19
copies or substantial portions of the Software.
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    20
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    21
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    22
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    23
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    24
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    25
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    26
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    27
*/
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    28
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    29
class Iterator {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    30
    constructor(generator) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    31
        this[Symbol.iterator] = generator;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    32
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    33
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    34
    async * [Symbol.asyncIterator]() {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    35
        for (const element of this) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    36
            yield await element;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    37
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    38
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    39
3485
5f417d3c2d03 SVGHMI: Add forEach to local copy of pythonic.js
Edouard Tisserant
parents: 3484
diff changeset
    40
    forEach(callback) {
5f417d3c2d03 SVGHMI: Add forEach to local copy of pythonic.js
Edouard Tisserant
parents: 3484
diff changeset
    41
        for (const element of this) {
5f417d3c2d03 SVGHMI: Add forEach to local copy of pythonic.js
Edouard Tisserant
parents: 3484
diff changeset
    42
            callback(element);
5f417d3c2d03 SVGHMI: Add forEach to local copy of pythonic.js
Edouard Tisserant
parents: 3484
diff changeset
    43
        }
5f417d3c2d03 SVGHMI: Add forEach to local copy of pythonic.js
Edouard Tisserant
parents: 3484
diff changeset
    44
    }
5f417d3c2d03 SVGHMI: Add forEach to local copy of pythonic.js
Edouard Tisserant
parents: 3484
diff changeset
    45
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    46
    map(callback) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    47
        const result = [];
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    48
        for (const element of this) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    49
            result.push(callback(element));
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    50
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    51
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    52
        return result;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    53
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    54
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    55
    filter(callback) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    56
        const result = [];
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    57
        for (const element of this) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    58
            if (callback(element)) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    59
                result.push(element);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    60
            }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    61
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    62
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    63
        return result;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    64
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    65
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    66
    reduce(callback, initialValue) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    67
        let empty = typeof initialValue === 'undefined';
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    68
        let accumulator = initialValue;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    69
        let index = 0;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    70
        for (const currentValue of this) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    71
            if (empty) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    72
                accumulator = currentValue;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    73
                empty = false;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    74
                continue;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    75
            }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    76
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    77
            accumulator = callback(accumulator, currentValue, index, this);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    78
            index++;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    79
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    80
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    81
        if (empty) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    82
            throw new TypeError('Reduce of empty Iterator with no initial value');
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    83
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    84
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    85
        return accumulator;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    86
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    87
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    88
    some(callback) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    89
        for (const element of this) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    90
            if (callback(element)) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    91
                return true;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    92
            }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    93
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    94
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    95
        return false;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    96
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    97
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    98
    every(callback) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
    99
        for (const element of this) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   100
            if (!callback(element)) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   101
                return false;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   102
            }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   103
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   104
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   105
        return true;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   106
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   107
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   108
    static fromIterable(iterable) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   109
        return new Iterator(function * () {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   110
            for (const element of iterable) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   111
                yield element;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   112
            }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   113
        });
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   114
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   115
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   116
    toArray() {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   117
        return Array.from(this);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   118
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   119
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   120
    next() {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   121
        if (!this.currentInvokedGenerator) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   122
            this.currentInvokedGenerator = this[Symbol.iterator]();
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   123
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   124
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   125
        return this.currentInvokedGenerator.next();
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   126
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   127
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   128
    reset() {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   129
        delete this.currentInvokedGenerator;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   130
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   131
}
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   132
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   133
function rangeSimple(stop) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   134
    return new Iterator(function * () {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   135
        for (let i = 0; i < stop; i++) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   136
            yield i;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   137
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   138
    });
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   139
}
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   140
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   141
function rangeOverload(start, stop, step = 1) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   142
    return new Iterator(function * () {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   143
        for (let i = start; i < stop; i += step) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   144
            yield i;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   145
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   146
    });
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   147
}
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   148
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   149
function range(...args) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   150
    if (args.length < 2) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   151
        return rangeSimple(...args);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   152
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   153
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   154
    return rangeOverload(...args);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   155
}
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   156
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   157
function enumerate(iterable) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   158
    return new Iterator(function * () {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   159
        let index = 0;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   160
        for (const element of iterable) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   161
            yield [index, element];
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   162
            index++;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   163
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   164
    });
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   165
}
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   166
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   167
const _zip = longest => (...iterables) => {
3594
30f7eade322f SVGHMI: add support for "enable expressions" with arbitrary variable name assignment.
Edouard Tisserant
parents: 3485
diff changeset
   168
    if (iterables.length == 0) {
30f7eade322f SVGHMI: add support for "enable expressions" with arbitrary variable name assignment.
Edouard Tisserant
parents: 3485
diff changeset
   169
        // works starting with 1 iterable
30f7eade322f SVGHMI: add support for "enable expressions" with arbitrary variable name assignment.
Edouard Tisserant
parents: 3485
diff changeset
   170
        // [a,b,c] -> [[a],[b],[c]]
30f7eade322f SVGHMI: add support for "enable expressions" with arbitrary variable name assignment.
Edouard Tisserant
parents: 3485
diff changeset
   171
        // [a,b,c],[d,e,f] -> [[a,d],[b,e],[c,f]]
30f7eade322f SVGHMI: add support for "enable expressions" with arbitrary variable name assignment.
Edouard Tisserant
parents: 3485
diff changeset
   172
        throw new TypeError("zip takes 1 iterables at least, "+iterables.length+" given");
3484
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   173
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   174
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   175
    return new Iterator(function * () {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   176
        const iterators = iterables.map(iterable => Iterator.fromIterable(iterable));
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   177
        while (true) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   178
            const row = iterators.map(iterator => iterator.next());
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   179
            const check = longest ? row.every.bind(row) : row.some.bind(row);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   180
            if (check(next => next.done)) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   181
                return;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   182
            }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   183
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   184
            yield row.map(next => next.value);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   185
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   186
    });
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   187
};
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   188
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   189
const zip = _zip(false), zipLongest= _zip(true);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   190
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   191
function items(obj) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   192
    let {keys, get} = obj;
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   193
    if (obj instanceof Map) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   194
        keys = keys.bind(obj);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   195
        get = get.bind(obj);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   196
    } else {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   197
        keys = function () {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   198
            return Object.keys(obj);
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   199
        };
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   200
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   201
        get = function (key) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   202
            return obj[key];
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   203
        };
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   204
    }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   205
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   206
    return new Iterator(function * () {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   207
        for (const key of keys()) {
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   208
            yield [key, get(key)];
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   209
        }
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   210
    });
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   211
}
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   212
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   213
/*
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   214
module.exports = {Iterator, range, enumerate, zip: _zip(false), zipLongest: _zip(true), items};
32eaba9cf30e SVGHMI: many fixes on xy trend graph. WIP.
Edouard Tisserant
parents:
diff changeset
   215
*/