Finish up episode 6

This commit is contained in:
Sameer Rahmani 2022-05-18 20:19:18 +01:00
parent ca50c4709a
commit 701d3d912c
1 changed files with 104 additions and 1 deletions

View File

@ -177,7 +177,8 @@ CLOSED: [2022-04-09 Sat 11:24]
- boundp
- More at https://www.gnu.org/software/emacs/manual/html_node/elisp/Type-Predicates.html
** type-of
* Episode 5 - Lists
* DONE Episode 5 - Lists
CLOSED: [2022-05-18 Wed 12:40]
** What is a list?
- Not a primitive type
- A linked list made out of cons cells
@ -310,3 +311,105 @@ digraph {
#+END_SRC
* Episode 6 - Property & Association Lists
** I have an idea
- Develop *FG42* on stream
- I'll announce the date and time on:
+ Twitter: @lxsameer
+ Youtube Community (maybe)
** eq vs equal
- ~eq~ checks whether two objects are the same
- ~equal~ checks whether two objects have similar structure and content.
** Property List (plist)
- Is a list of paired elements
- Each of the pairs associates a property name with a property or value
- The order of the property names (keys) is not significant
- Keys must be unique
- Emacs uses plists to store text properties and symbol properties
*** Examples
#+BEGIN_SRC emacs-lisp
(setq x '(:a 1 "b" 2 c 3 :d ("foo" "bar") :j nil))
x
#+END_SRC
*** Functions
- ~plist-get~ or ~lax-plist-get~
#+BEGIN_SRC emacs-lisp
(plist-get x :a)
(plist-get x "b")
(lax-plist-get x "b")
#+END_SRC
- ~plist-put~ or ~lax-plist-put~
#+BEGIN_SRC emacs-lisp
(plist-put x :z 325)
(plist-put x :z 10)
(lax-plist-put x "b" 20)
#+END_SRC
- ~plist-member~
#+BEGIN_SRC emacs-lisp
(plist-get x :j)
(plist-get x :g)
(plist-member x :j)
(plist-member x :g)
#+END_SRC
** An example use case of plist
#+BEGIN_SRC emacs-lisp
(defun foo (&rest args)
(message "ARGS: %s" args)
(plist-get args :name))
(foo :age 20 :name 'bob "blah" '(12 34 4))
#+END_SRC
** Association List (alist)
- It is a list of cons cells called associations
- Mapping some keys to some values
- The ~CAR~ of each cons cell is the key, and the ~CDR~ is the associated value
- The order of associations matters
*** Examples
#+BEGIN_SRC emacs-lisp
(setq y '((:a . 1) ("b" . 2) (c . 3) (:d "foo" "bar")))
y
#+END_SRC
*** Functions
- ~assoc~
#+BEGIN_SRC emacs-lisp
(assoc :a y)
(assoc "b" y)
#+END_SRC
- ~rassoc~
#+BEGIN_SRC emacs-lisp
(rassoc 1 y)
(rassoc 3 y)
#+END_SRC
- ~assq~
#+BEGIN_SRC emacs-lisp
(assq :a y)
(assq "b" y)
#+END_SRC
- ~alist-get~
#+BEGIN_SRC emacs-lisp
(alist-get :a y)
(alist-get :z y "blah")
#+END_SRC
- ~cons~
#+BEGIN_SRC emacs-lisp
(setq y1 (cons '(:z . 31) y))
(assoc :z y1)
#+END_SRC
** References
- https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html
- https://www.gnu.org/software/emacs/manual/html_node/elisp/Plist-Access.html
- https://www.gnu.org/software/emacs/manual/html_node/elisp/Plists-and-Alists.html