@lsby/ts-fp-data 0.1.4 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,6 +25,80 @@ __export(either_exports, {
25
25
  Right: () => Right
26
26
  });
27
27
  module.exports = __toCommonJS(either_exports);
28
+
29
+ // src/data/maybe.ts
30
+ var Maybe = class {
31
+ static pure(a) {
32
+ return new Just(a);
33
+ }
34
+ };
35
+ var Just = class _Just extends Maybe {
36
+ constructor(\u503C) {
37
+ super();
38
+ this.\u503C = \u503C;
39
+ }
40
+ map(f) {
41
+ return new _Just(f(this.\u503C));
42
+ }
43
+ bind(f) {
44
+ return f(this.\u503C);
45
+ }
46
+ isJust() {
47
+ return true;
48
+ }
49
+ isNothing() {
50
+ return false;
51
+ }
52
+ assertJust() {
53
+ return this;
54
+ }
55
+ assertNothing() {
56
+ throw new Error("\u65AD\u8A00\u5931\u8D25");
57
+ }
58
+ getJust() {
59
+ return this.\u503C;
60
+ }
61
+ getJustOrNull() {
62
+ return this.\u503C;
63
+ }
64
+ match(_onNothing, onJust) {
65
+ return onJust(this.\u503C);
66
+ }
67
+ mplus(_a) {
68
+ return this;
69
+ }
70
+ };
71
+ var Nothing = class _Nothing extends Maybe {
72
+ map(_f) {
73
+ return new _Nothing();
74
+ }
75
+ bind(_f) {
76
+ return new _Nothing();
77
+ }
78
+ isJust() {
79
+ return false;
80
+ }
81
+ isNothing() {
82
+ return true;
83
+ }
84
+ assertJust() {
85
+ throw new Error("\u65AD\u8A00\u5931\u8D25");
86
+ }
87
+ assertNothing() {
88
+ return this;
89
+ }
90
+ getJustOrNull() {
91
+ return null;
92
+ }
93
+ match(onNothing, _onJust) {
94
+ return onNothing();
95
+ }
96
+ mplus(a) {
97
+ return a;
98
+ }
99
+ };
100
+
101
+ // src/data/either.ts
28
102
  var Either = class {
29
103
  static pure(a) {
30
104
  return new Right(a);
@@ -65,6 +139,12 @@ var Left = class _Left extends Either {
65
139
  match(onLeft, _onRight) {
66
140
  return onLeft(this.\u503C);
67
141
  }
142
+ mplus(a) {
143
+ return a;
144
+ }
145
+ toMaybe() {
146
+ return new Nothing();
147
+ }
68
148
  };
69
149
  var Right = class _Right extends Either {
70
150
  constructor(\u503C) {
@@ -101,6 +181,12 @@ var Right = class _Right extends Either {
101
181
  match(_onLeft, onRight) {
102
182
  return onRight(this.\u503C);
103
183
  }
184
+ mplus(_a) {
185
+ return this;
186
+ }
187
+ toMaybe() {
188
+ return new Just(this.\u503C);
189
+ }
104
190
  };
105
191
  // Annotate the CommonJS export names for ESM import in node:
106
192
  0 && (module.exports = {
@@ -1,3 +1,5 @@
1
+ import { Maybe } from './maybe.cjs';
2
+
1
3
  declare abstract class Either<L, R> {
2
4
  static pure<L, R>(a: R): Either<L, R>;
3
5
  abstract map<B>(_f: (a: R) => B): Either<L, B>;
@@ -9,6 +11,8 @@ declare abstract class Either<L, R> {
9
11
  abstract assertLeft(): Left<L, R>;
10
12
  abstract assertRight(): Right<L, R>;
11
13
  abstract match<X>(onLeft: (a: L) => X, onRight: (a: R) => X): X;
14
+ abstract mplus(a: Either<L, R>): Either<L, R>;
15
+ abstract toMaybe(): Maybe<R>;
12
16
  }
13
17
  declare class Left<L, R> extends Either<L, R> {
14
18
  private 值;
@@ -23,6 +27,8 @@ declare class Left<L, R> extends Either<L, R> {
23
27
  assertLeft(): Left<L, R>;
24
28
  assertRight(): Right<L, R>;
25
29
  match<X>(onLeft: (a: L) => X, _onRight: (a: R) => X): X;
30
+ mplus(a: Either<L, R>): Either<L, R>;
31
+ toMaybe(): Maybe<R>;
26
32
  }
27
33
  declare class Right<L, R> extends Either<L, R> {
28
34
  private 值;
@@ -37,6 +43,8 @@ declare class Right<L, R> extends Either<L, R> {
37
43
  assertLeft(): Left<L, R>;
38
44
  assertRight(): Right<L, R>;
39
45
  match<X>(_onLeft: (a: L) => X, onRight: (a: R) => X): X;
46
+ mplus(_a: Either<L, R>): Either<L, R>;
47
+ toMaybe(): Maybe<R>;
40
48
  }
41
49
 
42
50
  export { Either, Left, Right };
@@ -59,9 +59,12 @@ var Just = class _Just extends Maybe {
59
59
  getJustOrNull() {
60
60
  return this.\u503C;
61
61
  }
62
- match(onJust, _onNothing) {
62
+ match(_onNothing, onJust) {
63
63
  return onJust(this.\u503C);
64
64
  }
65
+ mplus(_a) {
66
+ return this;
67
+ }
65
68
  };
66
69
  var Nothing = class _Nothing extends Maybe {
67
70
  map(_f) {
@@ -85,9 +88,12 @@ var Nothing = class _Nothing extends Maybe {
85
88
  getJustOrNull() {
86
89
  return null;
87
90
  }
88
- match(_onJust, onNothing) {
91
+ match(onNothing, _onJust) {
89
92
  return onNothing();
90
93
  }
94
+ mplus(a) {
95
+ return a;
96
+ }
91
97
  };
92
98
  // Annotate the CommonJS export names for ESM import in node:
93
99
  0 && (module.exports = {
@@ -5,9 +5,10 @@ declare abstract class Maybe<A> {
5
5
  abstract isJust(): this is Just<A>;
6
6
  abstract isNothing(): this is Nothing<A>;
7
7
  abstract assertJust(): Just<A>;
8
- abstract assertNothing(): Nothing<A>;
8
+ abstract assertNothing(): Nothing<any>;
9
9
  abstract getJustOrNull(): A | null;
10
- abstract match<B>(onJust: (a: A) => B, onNothing: () => B): B;
10
+ abstract match<B>(onNothing: () => B, onJust: (a: A) => B): B;
11
+ abstract mplus(a: Maybe<A>): Maybe<A>;
11
12
  }
12
13
  declare class Just<A> extends Maybe<A> {
13
14
  private 值;
@@ -20,7 +21,8 @@ declare class Just<A> extends Maybe<A> {
20
21
  assertNothing(): Nothing<A>;
21
22
  getJust(): A;
22
23
  getJustOrNull(): A | null;
23
- match<B>(onJust: (a: A) => B, _onNothing: () => B): B;
24
+ match<B>(_onNothing: () => B, onJust: (a: A) => B): B;
25
+ mplus(_a: Maybe<A>): Maybe<A>;
24
26
  }
25
27
  declare class Nothing<A> extends Maybe<A> {
26
28
  map<B>(_f: (a: A) => B): Maybe<B>;
@@ -30,7 +32,8 @@ declare class Nothing<A> extends Maybe<A> {
30
32
  assertJust(): Just<A>;
31
33
  assertNothing(): Nothing<A>;
32
34
  getJustOrNull(): A | null;
33
- match<B>(_onJust: (a: A) => B, onNothing: () => B): B;
35
+ match<B>(onNothing: () => B, _onJust: (a: A) => B): B;
36
+ mplus(a: Maybe<A>): Maybe<A>;
34
37
  }
35
38
 
36
39
  export { Just, Maybe, Nothing };
@@ -25,6 +25,78 @@ __export(task_exports, {
25
25
  });
26
26
  module.exports = __toCommonJS(task_exports);
27
27
 
28
+ // src/data/maybe.ts
29
+ var Maybe = class {
30
+ static pure(a) {
31
+ return new Just(a);
32
+ }
33
+ };
34
+ var Just = class _Just extends Maybe {
35
+ constructor(\u503C) {
36
+ super();
37
+ this.\u503C = \u503C;
38
+ }
39
+ map(f) {
40
+ return new _Just(f(this.\u503C));
41
+ }
42
+ bind(f) {
43
+ return f(this.\u503C);
44
+ }
45
+ isJust() {
46
+ return true;
47
+ }
48
+ isNothing() {
49
+ return false;
50
+ }
51
+ assertJust() {
52
+ return this;
53
+ }
54
+ assertNothing() {
55
+ throw new Error("\u65AD\u8A00\u5931\u8D25");
56
+ }
57
+ getJust() {
58
+ return this.\u503C;
59
+ }
60
+ getJustOrNull() {
61
+ return this.\u503C;
62
+ }
63
+ match(_onNothing, onJust) {
64
+ return onJust(this.\u503C);
65
+ }
66
+ mplus(_a) {
67
+ return this;
68
+ }
69
+ };
70
+ var Nothing = class _Nothing extends Maybe {
71
+ map(_f) {
72
+ return new _Nothing();
73
+ }
74
+ bind(_f) {
75
+ return new _Nothing();
76
+ }
77
+ isJust() {
78
+ return false;
79
+ }
80
+ isNothing() {
81
+ return true;
82
+ }
83
+ assertJust() {
84
+ throw new Error("\u65AD\u8A00\u5931\u8D25");
85
+ }
86
+ assertNothing() {
87
+ return this;
88
+ }
89
+ getJustOrNull() {
90
+ return null;
91
+ }
92
+ match(onNothing, _onJust) {
93
+ return onNothing();
94
+ }
95
+ mplus(a) {
96
+ return a;
97
+ }
98
+ };
99
+
28
100
  // src/data/either.ts
29
101
  var Either = class {
30
102
  static pure(a) {
@@ -66,6 +138,12 @@ var Left = class _Left extends Either {
66
138
  match(onLeft, _onRight) {
67
139
  return onLeft(this.\u503C);
68
140
  }
141
+ mplus(a) {
142
+ return a;
143
+ }
144
+ toMaybe() {
145
+ return new Nothing();
146
+ }
69
147
  };
70
148
  var Right = class _Right extends Either {
71
149
  constructor(\u503C) {
@@ -102,6 +180,12 @@ var Right = class _Right extends Either {
102
180
  match(_onLeft, onRight) {
103
181
  return onRight(this.\u503C);
104
182
  }
183
+ mplus(_a) {
184
+ return this;
185
+ }
186
+ toMaybe() {
187
+ return new Just(this.\u503C);
188
+ }
105
189
  };
106
190
 
107
191
  // src/data/task.ts
@@ -1,4 +1,5 @@
1
1
  import { Either } from './either.cjs';
2
+ import './maybe.cjs';
2
3
 
3
4
  type TaskDoTypeBase<Env extends Record<string, any>, A> = {
4
5
  let<K extends string, X>(name: K, task: (env: Env) => X): TaskDoTypeNoGet<Env & Record<K, X>, A>;
@@ -20,15 +20,90 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/func/seq.ts
21
21
  var seq_exports = {};
22
22
  __export(seq_exports, {
23
- seqArrayEither: () => seqArrayEither,
23
+ seqArrayEitherCollectRight: () => seqArrayEitherCollectRight,
24
+ seqArrayEitherFirstLeft: () => seqArrayEitherFirstLeft,
25
+ seqArrayMaybeCollectJust: () => seqArrayMaybeCollectJust,
26
+ seqArrayMaybeFirstNothing: () => seqArrayMaybeFirstNothing,
24
27
  seqArrayTask: () => seqArrayTask,
25
28
  seqEitherArray: () => seqEitherArray,
26
- seqEitherPromise: () => seqEitherPromise,
27
29
  seqEitherTask: () => seqEitherTask,
28
- seqMaybePromise: () => seqMaybePromise
30
+ seqMaybeArray: () => seqMaybeArray,
31
+ seqMaybeTask: () => seqMaybeTask
29
32
  });
30
33
  module.exports = __toCommonJS(seq_exports);
31
34
 
35
+ // src/data/maybe.ts
36
+ var Maybe = class {
37
+ static pure(a) {
38
+ return new Just(a);
39
+ }
40
+ };
41
+ var Just = class _Just extends Maybe {
42
+ constructor(\u503C) {
43
+ super();
44
+ this.\u503C = \u503C;
45
+ }
46
+ map(f) {
47
+ return new _Just(f(this.\u503C));
48
+ }
49
+ bind(f) {
50
+ return f(this.\u503C);
51
+ }
52
+ isJust() {
53
+ return true;
54
+ }
55
+ isNothing() {
56
+ return false;
57
+ }
58
+ assertJust() {
59
+ return this;
60
+ }
61
+ assertNothing() {
62
+ throw new Error("\u65AD\u8A00\u5931\u8D25");
63
+ }
64
+ getJust() {
65
+ return this.\u503C;
66
+ }
67
+ getJustOrNull() {
68
+ return this.\u503C;
69
+ }
70
+ match(_onNothing, onJust) {
71
+ return onJust(this.\u503C);
72
+ }
73
+ mplus(_a) {
74
+ return this;
75
+ }
76
+ };
77
+ var Nothing = class _Nothing extends Maybe {
78
+ map(_f) {
79
+ return new _Nothing();
80
+ }
81
+ bind(_f) {
82
+ return new _Nothing();
83
+ }
84
+ isJust() {
85
+ return false;
86
+ }
87
+ isNothing() {
88
+ return true;
89
+ }
90
+ assertJust() {
91
+ throw new Error("\u65AD\u8A00\u5931\u8D25");
92
+ }
93
+ assertNothing() {
94
+ return this;
95
+ }
96
+ getJustOrNull() {
97
+ return null;
98
+ }
99
+ match(onNothing, _onJust) {
100
+ return onNothing();
101
+ }
102
+ mplus(a) {
103
+ return a;
104
+ }
105
+ };
106
+
32
107
  // src/data/either.ts
33
108
  var Either = class {
34
109
  static pure(a) {
@@ -70,6 +145,12 @@ var Left = class _Left extends Either {
70
145
  match(onLeft, _onRight) {
71
146
  return onLeft(this.\u503C);
72
147
  }
148
+ mplus(a) {
149
+ return a;
150
+ }
151
+ toMaybe() {
152
+ return new Nothing();
153
+ }
73
154
  };
74
155
  var Right = class _Right extends Either {
75
156
  constructor(\u503C) {
@@ -106,71 +187,11 @@ var Right = class _Right extends Either {
106
187
  match(_onLeft, onRight) {
107
188
  return onRight(this.\u503C);
108
189
  }
109
- };
110
-
111
- // src/data/maybe.ts
112
- var Maybe = class {
113
- static pure(a) {
114
- return new Just(a);
115
- }
116
- };
117
- var Just = class _Just extends Maybe {
118
- constructor(\u503C) {
119
- super();
120
- this.\u503C = \u503C;
121
- }
122
- map(f) {
123
- return new _Just(f(this.\u503C));
124
- }
125
- bind(f) {
126
- return f(this.\u503C);
127
- }
128
- isJust() {
129
- return true;
130
- }
131
- isNothing() {
132
- return false;
133
- }
134
- assertJust() {
135
- return this;
136
- }
137
- assertNothing() {
138
- throw new Error("\u65AD\u8A00\u5931\u8D25");
139
- }
140
- getJust() {
141
- return this.\u503C;
142
- }
143
- getJustOrNull() {
144
- return this.\u503C;
145
- }
146
- match(onJust, _onNothing) {
147
- return onJust(this.\u503C);
148
- }
149
- };
150
- var Nothing = class _Nothing extends Maybe {
151
- map(_f) {
152
- return new _Nothing();
153
- }
154
- bind(_f) {
155
- return new _Nothing();
156
- }
157
- isJust() {
158
- return false;
159
- }
160
- isNothing() {
161
- return true;
162
- }
163
- assertJust() {
164
- throw new Error("\u65AD\u8A00\u5931\u8D25");
165
- }
166
- assertNothing() {
190
+ mplus(_a) {
167
191
  return this;
168
192
  }
169
- getJustOrNull() {
170
- return null;
171
- }
172
- match(_onJust, onNothing) {
173
- return onNothing();
193
+ toMaybe() {
194
+ return new Just(this.\u503C);
174
195
  }
175
196
  };
176
197
 
@@ -286,31 +307,47 @@ function seqEitherArray(value) {
286
307
  (data) => data.map((a) => new Right(a))
287
308
  );
288
309
  }
289
- function seqArrayEither(value) {
290
- const err = value.filter((a) => a.isLeft())[0];
310
+ function seqArrayEitherFirstLeft(value) {
311
+ var err = value.filter((a) => a.isLeft())[0];
291
312
  if (err)
292
313
  return err;
293
- const data = value.map((a) => a.assertRight().getRight());
314
+ var data = value.map((a) => a.assertRight().getRight());
294
315
  return new Right(data);
295
316
  }
296
- function seqMaybePromise(a) {
317
+ function seqArrayEitherCollectRight(value) {
318
+ return seqArrayEitherFirstLeft(value.filter((a) => a.isRight()));
319
+ }
320
+ function seqArrayMaybeFirstNothing(a) {
321
+ const firstNothing = a.find((maybe) => maybe.isNothing());
322
+ if (firstNothing)
323
+ return new Nothing();
324
+ const values = a.map((maybe) => maybe.assertJust().getJust());
325
+ return new Just(values);
326
+ }
327
+ function seqArrayMaybeCollectJust(a) {
328
+ return seqArrayMaybeFirstNothing(a.filter((a2) => a2.isJust()));
329
+ }
330
+ function seqMaybeArray(a) {
297
331
  return a.match(
298
- async (a2) => new Just(await a2),
299
- async () => new Nothing()
332
+ () => [new Nothing()],
333
+ (array) => array.map((x) => new Just(x))
300
334
  );
301
335
  }
302
- function seqEitherPromise(a) {
336
+ function seqMaybeTask(a) {
303
337
  return a.match(
304
- async (a2) => new Left(a2),
305
- async (a2) => new Right(await a2)
338
+ () => Task.pure(new Nothing()),
339
+ (task) => task.map((a2) => new Just(a2))
306
340
  );
307
341
  }
308
342
  // Annotate the CommonJS export names for ESM import in node:
309
343
  0 && (module.exports = {
310
- seqArrayEither,
344
+ seqArrayEitherCollectRight,
345
+ seqArrayEitherFirstLeft,
346
+ seqArrayMaybeCollectJust,
347
+ seqArrayMaybeFirstNothing,
311
348
  seqArrayTask,
312
349
  seqEitherArray,
313
- seqEitherPromise,
314
350
  seqEitherTask,
315
- seqMaybePromise
351
+ seqMaybeArray,
352
+ seqMaybeTask
316
353
  });
@@ -5,8 +5,11 @@ import { Task } from '../data/task.cjs';
5
5
  declare function seqArrayTask<A>(a: Array<Task<A>>): Task<Array<A>>;
6
6
  declare function seqEitherTask<L, R>(value: Either<L, Task<R>>): Task<Either<L, R>>;
7
7
  declare function seqEitherArray<A, B>(value: Either<A, Array<B>>): Array<Either<A, B>>;
8
- declare function seqArrayEither<A, B>(value: Array<Either<A, B>>): Either<A, Array<B>>;
9
- declare function seqMaybePromise<A>(a: Maybe<Promise<A>>): Promise<Maybe<A>>;
10
- declare function seqEitherPromise<L, R>(a: Either<L, Promise<R>>): Promise<Either<L, R>>;
8
+ declare function seqArrayEitherFirstLeft<A, B>(value: Array<Either<A, B>>): Either<A, Array<B>>;
9
+ declare function seqArrayEitherCollectRight<A, B>(value: Array<Either<A, B>>): Either<A, Array<B>>;
10
+ declare function seqArrayMaybeFirstNothing<X>(a: Array<Maybe<X>>): Maybe<Array<X>>;
11
+ declare function seqArrayMaybeCollectJust<X>(a: Array<Maybe<X>>): Maybe<Array<X>>;
12
+ declare function seqMaybeArray<X>(a: Maybe<Array<X>>): Array<Maybe<X>>;
13
+ declare function seqMaybeTask<X>(a: Maybe<Task<X>>): Task<Maybe<X>>;
11
14
 
12
- export { seqArrayEither, seqArrayTask, seqEitherArray, seqEitherPromise, seqEitherTask, seqMaybePromise };
15
+ export { seqArrayEitherCollectRight, seqArrayEitherFirstLeft, seqArrayMaybeCollectJust, seqArrayMaybeFirstNothing, seqArrayTask, seqEitherArray, seqEitherTask, seqMaybeArray, seqMaybeTask };
@@ -31,15 +31,90 @@ __export(src_exports, {
31
31
  Right: () => Right,
32
32
  Task: () => Task,
33
33
  TaskDo: () => TaskDo,
34
- seqArrayEither: () => seqArrayEither,
34
+ seqArrayEitherCollectRight: () => seqArrayEitherCollectRight,
35
+ seqArrayEitherFirstLeft: () => seqArrayEitherFirstLeft,
36
+ seqArrayMaybeCollectJust: () => seqArrayMaybeCollectJust,
37
+ seqArrayMaybeFirstNothing: () => seqArrayMaybeFirstNothing,
35
38
  seqArrayTask: () => seqArrayTask,
36
39
  seqEitherArray: () => seqEitherArray,
37
- seqEitherPromise: () => seqEitherPromise,
38
40
  seqEitherTask: () => seqEitherTask,
39
- seqMaybePromise: () => seqMaybePromise
41
+ seqMaybeArray: () => seqMaybeArray,
42
+ seqMaybeTask: () => seqMaybeTask
40
43
  });
41
44
  module.exports = __toCommonJS(src_exports);
42
45
 
46
+ // src/data/maybe.ts
47
+ var Maybe = class {
48
+ static pure(a) {
49
+ return new Just(a);
50
+ }
51
+ };
52
+ var Just = class _Just extends Maybe {
53
+ constructor(\u503C) {
54
+ super();
55
+ this.\u503C = \u503C;
56
+ }
57
+ map(f) {
58
+ return new _Just(f(this.\u503C));
59
+ }
60
+ bind(f) {
61
+ return f(this.\u503C);
62
+ }
63
+ isJust() {
64
+ return true;
65
+ }
66
+ isNothing() {
67
+ return false;
68
+ }
69
+ assertJust() {
70
+ return this;
71
+ }
72
+ assertNothing() {
73
+ throw new Error("\u65AD\u8A00\u5931\u8D25");
74
+ }
75
+ getJust() {
76
+ return this.\u503C;
77
+ }
78
+ getJustOrNull() {
79
+ return this.\u503C;
80
+ }
81
+ match(_onNothing, onJust) {
82
+ return onJust(this.\u503C);
83
+ }
84
+ mplus(_a) {
85
+ return this;
86
+ }
87
+ };
88
+ var Nothing = class _Nothing extends Maybe {
89
+ map(_f) {
90
+ return new _Nothing();
91
+ }
92
+ bind(_f) {
93
+ return new _Nothing();
94
+ }
95
+ isJust() {
96
+ return false;
97
+ }
98
+ isNothing() {
99
+ return true;
100
+ }
101
+ assertJust() {
102
+ throw new Error("\u65AD\u8A00\u5931\u8D25");
103
+ }
104
+ assertNothing() {
105
+ return this;
106
+ }
107
+ getJustOrNull() {
108
+ return null;
109
+ }
110
+ match(onNothing, _onJust) {
111
+ return onNothing();
112
+ }
113
+ mplus(a) {
114
+ return a;
115
+ }
116
+ };
117
+
43
118
  // src/data/either.ts
44
119
  var Either = class {
45
120
  static pure(a) {
@@ -81,6 +156,12 @@ var Left = class _Left extends Either {
81
156
  match(onLeft, _onRight) {
82
157
  return onLeft(this.\u503C);
83
158
  }
159
+ mplus(a) {
160
+ return a;
161
+ }
162
+ toMaybe() {
163
+ return new Nothing();
164
+ }
84
165
  };
85
166
  var Right = class _Right extends Either {
86
167
  constructor(\u503C) {
@@ -117,6 +198,12 @@ var Right = class _Right extends Either {
117
198
  match(_onLeft, onRight) {
118
199
  return onRight(this.\u503C);
119
200
  }
201
+ mplus(_a) {
202
+ return this;
203
+ }
204
+ toMaybe() {
205
+ return new Just(this.\u503C);
206
+ }
120
207
  };
121
208
 
122
209
  // src/data/lazy.ts
@@ -151,72 +238,6 @@ var LazyForceValue = class _LazyForceValue extends Lazy {
151
238
  }
152
239
  };
153
240
 
154
- // src/data/maybe.ts
155
- var Maybe = class {
156
- static pure(a) {
157
- return new Just(a);
158
- }
159
- };
160
- var Just = class _Just extends Maybe {
161
- constructor(\u503C) {
162
- super();
163
- this.\u503C = \u503C;
164
- }
165
- map(f) {
166
- return new _Just(f(this.\u503C));
167
- }
168
- bind(f) {
169
- return f(this.\u503C);
170
- }
171
- isJust() {
172
- return true;
173
- }
174
- isNothing() {
175
- return false;
176
- }
177
- assertJust() {
178
- return this;
179
- }
180
- assertNothing() {
181
- throw new Error("\u65AD\u8A00\u5931\u8D25");
182
- }
183
- getJust() {
184
- return this.\u503C;
185
- }
186
- getJustOrNull() {
187
- return this.\u503C;
188
- }
189
- match(onJust, _onNothing) {
190
- return onJust(this.\u503C);
191
- }
192
- };
193
- var Nothing = class _Nothing extends Maybe {
194
- map(_f) {
195
- return new _Nothing();
196
- }
197
- bind(_f) {
198
- return new _Nothing();
199
- }
200
- isJust() {
201
- return false;
202
- }
203
- isNothing() {
204
- return true;
205
- }
206
- assertJust() {
207
- throw new Error("\u65AD\u8A00\u5931\u8D25");
208
- }
209
- assertNothing() {
210
- return this;
211
- }
212
- getJustOrNull() {
213
- return null;
214
- }
215
- match(_onJust, onNothing) {
216
- return onNothing();
217
- }
218
- };
219
-
220
241
  // src/data/task.ts
221
242
  var TaskDo = class _TaskDo {
222
243
  constructor(accTask) {
@@ -329,23 +350,36 @@ function seqEitherArray(value) {
329
350
  (data) => data.map((a) => new Right(a))
330
351
  );
331
352
  }
332
- function seqArrayEither(value) {
333
- const err = value.filter((a) => a.isLeft())[0];
353
+ function seqArrayEitherFirstLeft(value) {
354
+ var err = value.filter((a) => a.isLeft())[0];
334
355
  if (err)
335
356
  return err;
336
- const data = value.map((a) => a.assertRight().getRight());
357
+ var data = value.map((a) => a.assertRight().getRight());
337
358
  return new Right(data);
338
359
  }
339
- function seqMaybePromise(a) {
360
+ function seqArrayEitherCollectRight(value) {
361
+ return seqArrayEitherFirstLeft(value.filter((a) => a.isRight()));
362
+ }
363
+ function seqArrayMaybeFirstNothing(a) {
364
+ const firstNothing = a.find((maybe) => maybe.isNothing());
365
+ if (firstNothing)
366
+ return new Nothing();
367
+ const values = a.map((maybe) => maybe.assertJust().getJust());
368
+ return new Just(values);
369
+ }
370
+ function seqArrayMaybeCollectJust(a) {
371
+ return seqArrayMaybeFirstNothing(a.filter((a2) => a2.isJust()));
372
+ }
373
+ function seqMaybeArray(a) {
340
374
  return a.match(
341
- async (a2) => new Just(await a2),
342
- async () => new Nothing()
375
+ () => [new Nothing()],
376
+ (array) => array.map((x) => new Just(x))
343
377
  );
344
378
  }
345
- function seqEitherPromise(a) {
379
+ function seqMaybeTask(a) {
346
380
  return a.match(
347
- async (a2) => new Left(a2),
348
- async (a2) => new Right(await a2)
381
+ () => Task.pure(new Nothing()),
382
+ (task) => task.map((a2) => new Just(a2))
349
383
  );
350
384
  }
351
385
  // Annotate the CommonJS export names for ESM import in node:
@@ -361,10 +395,13 @@ function seqEitherPromise(a) {
361
395
  Right,
362
396
  Task,
363
397
  TaskDo,
364
- seqArrayEither,
398
+ seqArrayEitherCollectRight,
399
+ seqArrayEitherFirstLeft,
400
+ seqArrayMaybeCollectJust,
401
+ seqArrayMaybeFirstNothing,
365
402
  seqArrayTask,
366
403
  seqEitherArray,
367
- seqEitherPromise,
368
404
  seqEitherTask,
369
- seqMaybePromise
405
+ seqMaybeArray,
406
+ seqMaybeTask
370
407
  });
@@ -2,4 +2,4 @@ export { Either, Left, Right } from './data/either.cjs';
2
2
  export { Lazy, LazyDeferValue, LazyForceValue } from './data/lazy.cjs';
3
3
  export { Just, Maybe, Nothing } from './data/maybe.cjs';
4
4
  export { Task, TaskDo, TaskDoType, TaskDoTypeBase, TaskDoTypeGet, TaskDoTypeIf, TaskDoTypeIfReturn, TaskDoTypeNoGet, TaskDoTypeReturn } from './data/task.cjs';
5
- export { seqArrayEither, seqArrayTask, seqEitherArray, seqEitherPromise, seqEitherTask, seqMaybePromise } from './func/seq.cjs';
5
+ export { seqArrayEitherCollectRight, seqArrayEitherFirstLeft, seqArrayMaybeCollectJust, seqArrayMaybeFirstNothing, seqArrayTask, seqEitherArray, seqEitherTask, seqMaybeArray, seqMaybeTask } from './func/seq.cjs';
@@ -0,0 +1,74 @@
1
+ import {
2
+ Task
3
+ } from "./chunk-IB4BVSOW.js";
4
+ import {
5
+ Left,
6
+ Right
7
+ } from "./chunk-BE7V6GBG.js";
8
+ import {
9
+ Just,
10
+ Nothing
11
+ } from "./chunk-NXA54GUD.js";
12
+
13
+ // src/func/seq.ts
14
+ function seqArrayTask(a) {
15
+ return new Task(async () => {
16
+ return Promise.all(a.map(async (a2) => await a2.run()));
17
+ });
18
+ }
19
+ function seqEitherTask(value) {
20
+ return value.match(
21
+ (err) => Task.pure(new Left(err)),
22
+ (data) => data.map((a) => new Right(a))
23
+ );
24
+ }
25
+ function seqEitherArray(value) {
26
+ return value.match(
27
+ (err) => [new Left(err)],
28
+ (data) => data.map((a) => new Right(a))
29
+ );
30
+ }
31
+ function seqArrayEitherFirstLeft(value) {
32
+ var err = value.filter((a) => a.isLeft())[0];
33
+ if (err)
34
+ return err;
35
+ var data = value.map((a) => a.assertRight().getRight());
36
+ return new Right(data);
37
+ }
38
+ function seqArrayEitherCollectRight(value) {
39
+ return seqArrayEitherFirstLeft(value.filter((a) => a.isRight()));
40
+ }
41
+ function seqArrayMaybeFirstNothing(a) {
42
+ const firstNothing = a.find((maybe) => maybe.isNothing());
43
+ if (firstNothing)
44
+ return new Nothing();
45
+ const values = a.map((maybe) => maybe.assertJust().getJust());
46
+ return new Just(values);
47
+ }
48
+ function seqArrayMaybeCollectJust(a) {
49
+ return seqArrayMaybeFirstNothing(a.filter((a2) => a2.isJust()));
50
+ }
51
+ function seqMaybeArray(a) {
52
+ return a.match(
53
+ () => [new Nothing()],
54
+ (array) => array.map((x) => new Just(x))
55
+ );
56
+ }
57
+ function seqMaybeTask(a) {
58
+ return a.match(
59
+ () => Task.pure(new Nothing()),
60
+ (task) => task.map((a2) => new Just(a2))
61
+ );
62
+ }
63
+
64
+ export {
65
+ seqArrayTask,
66
+ seqEitherTask,
67
+ seqEitherArray,
68
+ seqArrayEitherFirstLeft,
69
+ seqArrayEitherCollectRight,
70
+ seqArrayMaybeFirstNothing,
71
+ seqArrayMaybeCollectJust,
72
+ seqMaybeArray,
73
+ seqMaybeTask
74
+ };
@@ -1,3 +1,8 @@
1
+ import {
2
+ Just,
3
+ Nothing
4
+ } from "./chunk-NXA54GUD.js";
5
+
1
6
  // src/data/either.ts
2
7
  var Either = class {
3
8
  static pure(a) {
@@ -39,6 +44,12 @@ var Left = class _Left extends Either {
39
44
  match(onLeft, _onRight) {
40
45
  return onLeft(this.\u503C);
41
46
  }
47
+ mplus(a) {
48
+ return a;
49
+ }
50
+ toMaybe() {
51
+ return new Nothing();
52
+ }
42
53
  };
43
54
  var Right = class _Right extends Either {
44
55
  constructor(\u503C) {
@@ -75,6 +86,12 @@ var Right = class _Right extends Either {
75
86
  match(_onLeft, onRight) {
76
87
  return onRight(this.\u503C);
77
88
  }
89
+ mplus(_a) {
90
+ return this;
91
+ }
92
+ toMaybe() {
93
+ return new Just(this.\u503C);
94
+ }
78
95
  };
79
96
 
80
97
  export {
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  Left,
3
3
  Right
4
- } from "./chunk-VTI5CRVQ.js";
4
+ } from "./chunk-BE7V6GBG.js";
5
5
 
6
6
  // src/data/task.ts
7
7
  var TaskDo = class _TaskDo {
@@ -33,9 +33,12 @@ var Just = class _Just extends Maybe {
33
33
  getJustOrNull() {
34
34
  return this.\u503C;
35
35
  }
36
- match(onJust, _onNothing) {
36
+ match(_onNothing, onJust) {
37
37
  return onJust(this.\u503C);
38
38
  }
39
+ mplus(_a) {
40
+ return this;
41
+ }
39
42
  };
40
43
  var Nothing = class _Nothing extends Maybe {
41
44
  map(_f) {
@@ -59,9 +62,12 @@ var Nothing = class _Nothing extends Maybe {
59
62
  getJustOrNull() {
60
63
  return null;
61
64
  }
62
- match(_onJust, onNothing) {
65
+ match(onNothing, _onJust) {
63
66
  return onNothing();
64
67
  }
68
+ mplus(a) {
69
+ return a;
70
+ }
65
71
  };
66
72
 
67
73
  export {
@@ -1,3 +1,5 @@
1
+ import { Maybe } from './maybe.js';
2
+
1
3
  declare abstract class Either<L, R> {
2
4
  static pure<L, R>(a: R): Either<L, R>;
3
5
  abstract map<B>(_f: (a: R) => B): Either<L, B>;
@@ -9,6 +11,8 @@ declare abstract class Either<L, R> {
9
11
  abstract assertLeft(): Left<L, R>;
10
12
  abstract assertRight(): Right<L, R>;
11
13
  abstract match<X>(onLeft: (a: L) => X, onRight: (a: R) => X): X;
14
+ abstract mplus(a: Either<L, R>): Either<L, R>;
15
+ abstract toMaybe(): Maybe<R>;
12
16
  }
13
17
  declare class Left<L, R> extends Either<L, R> {
14
18
  private 值;
@@ -23,6 +27,8 @@ declare class Left<L, R> extends Either<L, R> {
23
27
  assertLeft(): Left<L, R>;
24
28
  assertRight(): Right<L, R>;
25
29
  match<X>(onLeft: (a: L) => X, _onRight: (a: R) => X): X;
30
+ mplus(a: Either<L, R>): Either<L, R>;
31
+ toMaybe(): Maybe<R>;
26
32
  }
27
33
  declare class Right<L, R> extends Either<L, R> {
28
34
  private 值;
@@ -37,6 +43,8 @@ declare class Right<L, R> extends Either<L, R> {
37
43
  assertLeft(): Left<L, R>;
38
44
  assertRight(): Right<L, R>;
39
45
  match<X>(_onLeft: (a: L) => X, onRight: (a: R) => X): X;
46
+ mplus(_a: Either<L, R>): Either<L, R>;
47
+ toMaybe(): Maybe<R>;
40
48
  }
41
49
 
42
50
  export { Either, Left, Right };
@@ -2,7 +2,8 @@ import {
2
2
  Either,
3
3
  Left,
4
4
  Right
5
- } from "../chunk-VTI5CRVQ.js";
5
+ } from "../chunk-BE7V6GBG.js";
6
+ import "../chunk-NXA54GUD.js";
6
7
  export {
7
8
  Either,
8
9
  Left,
@@ -5,9 +5,10 @@ declare abstract class Maybe<A> {
5
5
  abstract isJust(): this is Just<A>;
6
6
  abstract isNothing(): this is Nothing<A>;
7
7
  abstract assertJust(): Just<A>;
8
- abstract assertNothing(): Nothing<A>;
8
+ abstract assertNothing(): Nothing<any>;
9
9
  abstract getJustOrNull(): A | null;
10
- abstract match<B>(onJust: (a: A) => B, onNothing: () => B): B;
10
+ abstract match<B>(onNothing: () => B, onJust: (a: A) => B): B;
11
+ abstract mplus(a: Maybe<A>): Maybe<A>;
11
12
  }
12
13
  declare class Just<A> extends Maybe<A> {
13
14
  private 值;
@@ -20,7 +21,8 @@ declare class Just<A> extends Maybe<A> {
20
21
  assertNothing(): Nothing<A>;
21
22
  getJust(): A;
22
23
  getJustOrNull(): A | null;
23
- match<B>(onJust: (a: A) => B, _onNothing: () => B): B;
24
+ match<B>(_onNothing: () => B, onJust: (a: A) => B): B;
25
+ mplus(_a: Maybe<A>): Maybe<A>;
24
26
  }
25
27
  declare class Nothing<A> extends Maybe<A> {
26
28
  map<B>(_f: (a: A) => B): Maybe<B>;
@@ -30,7 +32,8 @@ declare class Nothing<A> extends Maybe<A> {
30
32
  assertJust(): Just<A>;
31
33
  assertNothing(): Nothing<A>;
32
34
  getJustOrNull(): A | null;
33
- match<B>(_onJust: (a: A) => B, onNothing: () => B): B;
35
+ match<B>(onNothing: () => B, _onJust: (a: A) => B): B;
36
+ mplus(a: Maybe<A>): Maybe<A>;
34
37
  }
35
38
 
36
39
  export { Just, Maybe, Nothing };
@@ -2,7 +2,7 @@ import {
2
2
  Just,
3
3
  Maybe,
4
4
  Nothing
5
- } from "../chunk-CQG32QQC.js";
5
+ } from "../chunk-NXA54GUD.js";
6
6
  export {
7
7
  Just,
8
8
  Maybe,
@@ -1,4 +1,5 @@
1
1
  import { Either } from './either.js';
2
+ import './maybe.js';
2
3
 
3
4
  type TaskDoTypeBase<Env extends Record<string, any>, A> = {
4
5
  let<K extends string, X>(name: K, task: (env: Env) => X): TaskDoTypeNoGet<Env & Record<K, X>, A>;
@@ -1,8 +1,9 @@
1
1
  import {
2
2
  Task,
3
3
  TaskDo
4
- } from "../chunk-OYLE35IQ.js";
5
- import "../chunk-VTI5CRVQ.js";
4
+ } from "../chunk-IB4BVSOW.js";
5
+ import "../chunk-BE7V6GBG.js";
6
+ import "../chunk-NXA54GUD.js";
6
7
  export {
7
8
  Task,
8
9
  TaskDo
@@ -5,8 +5,11 @@ import { Task } from '../data/task.js';
5
5
  declare function seqArrayTask<A>(a: Array<Task<A>>): Task<Array<A>>;
6
6
  declare function seqEitherTask<L, R>(value: Either<L, Task<R>>): Task<Either<L, R>>;
7
7
  declare function seqEitherArray<A, B>(value: Either<A, Array<B>>): Array<Either<A, B>>;
8
- declare function seqArrayEither<A, B>(value: Array<Either<A, B>>): Either<A, Array<B>>;
9
- declare function seqMaybePromise<A>(a: Maybe<Promise<A>>): Promise<Maybe<A>>;
10
- declare function seqEitherPromise<L, R>(a: Either<L, Promise<R>>): Promise<Either<L, R>>;
8
+ declare function seqArrayEitherFirstLeft<A, B>(value: Array<Either<A, B>>): Either<A, Array<B>>;
9
+ declare function seqArrayEitherCollectRight<A, B>(value: Array<Either<A, B>>): Either<A, Array<B>>;
10
+ declare function seqArrayMaybeFirstNothing<X>(a: Array<Maybe<X>>): Maybe<Array<X>>;
11
+ declare function seqArrayMaybeCollectJust<X>(a: Array<Maybe<X>>): Maybe<Array<X>>;
12
+ declare function seqMaybeArray<X>(a: Maybe<Array<X>>): Array<Maybe<X>>;
13
+ declare function seqMaybeTask<X>(a: Maybe<Task<X>>): Task<Maybe<X>>;
11
14
 
12
- export { seqArrayEither, seqArrayTask, seqEitherArray, seqEitherPromise, seqEitherTask, seqMaybePromise };
15
+ export { seqArrayEitherCollectRight, seqArrayEitherFirstLeft, seqArrayMaybeCollectJust, seqArrayMaybeFirstNothing, seqArrayTask, seqEitherArray, seqEitherTask, seqMaybeArray, seqMaybeTask };
@@ -1,19 +1,25 @@
1
1
  import {
2
- seqArrayEither,
2
+ seqArrayEitherCollectRight,
3
+ seqArrayEitherFirstLeft,
4
+ seqArrayMaybeCollectJust,
5
+ seqArrayMaybeFirstNothing,
3
6
  seqArrayTask,
4
7
  seqEitherArray,
5
- seqEitherPromise,
6
8
  seqEitherTask,
7
- seqMaybePromise
8
- } from "../chunk-UQKW2A3C.js";
9
- import "../chunk-CQG32QQC.js";
10
- import "../chunk-OYLE35IQ.js";
11
- import "../chunk-VTI5CRVQ.js";
9
+ seqMaybeArray,
10
+ seqMaybeTask
11
+ } from "../chunk-B2G2C6YM.js";
12
+ import "../chunk-IB4BVSOW.js";
13
+ import "../chunk-BE7V6GBG.js";
14
+ import "../chunk-NXA54GUD.js";
12
15
  export {
13
- seqArrayEither,
16
+ seqArrayEitherCollectRight,
17
+ seqArrayEitherFirstLeft,
18
+ seqArrayMaybeCollectJust,
19
+ seqArrayMaybeFirstNothing,
14
20
  seqArrayTask,
15
21
  seqEitherArray,
16
- seqEitherPromise,
17
22
  seqEitherTask,
18
- seqMaybePromise
23
+ seqMaybeArray,
24
+ seqMaybeTask
19
25
  };
@@ -2,4 +2,4 @@ export { Either, Left, Right } from './data/either.js';
2
2
  export { Lazy, LazyDeferValue, LazyForceValue } from './data/lazy.js';
3
3
  export { Just, Maybe, Nothing } from './data/maybe.js';
4
4
  export { Task, TaskDo, TaskDoType, TaskDoTypeBase, TaskDoTypeGet, TaskDoTypeIf, TaskDoTypeIfReturn, TaskDoTypeNoGet, TaskDoTypeReturn } from './data/task.js';
5
- export { seqArrayEither, seqArrayTask, seqEitherArray, seqEitherPromise, seqEitherTask, seqMaybePromise } from './func/seq.js';
5
+ export { seqArrayEitherCollectRight, seqArrayEitherFirstLeft, seqArrayMaybeCollectJust, seqArrayMaybeFirstNothing, seqArrayTask, seqEitherArray, seqEitherTask, seqMaybeArray, seqMaybeTask } from './func/seq.js';
package/dist/esm/index.js CHANGED
@@ -4,27 +4,30 @@ import {
4
4
  LazyForceValue
5
5
  } from "./chunk-NEBLA4MD.js";
6
6
  import {
7
- seqArrayEither,
7
+ seqArrayEitherCollectRight,
8
+ seqArrayEitherFirstLeft,
9
+ seqArrayMaybeCollectJust,
10
+ seqArrayMaybeFirstNothing,
8
11
  seqArrayTask,
9
12
  seqEitherArray,
10
- seqEitherPromise,
11
13
  seqEitherTask,
12
- seqMaybePromise
13
- } from "./chunk-UQKW2A3C.js";
14
- import {
15
- Just,
16
- Maybe,
17
- Nothing
18
- } from "./chunk-CQG32QQC.js";
14
+ seqMaybeArray,
15
+ seqMaybeTask
16
+ } from "./chunk-B2G2C6YM.js";
19
17
  import {
20
18
  Task,
21
19
  TaskDo
22
- } from "./chunk-OYLE35IQ.js";
20
+ } from "./chunk-IB4BVSOW.js";
23
21
  import {
24
22
  Either,
25
23
  Left,
26
24
  Right
27
- } from "./chunk-VTI5CRVQ.js";
25
+ } from "./chunk-BE7V6GBG.js";
26
+ import {
27
+ Just,
28
+ Maybe,
29
+ Nothing
30
+ } from "./chunk-NXA54GUD.js";
28
31
  export {
29
32
  Either,
30
33
  Just,
@@ -37,10 +40,13 @@ export {
37
40
  Right,
38
41
  Task,
39
42
  TaskDo,
40
- seqArrayEither,
43
+ seqArrayEitherCollectRight,
44
+ seqArrayEitherFirstLeft,
45
+ seqArrayMaybeCollectJust,
46
+ seqArrayMaybeFirstNothing,
41
47
  seqArrayTask,
42
48
  seqEitherArray,
43
- seqEitherPromise,
44
49
  seqEitherTask,
45
- seqMaybePromise
50
+ seqMaybeArray,
51
+ seqMaybeTask
46
52
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lsby/ts-fp-data",
3
- "version": "0.1.4",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "require": "./dist/cjs/index.cjs",
@@ -1,58 +0,0 @@
1
- import {
2
- Just,
3
- Nothing
4
- } from "./chunk-CQG32QQC.js";
5
- import {
6
- Task
7
- } from "./chunk-OYLE35IQ.js";
8
- import {
9
- Left,
10
- Right
11
- } from "./chunk-VTI5CRVQ.js";
12
-
13
- // src/func/seq.ts
14
- function seqArrayTask(a) {
15
- return new Task(async () => {
16
- return Promise.all(a.map(async (a2) => await a2.run()));
17
- });
18
- }
19
- function seqEitherTask(value) {
20
- return value.match(
21
- (err) => Task.pure(new Left(err)),
22
- (data) => data.map((a) => new Right(a))
23
- );
24
- }
25
- function seqEitherArray(value) {
26
- return value.match(
27
- (err) => [new Left(err)],
28
- (data) => data.map((a) => new Right(a))
29
- );
30
- }
31
- function seqArrayEither(value) {
32
- const err = value.filter((a) => a.isLeft())[0];
33
- if (err)
34
- return err;
35
- const data = value.map((a) => a.assertRight().getRight());
36
- return new Right(data);
37
- }
38
- function seqMaybePromise(a) {
39
- return a.match(
40
- async (a2) => new Just(await a2),
41
- async () => new Nothing()
42
- );
43
- }
44
- function seqEitherPromise(a) {
45
- return a.match(
46
- async (a2) => new Left(a2),
47
- async (a2) => new Right(await a2)
48
- );
49
- }
50
-
51
- export {
52
- seqArrayTask,
53
- seqEitherTask,
54
- seqEitherArray,
55
- seqArrayEither,
56
- seqMaybePromise,
57
- seqEitherPromise
58
- };