# Functional Classes in Clojure

DevFeed: [Functional Classes in Clojure](<https://devfeed.tech/articles/functional-classes-in-clojure-21800.md>)

Original publisher: [Read original article](<http://blog.cleancoder.com/uncle-bob/2023/01/19/functional-classes-clojure.html>)

Published: 2023-01-19T00:00:00Z

Content type: article

Language: en

Sources: [Robert C. Martin](<https://devfeed.tech/sources/robert-c-martin.md>), [The Clean Code Blog](<https://devfeed.tech/sources/the-clean-code-blog.md>)

Topics: [Clojure](<https://devfeed.tech/topics/clojure.md>), [Functional programming](<https://devfeed.tech/topics/functional-programming.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Code](<https://devfeed.tech/topics/code.md>), [Polymorphism](<https://devfeed.tech/topics/polymorphism.md>)

Tags: [clojure](<https://devfeed.tech/tags/clojure.md>), [code](<https://devfeed.tech/tags/code.md>), [functional-programming](<https://devfeed.tech/tags/functional-programming.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

The article explains how class-like abstractions can be implemented in functional programming using Clojure. It presents a Dilithium Cloud example built from named functions, an internally defined data structure, dynamic type specifications, constructors, methods, and unit-test validation, while arguing that class syntax, inheritance, and polymorphic dispatch are not required.

## Source excerpt

My previous blog seemed only to continue the confusion regarding classes in Functional Programming. Indeed, many people got quite irate. So perhaps a bit of code will help. Trigger Warning: Object Oriented Terminology. Dynamically Typed Language. Mixed Metaphors. Distracting Animations. To all the adherents of the Statically Typed Functional Programming religion: I know that you believe that Static Typing is an essential aspect of Functional Programming and that no mere dynamically typed language could ever begin to approach the heights and glory of The One True and Holy TYPED Functional Apotheotic Paradigm. But we lowly programmers quivering down here at the base of Orthanc can only hope to meekly subsist on the dregs that fall from on high. (R.I.P. Kirstie Alley OK, so, once again... A class is an intentionally named abstraction that consists of a set of narrowly cohesive functions that operate over an internally defined data structure. We do not need the class keyword. Nor do we need polymorphic dispatch. Nor do we need inheritance. A class is just a description, whether in full or in part, of an object. For example - it's time we talked about clouds (which I have looked at from both sides now; and do, in fact, understand pretty well). So... Here come your father's parentheses! (ns spacewar.game-logic.clouds (:require [clojure.spec.alpha :as s] [spacewar.geometry :as geo] [spacewar.game-logic.config :as glc])) (s/def ::x number?) (s/def ::y number?) (s/def ::concentration number?) (s/def ::cloud (s/keys :req-un [::x ::y ::concentration])) (s/def ::clouds (s/coll-of ::cloud)) (defn valid-cloud? [cloud] (let [valid (s/valid? ::cloud cloud)] (when (not valid) (println (s/explain-str ::cloud cloud))) valid)) (defn make-cloud ([] (make-cloud 0 0 0)) ([x y concentration] {:x x :y y :concentration concentration})) (defn harvest-dilithium [ms ship cloud] (let [ship-pos [(:x ship) (:y ship)] cloud-pos [(:x cloud) (:y cloud)]] (if (< (geo/distance ship-pos cloud-pos) glc/dilit