1//////////////////////////////////////////////////////////////////////
   2// LibFile: fnliterals.scad
   3//   Handlers for function literals, and Function literal generators.
   4// Includes:
   5//   include <BOSL2/std.scad>
   6//   include <BOSL2/fnliterals.scad>
   7// FileGroup: Data Management
   8// FileSummary: Function Literal Algorithms, and factories for generating function literals for builtin functions.
   9// DefineHeader(Table;Headers=Positional|Definition||Named|Definition): FunctionLiteral Args
  10//////////////////////////////////////////////////////////////////////
  11
  12
  13//////////////////////////////////////////////////////////////////////
  14// Section: Function Literal Algorithms
  15
  16
  17// Function: map()
  18// Topics: Function Literals, Looping
  19// Usage:
  20//   lst = map(func, list);
  21//   lst = map(function (x) x+1, list);
  22// Description:
  23//   Applies the function `func` to all items in `list`, returning the list of results.
  24//   In pseudo-code, this is effectively:
  25//   ```
  26//   function map(func,list):
  27//       out = [];
  28//       foreach item in list:
  29//           append func(item) to out;
  30//       return out;
  31//   ```
  32// Arguments:
  33//   func = The function of signature (x) to evaluate for each item in `list`.
  34//   list = The input list.
  35// See Also: filter(), reduce(), accumulate(), while(), for_n()
  36// Example:
  37//   func = function(x) x*x;
  38//   echo(map(func, [1,2,3,4]));
  39//   // ECHO: [1,4,9,16]
  40// Example:
  41//   path = star(n=5,step=2,d=100);
  42//   seglens = map(function (p) norm(p[1]-p[0]), pair(path,wrap=true));
  43function map(func, list) =
  44    assert(is_function(func))
  45    assert(is_list(list))
  46    [for (x=list) func(x)];
  47
  48
  49// Function: filter()
  50// Topics: Function Literals, Looping, Filters
  51// Usage:
  52//   lst = filter(func, list);
  53//   lst = filter(function (x) x>1, list);
  54// Description:
  55//   Returns all items in `list` that the function `func` returns true for.
  56//   In pseudo-code, this is effectively:
  57//   ```
  58//   function filter(func,list):
  59//       out = [];
  60//       foreach item in list:
  61//           if func(item) is true:
  62//               append item to out;
  63//       return out;
  64//   ```
  65// Arguments:
  66//   func = The function of signature `function (x)` to evaluate for each item in `list`.
  67//   list = The input list.
  68// See Also: map(), reduce(), accumulate(), while(), for_n()
  69// Example:
  70//   func = function(x) x>5;
  71//   echo(filter(func, [3,4,5,6,7]));
  72//   // ECHO: [6,7]
  73function filter(func, list) =
  74    assert(is_function(func))
  75    assert(is_list(list))
  76    [for (x=list) if (func(x)) x];
  77
  78
  79// Function: reduce()
  80// Topics: Function Literals, Looping
  81// Usage:
  82//   res = reduce(func, list, [init]);
  83//   res = reduce(function (a,b) a+b, list, <init=);
  84// Description:
  85//   First the accumulator is set to the value in `init`.  Then, for each item in `list`, the function
  86//   in `func` is called with the accumulator and that list item, and the result is stored in the
  87//   acumulator for the next iteration.  Once all list items have been processed, the value in the
  88//   accumulator is returned.  Ie: `reduce(function (a,b) a+b, list)` is the equivalent of `sum(list)`.
  89//   In pseduo-code, this is effectively:
  90//   ```
  91//   function reduce(func, list, init=0):
  92//       x = init;
  93//       foreach item in list:
  94//           x = func(x, item);
  95//       return x;
  96//   ```
  97// Arguments:
  98//   func = The function of signature `function (x)` to evaluate for each item in `list`.
  99//   list = The input list.
 100//   init = The starting value for the accumulator.  Default: 0
 101// See Also: map(), filter(), accumulate(), while(), for_n()
 102// Example: Re-Implement sum()
 103//   x = reduce(f_add(),[3,4,5]);  // Returns: 12
 104// Example: Re-Implement product()
 105//   x = reduce(f_mul(),[3,4,5]);  // Returns: 60
 106// Example: Re-Implement all()
 107//   x = reduce(f_and(),[true,true,true]);   // Returns: true
 108//   y = reduce(f_and(),[true,false,true]);  // Returns: false
 109// Example: Re-Implement any()
 110//   x = reduce(f_or(),[false,false,false]); // Returns: false
 111//   y = reduce(f_or(),[true,false,true]);   // Returns: true
 112function reduce(func, list, init=0) =
 113    assert(is_function(func))
 114    assert(is_list(list))
 115    let(
 116        l = len(list),
 117        a = function (x,i) i<l? a(func(x,list[i]), i+1) : x
 118    ) a(init,0);
 119
 120
 121// Function: accumulate()
 122// Topics: Function Literals, Looping
 123// Usage:
 124//   res = accumulate(func, list, [init]);
 125//   res = accumulate(function (a,b) a+b, list, [init=]);
 126// Description:
 127//   First the accumulator is set to the value in `init`.  Then, for each item in `list`, the function
 128//   in `func` is called with the accumulator and that list item, and the result is stored in the
 129//   acumulator for the next iteration.  That value is also appended to the output list.  Once all
 130//   list items have been processed, the list of accumulator values is returned.
 131//   In pseduo-code, this is effectively:
 132//   ```
 133//   function accumulate(func, list, init=0):
 134//       out = []
 135//       x = init;
 136//       foreach item in list:
 137//           x = func(x, item);
 138//           append x to out;
 139//       return out;
 140//   ```
 141// Arguments:
 142//   func = The function of signature `function (a,b)` to evaluate for each item in `list`.  Default: `f_add()`
 143//   list = The input list.
 144//   init = The starting value for the accumulator.  Default: 0
 145// See Also: map(), filter(), reduce(), while(), for_n()
 146// Example: Reimplement cumsum()
 147//   echo(accumulate(function (a,b) a+b, [3,4,5],0));  // ECHO: [3,7,12]
 148// Example: Reimplement cumprod()
 149//   echo(accumulate(f_mul(),[3,4,5],1)); // ECHO: [3,12,60,360]
 150function accumulate(func, list, init=0) =
 151    assert(is_function(func))
 152    assert(is_list(list))
 153    let(
 154        l = len(list),
 155        a = function (x, i, out)
 156            i >= l ? out :
 157            let( x=func(x,list[i]) )
 158            a(x, i+1, [each out, x])
 159    ) a(init, 0, []);
 160
 161
 162// Function: while()
 163// Topics: Function Literals, Looping, Iteration
 164// Usage:
 165//   x = while(init, cond, func);
 166// Description:
 167//   Repeatedly calls the function literals in `cond` and `func` until the `cond` call returns false.
 168//   Both `cond` and `func` have the signature `function (i,x)`. The variable `i` is passed the iteration
 169//   number, starting with 0.  On the first iteration, the variable `x` is given by `init`.  On subsequent
 170//   iterations, `x` is given by the results of the previous call to `func`.  Returns the resulting `x` of
 171//   the final iteration.  In pseudo-code, this is effectively:
 172//   ```
 173//   function while(init, cond, func):
 174//       x = init;
 175//       i = 0;
 176//       while cond(i, x):
 177//           x = func(i, x);
 178//           i = i + 1;
 179//       return x;
 180//   ```
 181// Arguments:
 182//   init = The initial value for `x`.
 183//   cond = A function literal with signature `function (i,x)`, called to determine if the loop should continue.  Returns true if the loop should continue.
 184//   func = A function literal with signature `function (i,x)`, called on each iteration.  The returned value is passed as `x` on the next iteration.
 185// See Also: map(), filter(), reduce(), accumulate(), while(), for_n()
 186// Example:
 187//   fibs = while(
 188//       init = [1,1],
 189//       cond = function (i,x) select(x,-1)<25,
 190//       func = function (i,x) concat(x, [sum(select(x,-2,-1))])
 191//   );  // Returns: [1,1,2,3,5,8,13,21]
 192function while(init, cond, func) =
 193    assert(is_function(cond))
 194    assert(is_function(func))
 195    let( a = function(x,i) cond(i,x) ? a(func(i,x),i+1) : x )
 196    a(init,0);
 197
 198
 199// Function: for_n()
 200// Topics: Function Literals, Looping, Iteration
 201// See Also: map(), filter(), reduce(), accumulate(), while()
 202// Usage:
 203//   x = for_n(n, init, func);
 204// Description:
 205//   Given the function literal `func`, with the signature `function (i,x)`, repeatedly calls it `n` times.
 206//   If `n` is given as a scalar, the `i` value will traverse the range `[0:1:n-1]`, one value per call.
 207//   If `n` is given as a range, the `i` value will traverse the given range, one value per call.
 208//   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.
 209//   In pseudo-code, this is effectively:
 210//   ```
 211//   function for_n(n, init, func):
 212//       x = init;
 213//       if is_range(n):
 214//           iterate i over range n:
 215//               x = func(i,x);
 216//       else:
 217//           iterate i from 0 to n-1 by 1:
 218//               x = func(i,x);
 219//       return x;
 220//   ```
 221// Arguments:
 222//   n = The number of iterations to perform, or, if given as a range, the range to traverse.
 223//   init = The initial value to pass as `x` to the function in `func`.
 224//   func = The function literal to call, with signature `function (i,x)`.
 225// Example:
 226//   fib = function(n) for_n(
 227//       n, [],
 228//       function(i,x) x? [x[1], x[0]+x[1]] : [0,1]
 229//   )[1];
 230function for_n(n,init,func) =
 231    assert(is_finite(n))
 232    assert(is_function(func))
 233    let(
 234        n = is_num(n)? [0:1:n-1] : n,
 235        a = function(x,i) i <= n[2]? a(func(i,x), i+n[1]) : x
 236    )
 237    a(init, n[0]);
 238
 239
 240// Function: find_all()
 241// Topics: Function Literals, Looping, Filters
 242// Usage:
 243//   indices = find_all(func, list);
 244//   indices = find_all(function (x) x>1, list);
 245// Description:
 246//   Returns the indices of all items in `list` that the function `func` returns true for.
 247//   In pseudo-code, this is effectively:
 248//   ```
 249//   function find_all(func,list):
 250//       out = [];
 251//       foreach item in list:
 252//           if func(item) is true:
 253//               append item index to out;
 254//       return out;
 255//   ```
 256// Arguments:
 257//   func = The function of signature `function (x)` to evaluate for each item in `list`.
 258//   list = The input list.
 259// See Also: find_all(), map(), reduce(), accumulate(), while(), for_n()
 260// Example:
 261//   func = function(x) x>5;
 262//   echo(find_all(func, [3,4,5,6,7]));
 263//   // ECHO: [3,4]
 264function find_all(func, list) =
 265    assert(is_function(func))
 266    assert(is_list(list))
 267    [for (indexnum=idx(list)) if (func(list[indexnum])) indexnum];
 268
 269
 270// Function: find_first()
 271// Topics: Function Literals, Searching
 272// Usage:
 273//   idx = find_first(func, list, [start=]);
 274// Description:
 275//   Finds the first item in `list`, after index `start`,  which the function literal in `func` will return true for.
 276//   The signature of the function literal in `func` is `function (x)`, and it is expected to return true when the
 277//   value compares as matching.  It should return false otherwise.  If you need to find *all* matching items in the
 278//   list, you should use {{find_all()}} instead.
 279// See Also: find_all(), map(), filter(), reduce(), accumulate(), while(), for_n(), binsearch()
 280// Arguments:
 281//   func = The function literal to use to check each item in `list`.  Expects the signature `function (x)`, and a boolean return value.
 282//   list = The list to search.
 283//   ---
 284//   start = The first item to check.
 285// Example:
 286//   data = [8,5,3,7,4,2,9];
 287//   echo(find_first(f_lte(4), data));
 288//   // ECHO: 2
 289// Example:
 290//   data = [8,5,3,7,4,2,9];
 291//   echo(find_first(f_lte(4), data, start=3));
 292//   // ECHO: 4
 293function find_first(func, list, start=0) =
 294    assert(is_function(func))
 295    assert(is_list(list))
 296    assert(is_finite(start))
 297    let(
 298        listlen = len(list),
 299        _find_first = function(indexnum) (
 300            indexnum >= listlen? undef :
 301            func(list[indexnum])? indexnum :
 302            _find_first(indexnum+1)
 303        )
 304    )
 305    _find_first(start);
 306
 307
 308// Function: binsearch()
 309// Topics: Function Literals, Data Structures, Searching
 310// Usage:
 311//   idx = binsearch(key,list, [cmp]);
 312// Description:
 313//   Searches a sorted list for an entry with the given key, using a binary search strategy.
 314//   Returns the index of the matching item found.  If none found, returns undef.
 315// Arguments:
 316//   key = The key to look for.
 317//   list = The list of items to search through.
 318//   idx = If given, the index of the item sublists to use as the item key.
 319//   cmp = The comparator function literal to use.  Default: `f_cmp()`
 320// See Also: map(), filter(), reduce(), accumulate(), hashmap()
 321// Example:
 322//   items = unique(rands(0,100,10000));
 323//   idx = binsearch(44, items);
 324// Example:
 325//   items = unique(rands(0,100,10000));
 326//   idx = binsearch(44, items, cmp=function(a,b) a-b);
 327// Example:
 328//   items = [for (i=[32:126]) [chr(i), i]];
 329//   idx = binsearch("G", items, idx=0);
 330function binsearch(key, list, idx, cmp=f_cmp()) =
 331    let(
 332        a = function(s,e)
 333            let(
 334                p = floor((s+e)/2),
 335                ikey = is_undef(idx)? list[p] : list[p][idx],
 336                c = cmp(ikey,key)
 337            )
 338            c == 0? p :
 339            c > 0? (p == s? undef : a(s, p-1)) :
 340            (p == e? undef : a(p+1, e))
 341    ) a(0,len(list)-1);
 342
 343
 344// Function: simple_hash()
 345// Topics: Function Literals, Hashing, Data Structures
 346// Usage:
 347//   hx = simple_hash(x);
 348// Description:
 349//   Given an arbitrary value, returns the integer hash value for it.
 350// Arguments:
 351//   x = The value to get the simple hash value  of.
 352// See Also: hashmap()
 353// Example:
 354//   x = simple_hash("Foobar");
 355//   x = simple_hash([[10,20],[-5,3]]);
 356function simple_hash(x) =
 357    let( m = 0.5 * (sqrt(5) - 1) )
 358    is_num(x)? floor(m*x*256) :
 359    is_list(x)? let(
 360        l = len(x),
 361        a = function(i,v) i>=l? v : a(i+1, m*v + simple_hash(x[i]))
 362    ) floor(a(0,0)*4096) : let(
 363        s = str(x),
 364        l = len(s),
 365        a = function(i,v) i>=l? v : a(i+1, m*v + ord(s[i]))
 366    ) floor(a(0,0)*4096);
 367
 368
 369// Function: hashmap()
 370// Topics: Function Literals, Data Structures, Hashing
 371// Usage: Creating an Empty HashMap.
 372//   hm = hashmap([hashsize=]);
 373// Usage: Creating a Populated HashMap.
 374//   hm = hashmap(items=KEYVAL_LIST, [hashsize=]);
 375// Usage: Adding an Entry
 376//   hm2 = hm(key, val);
 377// Usage: Adding Multiple Entries
 378//   hm2 = hm(additems=KEYVAL_LIST);
 379// Usage: Removing an Entry
 380//   hm2 = hm(del=KEY);
 381// Usage: Fetching a Value
 382//   x = hm(key);
 383// Usage: Iterating a HashMap
 384//   for (kv=hm()) let(k=kv[0], v=kv[1]) ...
 385// Description:
 386//   This is a factory function for creating hashmap data structure functions.  You can use a hashmap
 387//   to store large amounts of [key,value] data.  At around 4000 items, this becomes faster than using
 388//   `search()` through the list.
 389// Arguments:
 390//   ---
 391//   hashsize = The number of hashtable buckets to form.
 392//   items = A list of [key,value] pairs to initialize the hashmap with.
 393// FunctionLiteral Args:
 394//   k = The key name.
 395//   v = The value to store with the key.
 396//   ---
 397//   del = If given the key of an item to delete, makes a new hashmap with that item removed.
 398//   additems = If given a list of [key,val] pairs, makes a new hashmap with the items added.
 399// Example:
 400//   hm = hashmap(items=[for (i=[0:9999]) [str("foo",i),i]]);
 401//   a = hm("foo37");  // Returns: 37
 402//   hm2 = hm("Blah", 39);  // Adds entry "Blah" with val 39.
 403//   b = hm2("Blah");  // Returns: 39
 404//   hm3 = hm2(additems=[["bar",39],["qux",21]]);  // Adds "bar" and "qux"
 405//   hm4 = hm3(del="Blah");  // Deletes entry "Blah".
 406//   for (kv = hm4()) {  // Iterates over all key/value pairs.
 407//      echo(key=kv[0], val=kv[1]);
 408//   }
 409function hashmap(hashsize=127,items,table) =
 410    let(
 411        table = !is_undef(table)? table : [for (i=[0:1:hashsize-1]) []]
 412    )
 413    items != undef? hashmap(hashsize=hashsize, table=table)(additems=items) :
 414    function(k,v,del,additems)
 415        additems!=undef? let(
 416            hashes = [for (item = additems) simple_hash(item[0]) % hashsize],
 417            grouped = list_pad(group_data(hashes, additems), hashsize, []),
 418            table = [for (i=idx(table)) concat(table[i],grouped[i])]
 419        ) hashmap(hashsize=hashsize, table=table) :
 420        del!=undef? let(
 421            bnum = simple_hash(del) % hashsize,
 422            bucket = [for (item=table[bnum]) if (item[0]!=del) item],
 423            table = [for (i=idx(table)) i==bnum? bucket : table[i]]
 424        ) hashmap(hashsize=hashsize, table=table) :
 425        k==undef && v==undef? [for (bucket=table, item=bucket) item] :
 426        let(
 427            bnum = simple_hash(k) % hashsize,
 428            bucket = table[bnum],
 429            fnd = search([k], bucket)
 430        )
 431        k!=undef && v==undef? (fnd==[]? undef : bucket[fnd[0]][1]) :
 432        let(
 433            newtable = [
 434                for (i=idx(table))
 435                i!=bnum? table[i] :
 436                !fnd? [[k,v], each bucket] :
 437                [[k,v], for (j=idx(bucket)) if (j!=fnd[0]) bucket[i]]
 438            ]
 439        ) hashmap(hashsize=hashsize, table=newtable);
 440
 441
 442
 443//////////////////////////////////////////////////////////////////////
 444// Section: Function Meta-Generators
 445
 446
 447// Function: f_1arg()
 448// Topics: Function Literals, Function Literal Factories
 449// See Also: f_2arg(), f_3arg()
 450// Usage:
 451//   fn = f_1arg(func);
 452// Description:
 453//   Takes a function literal that accepts one argument, and returns a function
 454//   literal factory that can be used to pre-fill out that argument with a constant.
 455// Example:
 456//   f_str = f_1arg(function(a) str(a));
 457//   fn_str = f_str();   // = function(a) str(a);
 458//   fn_str3 = f_str(3); // = function() str(3);
 459function f_1arg(target_func) =
 460    function(a)
 461        a==undef? function(x) target_func(x) :
 462        function() target_func(a);
 463
 464
 465// Function: f_2arg()
 466// Topics: Function Literals, Function Literal Factories
 467// See Also: f_1arg(), f_3arg()
 468// Usage:
 469//   fn = f_2arg(target_func);
 470// Description:
 471//   Takes a function literal that accepts two arguments, and returns a function
 472//   literal factory that can be used to pre-fill out one or both of those arguments
 473//   with a constant.
 474// Example:
 475//   f_lt = f_2arg(function(a,b) a<b);
 476//   fn_lt = f_lt();      // = function(a,b) a<b;
 477//   fn_3lt = f_lt(3);    // = function(b) 3<b;
 478//   fn_3lt = f_lt(a=3);  // = function(b) 3<b;
 479//   fn_lt3 = f_lt(b=3);  // = function(a) a<3;
 480//   fn_3lt4 = f_lt(3,4); // = function() 3<4;
 481function f_2arg(target_func) =
 482    function(a,b)
 483        a==undef && b==undef? function(x,y) target_func(x,y) :
 484        a==undef? function(x) target_func(x,b) :
 485        b==undef? function(x) target_func(a,x) :
 486        function() target_func(a,b);
 487
 488
 489// Function: f_2arg_simple()
 490// Topics: Function Literals, Function Literal Factories
 491// See Also: f_1arg(), f_3arg()
 492// Usage:
 493//   fn = f_2arg_simple(target_func);
 494// Description:
 495//   Takes a function literal that accepts two arguments, and returns a function
 496//   literal factory that can be used to pre-fill out one or both of those arguments
 497//   with a constant.  When given a single argument, fills out the segond function
 498//   argument with a constant.
 499// Example:
 500//   f_lt = f_2arg_simple(function(a,b) a<b);
 501//   fn_lt = f_lt();       // = function(a,b) a<b;
 502//   fn_lt3 = f_lt(3);     // = function(a) a<3;
 503//   fn_3lt4 = f_lt(3,4);  // = function() 3<4;
 504function f_2arg_simple(target_func) =
 505    function(a,b)
 506        a==undef && b==undef? function(x,y) target_func(x,y) :
 507        b==undef? function(x) target_func(x,a) :
 508        function() target_func(a,b);
 509
 510
 511// Function: f_3arg()
 512// Topics: Function Literals, Function Literal Factories
 513// See Also: f_1arg(), f_2arg()
 514// Usage:
 515//   fn = f_3arg(target_func);
 516// Description:
 517//   Takes a function literal that accepts three arguments, and returns a function
 518//   literal factory that can be used to pre-fill out some or all of those arguments
 519//   with a constant.
 520// Example:
 521//   p1 = [10,4]; p2 = [3,7];
 522//   f_va = f_3arg(function(a,b,c) vector_angle(a,b,c));
 523//   fn_va = f_va();       // = function(a,b,c) vector_angle(a,b,c);
 524//   fn_va2 = f_lt(c=p1);  // = function(a,b) vector_angle(a,b,p1);
 525//   fn_va3 = f_lt(a=p2);  // = function(a,c) vector_angle(a,p2,c);
 526//   fn_va4 = f_lt(a=p1,c=p2); // = function() vector_angle(p1,b,p2);
 527function f_3arg(target_func) =
 528    function(a,b,c)
 529        a==undef && b==undef && c==undef? function(x,y,z) target_func(x,y,z) :
 530        a==undef && b==undef? function(x,y) target_func(x,y,c) :
 531        a==undef && c==undef? function(x,y) target_func(x,b,y) :
 532        b==undef && c==undef? function(x,y) target_func(a,x,y) :
 533        a==undef? function(x) target_func(x,b,c) :
 534        b==undef? function(x) target_func(a,x,c) :
 535        c==undef? function(x) target_func(a,b,x) :
 536        function() target_func(a,b,c);
 537
 538
 539// Function: ival()
 540// Usage:
 541//   newfunc = ival(func);
 542// Description:
 543//   Wraps a single-argument function literal so that it can take two arguments,
 544//   passing the first argument along to the wrapped function.
 545// Arguments:
 546//   target_func = The function of signature (x) to wrap.
 547// FunctionLiteral Args:
 548//   a = The argument that will be passed through.
 549//   b = The argumen that will be discarded.
 550// Example:
 551//   x = while(0, ival(f_lt(5)), xval(fngen_add(1)));
 552function ival(target_func) = function(a,b) target_func(a);
 553
 554
 555// Function: xval()
 556// Usage:
 557//   newfunc = xval(func);
 558// Description:
 559//   Wraps a single-argument function literal so that it can take two arguments,
 560//   passing the first argument along to the wrapped function.
 561// Arguments:
 562//   target_func = The function of signature (x) to wrap.
 563// FunctionLiteral Args:
 564//   a = The argument that will be passed through.
 565//   b = The argumen that will be discarded.
 566// Example:
 567//   x = while(0, ival(f_lt(5)), xval(fngen_add(1)));
 568function xval(target_func) = function(a,b) target_func(b);
 569
 570
 571
 572//////////////////////////////////////////////////////////////////////
 573// Section: Comparator Generators
 574
 575
 576// Function: f_cmp()
 577// Usage:
 578//   fn = f_cmp();
 579//   fn = f_cmp(b);
 580//   fn = f_cmp(a,b);
 581// Description:
 582//   A factory that generates function literals that compare `a` and `b`, where one or
 583//   both arguments can be replaced with constants.  If `a` and `b` are equal, the function
 584//   literal will return 0.  If a<b then -1 is returned.  If a>b then 1 is returned.
 585// Example:
 586//   fn_cmp = f_cmp();       // = function(a,b) a==b?0: a>b?1: -1;
 587//   fn_cmp3 = f_cmp(3);     // = function(a) a==3?0: a>3?1: -1;
 588//   fn_3cmp4 = f_cmp(3,4);  // = function() 3==4?0: 3>4?1: -1;
 589function f_cmp(a,b) = f_2arg_simple(function (a,b) a==b?0: a>b?1: -1)(a,b);
 590
 591
 592// Function: f_gt()
 593// Usage:
 594//   fn = f_gt();
 595//   fn = f_gt(b);
 596//   fn = f_gt(a,b);
 597// Description:
 598//   A factory that generates function literals based on `a > b`, where one
 599//   or both of the arguments can be replaced with constants.
 600// Example:
 601//   fn_gt = f_gt();       // = function(a,b) a>b;
 602//   fn_gt3 = f_gt(3);     // = function(a) a>3;
 603//   fn_3gt4 = f_gt(3,4);  // = function() 3>4;
 604function f_gt(a,b) = f_2arg_simple(function (a,b) a>b)(a,b);
 605
 606
 607// Function: f_lt()
 608// Usage:
 609//   fn = f_lt();
 610//   fn = f_lt(b);
 611//   fn = f_lt(a,b);
 612// Description:
 613//   A factory that generates function literals based on `a < b`, where one
 614//   or both of the arguments can be replaced with constants.
 615// Example:
 616//   fn_lt = f_lt();       // = function(a,b) a<b;
 617//   fn_lt3 = f_lt(3);     // = function(a) a<3;
 618//   fn_3lt4 = f_lt(3,4);  // = function() 3<4;
 619function f_lt(a,b) = f_2arg_simple(function (a,b) a<b)(a,b);
 620
 621
 622// Function: f_gte()
 623// Usage:
 624//   fn = f_gte();
 625//   fn = f_gte(b);
 626//   fn = f_gte(a,b);
 627// Description:
 628//   A factory that generates function literals based on `a >= b`, where one
 629//   or both of the arguments can be replaced with constants.
 630// Example:
 631//   fn_gte = f_gte();       // = function(a,b) a>=b;
 632//   fn_gte3 = f_gte(3);     // = function(a) a>=3;
 633//   fn_3gte4 = f_gte(3,4);  // = function() 3>=4;
 634function f_gte(a,b) = f_2arg_simple(function (a,b) a>=b)(a,b);
 635
 636
 637// Function: f_lte()
 638// Usage:
 639//   fn = f_lte();
 640//   fn = f_lte(b);
 641//   fn = f_lte(a,b);
 642// Description:
 643//   A factory that generates function literals based on `a <= b`, where
 644//   one or both arguments can be replaced with constants.
 645// Example:
 646//   fn_lte = f_lte();       // = function(a,b) a<=b;
 647//   fn_lte3 = f_lte(3);     // = function(a) a<=3;
 648//   fn_3lte4 = f_lte(3,4);  // = function() 3<=4;
 649function f_lte(a,b) = f_2arg_simple(function (a,b) a<=b)(a,b);
 650
 651
 652// Function: f_eq()
 653// Usage:
 654//   fn = f_eq();
 655//   fn = f_eq(b);
 656//   fn = f_eq(a,b);
 657// Description:
 658//   A factory that generates function literals based on `a == b`, where
 659//   one or both arguments can be replaced with constants.
 660// Example:
 661//   fn_eq = f_eq();       // = function(a,b) a==b;
 662//   fn_eq3 = f_eq(3);     // = function(a) a==3;
 663//   fn_3eq4 = f_eq(3,4);  // = function() 3==4;
 664function f_eq(a,b) = f_2arg_simple(function (a,b) a==b)(a,b);
 665
 666
 667// Function: f_neq()
 668// Usage:
 669//   fn = f_neq();
 670//   fn = f_neq(b);
 671//   fn = f_neq(a,b);
 672// Description:
 673//   A factory that generates function literals based on `a != b`, where
 674//   one or both arguments can be replaced with constants.
 675// Example:
 676//   fn_neq = f_neq();       // = function(a,b) a!=b;
 677//   fn_neq3 = f_neq(3);     // = function(a) a!=3;
 678//   fn_3neq4 = f_neq(3,4);  // = function() 3!=4;
 679function f_neq(a,b) = f_2arg_simple(function (a,b) a!=b)(a,b);
 680
 681
 682// Function: f_approx()
 683// Usage:
 684//   fn = f_approx();
 685//   fn = f_approx(b);
 686//   fn = f_approx(a,b);
 687// Description:
 688//   A factory that generates function literals based on `approx(a,b)`, where
 689//   one or both arguments can be replaced with constants.
 690// Example:
 691//   fn_approx = f_approx();       // = function(a,b) approx(a,b);
 692//   fn_approx3 = f_approx(3);     // = function(a) approx(a,3);
 693//   fn_3approx4 = f_approx(3,4);  // = function() approx(3,4);
 694function f_approx(a,b) = f_2arg_simple(function (a,b) approx(a,b))(a,b);
 695
 696
 697// Function: f_napprox()
 698// Usage:
 699//   fn = f_napprox();
 700//   fn = f_napprox(b);
 701//   fn = f_napprox(a,b);
 702// Description:
 703//   A factory that generates function literals based on `!approx(a,b)`, where
 704//   one or both arguments can be replaced with constants.
 705// Example:
 706//   fn_napprox = f_napprox();       // = function(a,b) napprox(a,b);
 707//   fn_napprox3 = f_napprox(3);     // = function(a) napprox(a,3);
 708//   fn_3napprox4 = f_napprox(3,4);  // = function() napprox(3,4);
 709function f_napprox(a,b) = f_2arg_simple(function (a,b) !approx(a,b))(a,b);
 710
 711
 712
 713//////////////////////////////////////////////////////////////////////
 714// Section: Logic Operators
 715
 716
 717// Function: f_or()
 718// Topics: Function Literals, Logic, Boolean Operations
 719// Usage:
 720//   fn = f_or();
 721//   fn = f_or(a=);
 722//   fn = f_or(b=);
 723//   fn = f_or(a=,b=);
 724// Description:
 725//   A factory that generates function literals based on `a || b`, where
 726//   either or both of the `a` or `b` arguments can be replaced with constants.
 727// Arguments:
 728//   a = If given, replaces the first argument.
 729//   b = If given, replaces the second argument.
 730// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
 731function f_or(a,b) = f_2arg(function(a,b) (a || b))(a,b);
 732
 733
 734// Function: f_and()
 735// Topics: Function Literals, Logic, Boolean Operations
 736// Usage:
 737//   fn = f_and();
 738//   fn = f_and(a=);
 739//   fn = f_and(b=);
 740//   fn = f_and(a=,b=);
 741// Description:
 742//   A factory that generates function literals based on `a && b`, where
 743//   either or both of the `a` or `b` arguments can be replaced with constants.
 744// Arguments:
 745//   a = If given, replaces the first argument.
 746//   b = If given, replaces the second argument.
 747// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
 748function f_and(a,b) = f_2arg(function(a,b) (a && b))(a,b);
 749
 750
 751// Function: f_nor()
 752// Topics: Function Literals, Logic, Boolean Operations
 753// Usage:
 754//   fn = f_nor();
 755//   fn = f_nor(a=);
 756//   fn = f_nor(b=);
 757//   fn = f_nor(a=,b=);
 758// Description:
 759//   A factory that generates function literals based on `!(a || b)`, where
 760//   either or both of the `a` or `b` arguments can be replaced with constants.
 761// Arguments:
 762//   a = If given, replaces the first argument.
 763//   b = If given, replaces the second argument.
 764// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
 765function f_nor(a,b) = f_2arg(function(a,b) !(a || b))(a,b);
 766
 767
 768// Function: f_nand()
 769// Topics: Function Literals, Logic, Boolean Operations
 770// Usage:
 771//   fn = f_nand();
 772//   fn = f_nand(a=);
 773//   fn = f_nand(b=);
 774//   fn = f_nand(a=,b=);
 775// Description:
 776//   A factory that generates function literals based on `!(a && b)`, where
 777//   either or both of the `a` or `b` arguments can be replaced with constants.
 778// Arguments:
 779//   a = If given, replaces the first argument.
 780//   b = If given, replaces the second argument.
 781// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
 782function f_nand(a,b) = f_2arg(function(a,b) !(a && b))(a,b);
 783
 784
 785// Function: f_xor()
 786// Topics: Function Literals, Logic, Boolean Operations
 787// Usage:
 788//   fn = f_xor();
 789//   fn = f_xor(a=);
 790//   fn = f_xor(b);
 791//   fn = f_xor(a=,b=);
 792// Description:
 793//   A factory that generates function literals based on `(!a && b) || (a && !b)`, where
 794//   either or both of the `a` or `b` arguments can be replaced with constants.
 795// Arguments:
 796//   a = If given, replaces the first argument.
 797//   b = If given, replaces the second argument.
 798// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
 799function f_xor(a,b) = f_2arg(function(a,b) (!a && b) || (a && !b))(a,b);
 800
 801
 802// Function: f_not()
 803// Topics: Function Literals, Logic, Boolean Operations
 804// Usage:
 805//   fn = f_not();
 806//   fn = f_not(a);
 807// Description:
 808//   A factory that generates function literals based on `!a`, where the `a`
 809//   argument can be replaced with a constant.
 810// Arguments:
 811//   a = If given, replaces the argument.
 812// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not()
 813function f_not(a) = f_1arg(function(a) !a)(a);
 814
 815
 816// Function: f_even()
 817// Topics: Function Literals, Math Operators
 818// Usage:
 819//   fn = f_even();
 820//   fn = f_even(a);
 821// Description:
 822//   A factory that generates function literals based on `a % 2 == 0`, where the `a`
 823//   argument can optionally be replaced with a constant.
 824// Arguments:
 825//   a = If given, replaces the argument.
 826// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not(), f_even(), f_odd()
 827// Example:
 828//   l2 = filter(f_even(), [3,4,5,6,7,8]);  // Returns: [4,6,8]
 829function f_even(a) = f_1arg(function(a) a % 2 == 0)(a);
 830
 831
 832// Function: f_odd()
 833// Topics: Function Literals, Math Operators
 834// Usage:
 835//   fn = f_odd();
 836//   fn = f_odd(a);
 837// Description:
 838//   A factory that generates function literals based on `a % 2 != 0`, where the `a`
 839//   argument can optionally be replaced with a constant.
 840// Arguments:
 841//   a = If given, replaces the argument.
 842// See Also: f_or(), f_and(), f_nor(), f_nand(), f_xor(), f_not(), f_even(), f_odd()
 843// Example:
 844//   l2 = filter(f_odd(), [3,4,5,6,7,8]);  // Returns: [3,5,7]
 845function f_odd(a) = f_1arg(function(a) a % 2 != 0)(a);
 846
 847
 848
 849//////////////////////////////////////////////////////////////////////
 850// Section: Math Operators
 851
 852
 853// Function: f_add()
 854// Topics: Function Literals, Math Operators
 855// Usage:
 856//   fn = f_add();
 857//   fn = f_add(a=);
 858//   fn = f_add(b);
 859//   fn = f_add(a=,b=);
 860// Description:
 861//   A factory that generates function literals based on `a + b`, where either
 862//   or both of the `a` or `b` arguments can be replaced with constants.
 863// Arguments:
 864//   a = If given, replaces the first argument.
 865//   b = If given, replaces the second argument.
 866// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
 867function f_add(a,b) = f_2arg(function(a,b) a + b)(a,b);
 868
 869
 870// Function: f_sub()
 871// Topics: Function Literals, Math Operators
 872// Usage:
 873//   fn = f_sub();
 874//   fn = f_sub(a=);
 875//   fn = f_sub(b);
 876//   fn = f_sub(a=,b=);
 877// Description:
 878//   A factory that generates function literals based on `a - b`, where either
 879//   or both of the `a` or `b` arguments can be replaced with constants.
 880// Arguments:
 881//   a = If given, replaces the first argument.
 882//   b = If given, replaces the second argument.
 883// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
 884function f_sub(a,b) = f_2arg(function(a,b) a - b)(a,b);
 885
 886
 887// Function: f_mul()
 888// Topics: Function Literals, Math Operators
 889// Usage:
 890//   fn = f_mul();
 891//   fn = f_mul(a=);
 892//   fn = f_mul(b);
 893//   fn = f_mul(a=,b=);
 894// Description:
 895//   A factory that generates function literals based on `a * b`, where either
 896//   or both of the `a` or `b` arguments can be replaced with constants.
 897// Arguments:
 898//   a = If given, replaces the first argument.
 899//   b = If given, replaces the second argument.
 900// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
 901function f_mul(a,b) = f_2arg(function(a,b) a * b)(a,b);
 902
 903
 904// Function: f_div()
 905// Topics: Function Literals, Math Operators
 906// Usage:
 907//   fn = f_div();
 908//   fn = f_div(a=);
 909//   fn = f_div(b);
 910//   fn = f_div(a=,b=);
 911// Description:
 912//   A factory that generates function literals based on `a / b`, where either
 913//   or both of the `a` or `b` arguments can be replaced with constants.
 914// Arguments:
 915//   a = If given, replaces the first argument.
 916//   b = If given, replaces the second argument.
 917// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
 918function f_div(a,b) = f_2arg(function(a,b) a / b)(a,b);
 919
 920
 921// Function: f_mod()
 922// Topics: Function Literals, Math Operators
 923// Usage:
 924//   fn = f_mod();
 925//   fn = f_mod(a=);
 926//   fn = f_mod(b);
 927//   fn = f_mod(a=,b=);
 928// Description:
 929//   A factory that generates function literals based on `a % b`, where either
 930//   or both of the `a` or `b` arguments can be replaced with constants.
 931// Arguments:
 932//   a = If given, replaces the first argument.
 933//   b = If given, replaces the second argument.
 934// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
 935function f_mod(a,b) = f_2arg(function(a,b) a % b)(a,b);
 936
 937
 938// Function: f_pow()
 939// Topics: Function Literals, Math Operators
 940// Usage:
 941//   fn = f_pow();
 942//   fn = f_pow(a=);
 943//   fn = f_pow(b);
 944//   fn = f_pow(a=,b=);
 945// Description:
 946//   A factory that generates function literals based on `pow(a,b)`, where either
 947//   or both of the `a` or `b` arguments can be replaced with constants.
 948// Arguments:
 949//   a = If given, replaces the first argument.
 950//   b = If given, replaces the second argument.
 951// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
 952function f_pow(a,b) = f_2arg(function(a,b) pow(a,b))(a,b);
 953
 954
 955// Function: f_neg()
 956// Topics: Function Literals, Math Operators
 957// Usage:
 958//   fn = f_neg();
 959//   fn = f_neg(a);
 960// Description:
 961//   A factory that generates function literals based on `-a`, where the `a`
 962//   argument can optionally be replaced with a constant.
 963// Arguments:
 964//   a = If given, replaces the argument.
 965// See Also: f_add(), f_sub(), f_mul(), f_div(), f_mod(), f_pow(), f_neg()
 966function f_neg(a) = f_1arg(function(a) -a)(a);
 967
 968
 969
 970//////////////////////////////////////////////////////////////////////
 971// Section: Min/Max Operators
 972
 973
 974// Function: f_min()
 975// Topics: Function Literals, Math Operators
 976// Usage:
 977//   fn = f_min();
 978//   fn = f_min(a);
 979// Description:
 980//   A factory that generates function literals based on `min(a)`, where the `a`
 981//   argument can optionally be replaced with a constant.
 982// Arguments:
 983//   a = If given, replaces the argument.
 984// See Also: f_min(), f_max(), f_min2(), f_max2(), f_min3(), f_max3()
 985function f_min(a) = f_1arg(function(a) min(a))(a);
 986
 987
 988// Function: f_max()
 989// Topics: Function Literals, Math Operators
 990// Usage:
 991//   fn = f_max();
 992//   fn = f_max(a);
 993// Description:
 994//   A factory that generates function literals based on `max(a)`, where the `a`
 995//   argument can optionally be replaced with a constant.
 996// Arguments:
 997//   a = If given, replaces the argument.
 998// See Also: f_min(), f_max(), f_min2(), f_max2(), f_min3(), f_max3()
 999function f_max(a) = f_1arg(function(a) max(a))(a);
