SHARE
TWEET

Untitled

a guest Jan 20th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Balancing speed and concision, with a preference for speed. Can we do better?
  2. // No ES6! Mesh is ES5-compatible.
  3.  
  4. // Added to global namespace by calling the following:
  5. // g._defFunctions = function()  {for (let k in _FUNCTIONS) g[k] = _FUNCTIONS[k]}
  6.  
  7. // Vanilla
  8. const _FUNCTIONS = {
  9.     SUM: function() {
  10.         const l=arguments.length,f=function(x,y){return x+y};
  11.         for(var i=0,s=0;i<l;i++){
  12.             let x=arguments[i];s+=x instanceof Array?x.reduce(f,0):x
  13.         };
  14.         return s
  15.     }
  16. }
  17.  
  18. // Alternative - shorten sum fn using 'F' shorthand (could be used for others)
  19. const _FUNCTIONS = {
  20.     F: function(_){return Function('α','β','γ','δ','"use strict";return '+_)},
  21.     SUM: function() {
  22.         const l=arguments.length,f=F("α+β");
  23.         for(var i=0,s=0;i<l;i++){let x=arguments[i];s+=x instanceof Array?x.reduce(f,0):x};
  24.         return s
  25.     }
  26. }
  27.  
  28. // Alternative 2: double shorthands via A, + reducing syntax length via get (also helps with closures)
  29. // ...trying to channel Arthur Whitney here
  30. const _FUNCTIONS = {
  31.     F: function(_){return Function('α','β','γ','δ','"use strict";return '+_)},
  32.     get A() {return F("Array.prototype.slice.call(α)")},
  33.     get SUM() {const f=F("α+β"); return function() { // surely the two uses of reduce can be avoided here
  34.         return A(arguments).reduce(function(a,b){return a+(b instanceof Array?b.reduce(f,0):b)},0);
  35.     }}
  36. }
  37.  
  38. // Of course, could always put them in the global namespace, but it may be harder for Mesh to manage insertion/deletion
  39. function F(_){return Function('α','β','γ','δ','"use strict";return '+_)};
  40. const A = F("Array.prototype.slice.call(α)");
  41. const SUM = (
  42.     function() {const f=F("α+β"); return function() { // surely the two uses of reduce can be avoided here
  43.         return A(arguments).reduce(function(a,b){return a+(b instanceof Array?b.reduce(f,0):b)},0);
  44.     }}
  45. )();
  46.  
  47. // Ideally this fn would drill into arrays with arbitrary nesting;
  48. // given we'll define lots of these builtin fns, it's probably worth abstracting out flattens, etc.
  49. // Maybe Greek letter variants take an array? Or we can define sigma in SUM? (feels wasteful)
  50. function _(f,x) {return x instanceof Array ? f(x) : x};
  51. function Σ(a)   {let s=0;for(i in a)s+=_(Σ,a[i]);return s};
  52. // maybe we can extract out 'recursive reduce to identity' from above (params: +, 0)
  53. function SUM()  {return Σ(arguments)};
  54.  
  55. // And in case "for in" is slower than a straight for loop:
  56. function _(f,x) {return x instanceof Array ? f(x) : x};
  57. function l(a)   {return a.length}; // define as array proto property instead?
  58. function Σ(a)   {for(var s=0,i=l(a);i--;)s+=_(Σ,a[i]);return s};
  59. function SUM()  {return Σ(arguments)};
  60.  
  61. // Feels like it may be worth defining a tiny APL or K and using that...
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top