Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING: proper support for closures #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mishoo
Copy link

@mishoo mishoo commented Apr 5, 2023

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 environment must be saved whenever a closure is created, and used
when the closure is invoked. Also adds BREAKING keyword which can define
anonymous functions.
@vercel
Copy link

vercel bot commented Apr 5, 2023

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

@Lumzdas
Copy link

Lumzdas commented Apr 7, 2023

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.

@mishoo
Copy link
Author

mishoo commented Apr 7, 2023

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...

@jcubic
Copy link

jcubic commented Apr 11, 2023

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.

@mishoo
Copy link
Author

mishoo commented Apr 12, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants