Thursday, 10 December 2015

Conway's Game of Life - my take on it in Clojure

If the board is represented as a vector of strings, where a string represents a row. E.g

[" "
" ## "
" ## "
" ## "
" ## "
" "]
view raw gof_board.clj hosted with ❤ by GitHub
The solution takes the board and returns the it in the next state.

(defn gof
"board is a vector of strings"
[board]
(let [rows (count board)
cols (count (first board))
[living-cell dead-cell] [\# \space]
alive? (fn [[row col]]
(= living-cell (nth (nth board row) col)))
live-neighbours (fn [[row col]]
(for [r [(dec row) row (inc row)]
c [(dec col) col (inc col)]
:when (and (< -1 r) (< r rows)
(< -1 c) (< c cols)
(not= [row col] [r c])
(alive? [r c]))]
[r c]))
next-state (fn [[x y]]
(let [ln (-> [x y] live-neighbours count)
live (if (alive? [x y])
(and (< 1 ln) (< ln 4))
(= 3 ln))]
(if live living-cell dead-cell)))
->row (fn [r] (apply str
(for [c (range cols)] (next-state [r c]))))]
(mapv ->row (range rows))))

Friday, 13 November 2015

On Scala

This will be a short one. Having been earning my bread and butter with Scala for 3 months now, I've come to realize: you can write beautiful code with Scala on both small and large scale. It has all the tools you need to do that. The same tools will enable undisciplined developers to make a terrible mess. Thank God we are a small team.

Tuesday, 7 July 2015

My book list

Everyone has to come up with a booklist. That's a fact. This year is the 10th of my professional life and I use the annual to look back over this decade and pick the books that taught or inspired me most. I try to structure the following list respecting chronology (subjective chronology, though - based on when I came across these items), level and theme, sometimes sacrificing one for the the sake of the others.

Head First Design Patterns

An epitome of beginners' books, requiring only basic programming knowledge, funnily written and accessible. Even though Java and OO is not as hip as they used to be, it's a worthy read. Must read even, if you are a young, aspiring Java programmer.

Robert C. Martin: Clean Code

One of my all-time favourites. As much as I dislike Robert C Martin as a speaker - he has apparently irredeemably fallen in love with his own voice - I respect him as a superb writer. Clean Code covers a broad swathe of programming must-knows, its arguments are crystal clear, the style is witty and entertaining. It also introduces, IMHO, one of the greatest practical set of guidelines in programming, the SOLID principles. I think they really capture the essence of good code, and DDD, Hexagonal Architecture,  microservices and many other good things can almost completely be derived from them.

Martin Fowler: Refactoring: Improving the Design of Existing Code

Another iconic book. We owe much to Mr Fowler and this book is one of his greatest contributions. Introduced the concept of "refactoring", changing the code in small, controlled steps for the better whilst keeping the functionality intact.

Joshua Block: Effective Java, Second Edition

I don't really know what sets it apart from the first edition, but this is the only way I have seen this referred to. This is a bit more advanced stuff than the previous ones, and more specific to Java. Not too specific, though, to prevent users of other languages to draw some good lessons. Akin to Clean Code, it concentrates on small-scale stuff you meet in your everyday job - builders, classes, exception handling, ... - and does it very well.

Java Concurrency in Practice

You cannot regard yourself a senior Java developer if you haven't read this book. This sounds smug, but has more than a grain of truth. Concurrency is a bit like uncomfortable truth. One usually procrastinate accepting the pain and dealing with it, but if you do that, sooner or later it will come and bite you. The book addresses this difficult and complex topic and does it in a structured and readable style.

Martin Fowler: Patterns of Enterprise Application Architecture

A book targeting the large-scale. It offers a plethora of patterns and time-honoured insights in Fowler's usual engaging style. Very classic.

Martin Fowler: UML Distilled

Far away the days are when we had to generate code from models. Thank God. Although UML has lost much of its charm in the last decade (for the better), it's still a ubiquitous and not-yet-superseded tool in software engineering. Martin Fowler treats it fair in this concise little book, presenting it as a useful tool for thinking and communication between humans as opposed to the "visual programming language" some of its proponents promised it to be and it's failed to live up to.

Michael Feathers: Working Effectively with Legacy Code

This is a truly wonderful book for developers with a couple of years behind their back. Also is one that is a bit too heavy to read from cover to cover in one go, but full of interesting chapters that can stand alone on their own. Although the theme is eponymously about problems and solutions around interacting with legacy code - mostly how one can incrementally carve out chunks of the legacy codebase and plug in something new and well tested in their place -, Mr Feathers offers some very valuable general insights on coding and especially on testing.

Kirk Knoernschild: Java Application Architecture: Modularity Patterns with Examples Using OSGi

The best book I've read about modularity and I have to admit I only skimmed through the second, OSGi-specific part. But the first part about the general principles, challenges and solutions of modularity is awesome. Lots of other books touches this very important topic, but none of them in this depth and clarity.

Robert C. Martin: Agile Software Development, Principles, Patterns, and Practices

This books deserves much more attention than it enjoys. I consider it the big brother of Clean Code, aiming at larger scale problems. Among other things it introduces principles of Package and Component Design, mirroring SOLID principles on a coarser granularity level. Although some static code analyser tools - including my own humble excuse for one, Principle - use some of them to calculate metrics, they are not widely known and appreciated enough.

Steve Freeman, Nat Pryce: Growing Object-Oriented Software Guided by Tests

With so many books and blog posts and rants on TDD it's very hard to imagine that someone can still come up with something fresh and insightful in the topic. Yet Freeman and Pryce delivers that and much more. Through a realistic example they show how to do what the title says and tell a lot about testing and design along the way.

Vaughn Vernon: Implementing Domain-Driven Design

The Red Book. This is a big and heavy volume, full of theory and practical examples, and almost indispensable if you want to grok DDD. Much more down to earth than its predecessor (the Blue Book by Eric Evans). Very-very useful.

Martin Fowler: NoSQL Distilled

Yet another great book of Fowler, exhibiting all the good traits of his other work. Exploring a fundamental topic, engaging style and clarity.

That's it.
What is missing? Probably 1-2 books I forgot, but mostly huge amount of on-line resources. I can't say there is any book about FP that fundamentally influenced me, although FP in general has in the last 2 years. The same could be said specifically about Clojure.

Thursday, 7 May 2015

Clojure's Protocol - when to use it?

Coming from an OO-background I instinctively turned to protocols during my early Clojure explorations. Need to model an Aggregate? Complex internal structure, should be hidden, small set of functions operating on it? Protocol, what else?
Eventually I've learnt a bit of FP and started moving towards pure functions, feeling less and less the need to resort to the OO-ish features of the language. However recently I've been spending some time writing a Twitter-reader application and had some trouble with functions with side-effects. Long story short, I've come up with a simple rule of thumb (don't expect anything earth-shattering)

