Book Image

Clojure Polymorphism

By : Paul Stadig
Book Image

Clojure Polymorphism

By: Paul Stadig

Overview of this book

Clojure is a modern, dynamic language that you can use to develop robust, multithreaded programs. Clojure Polymorphism is a comprehensive guide that shows you how to use Clojure’s features to your advantage. The book begins by describing examples that show how to define and implement abstractions with plain functions and multimethods. Then you'll analyze these examples and separate the good and bad aspects of their design principles. You'll also learn how to perform data transformation abstraction with a plain function and discover how to write new cross-platform predicates while keeping the core of your abstraction free from reader conditionals. The later chapters explain the considerations to keep in mind when implementing Clojure protocols on the Java Virtual Machine (JVM). By the end of this book, you’ll know how to use the various polymorphic tools of Clojure to your advantage while designing your applications.
Table of Contents (7 chapters)

Protocol

You could use protocol to define the abstraction:

1 (ns stadig.storage.protocol

2   (:refer-clojure :exclude [get]))

3

4 (defprotocol IStorage

5   (get [this bucket key])

6   (put [this bucket key value])

7   (delete [this bucket key])

8   (close [this]))

Implement it for each backend:

1 (ns stadig.storage.s3

2   (:require

3    [aws.sdk.s3 :as s3]

4    [stadig.storage.protocol :as proto]))

5

6 (defrecord S3Storage

7     [access-key secret-key]

8   proto/IStorage

9   (get [this bucket key]

10     (when-not bucket

11       (throw (ex-info "Expected bucket" {:type ::bucket-error})))

12     (s3/get-object this bucket key))

13   (put [this bucket key value]

14     (when-not...