Clojure looping and side effects and correct way to use for loop

One thing to remember about for in Clojure is its lazy and not supposed to be used to make side effects. Forget this may cause subtle bugs that hard to figure out.

Here is an example

How to copy range of PDF pages from pdf file with Clojure

Sometimes I want extract some pages from a pdf file, for example get out particular chapter from a book to focus on this chapter. Most PDF read software don't allow save part of the PDF as a new PDF. It should be called export PDF or save as, but allow the user to specify the page number range.

Split directory and file name from a path in Clojure

Suppose we have a full path string of a file like this

We want to split it to two parts: directory path and file name

Y combinator in Clojure

There are two types of Y combinator: lazy and eager. If you look at Wikipedia, you will see something like this

If we translate it to Clojure, it will looks like this

Using H2 in memory database in Clojure

drivers jar for Clojure] H2 database is a JAVA library implemented a SQL database engine ... in here is H2 database + Clojure + Clojure JDBC driver. Go to the h2 database site and get the jar ... this to project.clj [org.clojure/java.jdbc "0.3.6"] [Connect, CRUD in H2 database in Clojure] First ... to do the thing. You will find its a good choice for the job. [Prepare H2 database and jdbc

Get jar file location in Clojure

To get the jar file location in which a type is defined. We use a function like this

The parameter is a type, then we use a series of Java methods call to get the jar file location in which the type is defined. The equivalent Java code will be

Whats the difference between alter and commute in Clojure Ref type

The alter and commute are two ways to update a Ref type in a trasaction in Clojure. This post Clojure concurrency: Ref type and coordinate change example demonstrate how to use Ref type in a classic way.

The alter is pretty clear and easy to understand, it do the update operation based on the view of the world at the start of the transaction, this world is so called snapshot, the update operation itself has no contention, you can think of the operation itself is done in a way that the current thread is the only one which do the change. Thats STM all about, no lock, no race condition.

The alter is pretty clear and easy to understand, it do the update operation based on the view of the world at the .

Clojure concurrency: Ref type and coordinate change example

Clojure concurrency programming facilities provides four reference types: Ref, Agent, Atom, Var. Ref is designed for coordinate changes. Coordinate means two or more changes must happen as a whole or nothing should happen. The classic example is the transfer between bank account.

Lets define two bank account :

Clojure example: how to generate string of specified number of spaces

When printing a tree data structure, it will be great we can print each level of node in different indentation. Just like what print_r do in PHP.

If you output a tree like data structure in Clojure REPL, it will be printed in one line.

Clojure sequence, sequential and seq

The sequence is another name of cons cell of LISP in Clojure. But its an abstraction, not an implementation. So what we mean when we say something is sequence , something is not? Does it mean anything that implemented that abstraction is sequence, other wise not?

Previous Page 1 ... 4 5 6 Next Page