Use protocols when state is involved!

When is 'state' involved?

1. when the program maintains stateful entitites (either in DB or just in memory)
2. communicating with an external system (e.g. Twitter)

The two cases are not that different, actually. If you can abstract away from the communication details, is operating on the DB really much different from operating on Twitter? Twitter could run on your local machine on your DB after all, in theory.

So what's the benefit?

1. you can group functions together. Usually if you manage state, there are multiple operations around it. Save/Delete/Get/Update for DB, StartListeningToStream/StopListeningToStream for Twitter. Having separate methods only grouped by namespace seems somewhat off to me. Maybe a matter of taste.
2. Easier to provide new implementations. You want to replace your Mongo with Reddis, just give a new implementation. I think the cognitive load is much less if all the functions you need to change are bound together by the language itself, so you don't have to hold all those independent functions in your head.
3. Ordinary functions should be pure. If you hide your state behind a protocol, all the rest can be

An example

(ns mini-projects.twitter-reader.twitter-listener
(:require [clojure.edn :as edn]
[clojure.data.json :as json]
[clojure.java.io :as io]
[http.async.client :as ac]
[twitter.callbacks :as tc]
[twitter.callbacks.handlers :as tch]
[twitter.api.streaming :as tas]
[twitter.oauth :as oauth])
(:import [twitter.callbacks.protocols AsyncStreamingCallback]
[java.io ByteArrayInputStream]))
;; Tweet Listener Component.
(defn- create-oauth-creds
"Creates OAuth credentials for the given config."
[config]
(oauth/make-oauth-creds
(:consumer-key config)
(:consumer-secret config)
(:user-access-token config)
(:user-access-token-secret config)))
(defn- read-oauth-creds
"Reads up the OAuth credentials from a file and returns a map containing them"
[file]
(-> file slurp edn/read-string create-oauth-creds))
(defn- get-tweet-text
"Extracts the text field of the tweet from the ByteArrayInputStream"
[^ByteArrayInputStream baos]
(when-let [tweet (try
(json/read-str (str baos))
(catch Exception e
(println "Bad tweet")
nil))]
(get tweet "text")))
(defn- start-listening!
"Starts listening"
[{:keys [query oauth-creds callback]}]
(tas/statuses-filter
:params {:track query}
:oauth-creds oauth-creds
:callbacks (AsyncStreamingCallback.
(fn [response baos] ;; Tweet handler
(when-let [tweet-text (get-tweet-text baos)]
(callback tweet-text)))
tch/get-twitter-error-message ;; Tweet API error handler
tch/exception-print))) ;; exception handler))
;; ========== PUBLIC ===========================
(defprotocol TweetListener
"Interface for grouping the functions of starting and stopping the tweet stream"
(start-listener! [this] "Starts listening")
(stop-listener! [this] "Stops listening"))
(defn create-tweet-listener
"Factory function. Creates an instanse of the TweetListener protocol"
[{:keys [query tweet-callback]}]
(let [cancel-stream!-fn (atom nil)]
(reify TweetListener
(start-listener! [this]
(let [creds (read-oauth-creds "/Users/mate.magyari/twitter.conf")
user-stream (tas/user-stream :oauth-creds creds)]
(start-listening! {:query query :oauth-creds creds :callback tweet-callback})
(reset! cancel-stream!-fn (:cancel (meta user-stream)))))
(stop-listener! [this]
(@cancel-stream!-fn)))))
So the new, all-encompassing, completely universal principle you should absolutely start all your projects with, absolutely without exceptions

