-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTableAPI.ts
2090 lines (2016 loc) · 56.6 KB
/
TableAPI.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
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
import { AddColumn, CTop, Lookup, parseRow, parseTable, Row, SchemaOf, STop, Table, TTop, UpdateColumns, VTop } from "./EncodeTables";
import { students, gradebook, studentsMissing, jellyAnon, employees, departments, jellyNamed, gradebookMissing, gradebookSeq } from "./ExampleTables";
import { makeTester } from './unitTest'
import { average, concat, even, filter, ge, le, length, map, removeAll, removeDuplicates } from './helpers'
const Tester = makeTester()
export let emptyTable: Table<{}> = { header: [] as Array<keyof {}>, content: [] };
// constraints
() => {
// - [x] `schema(t)` is equal to `{}`
// - [ ] `nrows(t)` is equal to `0`
}
export let addRows = <S extends STop>(t1: Table<S>, rs: Array<Row<S>>): Table<S> => {
return {
header: t1.header,
content: [...t1.content, ...rs.map(({ content: [r] }) => r)]
}
}
() => {
// - [ ] for all `r` in `rs`, `schema(r)` is equal to `schema(t1)`
// - [ ] `schema(t2)` is equal to `schema(t1)`
// - [ ] `nrows(t2)` is equal to `nrows(t1) + length(rs)`
}
{
Tester.assertEqual(
'addRows 1',
() => addRows(
students,
[
parseRow([
["name", "Colton"], ["age", 19],
["favorite color", "blue"]])
]),
parseTable([
['name', 'age', 'favorite color'],
["Bob", 12, "blue"],
["Alice", 17, "green"],
["Eve", 13, "red"],
["Colton", 19, "blue"],
])
)
Tester.assertEqual(
'addRows 2',
() => addRows(gradebook, []),
parseTable([
['name', 'age', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final'],
["Bob", 12, 8, 9, 77, 7, 9, 87],
["Alice", 17, 6, 8, 88, 8, 7, 85],
["Eve", 13, 7, 9, 84, 8, 8, 77],
])
)
}
export let addColumn = <S extends STop, C extends CTop, V extends VTop>(t1: Table<S>, c: C, vs: Array<V>): Table<AddColumn<S, C, V>> => {
return {
header: [...t1.header, c],
content: t1.content.map((r, i) => {
return Object.assign({ [c as C]: vs[i] }, r) as AddColumn<S, C, V>;
})
}
}
// constraints
() => {
// - [ ] `c` is not in `header(t1)`
// - [ ] `length(vs)` is equal to `nrows(t1)`
// - [x] `header(t2)` is equal to `concat(header(t1), [c])`
// - [x] for all `c'` in `header(t1)`, `schema(t2)[c']` is equal to `schema(t1)[c']`
// - [x] `schema(t2)[c]` is the sort of elements of `vs`
// - [ ] `nrows(t2)` is equal to `nrows(t1)`
}
// examples
{
const hairColor = ["brown", "red", "blonde"]
Tester.assertEqual(
'addColumn 1',
() => addColumn(students, "hair-color", hairColor),
parseTable([
['name', 'age', 'favorite color', 'hair-color'],
["Bob", 12, "blue", "brown"],
["Alice", 17, "green", "red"],
["Eve", 13, "red", "blonde"]
])
)
const presentation: Array<number> = [9, 9, 6]
Tester.assertEqual(
'addColumn 2',
() => addColumn(gradebook, "presentation", presentation),
parseTable([
['name', 'age', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final', 'presentation'],
["Bob", 12, 8, 9, 77, 7, 9, 87, 9],
["Alice", 17, 6, 8, 88, 8, 7, 85, 9],
["Eve", 13, 7, 9, 84, 8, 8, 77, 6],
])
)
}
export let buildColumn = <S extends STop, C extends CTop, V extends VTop>(t1: Table<S>, c: C, f: (r: Row<S>) => V): Table<AddColumn<S, C, V>> => {
return addColumn(t1, c, t1.content.map((r) => f({ header: t1.header, content: [r] })))
}
// constraints
() => {
// - [ ] `c` is not in `header(t1)`
// - [x] `schema(r)` is equal to `schema(t1)`
// - [ ] `header(t2)` is equal to `concat(header(t1), [c])`
// - [x] for all `c'` in `header(t1)`, `schema(t2)[c']` is equal to `schema(t1)[c']`
// - [x] `schema(t2)[c]` is equal to the sort of `v`
// - [x] `nrows(t2)` is equal to `nrows(t1)`
}
// examples
{
const isTeenagerBuilder = (r: Row<SchemaOf<typeof students>>) => {
return 12 < getValue(r, 'age') && getValue(r, 'age') < 20
}
Tester.assertEqual(
'buildColumn 1',
() => buildColumn(students, 'is-teenager', isTeenagerBuilder),
parseTable([
['name', 'age', 'favorite color', 'is-teenager'],
["Bob", 12, "blue", false],
["Alice", 17, "green", true],
["Eve", 13, "red", true],
])
)
const didWellInFinal = (r: Row<SchemaOf<typeof gradebook>>) => {
return 85 <= getValue(r, 'final')
}
Tester.assertEqual(
'buildColumn 2',
() => buildColumn(gradebook, 'did-well-in-final', didWellInFinal),
parseTable([
['name', 'age', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final', 'did-well-in-final'],
["Bob", 12, 8, 9, 77, 7, 9, 87, true],
["Alice", 17, 6, 8, 88, 8, 7, 85, true],
["Eve", 13, 7, 9, 84, 8, 8, 77, false],
])
)
}
const vcat = <S extends STop>(t1: Table<S>, t2: Table<S>): Table<S> => {
return {
header: t1.header,
content: [...t1.content, ...t2.content]
}
}
// constraints
() => {
// - [ ] `schema(t1)` is equal to `schema(t2)`
// - [x] `schema(t3)` is equal to `schema(t1)`
// - [ ] `nrows(t3)` is equal to `nrows(t1) + nrows(t2)`
}
// examples
{
const increaseAge = (r: Row<SchemaOf<typeof students>>) => {
return parseRow([['age', getValue(r, 'age') + 1]])
}
Tester.assertEqual(
'vcat 1',
() => {
const o: Table<
{
name: string,
age: number,
'favorite color': string,
}
> = vcat(students, update(students, increaseAge))
return o;
},
parseTable([
['name', 'age', 'favorite color'],
["Bob", 12, "blue"],
["Alice", 17, "green"],
["Eve", 13, "red"],
["Bob", 13, "blue"],
["Alice", 18, "green"],
["Eve", 14, "red"],
])
)
const curveMidtermAndFinal = (r: Row<SchemaOf<typeof gradebook>>) => {
const curve = (n: number) => n + 5
return parseRow([
['midterm', curve(getValue(r, 'midterm'))],
['final', curve(getValue(r, 'final'))],
])
}
Tester.assertEqual(
'vcat 2',
// The explicit type application is necessary
() => vcat(gradebook, update(gradebook, curveMidtermAndFinal)),
parseTable([
['name', 'age', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final'],
["Bob", 12, 8, 9, 77, 7, 9, 87],
["Alice", 17, 6, 8, 88, 8, 7, 85],
["Eve", 13, 7, 9, 84, 8, 8, 77],
["Bob", 12, 8, 9, 82, 7, 9, 92],
["Alice", 17, 6, 8, 93, 8, 7, 90],
["Eve", 13, 7, 9, 89, 8, 8, 82],
])
)
}
export let hcat = <S1 extends STop, S2 extends STop>(t1: Table<S1>, t2: Table<S2>): Table<S1 & S2> => {
return {
header: [...t1.header, ...t2.header],
content: t1.content.map((r1, i) => {
return Object.assign({}, r1, t2.content[i]) as S1 & S2
})
}
}
// constraints
() => {
// - [ ] `concat(header(t1), header(t2))` has no duplicates
// - [x] `nrows(t1)` is equal to `nrows(t2)`
// - [x] `schema(t3)` is equal to `concat(schema(t1), schema(t2))`
// - [x] `nrows(t3)` is equal to `nrows(t1)`
// See the examples to confirm
}
// examples
{
Tester.assertEqual(
'hcat 1',
() => hcat(students, dropColumns(gradebook, ['name', 'age'])),
parseTable([
['name', 'age', 'favorite color', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final'],
["Bob", 12, "blue", 8, 9, 77, 7, 9, 87],
["Alice", 17, "green", 6, 8, 88, 8, 7, 85],
["Eve", 13, "red", 7, 9, 84, 8, 8, 77],
])
)
Tester.assertEqual(
'hcat 1',
() => hcat(dropColumns(students, ['name', 'age']), gradebook),
parseTable([
['favorite color', 'name', 'age', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final'],
["blue", "Bob", 12, 8, 9, 77, 7, 9, 87],
["green", "Alice", 17, 6, 8, 88, 8, 7, 85],
["red", "Eve", 13, 7, 9, 84, 8, 8, 77],
])
)
}
export let values = <S extends STop>(rs: Array<Row<S>>): Table<S> => {
return {
header: rs[0].header,
content: rs.map(({ content: [r] }) => r)
};
}
() => {
// - [ ] `length(rs)` is positive
// - [ ] for all `r` in `rs`, `schema(r)` is equal to `schema(rs[0])`
// - [ ] `schema(t)` is equal to `schema(rs[0])`
// - [ ] `nrows(t)` is equal to `length(rs)`
}
{
Tester.assertEqual(
'values 1',
() => values([
parseRow([['name', 'Alice']]),
parseRow([['name', 'Bob']])
]),
parseTable([
['name'],
['Alice'],
['Bob']
])
)
Tester.assertEqual(
'values 2',
() => values([
parseRow([['name', 'Alice'], ['age', 12]]),
parseRow([['name', 'Bob'], ['age', 13]])
]),
parseTable([
['name', 'age'],
['Alice', 12],
['Bob', 13]
])
)
}
export let crossJoin = <S1 extends STop, S2 extends STop>(t1: Table<S1>, t2: Table<S2>): Table<S1 & S2> => {
return {
header: [...t1.header, ...t2.header],
content: t1.content.flatMap((r1) => {
return t2.content.map((r2) => {
return Object.assign({}, r1, r2)
})
})
}
}
() => {
// - [ ] `concat(header(t1), header(t2))` has no duplicates
// - [ ] `schema(t3)` is equal to `concat(schema(t1), schema(t2))`
// - [ ] `nrows(t3)` is equal to `nrows(t1) * nrows(t2)`
}
{
Tester.assertEqual(
'crossJoin 1',
() => {
const petiteJelly = selectColumns2(selectRows1(jellyAnon, [0, 1]), [0, 1, 2])
return crossJoin(students, petiteJelly)
},
parseTable([
['name', 'age', 'favorite color', 'get acne', 'red', 'black'],
["Bob", 12, "blue", true, false, false],
["Bob", 12, "blue", true, false, true],
["Alice", 17, "green", true, false, false],
["Alice", 17, "green", true, false, true],
["Eve", 13, "red", true, false, false],
["Eve", 13, "red", true, false, true]
])
)
Tester.assertEqual(
'crossJoin 2',
() => {
const petiteJelly = selectColumns2(selectRows1(jellyAnon, [0, 1]), [0, 1, 2])
return crossJoin(emptyTable, petiteJelly)
},
parseTable([
['get acne', 'red', 'black']
])
)
}
export let leftJoin = <S1 extends STop, S2 extends STop>(t1: Table<S1>, t2: Table<S2>, cs: Array<CTop & keyof S1 & keyof S2>): Table<S1 & Omit<S2, keyof S1>> => {
const header = [...t1.header, ...t2.header.filter((c) => {
return !cs.includes(c as (CTop & keyof S1 & keyof S2))
})] as Array<CTop & keyof (S1 & Omit<S2, keyof S1>)>
return {
header,
content: t1.content.flatMap((r1): Array<S1 & S2> => {
const rs2 = t2.content.filter((r2) => {
return cs.every((c) => {
return r1[c] === r2[c]
})
})
if (rs2.length === 0) {
return [
Object.assign(
Object.fromEntries(header.map((c) => [c, null])) as S1 & S2,
r1)
]
} else {
return rs2.map((r2) => {
return Object.assign({}, r1, r2)
})
}
})
}
}
() => {
// - [ ] `header(t3)` is equal to `concat(header(t1), removeAll(header(t2), cs))`
// - [x] for all `c` in `header(t1)`, `schema(t3)[c]` is equal to `schema(t1)[c]`
// - [ ] for all `c` in `removeAll(header(t2), cs))`, `schema(t3)[c]` is equal to `schema(t2)[c]`
// - [ ] `nrows(t3)` is equal to `nrows(t1)`
}
{
Tester.assertEqual(
'leftJoin 1',
() => leftJoin(students, gradebook, ["name", "age"]),
parseTable(
[
["name", 'age', 'favorite color', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final'],
["Bob", 12, "blue", 8, 9, 77, 7, 9, 87],
["Alice", 17, "green", 6, 8, 88, 8, 7, 85],
["Eve", 13, "red", 7, 9, 84, 8, 8, 77]
]
)
)
Tester.assertEqual(
'leftJoin 2',
() => leftJoin(employees, departments, ["Department ID"]),
parseTable([
['Last Name', 'Department ID', 'Department Name'],
["Rafferty", 31, "Sales"],
["Jones", 32, null],
["Heisenberg", 33, "Engineering"],
["Robinson", 34, "Clerical"],
["Smith", 34, "Clerical"],
["Williams", null, null]
])
)
}
export let nrows = <S extends STop>(t: Table<S>): number => {
return t.content.length;
}
// constraints
() => {
// - [ ] `n` is equal to `nrows(t)`
}
// examples
{
Tester.assertEqual('nrows 1', () => nrows(emptyTable), 0)
Tester.assertEqual('nrows 2', () => nrows(studentsMissing), 3)
}
export let ncols = <S extends STop>(t: Table<S>): number => {
return t.header.length;
}
// constraints
() => {
// - [ ] `n` is equal to `ncols(t)`
}
// examples
{
Tester.assertEqual('ncols 1', () => ncols(students), 3)
Tester.assertEqual('ncols 2', () => ncols(studentsMissing), 3)
}
export let header = <S extends STop>(t: Table<S>): Array<CTop & keyof S> => {
return t.header;
}
// constraints
() => {
// - [ ] `cs` is equal to `header(t)`
}
// examples
{
Tester.assertEqual(
'header 1',
() => header(students),
["name", "age", "favorite color"]);
Tester.assertEqual(
'header 2',
() => header(gradebook),
["name", "age", "quiz1", "quiz2", "midterm", "quiz3", "quiz4", "final"]);
}
export let getRow = <S extends STop>(t: Table<S>, n: number): Row<S> => {
return {
header: t.header,
content: [t.content[n]]
}
}
// constraints
() => {
// - [ ] `n` is in `range(nrows(t))`
}
{
Tester.assertEqual(
'getRow 1',
() => getRow(students, 0),
parseRow([["name", "Bob"], ["age", 12], ["favorite color", "blue"]])
)
Tester.assertEqual(
'getRow 1',
() => getRow(gradebook, 1),
parseRow([
["name", "Alice"], ["age", 17],
["quiz1", 6], ["quiz2", 8], ["midterm", 88],
["quiz3", 8], ["quiz4", 7], ["final", 85]
]))
}
export let getValue = <S extends STop, C extends CTop & keyof S>(r: Row<S>, c: C): Lookup<S, C> => {
return r.content[0][c]
}
// constraints
() => {
// - [x] `c` is in header(r)
// - [x] `v` is of sort `schema(r)[c]`
}
// examples
{
Tester.assertEqual(
'getValue 1',
() => getValue(
parseRow([['name', 'Bob'], ['age', 12]]) as Row<{ 'name': string, 'age': 12 }>,
'name'),
'Bob')
Tester.assertEqual(
'getValue 2',
() => getValue(
parseRow([['name', 'Bob'], ['age', 12]]) as Row<{ 'name': string, 'age': 12 }>,
'age'),
12)
}
export let getColumn1 = <S extends STop>(t: Table<S>, n: number): Array<VTop> => {
return t.content.map((r) => r[t.header[n]])
}
() => {
// - [ ] `n` is in `range(ncols(t))`
// - [ ] `length(vs)` is equal to `nrows(t)`
// - [ ] for all `v` in `vs`, `v` is of sort `schema(t)[header(t)[n]]`
}
{
Tester.assertEqual(
'getColumn1 1',
() => getColumn1(students, 1),
[12, 17, 13]
)
Tester.assertEqual(
'getColumn1 2',
() => getColumn1(gradebook, 0),
["Bob", "Alice", "Eve"]
)
}
export let getColumn2 = <S extends STop, C extends keyof S>(t: Table<S>, c: C): Array<S[C]> => {
return t.content.map((r) => r[c])
}
() => {
// - [x] `c` is in `header(t)`
// - [x] for all `v` in `vs`, `v` is of sort `schema(t)[c]`
// - [ ] `length(vs)` is equal to `nrows(t)`
}
{
Tester.assertEqual(
'getColumn2 1',
() => getColumn2(students, "age"),
[12, 17, 13]
)
Tester.assertEqual(
'getColumn2 2',
() => getColumn2(gradebook, "name"),
["Bob", "Alice", "Eve"]
)
}
export let selectRows1 = <S extends STop>(t1: Table<S>, ns: Array<number>): Table<S> => {
return values(ns.map((n) => getRow(t1, n)))
}
() => {
// - [ ] for all `n` in `ns`, `n` is in `range(nrows(t1))`
// - [ ] `schema(t2)` is equal to `schema(t1)`
// - [ ] `nrows(t2)` is equal to `length(ns)`
}
{
Tester.assertEqual(
'selectRows1 1',
() => selectRows1(students, [2, 0, 2, 1]),
parseTable([
['name', 'age', 'favorite color'],
["Eve", 13, "red"],
["Bob", 12, "blue"],
["Eve", 13, "red"],
["Alice", 17, "green"]
]))
Tester.assertEqual(
'selectRows1 2',
() => selectRows1(gradebook, [2, 1]),
parseTable([
['name', 'age', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final'],
["Eve", 13, 7, 9, 84, 8, 8, 77],
["Alice", 17, 6, 8, 88, 8, 7, 85]
])
)
}
export let selectRows2 = <S extends STop>(t1: Table<S>, bs: Array<Boolean>): Table<S> => {
return {
header: t1.header,
content: t1.content.filter((_, i) => bs[i])
}
}
() => {
// - [ ] `length(bs)` is equal to `nrows(t1)`
// - [ ] `schema(t2)` is equal to `schema(t1)`
// - [ ] `nrows(t2)` is equal to `length(removeAll(bs, [false]))`
}
{
Tester.assertEqual(
'selectRow2 1',
() => selectRows2(students, [true, false, true]),
parseTable([
['name', 'age', 'favorite color'],
["Bob", 12, "blue"],
["Eve", 13, "red"]
])
)
Tester.assertEqual(
'selectRow2 1',
() => selectRows2(gradebook, [false, false, true]),
parseTable([
['name', 'age', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final'],
["Eve", 13, 7, 9, 84, 8, 8, 77]
])
)
}
export let selectColumns1 = <S extends STop>(t1: Table<S>, bs: Array<boolean>): TTop => {
const header = t1.header.filter((_, i) => bs[i])
return {
header: header as string[],
content: t1.content.map((r) => {
return Object.fromEntries(header.map((c) => [c, r[c]]))
})
}
}
() => {
// - [ ] `length(bs)` is equal to `ncols(t1)`
// - [ ] `header(t2)` is a subsequence of `header(t1)`
// - [ ] for all `i` in `range(ncols(t1))`, `header(t1)[i]` is in `header(t2)` if and only if `bs[i]` is equal to `true`
// - [ ] `schema(t2)` is a subsequence of `schema(t1)`
// - [ ] `nrows(t2)` is equal to `nrows(t1)`
}
{
Tester.assertEqual(
'selectColumns1 1',
() => selectColumns1(students, [true, true, false]),
parseTable([
['name', 'age'],
["Bob", 12],
["Alice", 17],
["Eve", 13]
])
)
Tester.assertEqual(
'selectColumns1 2',
() => selectColumns1(gradebook, [true, false, false, false, true, false, false, true]),
parseTable([
['name', 'midterm', 'final'],
["Bob", 77, 87],
["Alice", 88, 85],
["Eve", 84, 77]
])
)
}
export let selectColumns2 = <S extends STop>(t1: Table<S>, ns: Array<number>): Table<Partial<S>> => {
const header = ns.map((n) => t1.header[n])
const content = t1.content.map(
(r) => Object.fromEntries(header.map((c) => [c, r[c]])) as Partial<S>)
return { header, content }
}
() => {
// - [ ] `ns` has no duplicates
// - [ ] for all `n` in `ns`, `n` is in `range(ncols(t1))`
// - [ ] `ncols(t2)` is equal to `length(ns)`
// - [ ] for all `i` in `range(length(ns))`, `header(t2)[i]` is equal to `header(t1)[ns[i]]`
// - [ ] for all `c` in `header(t2)`, `schema(t2)[c]` is equal to `schema(t1)[c]`
// - [ ] `nrows(t2)` is equal to `nrows(t1)`
}
{
Tester.assertEqual(
'selectColumn2 1',
() => selectColumns2(students, [2, 1]),
parseTable([
['favorite color', 'age'],
["blue", 12],
["green", 17],
["red", 13]
])
)
Tester.assertEqual(
'selectColumn2 2',
() => selectColumns2(gradebook, [7, 0, 4]),
parseTable([
['final', 'name', 'midterm'],
[87, "Bob", 77],
[85, "Alice", 88],
[77, "Eve", 84],
])
)
}
export let selectColumns3 = <S extends STop, C extends CTop & keyof S>(t1: Table<S>, cs: Array<C>): Table<Pick<S, C>> => {
return {
header: cs,
content: t1.content.map((r) => {
return Object.fromEntries(cs.map((c) => [c, r[c]])) as Pick<S, C>
})
}
}
{
// - [ ] `cs` has no duplicates
// - [x] for all `c` in `cs`, `c` is in `header(t1)`
// - [ ] `header(t2)` is equal to `cs`
// - [x] for all `c` in `header(t2)`, `schema(t2)[c]` is equal to `schema(t1)[c]`
// - [ ] `nrows(t2)` is equal to `nrows(t1)`
}
{
Tester.assertEqual(
'selectColumns3 1',
() => selectColumns3(students, ["favorite color", "age"]),
parseTable([
['favorite color', 'age'],
["blue", 12],
["green", 17],
["red", 13]
])
)
Tester.assertEqual(
'selectColumns3 2',
() => selectColumns3(gradebook, ["final", "name", "midterm"]),
parseTable([
['final', 'name', 'midterm'],
[87, "Bob", 77],
[85, "Alice", 88],
[77, "Eve", 84]
])
)
}
export let head = <S extends STop>(t1: Table<S>, n: number): Table<S> => {
let end: number;
if (n >= 0) {
end = n
} else {
end = t1.content.length + n;
}
return {
header: t1.header,
content: t1.content.slice(0, end)
}
}
() => {
// - [ ] if `n` is non-negative then `n` is in `range(nrows(t1))`
// - [ ] if `n` is negative then `- n` is in `range(nrows(t1))`
// - [ ] `schema(t2)` is equal to `schema(t1)`
// - [ ] if `n` is non-negative then `nrows(t2)` is equal to `n`
// - [ ] if `n` is negative then `nrows(t2)` is equal to `nrows(t1) + n`
}
{
Tester.assertEqual(
'head 1',
() => head(students, 1),
parseTable([
['name', 'age', 'favorite color'],
["Bob", 12, "blue"],
])
)
Tester.assertEqual(
'head 2',
() => head(students, -2),
parseTable([
['name', 'age', 'favorite color'],
["Bob", 12, "blue"],
])
)
}
export let distinct = <S extends STop>(t1: Table<S>): Table<S> => {
const distinctRows = (rs: Array<S>): Array<S> => {
if (rs.length === 0) {
return []
} else {
return [
rs[0],
...distinctRows(rs.filter((r) => {
return !t1.header.every((c) => {
return r[c] === rs[0][c]
})
}))
]
}
}
return {
header: t1.header,
content: distinctRows(t1.content)
}
}
() => {
// - [ ] `schema(t2)` is equal to `schema(t1)`
}
{
Tester.assertEqual(
'distinct 1',
() => distinct(students),
parseTable([
['name', 'age', 'favorite color'],
["Bob", 12, "blue"],
["Alice", 17, "green"],
["Eve", 13, "red"],
])
)
Tester.assertEqual(
'distinct 2',
() => distinct(selectColumns3(gradebook, ["quiz3"])),
parseTable([
['quiz3'],
[7],
[8],
])
)
}
export let dropColumns = <S extends STop, C extends CTop & keyof S>(t1: Table<S>, cs: Array<C>): Table<Omit<S, C>> => {
const header = t1.header.filter((c) => {
return !cs.includes(c as any);
}) as Array<CTop & keyof Omit<S, C>>
const rows = t1.content.map((r) => {
return Object.fromEntries(header.map((c) => [c, r[c]])) as Omit<S, C>
})
return { header, content: rows }
}
() => {
// - [x] for all `c` in `cs`, `c` is in `header(t1)`
// - [ ] `cs` has no duplicates
// - [ ] `nrows(t2)` is equal to `nrows(t1)`
// - [ ] `header(t2)` is equal to `removeAll(header(t1), cs)`
// - [ ] `schema(t2)` is a subsequence of `schema(t1)`
}
{
Tester.assertEqual(
'dropColumns 1',
() => dropColumns(students, ["age"]),
parseTable([
['name', 'favorite color'],
["Bob", "blue"],
["Alice", "green"],
["Eve", "red"],
])
)
Tester.assertEqual(
'dropColumns 2',
() => dropColumns(gradebook, ["final", "midterm"]),
parseTable([
['name', 'age', 'quiz1', 'quiz2', 'quiz3', 'quiz4'],
["Bob", 12, 8, 9, 7, 9],
["Alice", 17, 6, 8, 8, 7],
["Eve", 13, 7, 9, 8, 8],
])
)
}
export let tfilter = <S extends STop>(t1: Table<S>, f: (r: Row<S>) => boolean): Table<S> => {
return {
header: t1.header,
content: t1.content.filter((r) => f({ header: t1.header, content: [r] }))
}
}
() => {
// - [ ] `schema(r)` is equal to `schema(t1)`
// - [ ] `schema(t2)` is equal to `schema(t1)`
}
{
const ageUnderFifteen = (r: Row<{ name: string; age: number; 'favorite color': string; }>) => {
return getValue(r, "age") < 15
}
Tester.assertEqual(
'tfilter 1',
() => tfilter(students, ageUnderFifteen),
parseTable([
['name', 'age', 'favorite color'],
["Bob", 12, "blue"],
["Eve", 13, "red"],
])
)
const nameLongerThan3Letters = (r: Row<SchemaOf<typeof gradebook>>) => {
return getValue(r, "name").length > 3
}
Tester.assertEqual(
'tfilter 2',
() => tfilter(gradebook, nameLongerThan3Letters),
parseTable([
['name', 'age', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final'],
["Alice", 17, 6, 8, 88, 8, 7, 85],
])
)
}
export let tsort = <C extends CTop, S extends STop & Record<C, number>>(t1: Table<S>, c: C, b: boolean): Table<S> => {
const content = t1.content.slice(0)
const sign = b ? 1 : -1;
content.sort((r1, r2) => {
const n1 = r1[c]
const n2 = r2[c]
if (n1 < n2) {
return sign * -1;
} else if (n1 > n2) {
return sign * 1;
} else {
return 0;
}
})
return {
header: t1.header,
content
}
}
() => {
// - [ ] `c` is in `header(t1)`
// - [x] `schema(t1)[c]` is `Number`
// rejected as expected
// tsort(gradebook, 'name', true)
// accepted as expected
tsort(gradebook, 'quiz1', true)
// - [ ] `nrows(t2)` is equal to `nrows(t1)`
// - [ ] `schema(t2)` is equal to `schema(t1)`
}
{
Tester.assertEqual(
'tsort 1',
() => tsort(students, "age", true),
parseTable([
['name', 'age', 'favorite color'],
["Bob", 12, "blue"],
["Eve", 13, "red"],
["Alice", 17, "green"],
])
)
Tester.assertEqual(
'tsort 2',
() => tsort(gradebook, "final", false),
parseTable([
['name', 'age', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final'],
["Bob", 12, 8, 9, 77, 7, 9, 87],
["Alice", 17, 6, 8, 88, 8, 7, 85],
["Eve", 13, 7, 9, 84, 8, 8, 77],
])
)
}
export let sortByColumns = <C extends CTop, S extends STop & Record<C, number>>(t1: Table<S>, cs: Array<C>): Table<S> => {
for (const c of [...cs].reverse()) {
t1 = tsort(t1, c, true)
}
return t1
}
() => {
// - [ ] `cs` has no duplicates
// - [x] for all `c` in `cs`, `c` is in `header(t1)`
// - [x] for all `c` in `cs`, `schema(t1)[c]` is `Number`
// - [ ] `nrows(t2)` is equal to `nrows(t1)`
// - [ ] `schema(t2)` is equal to `schema(t1)`
}
{
Tester.assertEqual(
'sortByColumns 1',
() => sortByColumns(students, ["age"]),
parseTable([
['name', 'age', 'favorite color'],
["Bob", 12, "blue"],
["Eve", 13, "red"],
["Alice", 17, "green"],
])
)
Tester.assertEqual(
'sortByColumns 2',
() => sortByColumns(gradebook, ["quiz2", "quiz1"]),
parseTable([
['name', 'age', 'quiz1', 'quiz2', 'midterm', 'quiz3', 'quiz4', 'final'],
["Alice", 17, 6, 8, 88, 8, 7, 85],
["Eve", 13, 7, 9, 84, 8, 8, 77],
["Bob", 12, 8, 9, 77, 7, 9, 87],
])
)
}
export let orderBy = <S extends STop>(t1: Table<S>, cmps: Array<[(r: Row<S>) => any, (k1: any, k2: any) => boolean]>): Table<S> => {
const compare = (r1: S, r2: S) => {
for (const [getKey, compare] of cmps) {
const k1 = getKey({ header: t1.header, content: [r1] })
const k2 = getKey({ header: t1.header, content: [r2] })
const le = compare(k1, k2)
if (le) {
if (compare(k2, k1)) {
continue
} else {
return -1
}
} else {
return 1;
}
}
return 0
}
return {
header: t1.header,
content: [...t1.content].sort(compare)
}
}
() => {
// - [ ] `schema(r)` is equal to `schema(t1)`
// - [ ] `schema(t2)` is equal to `schema(t1)`
// - [ ] `nrows(t2)` is equal to `nrows(t1)`
}
{
const nameLength = <S extends STop & Record<'name', string>>(r: Row<S>) => {
return getValue(r, "name").length
}
Tester.assertEqual(
'orderBy 1',
() => orderBy(students, [[nameLength, le]]),
parseTable([
['name', 'age', 'favorite color'],