-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Coordinate Transformation learning exercise (#587)
* remove space * change quote to backtick * remove space in code block * remove translate2d function from instructions * convert JavaScript calls to Clojure syntax * put code output on new line * provide additional line break to accomodate narrow width * link to function closures doc in general hints * update github user name * add closures and atoms concepts * add atoms to coordinate transformation concepts array * add atoms and closures concepts to config.json
- Loading branch information
1 parent
b1ca98c
commit d5bdff9
Showing
13 changed files
with
190 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"blurb": "Atoms in Clojure are a reference type for managing shared state.", | ||
"authors": [ | ||
"bobbicodes" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# About | ||
|
||
Since all of Clojure's standard data types are immutable, it offers *reference types* which offer controlled mutation. The simplest and most commonly used of these are called Atoms. | ||
|
||
Atoms are created with the `atom` function, which take an initial value: | ||
|
||
```clojure | ||
(def players (atom ())) | ||
``` | ||
|
||
The current value of the atom is dereferenced with either `deref` or the `@` shorthand: | ||
|
||
```clojure | ||
@players | ||
;;=> () | ||
``` | ||
|
||
The current value of the `players` atom can be modified using `swap!`, which updates the value by applying a function: | ||
|
||
```clojure | ||
(swap! players conj :player1) | ||
;;=> (:player1) | ||
``` | ||
|
||
The `reset!` function replaces the value of an atom without regard for its current value: | ||
|
||
```clojure | ||
(reset! players ()) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Introduction | ||
|
||
Since all of Clojure's standard data types are immutable, it offers *reference types* which offer controlled mutation. The simplest and most commonly used of these are called Atoms. | ||
|
||
Atoms are created with the `atom` function, which take an initial value: | ||
|
||
```clojure | ||
(def players (atom ())) | ||
``` | ||
|
||
The current value of the atom is dereferenced with either `deref` or the `@` shorthand: | ||
|
||
```clojure | ||
@players | ||
;;=> () | ||
``` | ||
|
||
The current value of the `players` atom can be modified using `swap!`, which updates the value by applying a function: | ||
|
||
```clojure | ||
(swap! players conj :player1) | ||
;;=> (:player1) | ||
``` | ||
|
||
The `reset!` function replaces the value of an atom without regard for its current value: | ||
|
||
```clojure | ||
(reset! players ()) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"url": "https://clojure.org/reference/atoms", | ||
"description": "Clojure reference: Atoms" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"blurb": "Clojure transparently supports closures, that means variables from an outer scope can also be used inside of a function.", | ||
"authors": ["bobbicodes"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# About | ||
|
||
**Closures** are a programming pattern [in Clojure][clojure-guide-closures] which allows variables from an outer [lexical scope][wiki-lexical-scope] to be used inside of a function. Clojure supports closures transparently, and they are often used without knowing what they are. | ||
|
||
```clojure | ||
;; Top-level definitions are global-scope | ||
(def dozen 12) | ||
|
||
;; Functions create a new scope. | ||
;; Referencing the outer variable here is a closure. | ||
(fn [n] (* dozen n)) | ||
``` | ||
|
||
## Closures to save state and pass along values | ||
|
||
Using an atom allows for some state to be preserved: | ||
|
||
```clojure | ||
;; This function closure increments the counter's state | ||
;; in the outer lexical context. | ||
;; This way the counter can be shared between many calling contexts. | ||
|
||
(def increment | ||
(let [counter (atom 0)] | ||
(fn [] (swap! counter inc)))) | ||
``` | ||
|
||
Each successive call to `increment` increments its counter: | ||
|
||
``` clojure | ||
(increment) | ||
;;=> 1 | ||
(increment) | ||
;;=> 2 | ||
``` | ||
|
||
[wiki-lexical-scope]: https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scoping | ||
[clojure-guide-closures]: https://clojure.org/guides/higher_order_functions#_functions_returning_functions_and_closures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Introduction | ||
|
||
**Closures** are a programming pattern [in Clojure][clojure-guide-closures] which allows variables from an outer [lexical scope][wiki-lexical-scope] to be used inside of a function. Clojure supports closures transparently, and they are often used without knowing what they are. | ||
|
||
```clojure | ||
;; Top-level definitions are global-scope | ||
(def dozen 12) | ||
|
||
;; Functions create a new scope. | ||
;; Referencing the outer variable here is a closure. | ||
(fn [n] (* dozen n)) | ||
``` | ||
|
||
## Closures to save state and pass along values | ||
|
||
Using an atom allows for some state to be preserved: | ||
|
||
```clojure | ||
;; This function closure increments the counter's state | ||
;; in the outer lexical context. | ||
;; This way the counter can be shared between many calling contexts. | ||
|
||
(def increment | ||
(let [counter (atom 0)] | ||
(fn [] (swap! counter inc)))) | ||
``` | ||
|
||
Each successive call to `increment` increments its counter: | ||
|
||
``` clojure | ||
(increment) | ||
;;=> 1 | ||
(increment) | ||
;;=> 2 | ||
``` | ||
|
||
[wiki-lexical-scope]: https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scoping | ||
[clojure-guide-closures]: https://clojure.org/guides/higher_order_functions#_functions_returning_functions_and_closures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"url": "https://clojure.org/guides/higher_order_functions#_functions_returning_functions_and_closures", | ||
"description": "Clojure guide: Functions returning functions and closures" | ||
}, | ||
{ | ||
"url": "https://en.wikipedia.org/wiki/Closure_(computer_programming)", | ||
"description": "Wikipedia: Closure" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters