This is my first post in my project to learn Clojure in public. It has been more reading than writing code at this point and I mostly covered types so far. There are more types in Clojure than the ones I used in Scheme, where even lists were made out of pairs (and pairs (and pairs (and pairs))).
I'm learning Clojure in public as I mentioned in my initial post with Athen's ClojureFam. I am part of the 4th cohort and our team is called Seneca. Each cohort is made up of very few people (we are 6) that focus on learning Clojure together for 5 weeks. How much interaction we are going to have remain to be seen and it feels pretty much up to us to decide (especially since we are a mentor-less cohort). So today marks the beginning of my Clojure journey, with ClojureFam.
- Completing 100 problems on 4clojure (this number is TBD)
- Working through Clojure from the Ground Up, completing exercises
- Working through Brave Clojure, completing exercises
- Contributing to Athens' codebase
One of the other learners in my cohort also decided to learn clojure in public so if you read this, be sure to hold him accountable as well! I guess I can check the "inspire" box!
The first thing you learn in any language is the types of values. Types are values that work together. The first chapter covered some syntax and lists but it was very similar to Scheme so I didn't feel it was worthy of taking in depth notes as it will be covered in more details later. The interesting thing is the notes I already took I was able to save for later chapters. It's already writing itself! Interestingly enough I ended up taking notes cheat sheet style. Something I should probably be doing as well fairly soon is making Anki flashcards out of it. The goal of all this is indeed to retain it forever. While I learned about the types I made a cheatsheet of all the functions I encountered while doing so. Turns out I got quite a few already.
Like 3
, (type 3)
will return java.lang.Long
. They are stored in 64 bits, one bit for the sign, the rest for the size. So the highest long will be 2^63-1. For anything beyond that we can use bigint
, which will be displayed like this 8N
. So (inc (bigint Long/MAX_VALUE))
will return 9223372036854775808N
.
There are smaller numbers too:
- Bytes
(byte 0)
, 8 bits so maximum value is2^7-1
- Shorts
(short 0)
, 16 bits so maximum value is2^15-1
- Integers
(int 0)
, 32 bits, so maximum value is2^31-1
Floating point numbers are either Doubles (64 bits and also the default) or Floats (32 bits). They are approximations and if we want to represent fractions exactly we can use ratios, like 1/3
.
They are of the type java.lang.String
. You can turn almost anything into a string with the str
function. TODO link to the str entry in the function cheatsheet.
Like in JavaScript, there are falsy values (false
, nil
). The rest is truthy. You can find out the truthiness of a value with the boolean
function. 0
is not considered falsy.
Symbols refer to things, point to other values. When a program is evaluated, they are replaced by their corresponding values.
Symbols can be namespaced with /
. These names are the fully qualified names that lets us access a symbol from anywhere (as opposed to the short name).
These are new, they were not to my knowledge in Scheme. Their usage is not very clear for now so I will probably have to come back to edit this later. They are of this form :cat
, (type :cat)
=> cat
.
Lists contain elements, or members. They can contain anything, including other lists. Lists are quoted with ' or constructed with list
, to prevent being evaluated. =
compares lists. (= (list 1 2) (list 1 2))
. first
returns the first element, last
the last, and nth
the nth element. The first element being at index 0.
Lists are well suited for small collections that are read in linear order. Getting an arbitrary member can be slow, vectors are better suited for this.
Vectors are like this [1, 2, 3]
. They are not evaluated so no need to quote them. Use vec
to build a vector. One thing to note is that conj
will as to the end of the vector (contrary to lists). nth
will be fast on vectors. count
will give us the size of a vector. You can return the element at an index i
like this ([1, 2, 3] 1)
, it will return 2
.
Lists and vectors containing the same members are considered equal.
This is for unordered collections of values, and it is written #{1, 2, 3}
. Sets cannot contain any element more than once. You can also use them as a verb (like vectors) and it will return the element itself. (#{1 2 3} 3)
returns 3
.
This is a data structure that associates keys with values. A map's member alternate between keys and values. (get {:name "maceo" :age 14} :name)
will return maceo
. An extra argument will be the default value if the key is not found. And the same way you can use a map as a verb, ({"amlodipine" 12 "ibuprofen" 50} "ibuprofen")
will return 50
. The other way around works too.
I read this article that was posted last week on Hacker News. While it was interesting it didn't bring more than what I got from this other article about the REPL I covered a few days ago.