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)

Summary

From this exercise, I would draw the following principles:

  • If you have an actual, factual performance concern, then avoid multimethods in favor of a plain function or protocol functions.
  • You should never extend a multimethod or protocol that you do not own to a type that you do not own.
  • If you are designing a library that requires some abstraction, you do not want to define a multimethod or protocol; you want to allow a user to pass in a function.
  • If you are extending an abstraction to types or something that can reasonably be modeled as types, then protocol functions will provide the best balance between flexibility and performance.
  • If you are more concerned about writing cross-platform code and less about flexibility or performance, then using a plain function with a cond and cross-platform predicates for dispatch would be best.
  • If you need very general dispatch and are fine with the performance penalty, then use multimethods.