1. identify the moving parts in your model
2. hide them behind protocols by having a namespace for each where only the protocol itself and the factory method is public

Still a bit OO-ish? Maybe. But no-one said OO is without merits.

Saturday, 18 April 2015

Creating Clojure "actors" with core.async

A plain vanilla "actor" built with core.async. Can wrap any type of logic as long as it can be described with a function with the given - very general - signature.

(ns mini_projects.actors.actors
(:require [clojure.core.async :as async]))
(defprotocol Actor
"Actor interface"
(in-chan [this] "Returns the in-channel of the actor")
(out-chan [this] "Returns the out-channel of the actor")
(get-state [this] "Returns the state of the actor")
(shut-down! [this] "Shuts down the actor"))
(defn create-actor!
"Creates an actor who processes messages coming to its in-channel.
Its behaviour is a (state,msg)=>(state,[msg]) function.
The output messages are put on its out-channel.
If it's shut down, it will consume and discard incoming messages."
[{:keys [in out init-state behaviour]}]
(let [alive? (atom true)
state (atom init-state)
actor (reify Actor
(in-chan [this] in)
(out-chan [this] out)
(get-state [this] @state)
(shut-down! [this]
(reset! alive? false)
(async/close! out)))
send-msgs! (fn [ms] (async/go (doseq [m ms]
(async/>! out m))))]
(async/go-loop []
(when-let [msg (async/<! in)]
(when @alive?
(let [r (behaviour {:state @state :msg msg})]
(send-msgs! (:msgs r))
(reset! state (:state r))))
(recur)))
actor))
(defn keep-and-report
"A valid function for an actor's behaviour. Attaches the message to the state and sends out the state as a message."
[{:keys [state msg]}]
(let [new-state (conj state msg)]
{:state new-state
:msgs [(str "state: " new-state)]}))
;; Create a simple actor
(def actor-1 (create-actor! {:in (async/chan)
:out (async/chan)
:init-state nil
:behaviour keep-and-report}))
(def in (in-chan actor-1))
(def out (out-chan actor-1))
;; Print out the messages sent by the actor
(async/go-loop []
(when-let [msg (async/<! out)]
(println (str "Message sent by actor: " msg))
(recur)))
(async/>!! in 1)
;;Message sent by actor: state: (1)
(async/>!! in 2)
;;Message sent by actor: state: (2 1)
(assert (= [2 1] (get-state actor-1)))
(shut-down! actor-1)
(async/>!! in 3)
(async/>!! in 4)
;;shut down actor ignores messages
(assert (= [2 1] (get-state actor-1)))
view raw actor.clj hosted with ❤ by GitHub

