Skip to content

Instantly share code, notes, and snippets.

@christianvdstap
Created November 19, 2024 16:31
Show Gist options
  • Save christianvdstap/ad2b4f1e7c122253c566fe410b4ff6d4 to your computer and use it in GitHub Desktop.
Save christianvdstap/ad2b4f1e7c122253c566fe410b4ff6d4 to your computer and use it in GitHub Desktop.
def = function(self) function(fName)
let(_definition = [
// Static
op("new", function()
def([])
),
// Access
op("get", function(name) function(default = undef)
safeGet(self, name, default)
),
op("toString", function()
doPrint() (self)
),
// Mutation
op("set", function(name)
def(set(self, name))
),
op("add", function(a)
let(b = safeGet(self, "sum", 0),
) def(set(self, field("sum", a+b)))
),
op("test", function(a, b)
let(s = safeGet(self, "sum", 0),
) def(set(self, field("sum", s+a+b)))
),
],
) get(_definition, fName)
;
function call(fName) =
let(unset=function() undef
) function(a=unset, b=unset, c=unset, d=unset, e=unset, f=unset)
let(params = filter([a, b, c, d, e, f], function(p) p != unset),
map = [
function(f) f(fName) (),
function(f) f(fName) (params[0]),
function(f) f(fName) (params[0], params[1]),
function(f) f(fName) (params[0], params[1], params[2]),
function(f) f(fName) (params[0], params[1], params[2], params[3]),
function(f) f(fName) (params[0], params[1], params[2], params[3], params[4]),
function(f) f(fName) (params[0], params[1], params[2], params[3], params[4], params[5]),
],
) map[len(params)]
;
echo(
compose([
call ("toString") (),
function(f) foldl([1,2,3], function(a, g) g ("test") (1, a), f),
call("test") (1, 1),
call("add") (1),
call("add") (1),
call("new") ()
]) (def())
);
@christianvdstap
Copy link
Author

christianvdstap commented Nov 19, 2024

Replacement of the inline foldl

function flatCall(fName) = 
  let(unset=function() undef
  ) function(a=unset, b=unset, c=unset, d=unset, e=unset)
      let(params = filter([a, b, c, d, e], function(p) p != unset),
          map = [
            function(a) call(fName) (a),
            function(a) call(fName) (params[0], a),
            function(a) call(fName) (params[0], params[1], a),
            function(a) call(fName) (params[0], params[1], params[2], a),
            function(a) call(fName) (params[0], params[1], params[2], params[3], a),
            function(a) call(fName) (params[0], params[1], params[2], params[3], params[4], a),
          ],
      ) function(v) function(f) foldl(v, function(a, g) map[len(params)](a)(g), f)
;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment