Curious (Clojure) Programmer Simplicity matters

Menu

  • Home
  • Archives
  • Tags
  • About
  • My Talks
  • Clojure Tip of the Day Screencast
  • (Open) Source
  • Weekly Bits & Pieces
  • RSS
May 23, 2022

Weekly Bits 11/2022 - Lisp in Small Pieces, Abstractions, Project Loom, and Fixing ring's redirects

Table of Contents
  • Clojure
    • Fixing :absolute-redirects's default in ring-defaults
    • Eric Normand on Abstraction.
    • prometheus.clj
    • Playing with Clojure regex functions
  • Java / JVM
    • Project Loom, Virtual Threads, and Clojure
  • AWS & Cloud
    • Glob expressions in Cloudwatch Insights
  • Reading (Books)
    • LiSP (Lisp in Small Pieces)
  • Writing (Blog, Articles)
  • MISC
    • Containers unplugged: Understanding user namespaces
    • Page faults, perf, et al.
    • List process threads on macOS
  • Links

Some of the interesting things I did, learned, or found between 9.5.2022 and 15.5.2022.

Clojure

Fixing :absolute-redirects's default in ring-defaults

I made a PR for ring-defaults to change :absolute-redirects default to false. This is to fix the value based on an outdated HTTP spec - I wrote about this problem in more detail before in Broken HTTP/S redirects, friend, and ring.

Eric Normand on Abstraction.

I read a few of Eric’s recent newsletters focused on how to make good abstractions.

The first one was 463: What is beautiful code?, in which he quotes Fran Allen from Peter Seibel’s interview in Coders At Work: Seibel asks "What makes a program beautiful?":

That it is a simple straightforward solution to a problem; that has some intrinsic structure and obviousness about it that isn’t obvious from the problem itself.

— Fran Allen

Eric then ellaborates on it:

  • To paraphrase: "The solution is obvious, but it isn’t obvious from the problem itself."

  • It reminds me a lot of Aristotle’s advice for a good ending to a story: both surprising and inevitable

In another newsletter, 465: Abstraction is the essence of programming, he tells us that we focus too much on the style of programming and not enough on the content.

I still contend that the most maintainable code is code that models essential concepts in the domain.

— Eric Normand

And he’s confident that the proper answer is abstraction:

  • Abstraction is a mapping from a concrete domain to an abstract domain.

  • We map the days of the week (concrete domain) to the integers 1-7 (abstract domain)

    • Then we can write useful operations on them because computers handle integers easily.

    • But Are integers the best way to represent the days? What other choices do we have? How do we choose between them?

  • The act of abstraction is the essence of programming.

He continues the Abstractions series in 466: You need two abstractions to model, showing an eample of two abstractions using the "days of the week" example:

  • a mapping from concrete domain (weeks of the day) to abstract domain (numbers)

  • a mapping from bits in the computer to numbers (the abstract domain)

prometheus.clj

Pure clojure, zero-deps prometheus client library.

Playing with Clojure regex functions

