/
callbag.go
840 lines (759 loc) · 17 KB
/
callbag.go
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
// All functions are designed to be async/lazy, which means you need to wait for them,
// or it will be killed once the program ends. One option is to wait for the end of the
// stream using ForEach or ToChannel function. All the examples provided the mechanisum
// to wait for the stream to be ended.
package callbag
import (
"sync"
"time"
)
type Type int
const (
Hello Type = iota
Data
Bye
)
func (t Type) String() string {
switch t {
case Hello:
return "Hello"
case Data:
return "Data"
case Bye:
return "Bye"
default:
return "Unknown"
}
}
type Option[T any] struct {
Data T
Err error
Func Func[T]
Type Type
}
type Func[T any] func(opt Option[T])
type number interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64
}
// FromInterval generates a tick and send an incermental integer value every n's `time.Duration`
func FromInterval[T number](period time.Duration) Func[T] {
var down Func[T]
return func(opt Option[T]) {
if opt.Type != Hello {
panic("Expected Hello")
}
down = opt.Func
reqNextItem := make(chan struct{}, 1)
go func() {
var i T
for {
_, ok := <-reqNextItem
if !ok {
return
}
time.Sleep(period)
i++
down(Option[T]{
Type: Data,
Data: i,
})
}
}()
down(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
switch opt.Type {
case Data:
reqNextItem <- struct{}{}
case Bye:
close(reqNextItem)
}
},
})
}
}
// FromSlice if the input is a slice, it can be converted into callbag stream by using
// FromSlice function as a source. Because slice is Finite, the stream will autoamatically
// ends once it processes the last item
func FromSlice[T any](values []T) Func[T] {
var down Func[T]
return func(opt Option[T]) {
if opt.Type != Hello {
panic("Expected Hello")
}
down = opt.Func
reqNextItem := make(chan struct{}, 1)
go func() {
i := 0
for {
_, ok := <-reqNextItem
if !ok {
return
}
if i >= len(values) {
down(Option[T]{
Type: Bye,
})
return
}
down(Option[T]{
Type: Data,
Data: values[i],
})
i++
}
}()
down(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
switch opt.Type {
case Data:
reqNextItem <- struct{}{}
case Bye:
close(reqNextItem)
}
},
})
}
}
// FromRange generates number values from n to m with step value. It will stream will be terminated by the last iteration
func FromRange[T number](a, b, step T) Func[T] {
var down Func[T]
return func(opt Option[T]) {
if opt.Type != Hello {
panic("Expected Hello")
}
down = opt.Func
reqNextItem := make(chan struct{}, 1)
go func() {
for {
_, ok := <-reqNextItem
if !ok {
return
}
if !(a < b) {
down(Option[T]{
Type: Bye,
})
return
}
down(Option[T]{
Type: Data,
Data: a,
})
a += step
}
}()
down(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
switch opt.Type {
case Data:
reqNextItem <- struct{}{}
case Bye:
close(reqNextItem)
}
},
})
}
}
// FromChannel Converts a given channel into callbag stream.
// The stream will be termnated once channel is closed
func FromChannel[T any](ch <-chan T) Func[T] {
var down Func[T]
return func(opt Option[T]) {
if opt.Type != Hello {
panic("Expected Hello")
}
down = opt.Func
reqNextItem := make(chan struct{}, 1)
go func() {
for {
_, ok := <-reqNextItem
if !ok {
return
}
value, ok := <-ch
if !ok {
down(Option[T]{
Type: Bye,
})
return
}
down(Option[T]{
Type: Data,
Data: value,
})
}
}()
down(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
switch opt.Type {
case Data:
reqNextItem <- struct{}{}
case Bye:
close(reqNextItem)
}
},
})
}
}
// Filter this function can be used to filter out any items
// inside stream from going through the stream
func Filter[T any](cond func(value T) bool) func(Func[T]) Func[T] {
var up Func[T]
var down Func[T]
return func(source Func[T]) Func[T] {
return func(opt Option[T]) { // this func will be called by downstream
switch opt.Type {
case Hello:
if down != nil {
panic("Too many Hello calls from downstream")
}
down = opt.Func
source(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
switch opt.Type {
case Hello:
up = opt.Func
down(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
up(opt)
},
})
case Data:
if cond(opt.Data) {
down(opt)
} else {
up(Option[T]{
Type: Data,
})
}
case Bye:
down(opt)
}
},
})
case Data:
up(Option[T]{
Type: Data,
})
case Bye:
up(Option[T]{
Type: Bye,
Err: opt.Err,
})
}
}
}
}
// Map transforms one value to another
func Map[T any, E any](fn func(T) E) func(Func[T]) Func[E] {
var up Func[T]
var down Func[E]
return func(source Func[T]) Func[E] {
return func(opt Option[E]) { // this func will be called by downstream
switch opt.Type {
case Hello:
if down != nil {
panic("Too many Hello calls from downstream")
}
down = opt.Func
source(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
switch opt.Type {
case Hello:
up = opt.Func
down(Option[E]{
Type: Hello,
Func: func(opt Option[E]) {
up(Option[T]{
Type: opt.Type,
Err: opt.Err,
})
},
})
case Data:
down(Option[E]{
Type: Data,
Data: fn(opt.Data),
})
case Bye:
down(Option[E]{
Type: Bye,
Err: opt.Err,
})
}
},
})
case Data:
up(Option[T]{
Type: Data,
})
case Bye:
up(Option[T]{
Type: Bye,
Err: opt.Err,
})
}
}
}
}
// ParallelMap is a special case of map that creates N number of goroutines
// to process the data. It put the processed data back to its original index
// location. The number of goroutines is determined by the length of input
func ParallelMap[T any, E any](fn func(T) E) func(Func[[]T]) Func[[]E] {
process := func(values []T) []E {
results := make([]E, len(values))
wg := sync.WaitGroup{}
for i, value := range values {
wg.Add(1)
go func(i int, value T) {
results[i] = fn(value)
wg.Done()
}(i, value)
}
wg.Wait()
return results
}
return Map(process)
}
// Group is a function that groups N number of items into a single slice item.
// This function is useful when you want to use ParallelMap to process N number and
// once the proces is done, you can use Flatten to flatten the data back to a stream of items
func Group[T any](n int) func(Func[T]) Func[[]T] {
var up Func[T]
var down Func[[]T]
var i int
buffer := make([]T, n)
return func(source Func[T]) Func[[]T] {
return func(opt Option[[]T]) { // this func will be called by downstream
switch opt.Type {
case Hello:
if down != nil {
panic("Too many Hello calls from downstream")
}
down = opt.Func
source(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
switch opt.Type {
case Hello:
up = opt.Func
down(Option[[]T]{
Type: Hello,
Func: func(opt Option[[]T]) {
up(Option[T]{
Type: opt.Type,
Err: opt.Err,
})
},
})
case Data:
buffer[i] = opt.Data
i++
if i == n {
i = 0
down(Option[[]T]{
Type: Data,
Data: buffer,
})
} else {
up(Option[T]{
Type: Data,
})
}
case Bye:
down(Option[[]T]{
Type: Bye,
Err: opt.Err,
})
}
},
})
case Data:
up(Option[T]{
Type: Data,
})
case Bye:
up(Option[T]{
Type: Bye,
Err: opt.Err,
})
}
}
}
}
// Flatten converts a stream of slices into a stream of single item
func Flatten[T any]() func(Func[[]T]) Func[T] {
var up Func[[]T]
var down Func[T]
var values []T
var i int = -1
return func(source Func[[]T]) Func[T] {
return func(opt Option[T]) { // this func will be called by downstream
switch opt.Type {
case Hello:
if down != nil {
panic("Too many Hello calls from downstream")
}
down = opt.Func
source(Option[[]T]{
Type: Hello,
Func: func(opt Option[[]T]) {
switch opt.Type {
case Hello:
up = opt.Func
down(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
switch opt.Type {
case Data:
if i == -1 {
//request data from upstream
up(Option[[]T]{
Type: Data,
})
return
}
i++
if i == len(values) {
i = 0
up(Option[[]T]{
Type: Data,
})
} else {
down(Option[T]{
Type: Data,
Data: values[i],
})
}
}
},
})
case Data:
i = 0
values = opt.Data
down(Option[T]{
Type: Data,
Data: values[i],
})
case Bye:
down(Option[T]{
Type: Bye,
Err: opt.Err,
})
}
},
})
case Bye:
up(Option[[]T]{
Type: Bye,
Err: opt.Err,
})
}
}
}
}
// Take gets the N number of items and stop the stream.
// This function is usful if you are dealing with large or infinite streams
func Take[T any](n int) func(Func[T]) Func[T] {
var up Func[T]
var down Func[T]
var i int
return func(source Func[T]) Func[T] {
return func(opt Option[T]) { // this func will be called by downstream
switch opt.Type {
case Hello:
if down != nil {
panic("Too many Hello calls from downstream")
}
down = opt.Func
source(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
switch opt.Type {
case Hello:
up = opt.Func
down(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
up(Option[T]{
Type: opt.Type,
Err: opt.Err,
})
},
})
case Data:
if !(i < n) {
up(Option[T]{
Type: Bye,
})
down(Option[T]{
Type: Bye,
})
return
}
i++
down(Option[T]{
Type: Data,
Data: opt.Data,
})
case Bye:
down(Option[T]{
Type: Bye,
Err: opt.Err,
})
}
},
})
case Data:
up(Option[T]{
Type: Data,
})
case Bye:
up(Option[T]{
Type: Bye,
Err: opt.Err,
})
}
}
}
}
// ForEach is a sink function and should be used as the last function in a stream
// once the stream is terminated, the done argument will be set to true
func ForEach[T any](fn func(item T, done bool)) func(Func[T]) {
return func(source Func[T]) {
var next Func[T]
source(Option[T]{
Type: Hello,
Func: func(opt Option[T]) {
switch opt.Type {
case Hello:
next = opt.Func
next(Option[T]{
Type: Data,
})
case Data:
fn(opt.Data, false)
next(Option[T]{
Type: Data,
})
case Bye:
fn(opt.Data, true)
}
},
})
}
}
// ToChannel is a special ForEach function that push all the items to a given channel.
// Once the stream is terminated, This fucntion will automatically close the channel
func ToChannel[T any](ch chan<- T) func(Func[T]) {
return ForEach(func(value T, done bool) {
if done {
close(ch)
return
}
ch <- value
})
}
// Pipe is a helper function that let's combined all types of callbag functions.
// There are number of varity of this function, `Pipe2`, `Pipe3`, `Pipe4`, ..., `Pipe10`.
// The N represents the number of callbag function you can combined. The first argument
// must be a source, and the last argument must be a sink function. The rest of the arguemnts
// can be set with all modifier callbag functions.
func Pipe2[A any](
src Func[A],
sink func(Func[A])) {
sink(src)
}
func Pipe3[A any, B any](
src Func[A],
m1 func(Func[A]) Func[B],
sink func(Func[B])) {
sink(m1(src))
}
func Pipe4[A any, B any, C any](
src Func[A],
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
sink func(Func[C])) {
sink(m2(m1(src)))
}
func Pipe5[A any, B any, C any, D any](
src Func[A],
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
sink func(Func[D])) {
sink(m3(m2(m1(src))))
}
func Pipe6[A any, B any, C any, D any, E any](
src Func[A],
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
sink func(Func[E])) {
sink(m4(m3(m2(m1(src)))))
}
func Pipe7[A any, B any, C any, D any, E any, F any](
src Func[A],
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
m5 func(Func[E]) Func[F],
sink func(Func[F])) {
sink(m5(m4(m3(m2(m1(src))))))
}
func Pipe8[A any, B any, C any, D any, E any, F any, G any](
src Func[A],
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
m5 func(Func[E]) Func[F],
m6 func(Func[F]) Func[G],
sink func(Func[G])) {
sink(m6(m5(m4(m3(m2(m1(src)))))))
}
func Pipe9[A any, B any, C any, D any, E any, F any, G any, H any](
src Func[A],
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
m5 func(Func[E]) Func[F],
m6 func(Func[F]) Func[G],
m7 func(Func[G]) Func[H],
sink func(Func[H])) {
sink(m7(m6(m5(m4(m3(m2(m1(src))))))))
}
func Pipe10[A any, B any, C any, D any, E any, F any, G any, H any, I any](
src Func[A],
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
m5 func(Func[E]) Func[F],
m6 func(Func[F]) Func[G],
m7 func(Func[G]) Func[H],
m8 func(Func[H]) Func[I],
sink func(Func[I])) {
sink(m8(m7(m6(m5(m4(m3(m2(m1(src)))))))))
}
// Similar to Pipe, Compose is a helper function to compose multiple modifier callbag functions.
// The compose functions can not accept either of soucre or sink functions
func Compose2[A any, B any, C any](
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
) func(Func[A]) Func[C] {
return func(in Func[A]) Func[C] {
return m2(m1(in))
}
}
func Compose3[A any, B any, C any, D any](
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
) func(Func[A]) Func[D] {
return func(in Func[A]) Func[D] {
return m3(m2(m1(in)))
}
}
func Compose4[A any, B any, C any, D any, E any](
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
) func(Func[A]) Func[E] {
return func(in Func[A]) Func[E] {
return m4(m3(m2(m1(in))))
}
}
func Compose5[A any, B any, C any, D any, E any, F any](
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
m5 func(Func[E]) Func[F],
) func(Func[A]) Func[F] {
return func(in Func[A]) Func[F] {
return m5(m4(m3(m2(m1(in)))))
}
}
func Compose6[A any, B any, C any, D any, E any, F any, G any](
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
m5 func(Func[E]) Func[F],
m6 func(Func[F]) Func[G],
) func(Func[A]) Func[G] {
return func(in Func[A]) Func[G] {
return m6(m5(m4(m3(m2(m1(in))))))
}
}
func Compose7[A any, B any, C any, D any, E any, F any, G any, H any](
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
m5 func(Func[E]) Func[F],
m6 func(Func[F]) Func[G],
m7 func(Func[G]) Func[H],
) func(Func[A]) Func[H] {
return func(in Func[A]) Func[H] {
return m7(m6(m5(m4(m3(m2(m1(in)))))))
}
}
func Compose8[A any, B any, C any, D any, E any, F any, G any, H any, I any](
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
m5 func(Func[E]) Func[F],
m6 func(Func[F]) Func[G],
m7 func(Func[G]) Func[H],
m8 func(Func[H]) Func[I],
) func(Func[A]) Func[I] {
return func(in Func[A]) Func[I] {
return m8(m7(m6(m5(m4(m3(m2(m1(in))))))))
}
}
func Compose9[A any, B any, C any, D any, E any, F any, G any, H any, I any, J any](
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
m5 func(Func[E]) Func[F],
m6 func(Func[F]) Func[G],
m7 func(Func[G]) Func[H],
m8 func(Func[H]) Func[I],
m9 func(Func[I]) Func[J],
) func(Func[A]) Func[J] {
return func(in Func[A]) Func[J] {
return m9(m8(m7(m6(m5(m4(m3(m2(m1(in)))))))))
}
}
func Compose10[A any, B any, C any, D any, E any, F any, G any, H any, I any, J any, K any](
m1 func(Func[A]) Func[B],
m2 func(Func[B]) Func[C],
m3 func(Func[C]) Func[D],
m4 func(Func[D]) Func[E],
m5 func(Func[E]) Func[F],
m6 func(Func[F]) Func[G],
m7 func(Func[G]) Func[H],
m8 func(Func[H]) Func[I],
m9 func(Func[I]) Func[J],
m10 func(Func[J]) Func[K],
) func(Func[A]) Func[K] {
return func(in Func[A]) Func[K] {
return m10(m9(m8(m7(m6(m5(m4(m3(m2(m1(in))))))))))
}
}