How to Iterate over Map Keys and Values in Clojure?

That’s expected behavior. (doseq [x … y …]) will iterate over every item in y for every item in x. Instead, you should iterate over the map itself once. (seq some-map) will return a list of two-item vectors, one for each key/value pair in the map. (Really they’re clojure.lang.MapEntry, but behave like 2-item vectors.) user> … Read more

What is the difference between ; and ;; in Clojure code comments?

There is no difference as far as the interpreter is concerned. Think of ; ;; ;;; and ;;;; as different heading levels. Here is my personal use convention: ;;;; Top-of-file level comments, such as a description of the whole file/module/namespace ;;; Documentation for major code sections (i.e. groups of functions) within the file. ;; Documentation … Read more

How to list the functions of a namespace?

I normally call (keys (ns-publics ‘foo)) to list Vars exported by the namespace foo; e.g. for clojure.contrib.monads this returns (defmonad censor m-when-not m+write+m maybe-m maybe-t …) (the … stands for quite a lot more). More generally, there’s a bunch of functions whose names start in ns- which list Vars by namespace, with certain additional criteria … Read more

How to read mentally Lisp/Clojure code

I think concat is a bad example to try to understand. It’s a core function and it’s more low-level than code you would normally write yourself, because it strives to be efficient. Another thing to keep in mind is that Clojure code is extremely dense compared to Java code. A little Clojure code does a … Read more

Building a Clojure app with a command-line interface?

Here is an example of using its with-command-line macro. The following code specifies a trivial class with a main method that does nothing but print out the values of its command line arguments. (ns cmd-line-demo (:gen-class) (:use clojure.contrib.command-line)) (defn -main [& args] (with-command-line args “Command line demo” [[foo “This is the description for foo” 1] … Read more

How many primitives does it take to build a LISP machine? Ten, seven or five?

Basic Predicates/F-functions McCarthy’s Elementary S-functions and Predicates were: atom Which was necessary because car and cdr are defined for lists only, which means you cannot count on any sort of answer to indicate what was happening if you gave car an atom. eq For testing equality between atoms. car For returning the first half (address) … Read more

Clojure – named arguments

In Clojure 1.2, you can destructure the rest argument just like you would destructure a map. This means you can do named non-positional keyword arguments. Here is an example: user> (defn blah [& {:keys [key1 key2 key3]}] (str key1 key2 key3)) #’user/blah user> (blah :key1 “Hai” :key2 ” there” :key3 10) “Hai there10” user> (blah … Read more

Why does Clojure have 5 ways to define a class instead of just one?

This is a mix of three different factors: The particular type system of the jvm The need for slightly different semantics for different use cases when defining types The fact that some of these were developed earlier, and some later, as the language has evolved. So first, let’s consider what these do. deftype and gen-class … Read more

What is the :: used for in clojure?

The double colon is there to fully qualify keywords with your current namespace. This is intended to avoid name clashes for keywords which are meaningful for different libraries. Without fully qualified keywords you might accidentally overwrite some values in a map and break compatibility with a library.

Is functional programming relevant to web development? [closed]

Functional programming matches web apps very well. The web app recieves a HTTP request and produces a HTML result. This could be considered a function from requests to pages. Compare with desktop apps, where we typically have a long running process, a stateful UI and dataflow in several directions. This is more suited to OO … Read more