From 7e0e38bd155fab76fa3b6776f9184611d044903d Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Sun, 18 Jul 2021 17:53:37 -0400 Subject: Finish lexical scope documentation --- docs/doc/embed.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/doc/embed.html') diff --git a/docs/doc/embed.html b/docs/doc/embed.html index de7709ca..4b4e0365 100644 --- a/docs/doc/embed.html +++ b/docs/doc/embed.html @@ -11,7 +11,7 @@

Probably you can figure out the easy things like calling bqn("×´1+↕6") to compute six factorial. But how do you get JS and BQN to talk to each other, for example to compute the factorial of a number n? Constructing a source string with bqn("×´1+↕"+n) isn't the best way—in fact I would recommend you never use this strategy.

Instead, return a function from BQN and call it: bqn("{×´1+↕𝕩}")(n). This strategy also has the advantage that you can store the function, so that it will only be compiled once. Define let fact = bqn("{×´1+↕𝕩}"); at the top of your program and use it as a function elsewhere.

BQN can also call JS functions, to use functionality that isn't native to BQN or interact with a program written in JS. For example, bqn("{𝕏'a'+↕26}")(alert) calls the argument alert from within BQN. The displayed output isn't quite right here, because a BQN string is stored as a JS array, not a string. See the next section for more information.

-

Cool, but none of these examples really use closures, just self-contained functions. Closures are functions that use outside state, which is maintained over the course of the program. Here's an example program that defines i and then returns a function that manipulates i and returns its new value.

+

Cool, but none of these examples really use closures, just self-contained functions. Closures are functions that use outside state, which is maintained over the course of the program. Here's an example program that defines i and then returns a function that manipulates i and returns its new value.

let push = bqn(`
     i40
     {i+𝕩»i}
-- 
cgit v1.2.3