1000
1001
1002// Function: f_min2()
1003// Topics: Function Literals, Math Operators
1004// Usage:
1005//   fn = f_min2();
1006//   fn = f_min2(a=);
1007//   fn = f_min2(b);
1008//   fn = f_min2(a=,b=);
1009// Description:
1010//   A factory that generates function literals based on `min(a,b)`, where either
1011//   or both of the `a` or `b` arguments can be replaced with constants.
1012// Arguments:
1013//   a = If given, replaces the first argument.
1014//   b = If given, replaces the second argument.
1015// See Also: f_min(), f_max(), f_min2(), f_max2(), f_min3(), f_max3()
1016function f_min2(a,b) = f_2arg(function(a,b) min(a,b))(a,b);
1017
1018
1019// Function: f_max2()
1020// Topics: Function Literals, Math Operators
1021// Usage:
1022//   fn = f_max2();
1023//   fn = f_max2(a=);
1024//   fn = f_max2(b);
1025//   fn = f_max2(a=,b=);
1026// Description:
1027//   A factory that generates function literals based on `max(a,b)`, where either
1028//   or both of the `a` or `b` arguments can be replaced with constants.
1029// Arguments:
1030//   a = If given, replaces the first argument.
1031//   b = If given, replaces the second argument.
1032// See Also: f_min(), f_max(), f_min2(), f_max2(), f_min3(), f_max3()
1033function f_max2(a,b) = f_2arg(function(a,b) max(a,b))(a,b);
1034
1035
1036// Function: f_min3()
1037// Topics: Function Literals, Math Operators
1038// Usage:
1039//   fn = f_min3();
1040//   fn = f_min3(a=);
1041//   fn = f_min3(b=);
1042//   fn = f_min3(c=);
1043//   fn = f_min3(a=,b=);
1044//   fn = f_min3(b=,c=);
1045//   fn = f_min3(a=,c=);
1046//   fn = f_min3(a=,b=,c=);
1047// Description:
1048//   A factory that generates function literals based on `min(a,b,c)`, where any
1049//   or all of the `a`, `b`, or`c` arguments can be replaced with constants.
1050// Arguments:
1051//   a = If given, replaces the first argument.
1052//   b = If given, replaces the second argument.
1053//   c = If given, replaces the third argument.
1054// See Also: f_min(), f_max(), f_min2(), f_max2(), f_min3(), f_max3()
1055function f_min3(a,b,c) = f_3arg(function(a,b,c) min(a,b,c))(a,b,c);
1056
1057
1058// Function: f_max3()
1059// Topics: Function Literals, Math Operators
1060// Usage:
1061//   fn = f_max3();
1062//   fn = f_max3(a=);
1063//   fn = f_max3(b=);
1064//   fn = f_max3(c=);
1065//   fn = f_max3(a=,b=);
1066//   fn = f_max3(b=,c=);
1067//   fn = f_max3(a=,c=);
1068//   fn = f_max3(a=,b=,c=);
1069// Description:
1070//   A factory that generates function literals based on `min(a,b,c)`, where any
1071//   or all of the `a`, `b`, or`c` arguments can be replaced with constants.
1072// Arguments:
1073//   a = If given, replaces the first argument.
1074//   b = If given, replaces the second argument.
1075//   c = If given, replaces the third argument.
1076// See Also: f_min(), f_max(), f_min2(), f_max2(), f_min3(), f_max3()
1077function f_max3(a,b,c) = f_3arg(function(a,b,c) max(a,b,c))(a,b,c);
1078
1079
1080
1081//////////////////////////////////////////////////////////////////////
1082// Section: Trigonometry Operators
1083
1084
1085// Function: f_sin()
1086// Topics: Function Literals, Math Operators
1087// Usage:
1088//   fn = f_sin();
1089//   fn = f_sin(a);
1090// Description:
1091//   A factory that generates function literals based on `sin(a)`, where the `a`
1092//   argument can optionally be replaced with a constant.
1093// Arguments:
1094//   a = If given, replaces the argument.
1095// See Also: f_sin(), f_cos(), f_tan(), f_asin(), f_acos(), f_atan(), f_atan2()
1096function f_sin(a)  = f_1arg(function(a) sin(a))(a);
1097
1098
1099// Function: f_cos()
1100// Topics: Function Literals, Math Operators
1101// Usage:
1102//   fn = f_cos();
1103//   fn = f_cos(a);
1104// Description:
1105//   A factory that generates function literals based on `cos(a)`, where the `a`
1106//   argument can optionally be replaced with a constant.
1107// Arguments:
1108//   a = If given, replaces the argument.
1109// See Also: f_sin(), f_cos(), f_tan(), f_asin(), f_acos(), f_atan(), f_atan2()
1110function f_cos(a)  = f_1arg(function(a) cos(a))(a);
1111
1112
1113// Function: f_tan()
1114// Topics: Function Literals, Math Operators
1115// Usage:
1116//   fn = f_tan();
1117//   fn = f_tan(a);
1118// Description:
1119//   A factory that generates function literals based on `tan(a)`, where the `a`
1120//   argument can optionally be replaced with a constant.
1121// Arguments:
1122//   a = If given, replaces the argument.
1123// See Also: f_sin(), f_cos(), f_tan(), f_asin(), f_acos(), f_atan(), f_atan2()
1124function f_tan(a)  = f_1arg(function(a) tan(a))(a);
1125
1126
1127// Function: f_asin()
1128// Topics: Function Literals, Math Operators
1129// Usage:
1130//   fn = f_asin();
1131//   fn = f_asin(a);
1132// Description:
1133//   A factory that generates function literals based on `asin(a)`, where the `a`
1134//   argument can optionally be replaced with a constant.
1135// Arguments:
1136//   a = If given, replaces the argument.
1137// See Also: f_sin(), f_cos(), f_tan(), f_asin(), f_acos(), f_atan(), f_atan2()
1138function f_asin(a) = f_1arg(function(a) asin(a))(a);
1139
1140
1141// Function: f_acos()
1142// Topics: Function Literals, Math Operators
1143// Usage:
1144//   fn = f_acos();
1145//   fn = f_acos(a);
1146// Description:
1147//   A factory that generates function literals based on `acos(a)`, where the `a`
1148//   argument can optionally be replaced with a constant.
1149// Arguments:
1150//   a = If given, replaces the argument.
1151// See Also: f_sin(), f_cos(), f_tan(), f_asin(), f_acos(), f_atan(), f_atan2()
1152function f_acos(a) = f_1arg(function(a) acos(a))(a);
1153
1154
1155// Function: f_atan()
1156// Topics: Function Literals, Math Operators
1157// Usage:
1158//   fn = f_atan();
1159//   fn = f_atan(a);
1160// Description:
1161//   A factory that generates function literals based on `atan(a)`, where the `a`
1162//   argument can optionally be replaced with a constant.
1163// Arguments:
1164//   a = If given, replaces the argument.
1165// See Also: f_sin(), f_cos(), f_tan(), f_asin(), f_acos(), f_atan(), f_atan2()
1166function f_atan(a) = f_1arg(function(a) atan(a))(a);
1167
1168
1169// Function: f_atan2()
1170// Topics: Function Literals, Math Operators
1171// Usage:
1172//   fn = f_atan2();
1173//   fn = f_atan2(a=);
1174//   fn = f_atan2(b);
1175//   fn = f_atan2(a=,b=);
1176// Description:
1177//   A factory that generates function literals based on `atan2(a,b)`, where either
1178//   or both of the `a` or `b` arguments can be replaced with constants.
1179// Arguments:
1180//   a = If given, replaces the first argument.
1181//   b = If given, replaces the second argument.
1182// See Also: f_sin(), f_cos(), f_tan(), f_asin(), f_acos(), f_atan(), f_atan2()
1183function f_atan2(a,b) = f_2arg(function(a,b) atan2(a,b))(a,b);
1184
1185
1186
1187//////////////////////////////////////////////////////////////////////
1188// Section: String Operators
1189
1190
1191// Function: f_len()
1192// Topics: Function Literals, String Operators
1193// Usage:
1194//   fn = f_len();
1195//   fn = f_len(a);
1196// Description:
1197//   A factory that generates function literals based on `len(a)`, where the `a`
1198//   argument can optionally be replaced with a constant.
1199// Arguments:
1200//   a = If given, replaces the argument.
1201// See Also: f_len(), f_chr(), f_ord(), f_str(), f_str2(), f_str3()
1202function f_len(a) = f_1arg(function(a) len(a))(a);
1203
1204
1205// Function: f_chr()
1206// Topics: Function Literals, String Operators
1207// Usage:
1208//   fn = f_chr();
1209//   fn = f_chr(a);
1210// Description:
1211//   A factory that generates function literals based on `chr(a)`, where the `a`
1212//   argument can optionally be replaced with a constant.
1213// Arguments:
1214//   a = If given, replaces the argument.
1215// See Also: f_len(), f_chr(), f_ord(), f_str(), f_str2(), f_str3()
1216function f_chr(a) = f_1arg(function(a) chr(a))(a);
1217
1218
1219// Function: f_ord()
1220// Topics: Function Literals, String Operators
1221// Usage:
1222//   fn = f_ord();
1223//   fn = f_ord(a);
1224// Description:
1225//   A factory that generates function literals based on `ord(a)`, where the `a`
1226//   argument can optionally be replaced with a constant.
1227// Arguments:
1228//   a = If given, replaces the argument.
1229// See Also: f_len(), f_chr(), f_ord(), f_str(), f_str2(), f_str3()
1230function f_ord(a) = f_1arg(function(a) ord(a))(a);
1231
1232
1233// Function: f_str()
1234// Topics: Function Literals, String Operators
1235// Usage:
1236//   fn = f_str();
1237//   fn = f_str(a);
1238// Description:
1239//   A factory that generates function literals based on `str(a)`, where the `a`
1240//   argument can optionally be replaced with a constant.
1241// Arguments:
1242//   a = If given, replaces the argument.
1243// See Also: f_len(), f_chr(), f_ord(), f_str(), f_str2(), f_str3()
1244function f_str(a) = f_1arg(function(a) str(a))(a);
1245
1246
1247// Function: f_str2()
1248// Topics: Function Literals, String Operators
1249// Usage:
1250//   fn = f_str2();
1251//   fn = f_str2(a=);
1252//   fn = f_str2(b);
1253//   fn = f_str2(a=,b=);
1254// Description:
1255//   A factory that generates function literals based on `str(a,b)`, where either
1256//   or both of the `a` or `b` arguments can be replaced with constants.
1257// Arguments:
1258//   a = If given, replaces the first argument.
1259//   b = If given, replaces the second argument.
1260// See Also: f_len(), f_chr(), f_ord(), f_str(), f_str2(), f_str3()
1261function f_str2(a,b) = f_2arg(function(a,b) str(a,b))(a,b);
1262
1263
1264// Function: f_str3()
1265// Topics: Function Literals, Math Operators
1266// Usage:
1267//   fn = f_str3();
1268//   fn = f_str3(a=);
1269//   fn = f_str3(b=);
1270//   fn = f_str3(c=);
1271//   fn = f_str3(a=,b=);
1272//   fn = f_str3(b=,c=);
1273//   fn = f_str3(a=,c=);
1274//   fn = f_str3(a=,b=,c=);
1275// Description:
1276//   A factory that generates function literals based on `str(a,b,c)`, where any
1277//   or all of the `a`, `b`, or`c` arguments can be replaced with constants.
1278// Arguments:
1279//   a = If given, replaces the first argument.
1280//   b = If given, replaces the second argument.
1281//   c = If given, replaces the third argument.
1282// See Also: f_len(), f_chr(), f_ord(), f_str(), f_str2(), f_str3()
1283function f_str3(a,b,c) = f_3arg(function(a,b,c) str(a,b,c))(a,b,c);
1284
1285
1286
1287//////////////////////////////////////////////////////////////////////
1288// Section: Miscellaneous Operators
1289
1290
1291// Function: f_floor()
1292// Topics: Function Literals, String Operators
1293// Usage:
1294//   fn = f_floor();
1295//   fn = f_floor(a);
1296// Description:
1297//   A factory that generates function literals based on `floor(a)`, where the `a`
1298//   argument can optionally be replaced with a constant.
1299// Arguments:
1300//   a = If given, replaces the argument.
1301// See Also: f_floor(), f_ceil(), f_round()
1302function f_floor(a) = f_1arg(function(a) floor(a))(a);
1303
1304
1305// Function: f_round()
1306// Topics: Function Literals, String Operators
1307// Usage:
1308//   fn = f_round();
1309//   fn = f_round(a);
1310// Description:
1311//   A factory that generates function literals based on `round(a)`, where the `a`
1312//   argument can optionally be replaced with a constant.
1313// Arguments:
1314//   a = If given, replaces the argument.
1315// See Also: f_floor(), f_ceil(), f_round()
1316function f_round(a) = f_1arg(function(a) round(a))(a);
1317
1318
1319// Function: f_ceil()
1320// Topics: Function Literals, String Operators
1321// Usage:
1322//   fn = f_ceil();
1323//   fn = f_ceil(a);
1324// Description:
1325//   A factory that generates function literals based on `ceil(a)`, where the `a`
1326//   argument can optionally be replaced with a constant.
1327// Arguments:
1328//   a = If given, replaces the argument.
1329// See Also: f_floor(), f_ceil(), f_round()
1330function f_ceil(a)  = f_1arg(function(a) ceil(a))(a);
1331
1332
1333// Function: f_abs()
1334// Topics: Function Literals, String Operators
1335// Usage:
1336//   fn = f_abs();
1337//   fn = f_abs(a);
1338// Description:
1339//   A factory that generates function literals based on `abs(a)`, where the `a`
1340//   argument can optionally be replaced with a constant.
1341// Arguments:
1342//   a = If given, replaces the argument.
1343// See Also: f_abs(), f_sign(), f_ln(), f_log(), f_exp(), f_sqr(), f_sqrt()
1344function f_abs(a)   = f_1arg(function(a) abs(a))(a);
1345
1346
1347// Function: f_sign()
1348// Topics: Function Literals, String Operators
1349// Usage:
1350//   fn = f_sign();
1351//   fn = f_sign(a);
1352// Description:
1353//   A factory that generates function literals based on `sign(a)`, where the `a`
1354//   argument can optionally be replaced with a constant.
1355// Arguments:
1356//   a = If given, replaces the argument.
1357// See Also: f_abs(), f_sign(), f_ln(), f_log(), f_exp(), f_sqr(), f_sqrt()
1358function f_sign(a)  = f_1arg(function(a) sign(a))(a);
1359
1360
1361// Function: f_ln()
1362// Topics: Function Literals, String Operators
1363// Usage:
1364//   fn = f_ln();
1365//   fn = f_ln(a);
1366// Description:
1367//   A factory that generates function literals based on `ln(a)`, where the `a`
1368//   argument can optionally be replaced with a constant.
1369// Arguments:
1370//   a = If given, replaces the argument.
1371// See Also: f_abs(), f_sign(), f_ln(), f_log(), f_exp(), f_sqr(), f_sqrt()
1372function f_ln(a)    = f_1arg(function(a) ln(a))(a);
1373
1374
1375// Function: f_log()
1376// Topics: Function Literals, String Operators
1377// Usage:
1378//   fn = f_log();
1379//   fn = f_log(a);
1380// Description:
1381//   A factory that generates function literals based on `log(a)`, where the `a`
1382//   argument can optionally be replaced with a constant.
1383// Arguments:
1384//   a = If given, replaces the argument.
1385// See Also: f_abs(), f_sign(), f_ln(), f_log(), f_exp(), f_sqr(), f_sqrt()
1386function f_log(a)   = f_1arg(function(a) log(a))(a);
1387
1388
1389// Function: f_exp()
1390// Topics: Function Literals, String Operators
1391// Usage:
1392//   fn = f_exp();
1393//   fn = f_exp(a);
1394// Description:
1395//   A factory that generates function literals based on `exp(a)`, where the `a`
1396//   argument can optionally be replaced with a constant.
1397// Arguments:
1398//   a = If given, replaces the argument.
1399// See Also: f_abs(), f_sign(), f_ln(), f_log(), f_exp(), f_sqr(), f_sqrt()
1400function f_exp(a)   = f_1arg(function(a) exp(a))(a);
1401
1402
1403// Function: f_sqr()
1404// Topics: Function Literals, String Operators
1405// Usage:
1406//   fn = f_sqr();
1407//   fn = f_sqr(a);
1408// Description:
1409//   A factory that generates function literals based on `a*a`, where the `a`
1410//   argument can optionally be replaced with a constant.
1411// Arguments:
1412//   a = If given, replaces the argument.
1413// See Also: f_abs(), f_sign(), f_ln(), f_log(), f_exp(), f_sqr(), f_sqrt()
1414function f_sqr(a)   = f_1arg(function(a) a*a)(a);
1415
1416
1417// Function: f_sqrt()
1418// Topics: Function Literals, String Operators
1419// Usage:
1420//   fn = f_sqrt();
1421//   fn = f_sqrt(a);
1422// Description:
1423//   A factory that generates function literals based on `sqrt(a)`, where the `a`
1424//   argument can optionally be replaced with a constant.
1425// Arguments:
1426//   a = If given, replaces the argument.
1427// See Also: f_abs(), f_sign(), f_ln(), f_log(), f_exp(), f_sqr(), f_sqrt()
1428function f_sqrt(a)  = f_1arg(function(a) sqrt(a))(a);
1429
1430
1431// Function: f_norm()
1432// Topics: Function Literals, String Operators
1433// Usage:
1434//   fn = f_norm();
1435//   fn = f_norm(a);
1436// Description:
1437//   A factory that generates function literals based on `norm(a)`, where the `a`
1438//   argument can optionally be replaced with a constant.
1439// Arguments:
1440//   a = If given, replaces the argument.
1441// See Also: f_abs(), f_sign(), f_ln(), f_log(), f_exp(), f_sqr(), f_sqrt()
1442function f_norm(a)  = f_1arg(function(a) norm(a))(a);
1443
1444
1445// Function: f_cross()
1446// Topics: Function Literals, String Operators
1447// Usage:
1448//   fn = f_cross();
1449//   fn = f_cross(a=);
1450//   fn = f_cross(b);
1451//   fn = f_cross(a=,b=);
1452// Description:
1453//   A factory that generates function literals based on `str(a,b)`, where either
1454//   or both of the `a` or `b` arguments can be replaced with constants.
1455// Arguments:
1456//   a = If given, replaces the first argument.
1457//   b = If given, replaces the second argument.
1458// See Also: f_norm(), f_abs(), f_sign(), f_cross()
1459function f_cross(a,b) = f_2arg(function(a,b) cross(a,b))(a,b);
1460
1461
1462// Section: Type Queries
1463
1464// Function: f_is_def()
1465// Topics: Function Literals, Type Queries
1466// Usage:
1467//   fn = f_is_def();
1468// Description:
1469//   A factory that returns function literals equivalent to `is_def(a)`.
1470// Arguments:
1471//   a = If given, replaces the argument.
1472// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_string(), f_is_list()
1473function f_is_def(x) = f_1arg(function (x) is_def(x));
1474
1475
1476// Function: f_is_undef()
1477// Topics: Function Literals, Type Queries
1478// Usage:
1479//   fn = f_is_undef();
1480// Description:
1481//   A factory that returns function literals equivalent to `is_undef(a)`.
1482// Arguments:
1483//   a = If given, replaces the argument.
1484// See Also: f_is_bool(), f_is_num(), f_is_string(), f_is_list()
1485function f_is_undef(x) = f_1arg(function (x) is_undef(x));
1486
1487
1488// Function: f_is_bool()
1489// Topics: Function Literals, Type Queries
1490// Usage:
1491//   fn = f_is_bool();
1492// Description:
1493//   A factory that returns function literals equivalent to `is_bool(a)`.
1494// Arguments:
1495//   a = If given, replaces the argument.
1496// See Also: f_is_undef(), f_is_num(), f_is_string(), f_is_list()
1497function f_is_bool(x) = f_1arg(function (x) is_bool(x));
1498
1499
1500// Function: f_is_num()
1501// Topics: Function Literals, Type Queries
1502// Usage:
1503//   fn = f_is_num();
1504// Description:
1505//   A factory that returns function literals equivalent to `is_num(a)`.
1506// Arguments:
1507//   a = If given, replaces the argument.
1508// See Also: f_is_undef(), f_is_bool(), f_is_string(), f_is_list()
1509function f_is_num(x) = f_1arg(function (x) is_num(x));
1510
1511
1512// Function: f_is_int()
1513// Topics: Function Literals, Type Queries
1514// Usage:
1515//   fn = f_is_int();
1516// Description:
1517//   A factory that returns function literals equivalent to `is_int(a)`.
1518// Arguments:
1519//   a = If given, replaces the argument.
1520// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_string(), f_is_list()
1521function f_is_int(x) = f_1arg(function (x) is_int(x));
1522
1523
1524// Function: f_is_nan()
1525// Topics: Function Literals, Type Queries
1526// Usage:
1527//   fn = f_is_nan();
1528// Description:
1529//   A factory that returns function literals equivalent to `is_nan(a)`.
1530// Arguments:
1531//   a = If given, replaces the argument.
1532// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_int(), f_is_string(), f_is_list()
1533function f_is_nan(x) = f_1arg(function (x) is_nan(x));
1534
1535
1536// Function: f_is_finite()
1537// Topics: Function Literals, Type Queries
1538// Usage:
1539//   fn = f_is_finite();
1540// Description:
1541//   A factory that returns function literals equivalent to `is_finite(a)`.
1542// Arguments:
1543//   a = If given, replaces the argument.
1544// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_int(), f_is_string(), f_is_list()
1545function f_is_finite(x) = f_1arg(function (x) is_finite(x));
1546
1547
1548// Function: f_is_string()
1549// Topics: Function Literals, Type Queries
1550// Usage:
1551//   fn = f_is_string();
1552// Description:
1553//   A factory that returns function literals equivalent to `is_string(a)`.
1554// Arguments:
1555//   a = If given, replaces the argument.
1556// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_int(), f_is_list()
1557function f_is_string(x) = f_1arg(function (x) is_string(x));
1558
1559
1560// Function: f_is_list()
1561// Topics: Function Literals, Type Queries
1562// Usage:
1563//   fn = f_is_list();
1564// Description:
1565//   A factory that returns function literals equivalent to `is_list(a)`.
1566// Arguments:
1567//   a = If given, replaces the argument.
1568// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_int(), f_is_string(), f_is_list()
1569function f_is_list(x) = f_1arg(function (x) is_list(x));
1570
1571
1572// Function: f_is_range()
1573// Topics: Function Literals, Type Queries
1574// Usage:
1575//   fn = f_is_range();
1576// Description:
1577//   A factory that returns function literals equivalent to `is_range(a)`.
1578// Arguments:
1579//   a = If given, replaces the argument.
1580// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_int(), f_is_string(), f_is_list()
1581function f_is_range(x) = f_1arg(function (x) is_range(x));
1582
1583
1584// Function: f_is_function()
1585// Topics: Function Literals, Type Queries
1586// Usage:
1587//   fn = f_is_function();
1588// Description:
1589//   A factory that returns function literals equivalent to `is_function(a)`.
1590// Arguments:
1591//   a = If given, replaces the argument.
1592// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_int(), f_is_string(), f_is_list()
1593function f_is_function(x) = f_1arg(function (x) is_function(x));
1594
1595
1596// Function: f_is_vector()
1597// Topics: Function Literals, Type Queries
1598// Usage:
1599//   fn = f_is_vector();
1600// Description:
1601//   A factory that returns function literals equivalent to `is_vector(a)`.
1602// Arguments:
1603//   a = If given, replaces the argument.
1604// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_int(), f_is_string(), f_is_list()
1605function f_is_vector(a,b) = f_2arg(function (a,b) is_vector(a,b));
1606
1607
1608// Function: f_is_path()
1609// Topics: Function Literals, Type Queries
1610// Usage:
1611//   fn = f_is_path();
1612// Description:
1613//   A factory that returns function literals equivalent to `is_path(a)`.
1614// Arguments:
1615//   a = If given, replaces the argument.
1616// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_int(), f_is_string(), f_is_list()
1617function f_is_path(a,b) = f_2arg(function (a,b) is_path(a,b));
1618
1619
1620// Function: f_is_region()
1621// Topics: Function Literals, Type Queries
1622// Usage:
1623//   fn = f_is_region();
1624// Description:
1625//   A factory that returns function literals equivalent to `is_region(a)`.
1626// Arguments:
1627//   a = If given, replaces the argument.
1628// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_int(), f_is_string(), f_is_list()
1629function f_is_region(a) = f_1arg(function (a) is_region(a));
1630
1631
1632// Function: f_is_vnf()
1633// Topics: Function Literals, Type Queries
1634// Usage:
1635//   fn = f_is_vnf();
1636// Description:
1637//   A factory that returns function literals equivalent to `is_vnf(a)`.
1638// Arguments:
1639//   a = If given, replaces the argument.
1640// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_int(), f_is_string(), f_is_list()
1641function f_is_vnf(a) = f_1arg(function (a) is_vnf(a));
1642
1643
1644// Function: f_is_patch()
1645// Topics: Function Literals, Type Queries
1646// Usage:
1647//   fn = f_is_patch();
1648// Description:
1649//   A factory that returns function literals equivalent to `is_patch(a)`.
1650// Arguments:
1651//   a = If given, replaces the argument.
1652// See Also: f_is_undef(), f_is_bool(), f_is_num(), f_is_int(), f_is_string(), f_is_list()
1653function f_is_patch(a) = f_1arg(function (a) is_patch(a));
1654
1655
1656
1657// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap