-
Notifications
You must be signed in to change notification settings - Fork 14
/
Lists.bas
1651 lines (1245 loc) · 39 KB
/
Lists.bas
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'@desc Util Class Lists
'@author Xiou Yang
'@license MIT
'@lastUpdate 13.08.2024
' bugfix slice
' load and heading
' sliceBy
' toDicts = new Map() js
'@TODO optional params
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Private pArr() As Variant ' the underlying array object
Private pMaxLen As Long ' the maximal length of array object
Private pLen As Long ' the length of current List Object
Private pRes ' res for the callback map/reduce/filter
Private pHeading() As Variant
Public Property Let callback(res)
If IsObject(res) Then
Set pRes = res
Else
pRes = res
End If
End Property
Public Property Get callback()
If IsObject(pRes) Then
Set callback = pRes
Else
callback = pRes
End If
End Property
Public Property Get length() As Long
length = pLen
End Property
Public Property Get heading() As Variant
heading = pHeading
End Property
Public Property Let heading(res)
pHeading = res
End Property
Public Function isOneDimensional() As Boolean
If pLen = 0 Then
isOneDimensional = True
Else
If IsArray(pArr(0)) Then
isOneDimensional = False
ElseIf Me.isLists(pArr(0)) Then
isOneDimensional = False
Else
isOneDimensional = True
End If
End If
End Function
Public Function init() As Lists
pMaxLen = 20
pLen = 0
ReDim pArr(0 To pMaxLen - 1)
Set init = Me
End Function
Public Function toDict() As Dicts
Dim res As New Dicts
Dim i
For i = 0 To pLen - 1
res.dict.add Me.getVal(i), i
Next i
Set toDict = res
Set res = Nothing
End Function
'[[k, v]] -> Dict
Public Function toDicts() As Dicts
Dim res As New Dicts
Dim i
For i = 0 To pLen - 1
If IsArray(pArr(i)) Then
res.add pArr(i)(0), pArr(i)(1)
Else
res.add pArr(i).getVal(0), pArr(i).getVal(1)
End If
Next i
Set toDicts = res
Set res = Nothing
End Function
' index to array
Public Function toMap(Optional ByVal asArr As Boolean = False) As Dicts
Dim res As New Dicts
Dim i
For i = 0 To pLen - 1
If asArr Then
res.dict.add i, Me.getVal(i).toArray
Else
res.dict.add i, Me.getVal(i)
End If
Next i
Set toMap = res
Set res = Nothing
End Function
Public Function clear() As Lists
Me.init
End Function
Private Sub Class_Initialize()
Me.init
End Sub
Private Sub check()
If pLen > pMaxLen * 0.75 Then
pMaxLen = Int(pMaxLen * 1.5)
ReDim Preserve pArr(0 To pMaxLen - 1)
End If
End Sub
Private Sub override(ByRef list As Lists)
pLen = list.length
pArr = list.toArray
pMaxLen = UBound(pArr) + 1
End Sub
Public Function isEmptyList() As Boolean
isEmptyList = True
Dim i
For i = 0 To pLen - 1
If Not isEmpty(pArr(i)) Then
isEmptyList = False
Exit For
End If
Next i
End Function
Private Function isInstance(obj, ByVal sign) As Boolean
isInstance = TypeName(obj) = sign
End Function
Public Function isLists(testObj As Variant) As Boolean
isLists = TypeName(testObj) = "Lists"
End Function
Function random(from As Long, till As Long) As Long
random = Int(Rnd() * (till - from + 1)) + from
End Function
' change the Lists-Obj in place
Function shuffle() As Lists
Dim i
Dim l As Long
l = Me.length - 1
For i = 0 To l
Me.swap i, random(0, l)
Next i
Set shuffle = Me
End Function
Public Function load(ByVal rng As Range, Optional ByVal hasHeading As Boolean = True, Optional ByVal asAddress As Boolean = False) As Lists
If hasHeading Then
Set load = Me.fromRng(rng, "v", False, asAddress).drop(1)
load.heading = Me.fromRng(rng.Rows(1), "v", True).toArray
Else
Set load = Me.fromRng(rng, "v", False, asAddress)
End If
End Function
Public Function loadSht(ByVal shtName As String, Optional ByVal hasHeading As Boolean = True, Optional ByRef wb As Workbook = Nothing, Optional ByVal asAddress As Boolean = False) As Lists
If wb Is Nothing Then
Set wb = ActiveWorkbook
End If
If shtName = "" Then
Set loadSht = load(ActiveSheet.UsedRange, hasHeading, asAddress)
Else
Set loadSht = load(wb.Worksheets(shtName).UsedRange, hasHeading, asAddress)
End If
End Function
'in case of 1 * N or N * 1 matrix and unboxing set to true, return 1-dimensional array
Public Function fromRng(ByRef rng As Range, Optional ByVal orientation As String = "v", Optional ByVal unboxing As Boolean = True, Optional ByVal asAddress As Boolean = False) As Lists
Dim res As New Lists
Dim rowNum As Long
rowNum = rng.Rows.Count
Dim colNum As Long
colNum = rng.Columns.Count
Dim i
If unboxing And (rowNum = 1 Or colNum = 1) Then
If asAddress Then
Dim c
For Each c In rng.Cells
res.add c.Address
Next c
Else
res.addAll rng
End If
Else
Dim tmp As New Lists
For i = 1 To rowNum
res.add tmp.fromRng(rng.Rows(i), asAddress:=asAddress)
Next i
End If
If orientation = "h" Or orientation = "H" Then
If unboxing And (rowNum = 1 Or colNum = 1) Then
Dim res1 As New Lists
Dim tmp1 As New Lists
For i = 0 To res.length - 1
tmp1.init
res1.add tmp1.add(res.getVal(i), False)
Set tmp1 = Nothing
Next i
Set res = res1
Set res1 = Nothing
Else
Set res = res.zipMe
End If
End If
Set fromRng = res
End Function
Public Function toRng(ByRef rng As Range)
If Me.length > 0 Then
Dim y
y = pLen
If y = 1 Then
rng.Resize(1, pArr(0).length).value = Me.toArray
Else
Dim lenArr As New Lists
Dim i
For i = 0 To pLen - 1
If isInstance(pArr(i), "Lists") Then
lenArr.add pArr(i).length
Else
lenArr.add 1
End If
Next i
Dim maxLen As Long
maxLen = lenArr.max_
rng.Resize(1, maxLen).Cells.clear
For i = 0 To pLen - 1
If isInstance(pArr(i), "Lists") Then
rng.offSet(i, 0).Resize(1, pArr(i).length).value = pArr(i).toArray
Else
rng.offSet(i, 0).value = pArr(i)
End If
Next i
End If
End If
End Function
Public Function fromString(ByVal str As String) As Lists
Dim l As New Lists
Dim i As Long
For i = 1 To Len(str)
l.add Mid(str, i, 1)
Next i
Set fromString = l
Set l = Nothing
End Function
Public Function join(ByVal delimiter As String) As String
join = Strings.join(Me.toArray, delimiter)
End Function
Public Function fromArray(arr, Optional ByVal iter As Boolean = True) As Lists
Dim l As New Lists
If iter Then
Dim i
For Each i In arr
If IsArray(i) Or TypeName(i) = "Collection" Then
l.add fromArray(i)
Else
l.add i
End If
Next i
Set fromArray = l
Else
Set fromArray = l.addAll(arr)
End If
Set l = Nothing
End Function
Private Function serial(ByVal start As Long, ByVal ending As Long, Optional ByVal steps As Long = 1) As Variant
Dim res()
Dim cnt As Long
cnt = -1
Dim i As Long
For i = start To ending Step steps
cnt = cnt + 1
Next i
If cnt > -1 Then
ReDim res(0 To cnt)
Dim cnt1 As Long
cnt1 = 0
For i = start To ending Step steps
res(cnt1) = i
cnt1 = cnt1 + 1
Next i
End If
serial = res
End Function
' Transfer hierarchy to square matrix
' { "2" : {
' "B" : 3,
' "C" : 9}}
' to
' 2 B 3
' 2 C 9
' Dict -> [[k, v], [k1, v1], ... ]
Public Function fromDict(ByRef d As Dicts) As Lists
Dim l As New Lists
Dim e
For Each e In d.Keys
If TypeName(d.Item(e)) = "Dicts" Then
l.addAll l.fromDict(d.Item(e)).prependToAllLists__(e)
Else
l.add l.of(e, d.Item(e))
End If
Next e
Set fromDict = l
Set l = Nothing
End Function
Public Function prependToAllLists__(e) As Lists
If Me.length > 0 Then
Dim i
For i = 0 To Me.length - 1
Me.setVal i, Me.getVal(i).unshift(e)
Next i
End If
Set prependToAllLists__ = Me
End Function
Public Function fromSerial(ByVal start As Long, ByVal ending As Long, Optional ByVal steps As Long = 1) As Lists
Set fromSerial = Me.fromArray(serial(start, ending, steps))
End Function
Public Function ones(ByVal n As Long) As Lists
Set ones = Me.fromSerial(1, n).map("_/({i}+1)")
End Function
Public Function last() As Variant
If IsObject(Me.getVal(Me.length - 1)) Then
Set last = Me.getVal(Me.length - 1)
Else
last = Me.getVal(Me.length - 1)
End If
End Function
Public Function add(ele, Optional ByVal keepOldElements As Boolean = True) As Lists
Call check
If Not keepOldElements Then
Me.clear
End If
If IsObject(ele) Then
Set pArr(pLen) = ele
Else
pArr(pLen) = ele
End If
pLen = pLen + 1
Set add = Me
End Function
Public Function repeat(ByVal ele, ByVal times As Integer) As Lists
Dim res As New Lists
Dim i
For i = 1 To times
res.add ele
Next i
Set repeat = res
Set res = Nothing
End Function
Public Function Remove(ByVal ele) As Lists
If Me.contains(ele) Then
Set Remove = Me.removeAt(Me.indexOf(ele))
Else
Set Remove = Me
End If
End Function
Public Function removeAt(ByVal index As Long) As Lists
Dim res As New Lists
Set res = Me.slice(, index).addList(Me.slice(index + 1))
Call override(res)
Set removeAt = Me
End Function
Public Function addAt(ByVal ele, ByVal index As Long) As Lists
Dim res As Lists
Set res = Me.slice(, index).add(ele).addList(Me.slice(index))
Call override(res)
Set addAt = Me
End Function
Public Function addAllAt(ByVal eles, ByVal index As Long) As Lists
Dim res As Lists
Set res = Me.slice(, index).addAll(eles).addList(Me.slice(index))
Call override(res)
Set addAllAt = Me
End Function
Public Function replaceAllAt(ByVal eles, ByVal index As Long) As Lists
Dim res As Lists
Set res = Me.slice(, index).addAll(eles).addList(Me.slice(index + 1))
Call override(res)
Set replaceAllAt = Me
End Function
Public Function labelIndex(ByVal label As String) As Integer
Dim e
Dim res As Integer
res = -1
Dim cnt As Integer
cnt = 0
For Each e In pHeading
If e = label Then
res = cnt
Exit For
End If
cnt = cnt + 1
Next e
labelIndex = res
End Function
Public Function unshift(ParamArray l() As Variant) As Lists
Set unshift = Me.addAllAt(l, 0)
End Function
Public Function push(ParamArray l() As Variant) As Lists
Set push = Me.addAllAt(l, Me.length)
End Function
Public Function permutation() As Lists
Dim res As New Lists
If Me.length <= 1 Then
res.add Me
Else
Dim i, j
Dim tmp As Lists
Dim ele
For i = 0 To Me.length - 1
Set tmp = Me.copy.removeAt(i).permutation
For j = 0 To tmp.length - 1
res.add tmp.getVal(j).unshift(Me.getVal(i))
Next j
Next i
End If
Set permutation = res
End Function
Public Function addAll(arr, Optional ByVal keepOldElements As Boolean = True) As Lists
If Not keepOldElements Then
Me.clear
End If
Dim i
If isInstance(arr, "Lists") Then
For i = 0 To arr.length - 1
Me.add arr.getVal(i)
Next i
ElseIf TypeName(arr) = "Range" Then
If arr.Cells.Count = 1 Then
Me.add arr.Cells(1).value
Else
For Each i In arr.value
Me.add i
Next i
End If
ElseIf IsArray(arr) Then
If Not isArrayEmpty(arr) Then
For Each i In arr
Me.add i
Next i
End If
Else
Me.add arr
End If
Set addAll = Me
End Function
Private Function isArrayEmpty(arr) As Boolean
On Error GoTo hdl
Dim res As Boolean
res = True
Dim tmp
If IsArray(arr) Then
tmp = arr(LBound(arr))
End If
hdl:
If Err.Number = 0 Then
res = False
End If
isArrayEmpty = res
End Function
Public Function addList(ByRef l As Lists) As Lists
If l.length > 0 Then
Me.addAll (l.toArray)
End If
Set addList = Me
End Function
Public Function of(ParamArray l() As Variant) As Lists
Dim tmp
Dim res As New Lists
For Each tmp In l
res.add tmp
Next tmp
Set of = res
Set res = Nothing
End Function
Public Function zip(ParamArray l() As Variant) As Lists
Dim res As New Lists
Dim targLen As Long ' the length of res
targLen = pLen
Dim tmp
Dim i
For Each tmp In l
If targLen > tmp.length Then
targLen = tmp.length
End If
Next tmp
For i = 0 To targLen - 1
Dim tmpList As New Lists
tmpList.init
tmpList.add pArr(i)
For Each tmp In l
tmpList.add tmp.getVal(i)
Next tmp
res.add tmpList
Set tmpList = Nothing
Next i
Set zip = res
End Function
' zip the Lists within the Lists
Public Function zipMe() As Lists
If pLen = 0 Then
Set zipMe = Me
ElseIf pLen = 1 Then
If Me.isLists(Me.getVal(0)) Then
Dim k
Dim l As New Lists
Dim tmp1 As New Lists
For k = 0 To Me.getVal(0).length - 1
l.add tmp1.add(Me.getVal(0).getVal(k))
Set tmp1 = Nothing
Next k
Set zipMe = l
Set l = Nothing
Set tmp1 = Nothing
Else
Set zipMe = Me
End If
Else
Dim i
Dim j
Dim res As New Lists
Dim lenArr As New Lists
lenArr.init
For i = 0 To pLen - 1
lenArr.add pArr(i).length
Next i
For j = 0 To lenArr.min_ - 1
Dim tmp As New Lists
tmp.init
For i = 0 To pLen - 1
tmp.add pArr(i).getVal(j)
Next i
res.add tmp
Set tmp = Nothing
Next j
Set zipMe = res
End If
End Function
Public Function getVal(ByVal index As Long, Optional ByVal index2)
If index >= pLen Or index < 0 Then
Err.Raise 8888, , "ArrayIndexOutOfBoundException"
End If
If Not IsObject(pArr(index)) Then
getVal = pArr(index)
Else
If IsMissing(index2) Then
Set getVal = pArr(index)
Else
If IsObject(pArr(index).getVal(index2)) Then
Set getVal = pArr(index).getVal(index2)
Else
getVal = pArr(index).getVal(index2)
End If
End If
End If
End Function
Public Function setVal(ByVal index As Long, ByVal ele As Variant) As Lists
If index >= pLen Or index < 0 Then
Err.Raise 8888, , "ArrayIndexOutOfBoundException"
End If
If IsObject(ele) Then
Set pArr(index) = ele
Else
pArr(index) = ele
End If
Set setVal = Me
End Function
Public Function indexOf(ByVal ele) As Long
Dim i As Long
Dim hasFound As Boolean
hasFound = False
For i = 0 To Me.length - 1
If pArr(i) = ele Then
hasFound = True
Exit For
End If
Next i
If hasFound Then
indexOf = i
Else
indexOf = -1
End If
End Function
' l the length of the subgroups
' offSet from the first beginnig to next beginning
' sobgroupBy(2,3) [1,2,3,4,5] -> [[1, 2], [4, 5] ] [[i0, i1, ...i(l-1)], [i(0 + offset), i(1 + offset), ... i(l-1+offset)]]
Public Function subgroupBy(l As Long, offSet As Long) As Lists
Dim res As New Lists
Dim tmp As Lists
Dim cnt As Long
cnt = 0
Do While True
Set tmp = Me.slice(min__(offSet * cnt, Me.length), min__(l + offSet * cnt, Me.length))
If tmp.length = 0 Then
Exit Do
End If
res.add tmp
cnt = cnt + 1
Loop
Set subgroupBy = res
Set res = Nothing
Set tmp = Nothing
End Function
Private Function min__(a, b) As Variant
min__ = IIf(a > b, b, a)
End Function
Private Function max__(a, b) As Variant
max__ = IIf(a > b, a, b)
End Function
Public Function contains(ByVal ele) As Boolean
contains = False
Dim i As Long
For i = 0 To Me.length - 1
If Me.getVal(i) = ele Then
contains = True
Exit For
End If
Next i
End Function
Public Function min_() As Variant
Dim res
res = pArr(0)
Dim i As Long
For i = 1 To pLen - 1
If pArr(i) < res Then
res = pArr(i)
End If
Next i
min_ = res
End Function
Public Function max_() As Variant
Dim res
res = pArr(0)
Dim i As Long
For i = 1 To pLen - 1
If pArr(i) > res Then
res = pArr(i)
End If
Next i
max_ = res
End Function
Public Function avg() As Double
avg = Me.reduce("?+_", 0) / pLen
End Function
Public Function containsAll(ByVal arr) As Boolean
Dim res As Boolean
res = True
Dim i
For Each i In arr
If Not Me.contains(i) Then
res = False
Exit For
Else
Next i
containsAll = res
End Function
Public Function subList(ByVal fromIndex As Long, ByVal toIndex As Long) As Lists
Set subList = Me.slice(fromIndex, toIndex, 1)
End Function
Public Function every(ByVal judgement As String, Optional ByVal placeholder As String = "_", Optional ByVal idx As String = "{i}", Optional ByVal replaceDecimalPoint As Boolean = True) As Boolean
Dim res As Boolean
res = True
Dim i
Dim cnt As Long
cnt = 0
If replaceDecimalPoint Then
For Each i In Me.toArray
If Not Application.Evaluate(Replace(Replace(judgement, placeholder, Replace("" & i, ",", ".")), idx, cnt)) Then
res = False
Exit For
End If
cnt = cnt + 1
Next i
Else
For Each i In Me.toArray
If Not Application.Evaluate(Replace(Replace(judgement, placeholder, "" & i), idx, cnt)) Then
res = False
Exit For
End If
cnt = cnt + 1
Next i
End If
every = res
End Function
Public Function some(ByVal judgement As String, Optional ByVal placeholder As String = "_", Optional ByVal idx As String = "{i}", Optional ByVal replaceDecimalPoint As Boolean = True) As Boolean
Dim res As Boolean
res = False
Dim i
Dim cnt As Long
cnt = 0
If replaceDecimalPoint Then
For Each i In Me.toArray
If Application.Evaluate(Replace(Replace(judgement, placeholder, Replace("" & i, ",", ".")), idx, cnt)) Then
res = True
Exit For
End If
cnt = cnt + 1
Next i
Else
For Each i In Me.toArray
If Application.Evaluate(Replace(Replace(judgement, placeholder, "" & i), idx, cnt)) Then
res = True
Exit For
End If
Next i
cnt = cnt + 1
End If
some = res
End Function
''''''''''''
'@param operation: string to be evaluated, e.g. _*2 will be interpreated as ele * 2
' placeholder: placeholder to be replaced by the value
' idx: index of the element
' replaceDecimalPoint: whether the Germany Decimal Point should be replace by "."
''''''''''''
Public Function map(ByVal operation As String, Optional ByVal placeholder As String = "_", Optional ByVal idx As String = "{i}", Optional ByVal replaceDecimalPoint As Boolean = True, Optional ByVal setNullValTo = 0) As Lists
Dim res As New Lists
Dim i
Dim cnt As Long
cnt = 0
If replaceDecimalPoint Then
For Each i In Me.toArray
i = IIf(isEmpty(i), setNullValTo, i)
res.add (Application.Evaluate(Replace(Replace(operation, placeholder, Replace("" & i, ",", ".")), idx, cnt & "")))
cnt = cnt + 1
Next i
Else
For Each i In Me.toArray
i = IIf(isEmpty(i), setNullValTo, i)
res.add (Application.Evaluate(Replace(Replace(operation, placeholder, "" & i), idx, cnt & "")))
cnt = cnt + 1
Next i
End If
Set map = res
Set res = Nothing
End Function
' signature of callback should be,
' sub callback(byref l as Lists, e, optional byval i as Long)
' update Me.modification
Public Function mapX(Optional ByVal callback As String = "callback") As Lists
Dim res As New Lists
Dim i
For i = 0 To Me.length - 1
Application.Run callback, Me, Me.getVal(i), i
res.add pRes
Next i
Set mapX = res
Set res = Nothing
End Function
' first reduce then map
Public Function mapList(ByVal operation As String, ByVal reduceOp As String, Optional ByVal placeholder As String = "_", Optional ByVal idx As String = "{i}", Optional ByVal replaceDecimalPoint As Boolean = True, Optional ByVal initialVal = 1, Optional ByVal placeholderInitialVal As String = "?") As Lists
Dim res As New Lists
Dim tmp As New Lists
Dim i
Dim cnt As Long
cnt = 0
If replaceDecimalPoint Then
For Each i In Me.toArray
i = tmp.addAll(i).reduce(reduceOp, initialVal, placeholder, placeholderInitialVal, idx, replaceDecimalPoint)
res.add (Application.Evaluate(Replace(Replace(operation, placeholder, Replace("" & i, ",", ".")), idx, cnt & "")))
cnt = cnt + 1
Next i