-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding associative destructuring, destructuring in clojure and collec…
…tion functions
- Loading branch information
1 parent
41c88d1
commit 071998e
Showing
12 changed files
with
151 additions
and
81 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,70 @@ | ||
# Collection Functions | ||
#clojure #language #programming #functions | ||
|
||
|
||
The main function of **Collection Abstraction** is `into`, that catch elements from one collection (second) and add to | ||
another one (first): | ||
```clojure | ||
(map identity {:sunlight-reaction "Glitter!"}) | ||
; => ([:sunlight-reaction "Glitter!"]) | ||
|
||
(into {} (map identity {:sunlight-reaction "Glitter!"})) | ||
; => {:sunlight-reaction "Glitter!"} | ||
``` | ||
## Conj | ||
|
||
It does the same as `into` but with `scalar`. It is common two functions do the same in `Clojure`, however one with `scalar` and | ||
another one with `collections`. | ||
```clojure | ||
(conj [0] 1) | ||
; => [0 1] | ||
``` | ||
|
||
## Apply | ||
|
||
Explode the elements of a sequence and apply the function on it | ||
```clojure | ||
(apply max[0 1 2]) | ||
; => 2 | ||
``` | ||
|
||
## Partial | ||
|
||
Return a new function with part of it filled with values | ||
```clojure | ||
(def add 10 (partial +10)) | ||
(add10 3) | ||
; => 13 | ||
``` | ||
|
||
```Clojure | ||
(defn my-partial | ||
[partialized-fn & args] | ||
(fn [& more-args] | ||
(apply partialized-fn (into args more-args)))) | ||
|
||
(def add20 (my-partial + 20)) | ||
(add20 3) | ||
; => 23 | ||
``` | ||
The example above, the value of *add20* is the anonymous function returned by *my-partial*. | ||
|
||
## Complement | ||
|
||
```clojure | ||
;; Define a predicate function that checks if a number is even | ||
(defn even? [n] | ||
(zero? (mod n 2))) | ||
|
||
;; Use complement to create a predicate that checks if a number is not even (i.e., odd) | ||
(def odd? (complement even?)) | ||
|
||
;; Test the complement function | ||
; =>(println (even? 4)) ;; Output: true | ||
; =>(println (odd? 4)) ;; Output: false | ||
; =>(println (even? 5)) ;; Output: false | ||
; =>(println (odd? 5)) ;; Output: true | ||
``` | ||
|
||
### References | ||
- Higginbotham, 2015, p88-93 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Python Dictionary | ||
#python #datastructure #language #programming | ||
|
||
- [[x4fp-dictcomp]]# | ||
- Dictionaries Comprehesion: [[x4fp-dictcomp]]# | ||
- Unpacking dictionaries: [[a1mq-unpacking-sequences-and-iterables]] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Associative Destructuring | ||
#clojure #programming #datastructure #language | ||
|
||
- Associative destructuring is for map or other #[[worn-collection-abstraction]] like sets. | ||
- They have the commitment to associate the pair `key-value`. | ||
|
||
```clojure | ||
(def client {:name "Super Co." | ||
:location "Philadelphia" | ||
:description "The worldwide leader in plastic tableware."}) | ||
|
||
(let [name (:name client) | ||
location (:location client) | ||
description (:description client)] | ||
(println name location "-" description)) | ||
;= Super Co. Philadelphia - The worldwide leader in plastic tableware. | ||
``` | ||
|
||
### References | ||
- https://clojure.org/guides/destructuring |
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,44 @@ | ||
# Destructuring in Clojure | ||
#clojure #language #programming #datastructure | ||
|
||
- You can be more concise and readable code. For example: | ||
```clojure | ||
(def my-line [[5 10] [10 20]]) | ||
(let [[p1 p2] my-line | ||
[x1 y1] p1 | ||
[x2 y2] p2] | ||
(println "Line from (" x1 "," y1 ") to (" x2 ", " y2 ")")) | ||
;= "Line from ( 5 , 10 ) to ( 10 , 20 )" | ||
``` | ||
- Large list is a list with 10 elements however, we only get the first three: | ||
```clojure | ||
(def large-list '(1 2 3 4 5 6 7 8 9 10)) | ||
(let [[a b c] large-list] | ||
(println a b c)) | ||
;= 1 2 3 | ||
``` | ||
- What happens with the remaining ones? If we want to capture them: | ||
```clojure | ||
(def large-list '(1 2 3 4 5 6 7 8 9 10)) | ||
(let [[a b c & remaining] large-list] | ||
(println a b c)) | ||
(apply println remaining) | ||
;= 1 2 3 | ||
;= (4 5 6 7 8 9 10) | ||
``` | ||
- There is [[k89b-associative-destructuring]]# for maps. | ||
- Usually destructuring is passed used for arguments in a function: | ||
```clojure | ||
(defn print-coordinates-1 [point] | ||
(let [x (first point) | ||
y (second point) | ||
z (last point)] | ||
(println "x:" x ", y:" y ", z:" z))) | ||
; or | ||
(defn print-coordinates-2 [point] | ||
(let [[x y z] point] | ||
(println "x:" x ", y:" y ", z:" z))) | ||
``` | ||
|
||
### References | ||
- https://clojure.org/guides/destructuring |
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