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

(docs) Adding an example of a closure that contains mutable state in Mutable State & Imperative Tutorial #2046

Merged
merged 5 commits into from Feb 16, 2024

Conversation

The-Amoghavarsha
Copy link
Contributor

Closes #2035

Copy link
Collaborator

@cuihtlauac cuihtlauac left a 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.

Comment on lines 534 to 536
let counter () =
let n = ref 0 in
fun () -> incr n; !n;;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### 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.
Copy link
Collaborator

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

@sabine sabine changed the title Adding an example of closure Adding an example of a closure that contains mutable state Feb 12, 2024
@cuihtlauac
Copy link
Collaborator

Also, checks are failing because the formatting was broken upstream. Please rebase.

Copy link
Collaborator

@cuihtlauac cuihtlauac left a 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?

Copy link
Collaborator

@christinerose christinerose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!

@sabine sabine changed the title Adding an example of a closure that contains mutable state (docs) Adding an example of a closure that contains mutable state in Mutable State & Imperative Tutorial Feb 16, 2024
@sabine sabine merged commit 0618a8f into ocaml:main Feb 16, 2024
3 checks passed
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.

Add example on closure and references
4 participants