Thursday, 16 April 2015

Improving existing code - imaginary checklist - 3

Eliminate cyclic dependencies

Almost forgot about this one, despite being the most obvious (and challenging, possibly).

Decrease ACD (Acyclic Component Dependency)

Related to the previous point, but a quite vague target. I don't know what the proper value is for relative ACD, it possibly depends on the size of the application and other traits of it. STAN is a great tool to help you in that, however.

Tuesday, 31 March 2015

Improving existing code - imaginary checklist - 2

Continuing the completely random and unprioritized checklist. Some of them are easy to fix, some are heavy.

Eliminate mutable state where possible

Immutability has many advantages over mutable objects, none of them will I mention here. For an example see the Query class in the previous post. Avoid setters, use final keyword and defensive copies.

Thread safety

Quite self-explanatory.

Separation of concurrency and domain logic

Multi-threading logic should be separated from the rest of the code. It's quite complex on its own, even more when is intertwined with other things.

Invariants and UnitOfWork-s

Check what are the units of work, and what are the invariants. The formers should encapsulate the latter. This is quite an important point. And a not so low hanging fruit, probably.

Push third-party code to the outer layers

This might be a big one. Practically I think almost all application should follow the Hexagonal Architecture style. This can be achieved iteratively, but could take a long time to get there if the code hasn't been written that way.

To be continued...

Binary Tree implementation in Haskell, Clojure and Scala - 2

And thanks to @Kaloz, here is the Scala implementation with a slightly altered/extended functionality.

object TreeOrd extends App {
implicit val ageOrderting = org.kaloz.twitter.TreeOrdImplicits.AgeOrdering
implicit val carOrdering = org.kaloz.twitter.TreeOrdImplicits.CarOrdering
val personTree = Tree(Person("A", 10), Person("G", 5), Person("C", 2), Person("B", 7))
println(personTree)
val carTree = Tree(Car("Trabant", 40), Car("Porsche", 300), Car("Opel", 150), Car("Skoda", 100))
println(carTree)
}
case class Person(name: String, age: Int)
case class Car(name: String, maxSpeed: Int)
object TreeOrdImplicits {
implicit object AgeOrdering extends Ordering[Person] {
def compare(a: Person, b: Person) = a.age compare b.age
}
implicit object CarOrdering extends Ordering[Car] {
def compare(a: Car, b: Car) = a.maxSpeed compare b.maxSpeed
}
}
object Tree {
def apply[A](elements: A*)(implicit ordering: scala.math.Ordering[A]): Tree[A] = {
elements.foldLeft(EmptyTree: Tree[A])((sum, item) => sum.insert(item))
}
sealed trait Tree[+A] {
def insert[B >: A](elem: B)(implicit ordering: scala.math.Ordering[B]): Tree[B] = this match {
case EmptyTree => Node(elem)
case Node(v, l, r) => v match {
case _ if ordering.lt(elem, v) => Node(v, l.insert(elem), r)
case _ if ordering.gt(elem, v) => Node(v, l, r.insert(elem))
}
}
}
case object EmptyTree extends Tree[Nothing]
case class Node[A](value: A, left: Tree[A] = EmptyTree, right: Tree[A] = EmptyTree) extends Tree[A]
}