;; re-find returns the first match within the string
(re-find #"abc" "abcdabc")
;; => "abc"

;; re-seq returns all the matches as a sequence
(re-seq #"abc" "abcdabc")
;; => ("abc" "abc")

;; with re-matches, the whole string must match the regex
(re-matches #"abc" "abcdabc")
;; => nil
(re-matches #"abc" "abc")
;; => "abc"

Java / JVM

Project Loom, Virtual Threads, and Clojure

Aleš Najmann published a great article about using virtual threads with Clojure. Virtual threads were recently made available as a preview feature in JDK 19.

Here’s just a very simple example how to start a virtual thread in Clojure using JDK 19:

;; Note: clojure functions implement Callable so they work out of the box
(Thread/startVirtualThread #(println "Hello world!"))

I also learned, that it’s possible to replace the default threadpool for agents and futures:

defn thread-factory [name]
  (-> (Thread/ofVirtual)
      (.name name 0)
      (.factory)))

(set-agent-send-off-executor!
  (Executors/newThreadPerTaskExecutor
    (thread-factory "clojure-agent-send-off-pool-")))

You can find all my examples here: https://github.com/jumarko/clojure-experiments/blob/master/src/clojure_experiments/concurrency/loom.clj#L1

AWS & Cloud

Glob expressions in Cloudwatch Insights

Glob expressions can be used for easier parsing of fields:

fields @message
| filter @message like /timed-analysis-progress/
| parse @message "step-name='*' duration=*" as stepName, duration
| stats min(duration), avg(duration), max(duration) by stepName

Reading (Books)

LiSP (Lisp in Small Pieces)

I said before, that Chris Houser started a book club reading Lisp in Small Pieces: https://chouser.us/lisp2022/ I started doing some real work and notably did a pairing session with another member of the book club. It was fun and useful to go through the code with somebody else. It was so good that we actually did another pairing session a couple of days later.

I spent a lot of extra time on this book - I’m a slow reader and to digest the material, I need to go through it several times, trying to implement as much of the code as possible myself. This week I got to the end of the section 1.5 and also read intro to 1.6 (function execution environment) where the things start getting really interesting.

On Slack, I found a reference to https://clojure.org/reference/lisps which compares Clojure features with Common Lisp and Scheme

Writing (Blog, Articles)

I wroted down several sketches for future blog posts. These are only very early drafts so I’ll see if I can complete any of them soon.

MISC

Containers unplugged: Understanding user namespaces

A great talk by Michael Kerrisk (the author of The Linux Programming Interface). If you want to understand a foundational building block for Docker and other container technologies, watch this talk!

Page faults, perf, et al.

I revisited my notes about page faults and benchmarking with perf. Important points:

  • What are page faults in perf? https://quick-adviser.com/what-are-page-faults-in-perf/

    • The page fault counters in Performance Monitor do not distinguish between hard and soft faults, so you have to do a little bit of work to determine the number of hard faults.

  • Difference between minor page faults vs major page faults: https://www.learnsteps.com/difference-between-minor-page-faults-vs-major-page-faults/

    • Minor page faults occurs when the page is present in the memory but is not mapped properly either because of invalid mapping or data is not cleared from the page by its previous process

    • a Major Page Fault occurs because of the absence of the required page from the RAM and bringing it in costs heavy penalty in terms of cpu cycles occurred due to swap in ,swap out and restarting of the process altogether.

  • To measure page stats with sar:

    sar -B 1

List process threads on macOS

On linux, you can use ps -T to list process threads but it doesn’t work on macOS. What you can use instead, is the -M option:

ps -M $(pgrep java | head -1)

USER    PID   TT   %CPU STAT PRI     STIME     UTIME COMMAND
jumar  1528 s034    0.0 S    31T   0:00.02   0:00.02 java -Dclojure.basis=.cpcache/870533677.basis -classpath ...
       1528         0.0 S    31T   0:00.00   0:00.00
       ...

You won’t see many details about the threads themselves but at least you can see how many of them there are.

Links

A quick recap of some of the links mentioned in this post:

  • change :absolute-redirects default to false.

  • Containers unplugged: Understanding user namespaces

  • Eric Normand on abstraction:

    • 463: What is beautiful code?,

    • 465: Abstraction is the essence of programming

    • 466: You need two abstractions to model

  • https://clojure.org/reference/lisps compares Clojure features with Common Lisp and Scheme

  • prometheus.clj

  • Clojure regex functions

  • article about using virtual threads with Clojure

    • all my examples here: https://github.com/jumarko/clojure-experiments/blob/master/src/clojure_experiments/concurrency/loom.clj#L1


Tags: sw-design clojure weekly-bits abstraction jvm

« Weekly Bits 12/2022 - Abstractions, Lisp in Small Pieces, lein repl vs JIT, Weekly Bits 10/2022 - Clojure, Cider 1.4, AWS architectures, Lisp in Small Pieces, and The Fieldstone Method »

Copyright © 2025 Juraj Martinka

Powered by Cryogen | Free Website Template by Download Website Templates