Model Results/States with Java 17's Records
I often want to represent a set of possible results or states with different possible values. For example a processing result:
A successful full result
And expect error with some error code
An unexpected error with some exception
Some other rare edge case I need special handling for.
Implement your own Clojure Atoms
Let’s implement our own Clojure atoms for fun and educational purpose. Do not use it for your actual application =).
Advanced Features of Clojure Atoms
Most Clojure apps use atom
s for managing state,
changing state with the swap!
or reset!
functions:
(def inventory (atom {:cheese 1 :bread 2}))
; Use swap! to update the atom with a function. In this example:
; Use the 'assoc' function to update the atom. The extra parameters are forwarded to 'assoc'
(swap! inventory assoc :cheese 3)
=> {:cheese 3, :bread 2}
; deref returns the current state. The @ prefix is 'syntactic' sugar to do the same
(str "use deref " (deref inventory) " or @ " @inventory " to get the value of the atom")
=> "use deref {:cheese 3, :bread 2} or @ {:cheese 3, :bread 2} to get the value of the atom"
; Use 'reset!' to reset the state to a specific value.
(reset! inventory {:cheese 0 :bread 0})
=> {:cheese 0, :bread 0}
In my Java/C# mind, an atom is an AtomicReference / Interlocked.CompareExchange. However, atoms do have more high-level features. Let’s take a look.
Automated Tests Advice, C# Edition
This post is part of C# Advent Calendar 2021.
I do like writing automated tests, for two reasons. First, it gives me a fast feedback loop. Testing on the fully running app is usually time-consuming compared to running a test. Second, over time it gives some confidence that changes in the code didn’t break your application in unexpected ways.
Basic Process information via /proc
When you need information about a process in Linux,
there are tons of command lines tools, like ps
, htop
, lsof
etc.
I often do not remember the flags to get the information I want.
Luckily, Linux has the /proc file system which gives details on every process.
Most Unix-like operation systems have it, but the formats differ.
I only take a look at the Linux version here.