-
Notifications
You must be signed in to change notification settings - Fork 3
/
list-es6.js
118 lines (110 loc) · 2.74 KB
/
list-es6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Description: list data structure
Author: Carlo Capelli
Version: 1.0.0
License: MIT
Copyright (c) 2017,2018 Carlo Capelli
*/
;(function(context) {
"use strict";
class List {
constructor(h, t) {
this.h = h
this.t = t
}
forEach(f) {
for (var s = this, c = 0, r; s; s = s.t, ++c)
if (f && (r = f(s.h, c, this)))
return r
return c
}
concat(y) {
var r = this.copy()
r.last().t = y.copy()
return r
}
slice(start) {
var l = this
while (l.t && start-- > 0)
l = l.t
if (l) return l.copy()
}
map(f) {
var p = 0, c = new List(f(this.h, p++, this)), C = c
for (var l = this.t; l; l = l.t)
c = c.t = new List(f(l.h, p++, this))
return C
}
to(what) {
if (what == undefined) what = []
if (what instanceof Array)
this.forEach(e => { what.push(e) })
if (typeof what == 'string')
what += this.toString()
return what
}
toArray() {
var a = Array(this.len())
this.forEach((e, i) => { a[i] = e })
return a
}
toString(sep) {
return this.toArray().join(sep)
}
len(l, f) {
const c = this.forEach()
if (l == undefined)
return c
if (l < c) { // trim
var s = this
while (--l > 0 && s.t)
s = s.t
delete s.t
}
if (l > c) { // fill
var t = this.last()
if (t.h == undefined && f)
t.h = f(t.h)
while (l-- > c)
t = t.t = f ? new List(f(t.h)) : new List()
}
return this
}
copy() {
var c = new List(this.h), C = c
for (var l = this.t; l; l = l.t)
c = c.t = new List(l.h)
return C
}
last() {
var l = this
while (l.t)
l = l.t
return l
}
static iota(n, from) {
n = n || N
from = from || 0
var l = new List(from), L = l
for(var i = 1; i < n; ++i)
l = l.t = new List(i + from)
return L
}
static from(s, sep) {
if (s instanceof Array && s.length) {
for (var L = new List(s[0]), l = L, i = 1; i < s.length; ++i)
l = l.t = new List(s[i])
return L
}
if (typeof s == 'string') {
if (sep == undefined)
sep = ','
else if (sep == '')
return List.from([... s])
return List.from(s.split(sep))
}
}
}
context.List = List
context.list = function list(h, t) { return new List(h, t) }
})(typeof module !== 'undefined' ? module.exports : self);