tabloid
tabloid copied to clipboard
BREAKING: proper support for closures
I was SHOCKED to notice that inner functions do not behave properly (they can't see bindings from the environment where they are defined, since they are all executed in one master environment). This PR resolves that (a bunch of methods that I added to the Environment class are taken from my PL tutorial).
This also adds a BREAKING
keyword (I didn't have a better idea) that one can use to define anonymous functions. With this additions I wrote the following code, which solves my old little JS quiz.
DISCOVER HOW TO cons WITH a, b
SHOCKING DEVELOPMENT BREAKING WITH f
SHOCKING DEVELOPMENT f OF a, b
DISCOVER HOW TO car WITH pair
SHOCKING DEVELOPMENT pair OF BREAKING WITH a, b
SHOCKING DEVELOPMENT a
DISCOVER HOW TO cdr WITH pair
SHOCKING DEVELOPMENT pair OF BREAKING WITH a, b
SHOCKING DEVELOPMENT b
DISCOVER HOW TO nil WITH f
SHOCKING DEVELOPMENT f OF nil, nil
DISCOVER HOW TO map WITH list, f
SHOCKING DEVELOPMENT
WHAT IF list IS ACTUALLY nil
nil
LIES! (cons OF (f OF (car OF list)),
(map OF (cdr OF list), f))
DISCOVER HOW TO print WITH x
YOU WON'T WANT TO MISS x
EXPERTS CLAIM foreach TO BE map
DISCOVER HOW TO range WITH start, stop
SHOCKING DEVELOPMENT
WHAT IF start BEATS stop
nil
LIES!
cons OF start, (range OF start PLUS 1, stop)
DISCOVER HOW TO reverse WITH list, ret
SHOCKING DEVELOPMENT
WHAT IF list IS ACTUALLY nil
ret
LIES!
reverse OF (cdr OF list),
(cons OF (car OF list), ret)
EXPERTS CLAIM numbers TO BE range OF 1, 10
EXPERTS CLAIM squares TO BE map OF numbers,
BREAKING WITH n
SHOCKING DEVELOPMENT n TIMES n
foreach OF (reverse OF squares, nil), print
PLEASE LIKE AND SUBSCRIBE
The latest updates on your projects. Learn more about Vercel for Git ↗︎
Name | Status | Preview | Comments | Updated (UTC) |
---|---|---|---|---|
tabloid | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | Apr 5, 2023 1:19pm |
BREAKING
keyword (I didn't have a better idea)
ONE WEIRD TRICK
could do the trick - resonates with the "throwaway" nature of anon functions in my EXPERT OPINION.
ONE WEIRD TRICK
could do the trick - resonates with the "throwaway" nature of anon functions in my EXPERT OPINION.
It does sound better followed by the WITH
keyword, which declares arguments (and an anonymous function will probably always take arguments), so I like it on this account.
Now, anonymous functions aren't exactly “throwaway” — on my example they actually create storage! If a language does not support arrays, objects, or any other kind of compound data structures, you can still get all that if you have closures. I've always found this fascinating...
You actually don't need anonymous functions. You can define code the same as in Python where functions need to have more than one line, you create a nested named function and return it.
DISCOVER HOW TO outer WITH n
RUMOR HAS IT
DISCOVER HOW TO inner WITH _
SHOCKING DEVELOPMENT n
SHOCKING DEVELOPMENT inner
END OF STORY
EXPERTS CLAIM fn TO BE outer OF 10
EXPERTS CLAIM result TO BE fn OF 0
YOU WON'T WANT TO MISS result
PLEASE LIKE AND SUBSCRIBE
The real problem is that it seems you can define a function without arguments and call a function without arguments.
You can define code the same as in Python where functions need to have more than one line, you create a nested named function and return it.
Yeah, I noticed that this works, but it's a little cumbersome.
The real problem is that it seems you can define a function without arguments and call a function without arguments.
Indeed, there seems to be no syntax for it.