# A Little More Clojure

DevFeed: [A Little More Clojure](<https://devfeed.tech/articles/a-little-more-clojure-21786.md>)

Original publisher: [Read original article](<http://blog.cleancoder.com/uncle-bob/2020/04/09/ALittleMoreClojure.html>)

Published: 2020-04-09T00:00:00Z

Content type: tutorial

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>), [Code](<https://devfeed.tech/topics/code.md>), [Test-driven development](<https://devfeed.tech/topics/tdd.md>), [Java](<https://devfeed.tech/topics/java.md>)

Tags: [clojure](<https://devfeed.tech/tags/clojure.md>), [code](<https://devfeed.tech/tags/code.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [java](<https://devfeed.tech/tags/java.md>), [libraries](<https://devfeed.tech/tags/libraries.md>), [tdd](<https://devfeed.tech/tags/tdd.md>), [test](<https://devfeed.tech/tags/test.md>)

## AI overview

An introduction to additional Clojure utility functions, including map, filter, range, let, and anonymous functions. The article applies these concepts to finding prime numbers up to one thousand and demonstrates a variant of test-driven development, immutable local bindings, and interoperability with Java libraries.

## Source excerpt

So let's learn just a little bit more of Clojure. Here are a few common utility functions: user=> (inc 1) ; increments argument 2 user=> (dec 3) ; decrements argument 2 user=> (empty? []) ; tests for empty true user=> (empty? [1 2]) false If you know Java or C# you probably know what the map function does. Here's an example: (map inc [1 2 3]) evaluates to (2 3 4). The first argument of map is a function. The second is a list. The map function returns a new list by applying the function to every element of the input list. The filter function also takes a function and a list. (filter odd? [1 2 3 4 5]) evaluates to (1 3 5). From that I think you can tell what both the filter and the odd? functions do. And so with that, let's try a little challenge. Let's find all the prime numbers between one and a thousand. We'll use a variant of TDD to do this. Our eyes will be the tests. The cycle will be the same size as normal TDD; but we'll write a bit of code first and then test it. I know. Blashphemy! So sue me. ;-) We begin like this: (defn primes [n] ) This returns nil. user=> (primes 1000) nil Now let's get all the numbers between 1 and n. (defn primes [n] (range 1 (inc n))) user=> (primes 10) (1 2 3 4 5 6 7 8 9 10) You've probably figured out what range does. It just returns a list of all the integers between it's arguments. OK, so now all we have to do is filter all the primes: (defn primes [n] (let [candidates (range 1 (inc n))] (filter prime? candidates))) CompilerException java.lang.RuntimeException: Unable to resolve symbol: prime? in this context, compiling:(null:3:5) Oh, oh. We need to implement prime? (defn prime? [n]) user=> (primes 10) () OK, that makes sense. But I should explain the let function. It allows you to create names that are bound to expressions. The names exist only within the parentheses of the let expression. So it's a way to create local variables - though the word "variable" is not quite right because they cannot be reassigned. They are immutable.