Skip to content

Commit

Permalink
Adding associative destructuring, destructuring in clojure and collec…
Browse files Browse the repository at this point in the history
…tion functions
  • Loading branch information
FelipeMarcelino committed Jun 27, 2024
1 parent 41c88d1 commit 071998e
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 81 deletions.
70 changes: 70 additions & 0 deletions 2fi4-collection-functions.md
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
4 changes: 2 additions & 2 deletions 38lz-zettelkasten.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Zettelkasten is a tool to create and writing personal thoughts on notes and conn
- Use napkins, papers, voice recording etc. to record your [[ave9-fleeting-notes]]#. It is good idea always have a pen
and scratchpad with you.
- Zotero is a good tool to be the reference management system.
- Use [[8imv-slip-box]]# to store the [[wz0b-permanent-notes]]#
- Use [[8imv-slip-box]]# to store the [[wz0b-permanent-notes]]
- [[ndec-literature-notes]]# need to have at least author and year. But can have page as well.
- There is a method on [[7t2m-how-to-take-smart-notes]]
- [[5quz-why-do-we-take-notes]]# is because we want to learn [[7h4q-how-to-learn-and-understand]]

### References

Expand Down
3 changes: 2 additions & 1 deletion 5quz-why-do-we-take-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
- Take notes make the writing easy, however the feedback of a good/bad note is not immediate. Although we have the
[[3j89-feedback-loop]] showing if your notes are good or not.
- Rely on the system you are used to writing you notes. Focusing on the ideas
- There is a correct way to write [[zkr2-question-about-a-topic]].
- There is a correct way to write [[zkr2-question-about-a-topic]]
- We take notes to learn things [[7h4q-how-to-learn-and-understand]]#
- There is a special way to take notes [[7t2m-how-to-take-smart-notes]]#
2 changes: 1 addition & 1 deletion 7h4q-how-to-learn-and-understand.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# How to learn and understand
#notes #writing #learn #system #organization #method

- Familiarity is as same of understand.
- Familiarity is not the same of understand.
- If we did not understand we cannot explain something with our words
- Try to search for patterns, detect the distinction and similarities between the concepts and questions
- Reformulate questions and piece of information is more important than to have extensively knowledge base.
Expand Down
11 changes: 5 additions & 6 deletions 7t2m-how-to-take-smart-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
- **Project Notes** are notes regarding a specific project.
- Collect notes from multiple sources for multiple projects and does not being stuck on only one project because the
connection between notes can be limited.
- Take care of transforming everything into [[ave9-fleeting-notes]] and in that way be induced to restart everything
- Take care of transforming everything into [[ave9-fleeting-notes]]# and in that way be induced to restart everything
again.
- [[wz0b-permanent-notes]] are sources of knowledge because they all have enough context to be understandable.
- Try to process [[ave9-fleeting-notes]] and [[ndec-literature-notes]] as fast as possible to because you can lose its
context before passing it to [[wz0b-permanent-notes]].
- Only [[wz0b-permanent-notes]] are put in the [[8imv-slip-box]].
- [[wz0b-permanent-notes]]# are sources of knowledge because they all have enough context to be understandable.
- Try to process [[ave9-fleeting-notes]]# and [[ndec-literature-notes]]# as fast as possible to because you can lose its
context before passing it to [[wz0b-permanent-notes]]#.
- Only [[wz0b-permanent-notes]]# are put in the [[8imv-slip-box]].
- The notes you are taking will give you a [[3j89-feedback-loop]] about how good they are and how good you are exploring a specific
topic. If you are writing something that you did not understand correctly the notes will show that you not concise
about a specific topic.
- When writing notes don't be a critic, do this after the notes is already done or during the proofread.
- [[5quz-why-do-we-take-notes]]#
- Handwriting notes is better to learning because they need to be more assertive because we write slow
[[mjpl-handwriting-with-our-words]]#.

Expand Down
3 changes: 2 additions & 1 deletion 9n4u-python-dictionary.md
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]]
2 changes: 1 addition & 1 deletion a1mq-unpacking-sequences-and-iterables.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Unpacking Sequences and Iterables
#python #programming #datastrcture #language

- Works with any iterable: [[aw9b-python-list]], [[x4fp-dictcomp]], [[82os-python-tuples]]
- Works with any iterable: #[[aw9b-python-list]], #[[9n4u-python-dictionary]], #[[82os-python-tuples]]
- Parallel assignment

```python
Expand Down
1 change: 0 additions & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ This is my Zettelkasten, you will encounter things related to **programming**, *

## Note-Taking
- [[38lz-zettelkasten]]#
- [[7t2m-how-to-take-smart-notes]]#
- [[tn2j-gtd-getting-things-done]]#

## Linux
Expand Down
20 changes: 20 additions & 0 deletions k89b-associative-destructuring.md
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
44 changes: 44 additions & 0 deletions lia6-destructuring-in-clojure.md
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
2 changes: 2 additions & 0 deletions vdzq-sequence-abstraction.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ Clojure deals with Sequence using `Indirection` or in other words `Polymorphism`
- List of Vectors (**into** - it is a function) → Map

Seq operations return sequence as well.

- [[lia6-destructuring-in-clojure]]
70 changes: 2 additions & 68 deletions worn-collection-abstraction.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,8 @@

Collection abstraction in Clojure are collections regarding data structures like sets for example or dictionaries. They
take of care of the whole, or all elements, which is different of [[vdzq-sequence-abstraction]]. Example of functions: `count`, `empty?`, `every?`.

## Into

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
```


- There are multiple [[2fi4-collection-functions]]#, but the most important are `into`, `conj`, `apply` and `complement`,
- How to destructure maps [[k89b-associative-destructuring]]#

### References
- Higginbotham, 2015, p88-93

0 comments on commit 071998e

Please sign in to comment.