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
(docs) Adding an example of a closure that contains mutable state in Mutable State & Imperative Tutorial #2046
(docs) Adding an example of a closure that contains mutable state in Mutable State & Imperative Tutorial #2046
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @The-Amoghavarsha. We need more details, this example is more profound that it may looks.
let counter () = | ||
let n = ref 0 in | ||
fun () -> incr n; !n;; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let counter () = | |
let n = ref 0 in | |
fun () -> incr n; !n;; | |
# let create_counter () = | |
let n = ref 0 in | |
fun () -> incr n; !n;; | |
val create_counter : unit -> unit -> int = <fun> | |
# let c1 = create_counter ();; | |
val c1 : unit -> int = <fun> | |
# let c2 = create_counter ();; | |
val c2 : unit -> int = <fun> | |
# c1 ();; | |
- : int = 1 | |
# c1 ();; | |
- : int = 2 | |
# c2 ();; | |
- : int = 1 | |
# c1 ();; | |
- : int = 3 |
@@ -526,6 +526,18 @@ The following example uses the `get_char` function we defined earlier (in the se | |||
|
|||
This `while` loop echoes characters typed on the keyboard. When the ASCII `Escape` character is read, the `Exit` exception is thrown, which terminates the iteration and displays the REPL reply: `- : unit = ()`. | |||
|
|||
### Closures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### Closures | |
### References Inside Closures |
@@ -526,6 +526,18 @@ The following example uses the `get_char` function we defined earlier (in the se | |||
|
|||
This `while` loop echoes characters typed on the keyboard. When the ASCII `Escape` character is read, the `Exit` exception is thrown, which terminates the iteration and displays the REPL reply: `- : unit = ()`. | |||
|
|||
### Closures | |||
|
|||
A closure is a function bundled together with its surrounding state, including any variables or references from the enclosing scope that it references. This bundled function-state package allows for the creation of self-contained units of behavior that can maintain internal state across multiple invocations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refer to the definition of closures from Values & Functios.
This example is meant to illustrate it is possible to “hide” a reference in a closure returned by a function. Which is a neat way to produce an "effectful function" (maybe we should say somewhere such a thing is not a all function, in the math sense).
Also, checks are failing because the formatting was broken upstream. Please rebase. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me.
@christinerose, is this ok for you too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!!
Closes #2035