Improving existing code - imaginary checklist - 1

I endeavour to prepare a checklist I would go through if I had to start to work on a brownfield project. Small things to improve, hunt for the low hanging fruits. Don't expect any enlightenment here, these are just pearls of blue-collar wisdom.

Handling unrecoverable exceptions

In my current project we use messaging via ActiveMQ. Should an exception occur, the message is bounced back to the queue, then retried. It's fine, as long as there is a chance of recovery and sometimes - for example when the message is invalid in some way - there is none. In this case bouncing the message back is a waste of time and resource, plus the message can and up in the Dead Letter Queue leading to memory loss. So instead we should catch these exceptions as close to the entry point as possible and simply log them.

Validating input

Related to the previous point. To adhere the fail-fast principle, the inputs of the system should be validated. Validation is usually against some domain criteria, so I would put the logic in the Domain layer, just as the input passed the ACL. Should the input fail to comply, throw an unrecoverable exception.

Validate domain objects

I wouldn't stop at the inputs. I'd validate every domain object upon creation. Design by contract is a very good practice.

public class Query {
private final String queryId;
private final PlayerSession playerSession;
private final SearchOptions searchOptions;
public BuddiesQuery(String queryId, PlayerSession playerSession, SearchOptions searchOptions) {
Validator.isTrue(StringUtils.isNotBlank(queryId));
Validator.isNotNull(playerSession);
Validator.isNotNull(searchOptions);
this.queryId = queryId;
this.playerSession = playerSession;
this.buddySearchOptions = buddySearchOptions;
}
//getters
view raw Query.java hosted with ❤ by GitHub
The validation should throw an unrecoverable exception. See the first point.

To be continued...

Tuesday, 24 March 2015

Binary Tree implementation in Haskell, Clojure and Scala

I've embarked on learning Haskell recently. I only start to suspect the power of this language, but I'm already impressed with its beautifully succinct and readable, no-nonsense syntax. Nuff' said, let's do a demonstration!

Haskell

data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
treeInsert :: (Ord a) => a -> Tree a -> Tree a
treeInsert x EmptyTree = Node x EmptyTree EmptyTree
treeInsert x (Node a left right)
| x == a = Node a left right
| x < a = Node a (treeInsert x left) right
| otherwise = Node a left (treeInsert x right)
treeElem :: (Ord a) => Tree a -> a -> Bool
treeElem EmptyTree _ = False
treeElem (Node y l r) x
| y == x = True
| x < y = treeElem l x
| otherwise = treeElem r x
buildTree :: (Ord a) => [a] -> Tree a
buildTree xs = foldr treeInsert EmptyTree xs
view raw binaryTree.hs hosted with ❤ by GitHub

Clojure

(ns clojure_study.ideas.tree
(:require [clojure.core.match :as m]))
(defn node [val left right]
{:value val :left left :right right})
(defn tree-insert [x t ord]
(m/match t
:empty-tree (node x :empty-tree :empty-tree)
{:value val
:left left
:right right} (condp = (ord x val)
:eq t
:gt (node val left (tree-insert x right ord))
:lt (node val (tree-insert x left ord) right))))
(defn tree-elem? [x t ord]
(m/match t
:empty-tree false
{:value val
:left left
:right right} (condp = (ord x val)
:eq true
:gt (tree-elem? x right ord)
:lt (tree-elem? x left ord))))
(defn build-tree [elements ord]
(reduce #(tree-insert %2 %1 ord) :empty-tree elements))
;;an ord function for numbers
(defn num-ord [x y]
(cond
(= x y) :eq
(> x y) :gt
(< x y) :lt))
view raw binary-tree.clj hosted with ❤ by GitHub

Scala - I've failed with this one. Maybe @Kaloz can help me out.