Skip to content

Latest commit

 

History

History
116 lines (81 loc) · 5.24 KB

2020-07-08.md

File metadata and controls

116 lines (81 loc) · 5.24 KB

Learning Clojure in Public - Week 3 Day 3 (17/35)

Expectations

Yesterday I finished Chapter 5 of Brave Clojure. I quickly skimmed through Chapter 6 and it's much shorter than the others with no exercises. The expectation for today is therefore to read and take notes on Chapter 5 and then look into the second issue that me and my cohort-mates are tackling together. This issue has a much larger scope that the one we merged today, but is also very interesting.

What I learned

Namespaces

  • You can refer to the current namespace with *ns*.
  • You can get the name of a namespace with (ns-name *ns*).
  • When starting the REPL, you are in the user namespace.
  • When you use def you are interning a var.
  • You can get a map of all the interned vars by doing (ns-interns *ns*), and then (get (ns-interns *ns*) 'great-books) will get a specific var.
  • (ns-map *ns*) will return the full map of the namespace.
  • #'user/fun is the reader form of a var.
  • deref and the reader form and you can get the object they point to.

Creating and switching to namespaces

  • create-ns like (create-ns 'hello.world)
  • in-ns will create (if it doesn't exist) and switch to that namespace

Accessing symbols from other namespaces

  • You can use symbols from other namespaces by using their fully qualified symbol, like clojure.string/split.
  • Another way is to use refer:
(clojure.core/refer 'other.namespace) ; contains symbol sym
sym

In effect this is like merging the current namespace with another one. I expect collisions might be on the menu.

It is also possible to use filters with it such as :only, :exclude and :rename

  • defn- let's you declare private functions only accessible within the same namespace.
  • finally there is alias, which lets you shorten a namespace name like so (clojure.core/alias 'taxonomy 'cheese.taxonomy)

Real project organization

Even though I have already covered this in Chapter 7 of Clojure from the Ground Up it is always good to review and especially find out more about namespaces in this context.

When looking at a namespace, like firstpart.secondpart, firstpart is the directory and the full thing is the namespace name.

require

require finds and evaluates the file matching the namespace name. Clojure expects that file to declare a namespace matching its path. It will also alias a namespace with :as like (require '[the-divine-cheese-code.visualization.svg :as svg]). This is equivalent to:

(require 'the-divine-cheese-code.visualization.svg)
(alias 'svg 'the-divine-cheese-code.visualization.svg)

Then there is use that does what require and then refer does so:

(require 'the-divine-cheese-code.visualization.svg)
(refer 'the-divine-cheese-code.visualization.svg)
(alias 'svg 'the-divine-cheese-code.visualization.svg)

can be replaced by this:

(use '[the-divine-cheese-code.visualization.svg :as svg])
(= svg/points points)
; => true

(= svg/latlng->point latlng->point)
; => true

The ns Macro

This is the one that is use in source code files.

  • ns will refer the clojure.core namespace by default
    • this two examples are equivalent:
(ns the-divine-cheese-code.core
  (:refer-clojure :exclude [println]))

(in-ns 'the-divine-cheese-code.core)
(refer 'clojure.core :exclude ['println])
  • There is no need to quote the symbols with ns.
  • The other references are the following
    • (:refer-clojure)
    • (:require), works a lot like require, can also use :as to alias
    • (:use)
    • (:import)
    • (:load)
    • (:gen-class

Researching our second issue

Athena is the search bar used in Athens. The issue that we are taking is covering a few things:

  • Up and Down to go through the list, and hover over elements.
  • having only one element be highlighted at the same time. We're currently leaning towards highlighting items on hover and also on keyboard movements.
  • figuring out why the search bar is not working on non-Chromium browsers Unfortunately I didn't go further than just running these different scenarios on my local version of Athens.

Takeaways

I did a good review today: I feel like I have all the tools I need to start organizing projects. I looked into how namespaces organize maps between symbols and vars, and that vars are references to Clojure objects. def stores an object and updates the current namespace with a map between a symbol and a var that points to the object. I also covered the ns macro but will surely have to refer to this chapter again in the future.

It was also interesting to do some 4clojure problems again (I had not done any since July 1st!). The functions I'm using are slightly more varied (read more tailored to the occasion), which is a good sign.

Let's end the day with a tally of what I completed:

  • Chapter 6 of Clojure for the Brave and True (there was no exercise this time)
  • 4 4clojure problems
  • And an initial overview of our next collaborative issue in Athens