Quick actions

cmd+k|ctrl+k

Navigation

Languages

closure

Snippet info

Language

Lua

Visibility

public

Author

danielmg17

Created

2020-03-03T21:00:26Z

Updated

2020-03-03T21:26:05Z

function counter()
  local a = 0
  local f = function()
    a = a+1
    return a
  end
  a = -1
  f()
  print(">",a)
  a = 10
  return f
end

tick = counter()
tock = counter()
print(tick())
print(tock())
print(tick())
print(tock())
print(tick())
print(tock())

--[[


idea for lisp:

for lambdas/functions closures:
-global env is passed on and immutable
-local env is created from copy of outer env (try to only copy parts that are needed, not entire env)

ideas:
-during the parsing stage, as sexpr tree is being parsed, check if variables are part of local scope (including arguments)
    or outer scope. If in outer scope, that var becomes captured (captures should be included in the ast stage) in an "upvalue" list.
    If in local scope, do nothing. If in global scope, again do nothing.
    If variable is not found anywhere, give error.

upvalue problem can be avoided if captured vars are simply copied, and mutation(side-effects) aren't allowed
    but that sorta makes it harder to use as a scripting/configuration language

#General
keywords:

exec, echo, def, defn, var

symbols:
(...) creates list (function call)
[...] creates literal list
{(k v)} creates map

operators:
+,-,*,/,^,%,==,!=,>,=>,<,<=, &&,||,!,
.\ or \ creates lambda: ex: ((\ [x y] (+ x y)) 1 2)


]]--
INFO