You are previewing Clojure Data Structures and Algorithms Cookbook.
O'Reilly logo
Clojure Data Structures and Algorithms Cookbook

Book Description

25 recipes to deeply understand and implement advanced algorithms in Clojure

About This Book

  • Explore various advanced algorithms and learn how they are used to address many real-world computing challenges
  • Construct elegant solutions using impressive techniques including zippers, parsing, and pattern matching
  • Solve complex problems by adopting innovative approaches such as logic or asynchronous programming
  • In Detail

    Data-structures and algorithms often cross your path when you compress files, compile programs, access databases, or simply use your favourite text editor. Understanding and implementing them can be daunting. Curious learners and industrial developers can find these complex, especially if they focus on the detailed implementation of these data structures.

    Clojure is a highly pragmatic and expressive language with efficient and easy data manipulation capabilities. As such, it is great for implementing these algorithms. By abstracting away a great share of the unnecessary complexity resulting from implementation, Clojure and its contrib libraries will help you address various algorithmic challenges, making your data exploration both profitable and enjoyable.

    Through 25 recipes, you'll explore advanced algorithms and data-structures, well served by a sound Clojure implementation.

    This book opens with an exploration of alternative uses of the array data-structure, covering LZ77 compression, drawing fractals using Pascal's triangles, simulating a multi-threaded program execution, and implementing a call-stack winding and un-winding operations.

    The book elaborates on linked lists, showing you how to construct doubly linked ones, speed up search times over the elements of such structures, use a linked-list as the foundation of a shift-reduce parser, and implement an immutable linked-list using skew binary numbers representation.

    After that, the tree data-structure is explored, focusing on building self-balancing Splay Trees, designing a B-Tree backing-up an efficient key-value data-store, constructing an undo capable Rope, and showing how Tries can make for an auto-completing facility.

    Next, some optimization and machine learning techniques are discussed, namely for building a co-occurrence-based recommendation engine, using branch-and-bound to optimize integral cost and profit problems, using Dijkstra's algorithm to determine optimal paths and summarizing texts using the LexRank algorithm.

    Particular attention is given to logic programming, you will learn to use this to discover interesting relations between social website data, by designing a simple type inferencer for a mini Java-like language, and by building a simple checkers game engine.

    Asynchronous programming will be addressed and you will design a concurrent web-crawler, an interactive HTML5 game, and an online taxi booking platform.

    Finally, you'll explore advanced cases for higher order functions in Clojure while implementing a recursive descent parser using efficient mutual resucrsion, devising a mini resusable firewall simulator thanks to Clojure 1.7 new tansducers feature or building a simple unification engine with the help of Continuation Passing Style.

    What You Will Learn

  • Explore alternative uses of classical data-structures like arrays and linked-lists
  • Discover advanced types of tree data-structures
  • Explore advanced machine learning and optimization techniques
  • Utilise powerful Clojure libraries, such as Instaparse for parsing, core.match for pattern matching, for zippers, and clojure.matrix for matrix operations
  • Learn logic programming through the usage of the library core.logic
  • Master asynchronous programming using the core.async library
  • See the transducers in action while resolving real-world use-cases
  • Who This Book Is For

    If you are an experienced Clojure developer, longing to take your knowledge to the next level by discovering and using advanced algorithms and seeing how they can be applied to real-world problems, then this book is for you.

    Style and approach

    This book consists of a set of step-by-step recipes, each demonstrating the material covered in action so it is put in context. When necessary, pointers to further resources are provided.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Clojure Data Structures and Algorithms Cookbook
      1. Table of Contents
      2. Clojure Data Structures and Algorithms Cookbook
      3. Credits
      4. About the Author
      5. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Sections
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Revisiting Arrays
        1. Introduction
        2. Efficiently compressing a byte array
          1. How to do it...
        3. Using Pascal's triangle to draw fractals
          1. How to do it...
        4. Simulating multithreading using time-sharing
          1. How to do it...
        5. Simulating a call stack using arrays
          1. How to do it...
      9. 2. Alternative Linked Lists
        1. Building a doubly linked XOR list
          1. How to do it…
        2. Speeding up access to linked list elements
          1. How to do it…
        3. Building a simple shift-reduce parser
          1. How to do it…
        4. Implementing a skew binary random access list
          1. How to do it…
      10. 3. Walking Down Forests of Data
        1. Introduction
        2. Building self-balancing, search-efficient splay trees
          1. How to do it...
        3. Designing an efficient key-value store using B-trees
          1. How to do it...
          2. How it works…
        4. Devising an undo-capable data structure using a rope
          1. How to do it...
        5. Designing an autocomplete system using a trie
          1. How to do it...
      11. 4. Making Decisions with the Help of Science
        1. Introduction
        2. Designing a live recommendation engine
          1. How to do it...
        3. Resolving cost and profit optimization problems
          1. How to do it...
        4. Finding optimal paths in a graph
          1. How to do it...
        5. Summarizing texts by extracting the most representative sentences
          1. How to do it...
      12. 5. Programming with Logic
        1. Introduction
        2. Querying a social website's data
          1. How to do it…
        3. Designing a type inferencer
          1. How to do it…
        4. Playing a round of checkers
          1. How to do it…
      13. 6. Sharing by Communicating
        1. Introduction
        2. Building a tiny web crawler
          1. How to do it…
        3. Designing an HTML5 game
          1. How to do it…
        4. Designing an online taxi-booking engine
          1. How to do it…
      14. 7. Transformations as First-class Citizens
        1. Introduction
        2. Building a recursive descent parser using trampoline
          1. How to do it…
        3. Implementing a reusable mini-firewall using transducers
          1. How to do it…
        4. Building a little unification engine with the continuation-passing style
          1. How to do it...
      15. Index