-
Notifications
You must be signed in to change notification settings - Fork 118
/
fnliterals.scad
1768 lines (1562 loc) · 63.5 KB
/
fnliterals.scad
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
//////////////////////////////////////////////////////////////////////
// LibFile: fnliterals.scad
// Handlers for function literals, and Function literal generators.
// Includes:
// include <BOSL2/std.scad>
// include <BOSL2/fnliterals.scad>
// FileGroup: Data Management
// FileSummary: Function Literal Algorithms, and factories for generating function literals for builtin functions.
// DefineHeader(Table;Headers=Positional|Definition||Named|Definition): FunctionLiteral Args
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Section: Function Literal Algorithms
// Function: map()
// Synopsis: Applies a function to each item in a list.
// Topics: Function Literals, Looping
// See Also: filter(), reduce(), accumulate(), while(), for_n()
// Usage:
// lst = map(func, list);
// lst = map(function (x) x+1, list);
// Description:
// Applies the function `func` to all items in `list`, returning the list of results.
// In pseudo-code, this is effectively:
// ```
// function map(func,list):
// out = [];
// foreach item in list:
// append func(item) to out;
// return out;
// ```
// Arguments:
// func = The function of signature (x) to evaluate for each item in `list`.
// list = The input list.
// Example:
// func = function(x) x*x;
// echo(map(func, [1,2,3,4]));
// // ECHO: [1,4,9,16]
// Example:
// path = star(n=5,step=2,d=100);
// seglens = map(function (p) norm(p[1]-p[0]), pair(path,wrap=true));
function map(func, list) =
assert(is_function(func))
assert(is_list(list))
[for (x=list) func(x)];
// Function: filter()
// Synopsis: Returns just the list items which the given function returns true for.
// Topics: Function Literals, Looping, Filters
// See Also: map(), reduce(), accumulate(), while(), for_n(), find_all()
// Usage:
// lst = filter(func, list);
// lst = filter(function (x) x>1, list);
// Description:
// Returns all items in `list` that the function `func` returns true for.
// In pseudo-code, this is effectively:
// ```
// function filter(func,list):
// out = [];
// foreach item in list:
// if func(item) is true:
// append item to out;
// return out;
// ```
// Arguments:
// func = The function of signature `function (x)` to evaluate for each item in `list`.
// list = The input list.
// Example:
// func = function(x) x>5;
// echo(filter(func, [3,4,5,6,7]));
// // ECHO: [6,7]
function filter(func, list) =
assert(is_function(func))
assert(is_list(list))
[for (x=list) if (func(x)) x];
// Function: reduce()
// Synopsis: Applies a 2-arg function cumulatively to the items of a list, returning the final result.
// Topics: Function Literals, Looping
// See Also: map(), filter(), accumulate(), while(), for_n()
// Usage:
// res = reduce(func, list, [init]);
// res = reduce(function (a,b) a+b, list, <init=);
// Description:
// First the accumulator is set to the value in `init`. Then, for each item in `list`, the function
// in `func` is called with the accumulator and that list item, and the result is stored in the
// acumulator for the next iteration. Once all list items have been processed, the value in the
// accumulator is returned. Ie: `reduce(function (a,b) a+b, list)` is the equivalent of `sum(list)`.
// In pseduo-code, this is effectively:
// ```
// function reduce(func, list, init=0):
// x = init;
// foreach item in list:
// x = func(x, item);
// return x;
// ```
// Arguments:
// func = The function of signature `function (x)` to evaluate for each item in `list`.
// list = The input list.
// init = The starting value for the accumulator. Default: 0
// Example: Re-Implement sum()
// x = reduce(f_add(),[3,4,5]); // Returns: 12
// Example: Re-Implement product()
// x = reduce(f_mul(),[3,4,5]); // Returns: 60
// Example: Re-Implement all()
// x = reduce(f_and(),[true,true,true]); // Returns: true
// y = reduce(f_and(),[true,false,true]); // Returns: false
// Example: Re-Implement any()
// x = reduce(f_or(),[false,false,false]); // Returns: false
// y = reduce(f_or(),[true,false,true]); // Returns: true
function reduce(func, list, init=0) =
assert(is_function(func))
assert(is_list(list))
let(
l = len(list),
a = function (x,i) i<l? a(func(x,list[i]), i+1) : x
) a(init,0);
// Function: accumulate()
// Synopsis: Applies a 2-arg function cumulatively to the items of a list, returning a list of every result.
// Topics: Function Literals, Looping
// See Also: map(), filter(), reduce(), while(), for_n()
// Usage:
// res = accumulate(func, list, [init]);
// res = accumulate(function (a,b) a+b, list, [init=]);
// Description:
// First the accumulator is set to the value in `init`. Then, for each item in `list`, the function
// in `func` is called with the accumulator and that list item, and the result is stored in the
// acumulator for the next iteration. That value is also appended to the output list. Once all
// list items have been processed, the list of accumulator values is returned.
// In pseduo-code, this is effectively:
// ```
// function accumulate(func, list, init=0):
// out = []
// x = init;
// foreach item in list:
// x = func(x, item);
// append x to out;
// return out;
// ```
// Arguments:
// func = The function of signature `function (a,b)` to evaluate for each item in `list`. Default: `f_add()`
// list = The input list.
// init = The starting value for the accumulator. Default: 0
// Example: Reimplement cumsum()
// echo(accumulate(function (a,b) a+b, [3,4,5],0)); // ECHO: [3,7,12]
// Example: Reimplement cumprod()
// echo(accumulate(f_mul(),[3,4,5],1)); // ECHO: [3,12,60,360]
function accumulate(func, list, init=0) =
assert(is_function(func))
assert(is_list(list))
let(
l = len(list),
a = function (x, i, out)
i >= l ? out :
let( x=func(x,list[i]) )
a(x, i+1, [each out, x])
) a(init, 0, []);
// Function: while()
// Synopsis: While a `cond` function returns true, iteratively calls a work function, returning the final result.
// Topics: Function Literals, Looping, Iteration
// See Also: map(), filter(), reduce(), accumulate(), while(), for_n()
// Usage:
// x = while(init, cond, func);
// Description:
// Repeatedly calls the function literals in `cond` and `func` until the `cond` call returns false.
// Both `cond` and `func` have the signature `function (i,x)`. The variable `i` is passed the iteration
// number, starting with 0. On the first iteration, the variable `x` is given by `init`. On subsequent
// iterations, `x` is given by the results of the previous call to `func`. Returns the resulting `x` of
// the final iteration. In pseudo-code, this is effectively:
// ```
// function while(init, cond, func):
// x = init;
// i = 0;
// while cond(i, x):
// x = func(i, x);
// i = i + 1;
// return x;
// ```
// Arguments:
// init = The initial value for `x`.
// cond = A function literal with signature `function (i,x)`, called to determine if the loop should continue. Returns true if the loop should continue.
// func = A function literal with signature `function (i,x)`, called on each iteration. The returned value is passed as `x` on the next iteration.
// Example:
// fibs = while(
// init = [1,1],
// cond = function (i,x) select(x,-1)<25,
// func = function (i,x) concat(x, [sum(select(x,-2,-1))])
// ); // Returns: [1,1,2,3,5,8,13,21]
function while(init, cond, func) =
assert(is_function(cond))
assert(is_function(func))
let( a = function(x,i) cond(i,x) ? a(func(i,x),i+1) : x )
a(init,0);
// Function: for_n()
// Synopsis: Iteratively calls a work function `n` times, returning the final result.
// Topics: Function Literals, Looping, Iteration
// See Also: map(), filter(), reduce(), accumulate(), while()
// Usage:
// x = for_n(n, init, func);
// Description:
// Given the function literal `func`, with the signature `function (i,x)`, repeatedly calls it `n` times.
// If `n` is given as a scalar, the `i` value will traverse the range `[0:1:n-1]`, one value per call.
// If `n` is given as a range, the `i` value will traverse the given range, one value per call.
// The `x` value for the first iteration is given in `init`, and in all subsequent iterations `x` will be the result of the previous call.
// In pseudo-code, this is effectively:
// ```
// function for_n(n, init, func):
// x = init;
// if is_range(n):
// iterate i over range n:
// x = func(i,x);
// else:
// iterate i from 0 to n-1 by 1:
// x = func(i,x);
// return x;
// ```
// Arguments:
// n = The number of iterations to perform, or, if given as a range, the range to traverse.
// init = The initial value to pass as `x` to the function in `func`.
// func = The function literal to call, with signature `function (i,x)`.
// Example:
// fib = function(n) for_n(
// n, [],
// function(i,x) x? [x[1], x[0]+x[1]] : [0,1]
// )[1];
function for_n(n,init,func) =
assert(is_finite(n))
assert(is_function(func))
let(
n = is_num(n)? [0:1:n-1] : n,
a = function(x,i) i <= n[2]? a(func(i,x), i+n[1]) : x
)
a(init, n[0]);
// Function: find_all()
// Synopsis: Returns the indices of all items in a list that a given function returns true for.
// Topics: Function Literals, Looping, Filters
// See Also: find_all(), reduce(), find_first(), binsearch()
// Usage:
// indices = find_all(func, list);
// indices = find_all(function (x) x>1, list);
// Description:
// Returns the indices of all items in `list` that the function `func` returns true for.
// In pseudo-code, this is effectively:
// ```
// function find_all(func,list):
// out = [];
// foreach item in list:
// if func(item) is true:
// append item index to out;
// return out;
// ```
// Arguments:
// func = The function of signature `function (x)` to evaluate for each item in `list`.
// list = The input list.
// Example:
// func = function(x) x>5;
// echo(find_all(func, [3,4,5,6,7]));
// // ECHO: [3,4]
function find_all(func, list) =
assert(is_function(func))
assert(is_list(list))
[for (indexnum=idx(list)) if (func(list[indexnum])) indexnum];
// Function: find_first()
// Synopsis: Returns the index of the first item in a list, after `start`, that a given function returns true for.
// Topics: Function Literals, Searching
// See Also: find_all(), filter(), binsearch(), find_all()
// Usage:
// idx = find_first(func, list, [start=]);
// Description:
// Finds the index of the first item in `list`, after index `start`, which the function literal in `func` will return true for.
// The signature of the function literal in `func` is `function (x)`, and it is expected to return true when the
// value compares as matching. It should return false otherwise. If you need to find *all* matching items in the
// list, you should use {{find_all()}} instead.
// Arguments:
// func = The function literal to use to check each item in `list`. Expects the signature `function (x)`, and a boolean return value.
// list = The list to search.
// ---
// start = The first item to check.
// Example:
// data = [8,5,3,7,4,2,9];
// echo(find_first(f_lte(4), data));
// // ECHO: 2
// Example:
// data = [8,5,3,7,4,2,9];
// echo(find_first(f_lte(4), data, start=3));
// // ECHO: 4
function find_first(func, list, start=0) =
assert(is_function(func))
assert(is_list(list))
assert(is_finite(start))
let(
listlen = len(list),
_find_first = function(indexnum) (
indexnum >= listlen? undef :
func(list[indexnum])? indexnum :
_find_first(indexnum+1)
)
)
_find_first(start);
// Function: binsearch()
// Synopsis: Does a binary search of a sorted list to find the index of a given value.
// Topics: Function Literals, Data Structures, Searching
// See Also: map(), filter(), reduce(), accumulate(), hashmap(), find_all(), find_first()
// Usage:
// idx = binsearch(key,list, [cmp]);
// Description:
// Searches a sorted list for an entry with the given key, using a binary search strategy.
// Returns the index of the matching item found. If none found, returns undef.
// Arguments:
// key = The key to look for.
// list = The list of items to search through.
// idx = If given, the index of the item sublists to use as the item key.
// cmp = The comparator function literal to use. Default: `f_cmp()`
// Example:
// items = unique(rands(0,100,10000));
// idx = binsearch(44, items);
// Example:
// items = unique(rands(0,100,10000));
// idx = binsearch(44, items, cmp=function(a,b) a-b);
// Example:
// items = [for (i=[32:126]) [chr(i), i]];
// idx = binsearch("G", items, idx=0);
function binsearch(key, list, idx, cmp=f_cmp()) =
let(
a = function(s,e)
let(
p = floor((s+e)/2),
ikey = is_undef(idx)? list[p] : list[p][idx],
c = cmp(ikey,key)
)
c == 0? p :
c > 0? (p == s? undef : a(s, p-1)) :
(p == e? undef : a(p+1, e))
) a(0,len(list)-1);
// Function: simple_hash()
// Synopsis: Returns an integer hash of a given value.
// Topics: Function Literals, Hashing, Data Structures
// See Also: hashmap()
// Usage:
// hx = simple_hash(x);
// Description:
// Given an arbitrary value, returns the integer hash value for it.
// Arguments:
// x = The value to get the simple hash value of.
// Example:
// x = simple_hash("Foobar");
// x = simple_hash([[10,20],[-5,3]]);
function simple_hash(x) =
let( m = 0.5 * (sqrt(5) - 1) )
is_num(x)? floor(m*x*256) :
is_list(x)? let(
l = len(x),
a = function(i,v) i>=l? v : a(i+1, m*v + simple_hash(x[i]))
) floor(a(0,0)*4096) : let(
s = str(x),
l = len(s),
a = function(i,v) i>=l? v : a(i+1, m*v + ord(s[i]))
) floor(a(0,0)*4096);
// Function: hashmap()
// Synopsis: Creates a hashmap manipulation function.
// Topics: Function Literals, Data Structures, Hashing
// See Also: simple_hash()
// Usage: Creating an Empty HashMap.
// hm = hashmap([hashsize=]);
// Usage: Creating a Populated HashMap.
// hm = hashmap(items=KEYVAL_LIST, [hashsize=]);
// Usage: Adding an Entry
// hm2 = hm(key, val);
// Usage: Adding Multiple Entries
// hm2 = hm(additems=KEYVAL_LIST);
// Usage: Removing an Entry
// hm2 = hm(del=KEY);
// Usage: Fetching a Value
// x = hm(key);
// Usage: Iterating a HashMap
// for (kv=hm()) let(k=kv[0], v=kv[1]) ...
// Description:
// This is a factory function for creating hashmap data structure functions. You can use a hashmap
// to store large amounts of [key,value] data. At around 4000 items, this becomes faster than using
// `search()` through the list.
// Arguments:
// ---
// hashsize = The number of hashtable buckets to form.
// items = A list of [key,value] pairs to initialize the hashmap with.
// FunctionLiteral Args:
// k = The key name.
// v = The value to store with the key.
// ---
// del = If given the key of an item to delete, makes a new hashmap with that item removed.
// additems = If given a list of [key,val] pairs, makes a new hashmap with the items added.
// Example:
// hm = hashmap(items=[for (i=[0:9999]) [str("foo",i),i]]);
// a = hm("foo37"); // Returns: 37
// hm2 = hm("Blah", 39); // Adds entry "Blah" with val 39.
// b = hm2("Blah"); // Returns: 39
// hm3 = hm2(additems=[["bar",39],["qux",21]]); // Adds "bar" and "qux"
// hm4 = hm3(del="Blah"); // Deletes entry "Blah".
// for (kv = hm4()) { // Iterates over all key/value pairs.
// echo(key=kv[0], val=kv[1]);
// }
function hashmap(hashsize=127,items,table) =
let(
table = !is_undef(table)? table : [for (i=[0:1:hashsize-1]) []]
)
items != undef? hashmap(hashsize=hashsize, table=table)(additems=items) :
function(k,v,del,additems)
additems!=undef? let(
hashes = [for (item = additems) simple_hash(item[0]) % hashsize],
grouped = list_pad(group_data(hashes, additems), hashsize, []),
table = [for (i=idx(table)) concat(table[i],grouped[i])]
) hashmap(hashsize=hashsize, table=table) :
del!=undef? let(
bnum = simple_hash(del) % hashsize,
bucket = [for (item=table[bnum]) if (item[0]!=del) item],
table = [for (i=idx(table)) i==bnum? bucket : table[i]]
) hashmap(hashsize=hashsize, table=table) :
k==undef && v==undef? [for (bucket=table, item=bucket) item] :
let(
bnum = simple_hash(k) % hashsize,
bucket = table[bnum],
fnd = search([k], bucket)
)
k!=undef && v==undef? (fnd==[]? undef : bucket[fnd[0]][1]) :
let(
newtable = [
for (i=idx(table))
i!=bnum? table[i] :
!fnd? [[k,v], each bucket] :
[[k,v], for (j=idx(bucket)) if (j!=fnd[0]) bucket[i]]
]
) hashmap(hashsize=hashsize, table=newtable);
//////////////////////////////////////////////////////////////////////
// Section: Function Meta-Generators
// Function: f_1arg()
// Synopsis: Creates a factory for a 2-arg function literal, where you can optionally pre-fill the arg.
// Topics: Function Literals
// See Also: f_2arg(), f_3arg()
// Usage:
// fn = f_1arg(func);
// Description:
// Takes a function literal that accepts one argument, and returns a function
// literal factory that can be used to pre-fill out that argument with a constant.
// Example:
// f_str = f_1arg(function(a) str(a));
// fn_str = f_str(); // = function(a) str(a);
// fn_str3 = f_str(3); // = function() str(3);
function f_1arg(target_func) =
function(a)
a==undef? function(x) target_func(x) :
function() target_func(a);
// Function: f_2arg()
// Synopsis: Creates a factory for a 2-arg function literal, where you can optionally pre-fill the args.
// Topics: Function Literals
// See Also: f_1arg(), f_3arg()
// Usage:
// fn = f_2arg(target_func);
// Description:
// Takes a function literal that accepts two arguments, and returns a function
// literal factory that can be used to pre-fill out one or both of those arguments
// with a constant.
// Example:
// f_lt = f_2arg(function(a,b) a<b);
// fn_lt = f_lt(); // = function(a,b) a<b;
// fn_3lt = f_lt(3); // = function(b) 3<b;
// fn_3lt = f_lt(a=3); // = function(b) 3<b;
// fn_lt3 = f_lt(b=3); // = function(a) a<3;
// fn_3lt4 = f_lt(3,4); // = function() 3<4;
function f_2arg(target_func) =
function(a,b)
a==undef && b==undef? function(x,y) target_func(x,y) :
a==undef? function(x) target_func(x,b) :
b==undef? function(x) target_func(a,x) :
function() target_func(a,b);
// Function: f_2arg_simple()
// Synopsis: Creates a factory for a 2-arg function literal, where you can optionally pre-fill the args.
// Topics: Function Literals
// See Also: f_1arg(), f_3arg()
// Usage:
// fn = f_2arg_simple(target_func);
// Description:
// Takes a function literal that accepts two arguments, and returns a function
// literal factory that can be used to pre-fill out one or both of those arguments
// with a constant. When given a single argument, fills out the segond function
// argument with a constant.
// Example:
// f_lt = f_2arg_simple(function(a,b) a<b);
// fn_lt = f_lt(); // = function(a,b) a<b;
// fn_lt3 = f_lt(3); // = function(a) a<3;
// fn_3lt4 = f_lt(3,4); // = function() 3<4;
function f_2arg_simple(target_func) =
function(a,b)
a==undef && b==undef? function(x,y) target_func(x,y) :
b==undef? function(x) target_func(x,a) :
function() target_func(a,b);
// Function: f_3arg()
// Synopsis: Creates a factory for a 3-arg function literal, where you can optionally pre-fill the args.
// Topics: Function Literals
// See Also: f_1arg(), f_2arg()
// Usage:
// fn = f_3arg(target_func);
// Description:
// Takes a function literal that accepts three arguments, and returns a function
// literal factory that can be used to pre-fill out some or all of those arguments
// with a constant.
// Example:
// p1 = [10,4]; p2 = [3,7];
// f_va = f_3arg(function(a,b,c) vector_angle(a,b,c));
// fn_va = f_va(); // = function(a,b,c) vector_angle(a,b,c);
// fn_va2 = f_lt(c=p1); // = function(a,b) vector_angle(a,b,p1);
// fn_va3 = f_lt(a=p2); // = function(a,c) vector_angle(a,p2,c);
// fn_va4 = f_lt(a=p1,c=p2); // = function() vector_angle(p1,b,p2);
function f_3arg(target_func) =
function(a,b,c)
a==undef && b==undef && c==undef? function(x,y,z) target_func(x,y,z) :
a==undef && b==undef? function(x,y) target_func(x,y,c) :
a==undef && c==undef? function(x,y) target_func(x,b,y) :
b==undef && c==undef? function(x,y) target_func(a,x,y) :
a==undef? function(x) target_func(x,b,c) :
b==undef? function(x) target_func(a,x,c) :
c==undef? function(x) target_func(a,b,x) :
function() target_func(a,b,c);
// Function: ival()
// Synopsis: Generates a function with signature `(i,x)` that calls `func(i)`
// Topics: Function Literals
// See Also: f_1arg(), f_2arg(), f_3arg(), ival(), xval()
// Usage:
// newfunc = ival(func);
// Description:
// Wraps a single-argument function literal so that it can take two arguments,
// passing the first argument along to the wrapped function.
// Arguments:
// target_func = The function of signature (x) to wrap.
// FunctionLiteral Args:
// a = The argument that will be passed through.
// b = The argumen that will be discarded.
// Example:
// x = while(0, ival(f_lt(5)), xval(f_add(1)));
function ival(target_func) = function(a,b) target_func(a);
// Function: xval()
// Synopsis: Generates a function with signature `(i,x)` that calls `func(x)`
// Topics: Function Literals
// See Also: f_1arg(), f_2arg(), f_3arg(), ival(), xval()
// Usage:
// newfunc = xval(func);
// Description:
// Wraps a single-argument function literal so that it can take two arguments,
// passing the first argument along to the wrapped function.
// Arguments:
// target_func = The function of signature (x) to wrap.
// FunctionLiteral Args:
// a = The argument that will be passed through.
// b = The argumen that will be discarded.
// Example:
// x = while(0, ival(f_lt(5)), xval(f_add(1)));
function xval(target_func) = function(a,b) target_func(b);
//////////////////////////////////////////////////////////////////////
// Section: Comparator Generators
// Function: f_cmp()
// Synopsis: Returns a function to compare values.
// Topics: Function Literals, Comparators
// See Also: f_cmp(), f_gt(), f_lt(), f_gte(), f_lte(), f_eq(), f_neq(), f_approx(), f_napprox()
// Usage:
// fn = f_cmp();
// fn = f_cmp(b);
// fn = f_cmp(a,b);
// Description:
// A factory that generates function literals that compare `a` and `b`, where one or
// both arguments can be replaced with constants. If `a` and `b` are equal, the function
// literal will return 0. If a<b then -1 is returned. If a>b then 1 is returned.
// Example:
// fn_cmp = f_cmp(); // = function(a,b) a==b?0: a>b?1: -1;
// fn_cmp3 = f_cmp(3); // = function(a) a==3?0: a>3?1: -1;
// fn_3cmp4 = f_cmp(3,4); // = function() 3==4?0: 3>4?1: -1;
function f_cmp(a,b) = f_2arg_simple(function (a,b) a==b?0: a>b?1: -1)(a,b);
// Function: f_gt()
// Synopsis: Returns a function to compare if `a` is greater than `b`.
// Topics: Function Literals, Comparators
// See Also: f_cmp(), f_gt(), f_lt(), f_gte(), f_lte(), f_eq(), f_neq(), f_approx(), f_napprox()
// Usage:
// fn = f_gt();
// fn = f_gt(b);
// fn = f_gt(a,b);
// Description:
// A factory that generates function literals based on `a > b`, where one
// or both of the arguments can be replaced with constants.
// Example:
// fn_gt = f_gt(); // = function(a,b) a>b;
// fn_gt3 = f_gt(3); // = function(a) a>3;
// fn_3gt4 = f_gt(3,4); // = function() 3>4;
function f_gt(a,b) = f_2arg_simple(function (a,b) a>b)(a,b);
// Function: f_lt()
// Synopsis: Returns a function to compare if `a` is less than `b`.
// Topics: Function Literals, Comparators
// See Also: f_cmp(), f_gt(), f_lt(), f_gte(), f_lte(), f_eq(), f_neq(), f_approx(), f_napprox()
// Usage:
// fn = f_lt();
// fn = f_lt(b);
// fn = f_lt(a,b);
// Description:
// A factory that generates function literals based on `a < b`, where one
// or both of the arguments can be replaced with constants.
// Example:
// fn_lt = f_lt(); // = function(a,b) a<b;
// fn_lt3 = f_lt(3); // = function(a) a<3;
// fn_3lt4 = f_lt(3,4); // = function() 3<4;
function f_lt(a,b) = f_2arg_simple(function (a,b) a<b)(a,b);
// Function: f_gte()
// Synopsis: Returns a function to compare if `a` is greater than or equal to `b`.
// Topics: Function Literals, Comparators
// See Also: f_cmp(), f_gt(), f_lt(), f_gte(), f_lte(), f_eq(), f_neq(), f_approx(), f_napprox()
// Usage:
// fn = f_gte();
// fn = f_gte(b);
// fn = f_gte(a,b);
// Description:
// A factory that generates function literals based on `a >= b`, where one
// or both of the arguments can be replaced with constants.
// Example:
// fn_gte = f_gte(); // = function(a,b) a>=b;
// fn_gte3 = f_gte(3); // = function(a) a>=3;
// fn_3gte4 = f_gte(3,4); // = function() 3>=4;
function f_gte(a,b) = f_2arg_simple(function (a,b) a>=b)(a,b);
// Function: f_lte()
// Synopsis: Returns a function to compare if `a` is less than or equal to `b`.
// Topics: Function Literals, Comparators
// See Also: f_cmp(), f_gt(), f_lt(), f_gte(), f_lte(), f_eq(), f_neq(), f_approx(), f_napprox()
// Usage:
// fn = f_lte();
// fn = f_lte(b);
// fn = f_lte(a,b);
// Description:
// A factory that generates function literals based on `a <= b`, where
// one or both arguments can be replaced with constants.
// Example:
// fn_lte = f_lte(); // = function(a,b) a<=b;
// fn_lte3 = f_lte(3); // = function(a) a<=3;
// fn_3lte4 = f_lte(3,4); // = function() 3<=4;
function f_lte(a,b) = f_2arg_simple(function (a,b) a<=b)(a,b);
// Function: f_eq()
// Synopsis: Returns a function to compare if `a` is exactly equal to `b`.
// Topics: Function Literals, Comparators
// See Also: f_cmp(), f_gt(), f_lt(), f_gte(), f_lte(), f_eq(), f_neq(), f_approx(), f_napprox()
// Usage:
// fn = f_eq();
// fn = f_eq(b);
// fn = f_eq(a,b);
// Description:
// A factory that generates function literals based on `a == b`, where
// one or both arguments can be replaced with constants.
// Example:
// fn_eq = f_eq(); // = function(a,b) a==b;
// fn_eq3 = f_eq(3); // = function(a) a==3;
// fn_3eq4 = f_eq(3,4); // = function() 3==4;
function f_eq(a,b) = f_2arg_simple(function (a,b) a==b)(a,b);
// Function: f_neq()
// Synopsis: Returns a function to compare if `a` is not exactly equal to `b`.
// Topics: Function Literals, Comparators
// See Also: f_cmp(), f_gt(), f_lt(), f_gte(), f_lte(), f_eq(), f_neq(), f_approx(), f_napprox()
// Usage:
// fn = f_neq();
// fn = f_neq(b);
// fn = f_neq(a,b);
// Description:
// A factory that generates function literals based on `a != b`, where
// one or both arguments can be replaced with constants.
// Example:
// fn_neq = f_neq(); // = function(a,b) a!=b;
// fn_neq3 = f_neq(3); // = function(a) a!=3;
// fn_3neq4 = f_neq(3,4); // = function() 3!=4;
function f_neq(a,b) = f_2arg_simple(function (a,b) a!=b)(a,b);
// Function: f_approx()
// Synopsis: Returns a function to compare if `a` is approximately equal to `b`.
// Topics: Function Literals, Comparators
// See Also: f_cmp(), f_gt(), f_lt(), f_gte(), f_lte(), f_eq(), f_neq(), f_approx(), f_napprox()
// Usage:
// fn = f_approx();
// fn = f_approx(b);
// fn = f_approx(a,b);
// Description:
// A factory that generates function literals based on `approx(a,b)`, where
// one or both arguments can be replaced with constants.
// Example:
// fn_approx = f_approx(); // = function(a,b) approx(a,b);
// fn_approx3 = f_approx(3); // = function(a) approx(a,3);
// fn_3approx4 = f_approx(3,4); // = function() approx(3,4);
function f_approx(a,b) = f_2arg_simple(function (a,b) approx(a,b))(a,b);
// Function: f_napprox()
// Synopsis: Returns a function to compare if `a` is not approximately equal to `b`.
// Topics: Function Literals, Comparators
// See Also: f_cmp(), f_gt(), f_lt(), f_gte(), f_lte(), f_eq(), f_neq(), f_approx(), f_napprox()
// Usage:
// fn = f_napprox();
// fn = f_napprox(b);
// fn = f_napprox(a,b);
// Description:
// A factory that generates function literals based on `!approx(a,b)`, where
// one or both arguments can be replaced with constants.
// Example:
// fn_napprox = f_napprox(); // = function(a,b) napprox(a,b);
// fn_napprox3 = f_napprox(3); // = function(a) napprox(a,3);
// fn_3napprox4 = f_napprox(3,4); // = function() napprox(3,4);
function f_napprox(a,b) = f_2arg_simple(function (a,b) !approx(a,b))(a,b);
//////////////////////////////////////////////////////////////////////
// Section: Logic Operators
// Function: f_or()
// Synopsis: Returns a function to check if either `a` or `b` is true.
// Topics: Function Literals, Logic, Boolean Operations
// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
// Usage:
// fn = f_or();
// fn = f_or(a=);
// fn = f_or(b=);
// fn = f_or(a=,b=);
// Description:
// A factory that generates function literals based on `a || b`, where
// either or both of the `a` or `b` arguments can be replaced with constants.
// Arguments:
// a = If given, replaces the first argument.
// b = If given, replaces the second argument.
function f_or(a,b) = f_2arg(function(a,b) (a || b))(a,b);
// Function: f_and()
// Synopsis: Returns a function to check if both `a` and `b` are true.
// Topics: Function Literals, Logic, Boolean Operations
// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
// Usage:
// fn = f_and();
// fn = f_and(a=);
// fn = f_and(b=);
// fn = f_and(a=,b=);
// Description:
// A factory that generates function literals based on `a && b`, where
// either or both of the `a` or `b` arguments can be replaced with constants.
// Arguments:
// a = If given, replaces the first argument.
// b = If given, replaces the second argument.
function f_and(a,b) = f_2arg(function(a,b) (a && b))(a,b);
// Function: f_nor()
// Synopsis: Returns a function to check if neither `a` nor `b` are true.
// Topics: Function Literals, Logic, Boolean Operations
// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
// Usage:
// fn = f_nor();
// fn = f_nor(a=);
// fn = f_nor(b=);
// fn = f_nor(a=,b=);
// Description:
// A factory that generates function literals based on `!(a || b)`, where
// either or both of the `a` or `b` arguments can be replaced with constants.
// Arguments:
// a = If given, replaces the first argument.
// b = If given, replaces the second argument.
function f_nor(a,b) = f_2arg(function(a,b) !(a || b))(a,b);
// Function: f_nand()
// Synopsis: Returns a function to check if `a` and `b` are not both true.
// Topics: Function Literals, Logic, Boolean Operations
// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
// Usage:
// fn = f_nand();
// fn = f_nand(a=);
// fn = f_nand(b=);
// fn = f_nand(a=,b=);
// Description:
// A factory that generates function literals based on `!(a && b)`, where
// either or both of the `a` or `b` arguments can be replaced with constants.
// Arguments:
// a = If given, replaces the first argument.
// b = If given, replaces the second argument.
function f_nand(a,b) = f_2arg(function(a,b) !(a && b))(a,b);
// Function: f_xor()
// Synopsis: Returns a function to check if either `a` or `b`, but not both, are true.
// Topics: Function Literals, Logic, Boolean Operations
// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
// Usage:
// fn = f_xor();
// fn = f_xor(a=);
// fn = f_xor(b);
// fn = f_xor(a=,b=);
// Description:
// A factory that generates function literals based on `(!a && b) || (a && !b)`, where
// either or both of the `a` or `b` arguments can be replaced with constants.
// Arguments:
// a = If given, replaces the first argument.
// b = If given, replaces the second argument.
function f_xor(a,b) = f_2arg(function(a,b) (!a && b) || (a && !b))(a,b);
// Function: f_not()
// Synopsis: Returns a function to check if `a` is not true.
// Topics: Function Literals, Logic, Boolean Operations
// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
// Usage:
// fn = f_not();
// fn = f_not(a);
// Description:
// A factory that generates function literals based on `!a`, where the `a`
// argument can be replaced with a constant.
// Arguments:
// a = If given, replaces the argument.
function f_not(a) = f_1arg(function(a) !a)(a);
// Function: f_even()
// Synopsis: Returns a function to check if `a` is an even number.
// Topics: Function Literals, Math Operators
// See Also: f_even(), f_odd()
// Usage:
// fn = f_even();
// fn = f_even(a);
// Description:
// A factory that generates function literals based on `a % 2 == 0`, where the `a`
// argument can optionally be replaced with a constant.
// Arguments:
// a = If given, replaces the argument.
// Example:
// l2 = filter(f_even(), [3,4,5,6,7,8]); // Returns: [4,6,8]
function f_even(a) = f_1arg(function(a) a % 2 == 0)(a);
// Function: f_odd()
// Synopsis: Returns a function to check if `a` is an odd number.
// Topics: Function Literals, Math Operators
// See Also: f_even(), f_odd()
// Usage:
// fn = f_odd();
// fn = f_odd(a);
// Description:
// A factory that generates function literals based on `a % 2 != 0`, where the `a`
// argument can optionally be replaced with a constant.
// Arguments:
// a = If given, replaces the argument.
// Example:
// l2 = filter(f_odd(), [3,4,5,6,7,8]); // Returns: [3,5,7]
function f_odd(a) = f_1arg(function(a) a % 2 != 0)(a);
//////////////////////////////////////////////////////////////////////
// Section: Math Operators
// Function: f_add()
// Synopsis: Returns a function to add `a` and `b`.
// Topics: Function Literals, Math Operators
// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
// Usage:
// fn = f_add();
// fn = f_add(a=);
// fn = f_add(b);
// fn = f_add(a=,b=);
// Description:
// A factory that generates function literals based on `a + b`, where either
// or both of the `a` or `b` arguments can be replaced with constants.
// Arguments:
// a = If given, replaces the first argument.
// b = If given, replaces the second argument.
function f_add(a,b) = f_2arg(function(a,b) a + b)(a,b);
// Function: f_sub()
// Synopsis: Returns a function to subtract `a` from `b`.
// Topics: Function Literals, Math Operators
// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
// Usage:
// fn = f_sub();
// fn = f_sub(a=);
// fn = f_sub(b);
// fn = f_sub(a=,b=);
// Description:
// A factory that generates function literals based on `a - b`, where either
// or both of the `a` or `b` arguments can be replaced with constants.
// Arguments:
// a = If given, replaces the first argument.
// b = If given, replaces the second argument.
function f_sub(a,b) = f_2arg(function(a,b) a - b)(a,b);
// Function: f_mul()
// Synopsis: Returns a function to multiply `a` by `b`.
// Topics: Function Literals, Math Operators
// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
// Usage:
// fn = f_mul();
// fn = f_mul(a=);
// fn = f_mul(b);
// fn = f_mul(a=,b=);
// Description:
// A factory that generates function literals based on `a * b`, where either
// or both of the `a` or `b` arguments can be replaced with constants.
// Arguments:
// a = If given, replaces the first argument.
// b = If given, replaces the second argument.
function f_mul(a,b) = f_2arg(function(a,b) a * b)(a,b);
// Function: f_div()
// Synopsis: Returns a function to divide `a` by `b`.
// Topics: Function Literals, Math Operators
// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
// Usage:
// fn = f_div();
// fn = f_div(a=);
// fn = f_div(b);
// fn = f_div(a=,b=);
// Description:
// A factory that generates function literals based on `a / b`, where either
// or both of the `a` or `b` arguments can be replaced with constants.
// Arguments:
// a = If given, replaces the first argument.
// b = If given, replaces the second argument.
function f_div(a,b) = f_2arg(function(a,b) a / b)(a,b);
// Function: f_mod()
// Synopsis: Returns a function to calculate the modulo of `a` divided by `b`.
// Topics: Function Literals, Math Operators
// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
// Usage:
// fn = f_mod();
// fn = f_mod(a=);
// fn = f_mod(b);
// fn = f_mod(a=,b=);
// Description:
// A factory that generates function literals based on `a % b`, where either
// or both of the `a` or `b` arguments can be replaced with constants.
// Arguments:
// a = If given, replaces the first argument.
// b = If given, replaces the second argument.
function f_mod(a,b) = f_2arg(function(a,b) a % b)(a,b);
// Function: f_pow()