forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsmonad.d.ts
640 lines (639 loc) · 20.4 KB
/
tsmonad.d.ts
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Type definitions for TsMonad
// Project: https://github.com/cbowdon/TsMonad
// Definitions by: Chris Bowdon <https://github.com/cbowdon>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module TsMonad {
/**
* @name EitherType
* @description Enumerate the different types contained by an Either object.
*/
enum EitherType {
Left = 0,
Right = 1,
}
/**
* @name EitherPatterns
* @description Define a contract to unwrap Either object using callbacks
* for Left and Right.
* @see Either#
*/
interface EitherPatterns<L, R, T> {
/**
* @name left
* @description Function to handle the Left.
* @type {(l: L) => T}
*/
left: (l: L) => T;
/**
* @name right
* @description Function to handle the Right.
* @type {(r: R) => T}
*/
right: (r: R) => T;
}
/**
* @name either
* @description Build an Either object.
* @function
* @param l The object as a Left (optional).
* @param r The object as a Right (optional).
* @returns {Either<L, R>} Either object containing the input.
* @throws {TypeError} If there are both or none of left and right
* parameter.
* @see Either#
*/
function either<L, R>(l?: L, r?: R): Either<L, R>;
/**
* @name Either
* @class Either has exactly two sub types, Left (L) and Right (R). If an
* Either<L, R> object contains an instance of L, then the Either is a
* Left. Otherwise it contains an instance of R and is a Right. By
* convention, the Left constructor is used to hold an error value and
* the Right constructor is used to hold a correct value.
*/
class Either<L, R> implements Monad<R>, Functor<R>, Eq<Either<L, R>> {
private type;
private l;
private r;
/**
* @description Build an Either object. For internal use only.
* @constructor
* @methodOf Either#
* @param {EitherType} type Indicates if the Either content is a Left or a Right.
* @param {L} l The Left value (optional).
* @param {R} l The Right value (optional).
*/
constructor(type: EitherType, l?: L, r?: R);
/**
* @name left
* @description Helper function to build an Either with a Left.
* @methodOf Either#
* @static
* @param {L} l The Left value.
* @returns {Either<L, R>} Either object containing a Left.
*/
static left<L, R>(l: L): Either<L, R>;
/**
* @name right
* @description Helper function to build an Either with a Right.
* @methodOf Either#
* @static
* @param {R} r The Right value.
* @returns {Either<L, R>} Either object containing a Right.
*/
static right<L, R>(r: R): Either<L, R>;
/**
* @name unit
* @description Wrap a value inside an Either Right object.
* @methodOf Either#
* @public
* @param {T} t
* @returns {Either<L, R>} Either object containing a Right.
* @see Monad#unit
*/
public unit<T>(t: T): Either<L, T>;
/**
* @name bind
* @description Apply the function passed as parameter on the object.
* @methodOf Either#
* @public
* @param {(r: R) => Either<L, T>} f Function applied on the Right.
* @returns {Either<L, T>} The result of the function f wrapped inside
* an Either object.
* @see Monad#bind
*/
public bind<T>(f: (r: R) => Either<L, T>): Either<L, T>;
/**
* @name of
* @description Alias for unit.
* @methodOf Either#
* @public
* @see Either#unit
* @see Monad#of
*/
public of: <T>(t: T) => Either<L, T>;
/**
* @name chain
* @description Alias for bind.
* @methodOf Either#
* @public
* @see Either#bind
* @see Monad#chain
*/
public chain: <T>(f: (r: R) => Either<L, T>) => Either<L, T>;
/**
* @name fmap
* @description Apply the function passed as parameter on the object.
* @methodOf Either#
* @public
* @param {(r: R) => T} f Function applied on the Right.
* @returns {Either<L, T>} The result of the function f wrapped inside
* an Either object.
* @see Functor#fmap
*/
public fmap<T>(f: (r: R) => T): Either<L, T>;
/**
* @name lift
* @description Alias for fmap.
* @methodOf Either#
* @public
* @see Either#fmap
* @see Functor#lift
*/
public lift: <T>(f: (r: R) => T) => Either<L, T>;
/**
* @name map
* @description Alias for fmap.
* @methodOf Either#
* @public
* @see Either#fmap
* @see Functor#map
*/
public map: <T>(f: (r: R) => T) => Either<L, T>;
/**
* @name caseOf
* @description Execute a function depending on the Either content.
* It allows to unwrap the object for Left or Right types.
* @methodOf Either#
* @public
* @param {EitherPatterns<L, R, T>} pattern Object containing the
* functions to applied on each Either types.
* @return {T} The returned value of the functions specified in the
* EitherPatterns interface.
* @see EitherPatterns#
*/
public caseOf<T>(pattern: EitherPatterns<L, R, T>): T;
/**
* @name equals
* @description Compare the type and the content of two Either
* objects.
* @methodOf Either#
* @public
* @param {Either<L, R>} other The Either to compare with.
* @return {boolean} True if the type and content value are equals,
* false otherwise.
* @see Eq#equals
*/
public equals(other: Either<L, R>): any;
}
}
declare module TsMonad {
/**
* @name eq
* @description Compare two objects :
* 1. if objects implement Eq, defer to their .equals
* 2. if are arrays, iterate and recur
* @function
* @param {any} a Any object.
* @param {any} b Any object.
* @returns {boolean} In case 1, the `.equals()` function returned value.
* In case 2, true if each elements are equals, false otherwise.
*/
function eq(a: any, b: any): any;
/**
* @name Eq
* @description Define a contract to compare (in)equalities between
* objects.
*/
interface Eq<T> {
/**
* @name equals
* @description Determine if two objects are equals.
* @methodOf Eq
* @public
* @param {T} The object to compare with.
* @returns {boolean} True if the objects are equals, false otherwise.
*/
equals(t: T): boolean;
}
interface Monad<T> {
/**
* @name unit
* @description Wrap an object inside a monad.
* @methodOf Monad#
* @public
* @param {U} t The object to wrap.
* @returns {Monad<U>} A Monad with the value wrapped inside.
*/
unit<U>(t: U): Monad<U>;
/**
* @name bind
* @description Apply the function passed as parameter on the object.
* @methodOf Monad#
* @public
* @param {(t: T) => Monad<U>} f Function applied on the Monad content.
* @returns {Monad<U>} The result of the function f wrapped inside
* a Monad object.
*/
bind<U>(f: (t: T) => Monad<U>): Monad<U>;
/**
* @name of
* @description Alias for unit. Fantasy Land Monad conformance.
* @methodOf Monad#
* @public
* @see Monad#unit
*/
of<U>(t: U): Monad<U>;
/**
* @name chain
* @description Alias for bind. Fantasy Land Monad conformance.
* @methodOf Monad#
* @public
* @see Monad#bind
*/
chain<U>(f: (t: T) => Monad<U>): Monad<U>;
}
/**
* @name Functor
* @description Define a contract to add basic functor functions to an
* object.
*/
interface Functor<T> {
/**
* @name fmap
* @description Apply the function passed as parameter on the object.
* @methodOf Functor#
* @public
* @param {(t: T) => U} f Function applied on the functor content.
* @returns {Functor<U>} The result of the function f wrapped inside
* an Functor object.
* @see Functor#fmap
*/
fmap<U>(f: (t: T) => U): Functor<U>;
/**
* @name lift
* @description Alias for fmap.
* @methodOf Functor#
* @public
* @see Functor#fmap
*/
lift<U>(f: (t: T) => U): Functor<U>;
/**
* @name map
* @description Alias for fmap. Fantasy Land Monad conformance.
* @methodOf Functor#
* @public
* @see Functor#fmap
*/
map<U>(f: (t: T) => U): Functor<U>;
}
}
declare module TsMonad {
/**
* @name MaybeType
* @description Enumerate the different types contained by an Maybe object.
* @see Maybe#
*/
enum MaybeType {
Nothing = 0,
Just = 1,
}
/**
* @name MaybePatterns
* @description Define a contract to unwrap Maybe object using callbacks
* for Just and Nothing.
* @see Maybe#
*/
interface MaybePatterns<T, U> {
/**
* @name just
* @description Function to handle the Just.
* @type {(t: T) => U}
*/
just: (t: T) => U;
/**
* @name nothing
* @description Function to handle the Nothing.
* @type {() => U}
*/
nothing: () => U;
}
/**
* @name maybe
* @description Build a Maybe object.
* @function
* @param {T} t The object to wrap.
* @returns {Maybe<T>} A Maybe object containing the input. If t is null
* or undefined, the Maybe object is filled with Nothing.
* @see Maybe#
*/
function maybe<T>(t: T): Maybe<T>;
/**
* @name Maybe
* @class Encapsulates an optional value. A value of type Maybe a either
* contains a value of type a (represented as Just a), or it is empty
* (represented as Nothing).
*/
class Maybe<T> implements Monad<T>, Functor<T>, Eq<Maybe<T>> {
private type;
private value;
/**
* @description Build a Maybe object. For internal use only.
* @constructor
* @methodOf Maybe#
* @param {MaybeType} type Indicates if the Maybe content is a Just or a Nothing.
* @param {T} value The value to wrap (optional).
*/
constructor(type: MaybeType, value?: T);
/**
* @name maybe
* @description Helper function to build a Maybe object.
* @methodOf Maybe#
* @static
* @param {T} t The value to wrap.
* @returns {Maybe<T>} A Maybe object containing the value passed in input. If t is null
* or undefined, the Maybe object is filled with Nothing.
*/
static maybe<T>(t: T): Maybe<T>;
/**
* @name just
* @description Helper function to build a Maybe object filled with a
* Just type.
* @methodOf Maybe#
* @static
* @param {T} t The value to wrap.
* @returns {Maybe<T>} A Maybe object containing the value passed in input.
* @throws {TypeError} If t is null or undefined.
*/
static just<T>(t: T): Maybe<T>;
/**
* @name nothing
* @description Helper function to build a Maybe object filled with a
* Nothing type.
* @methodOf Maybe#
* @static
* @returns {Maybe<T>} A Maybe with a Nothing type.
*/
static nothing<T>(): Maybe<T>;
/**
* @name unit
* @description Wrap an object inside a Maybe.
* @public
* @methodOf Maybe#
* @param {U} u The object to wrap.
* @returns {Monad<U>} A Monad with the value wrapped inside.
* @see Monad#unit
*/
public unit<U>(u: U): Maybe<U>;
/**
* @name bind
* @description Apply the function passed as parameter on the object.
* @methodOf Maybe#
* @public
* @param {(t: T) => Maybe<U>} f Function applied on the Maybe content.
* @returns {Maybe<U>} The result of the function f wrapped inside
* a Maybe object.
* @see Monad#bind
*/
public bind<U>(f: (t: T) => Maybe<U>): Maybe<U>;
/**
* @name of
* @description Alias for unit.
* @methodOf Maybe#
* @public
* @see Maybe#unit
* @see Monad#of
*/
public of: <U>(u: U) => Maybe<U>;
/**
* @name chain
* @description Alias for bind.
* @methodOf Maybe#
* @public
* @see Maybe#unit
* @see Monad#of
*/
public chain: <U>(f: (t: T) => Maybe<U>) => Maybe<U>;
/**
* @name fmap
* @description Apply the function passed as parameter on the object.
* @methodOf Maybe#
* @public
* @param {(t: T) => U} f Function applied on the Maybe content.
* @returns {Maybe<U>} The result of the function f wrapped inside
* an Maybe object.
* @see Functor#fmap
*/
public fmap<U>(f: (t: T) => U): Maybe<U>;
/**
* @name lift
* @description Alias for fmap.
* @methodOf Maybe#
* @public
* @see Maybe#fmap
* @see Monad#of
*/
public lift: <U>(f: (t: T) => U) => Maybe<U>;
/**
* @name map
* @description Alias for fmap.
* @methodOf Maybe#
* @public
* @see Maybe#fmap
* @see Monad#of
*/
public map: <U>(f: (t: T) => U) => Maybe<U>;
/**
* @name caseOf
* @description Execute a function depending on the Maybe content. It
* allows to unwrap the object for Just or Nothing types.
* @methodOf Maybe#
* @public
* @param {MaybePatterns<T, U>} pattern Object containing the
* functions to applied on each Maybe types.
* @return {U} The returned value of the functions specified in the
* MaybePatterns interface.
* @see MaybePatterns#
*/
public caseOf<U>(patterns: MaybePatterns<T, U>): U;
/**
* @name equals
* @description Compare the type and the content of two Maybe
* objects.
* @methodOf Maybe#
* @public
* @param {Maybe<T>} other The Maybe to compare with.
* @return {boolean} True if the type and content value are equals,
* false otherwise.
* @see Eq#equals
*/
public equals(other: Maybe<T>): any;
}
}
declare module TsMonad {
/**
* @name WriterPatterns
* @description Define a contract to unwrap Writer object using a
* callback.
* @see Writer#
*/
interface WriterPatterns<S, T, U> {
/**
* @name writer
* @description Function to handle the Writer content.
* @type {(story: S[], value: T) => U}
*/
writer: (story: S[], value: T) => U;
}
/**
* @name writer
* @description Build a Writer object.
* @function
* @param {S[]} story The collection to store logs.
* @param {T} value The object to wrap.
* @returns {Writer<S, T>} A Writer object containing the log collection
* and the wrapped value.
* @see Writer#
*/
function writer<S, T>(story: S[], value: T): Writer<S, T>;
/**
* @name Writer
* @class Allow to do computations while making sure that all the log
* values are combined into one log value that then gets attached to
* the result.
*/
class Writer<S, T> implements Monad<T>, Eq<Writer<S, T>> {
private story;
private value;
/**
* @description Build a Writer object. For internal use only.
* @constructor
* @methodOf Writer#
* @param {S[]} story The collection of logs.
* @param {T} value The object to wrap.
*/
constructor(story: S[], value: T);
/**
* @name writer
* @description Helper function to build a Writer object.
* @methodOf Writer#
* @static
* @param {S[]} story The collection of logs.
* @param {T} value The object to wrap.
* @returns {Writer<S, T>} A Writer object containing the collection of logs
* and the wrapped value.
*/
static writer<S, T>(story: S[], value: T): Writer<S, T>;
/**
* @name writer
* @description Helper function to build a Writer object with the log
* passed in input only.
* @methodOf Writer#
* @static
* @param {S} s A log to store.
* @returns {Writer<S, number>} A Writer object containing the collection of logs
* and a zeroed value.
*/
static tell<S>(s: S): Writer<S, number>;
/**
* @name unit
* @description Wrap an object inside a Writer.
* @public
* @methodOf Writer#
* @param {U} u The object to wrap.
* @returns {Monad<U>} A Writer with the value wrapped inside and an
* empty collection of logs.
* @see Monad#unit
*/
public unit<U>(u: U): Writer<any, U>;
/**
* @name bind
* @description Apply the function passed as parameter on the object.
* @methodOf Writer#
* @public
* @param {(t: T) => Writer<S, U>} f Function applied on the Writer content.
* @returns {Writer<S, U>} The result of the function f append to the
* Writer object.
* @see Monad#bind
*/
public bind<U>(f: (t: T) => Writer<S, U>): Writer<S, U>;
/**
* @name of
* @description Alias for unit.
* @methodOf Writer#
* @public
* @see Writer#unit
* @see Monad#of
*/
public of: <U>(u: U) => Writer<any, U>;
/**
* @name chain
* @description Alias for bind
* @methodOf Writer#
* @public
* @see Writer#unit
* @see Monad#of
*/
public chain: <U>(f: (t: T) => Writer<S, U>) => Writer<S, U>;
/**
* @name fmap
* @description Apply the function passed as parameter on the object.
* @methodOf Writer#
* @public
* @param {(t: T) => U} f Function applied on the wrapped value.
* @returns {Writer<S, U>} The result of the function f wrapped inside
* an Writer object. It has an empty collection of logs.
* @see Functor#fmap
*/
public fmap<U>(f: (t: T) => U): Writer<S, U>;
/**
* @name lift
* @description Alias for fmap
* @methodOf Writer#
* @public
* @see Writer#fmap
* @see Monad#of
*/
public lift: <U>(f: (t: T) => U) => Writer<S, U>;
/**
* @name map
* @description Alias for fmap
* @methodOf Writer#
* @public
* @see Writer#fmap
* @see Monad#of
*/
public map: <U>(f: (t: T) => U) => Writer<S, U>;
/**
* @name caseOf
* @description Execute a function on the Writer content. It allows to
* unwrap the object.
* @methodOf Writer#
* @public
* @param {WriterPatterns<S, T, U>} pattern Object containing the
* functions to applied on the Writer content.
* @return {U} The returned value of the function specified in the
* WriterPatterns interface.
* @see WriterPatterns#
*/
public caseOf<U>(patterns: WriterPatterns<S, T, U>): U;
/**
* @name equals
* @description Compare the type and the content of two Writer
* objects.
* @methodOf Writer#
* @public
* @param {Writer<S, T>} other The Writer to compare with.
* @return {boolean} True if the collection of logs and content value
* are equals, false otherwise.
* @see Eq#equals
*/
public equals(other: Writer<S, T>): boolean;
}
}
declare var module: {
exports: any;
require(id: string): any;
id: string;
filename: string;
loaded: boolean;
parent: any;
children: any[];
};
/**
* @name tsmonad
* @namespace Hold functionalities related to TsMonad library.
*/
declare module 'tsmonad' {
export = TsMonad;
}