Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Clojure approach Separation of Concerns?

Tags:

How does Clojure approach Separation of Concerns ? Since code is data, functions can be passed as parameters and used as returns...

And, since there is that principle "Better 1000 functions that work on 1 data structure, than 100 functions on 100 data structures" (or something like that).

I mean, pack everything a map, give it a keyword as key, and that's it ? functions, scalars, collections, everything...

The idea of Separation of Concerns is implemented, in Java, by means of Aspects (aspect oriented programming) and annotations. This is my view of the concept and might be somewhat limited, so don't take it for granted.

What is the right way (idiomatic way) to go about in Clojure, to avoid the WTFs of fellow programmers _

like image 658
Belun Avatar asked Dec 14 '10 17:12

Belun


People also ask

What can you do with Clojure?

Clojure is being used extensively for processing large volumes of data. It is very well suited to data mining/commercial-AI (ie: Runa) and large scale predictions (aka WeatherBill). Clojure's concurrency story really helps in these data heavy domains where parallel processing is simply the only answer.

Is Clojure object oriented?

Clojure is a functional lisp, reportedly not at all object-oriented, even though it runs on the JVM, a VM designed for an object oriented language. Clojure provides identical interfaces for iterating over lists and vectors by abstracting them to an interface called seq.


1 Answers

In a functional language, the best way to handle separation of concerns is to convert any programming problem into a set of transformations on a data structure. For instance, if you write a web app, the overall goal is to take a request and transform it into a response, which can be thought of as simply transforming the request data into response data. (In a non-trivial web app, the starting data would probably include not only the request, but also session and database information) Most programming tasks can be thought of in this way.

Each "concern" would be a function in a "pipeline" that helps make the transform possible. In this way, each function is completely decoupled from the other steps.

Note that this means that your data, as it undergoes these transformations, needs to be rich in its structure. Essentially, we want to put all the "intelligence" of our program into the data, not in the code. In a complicated functional program, the data at the different levels may be complex enough that in needs to look like a programming language in its own right- This is where the idea of "domain-specific languages" comes into play.

Clojure has excellent support for manipulating complex heterogenous data structures, which makes this less cumbersome than it may sound (i.e. it's not cumbersome at all if done right)

In addition, Clojure's support for lazy data structures allows these intermediate data structures to actually be (conceptually) infinite in size, which makes this decoupling possible in most scenarios. See the following paper for info on why having infinite data structures is so valuable in this situation: http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf

This "pipeline" approach can handle 90% of your needs for separating concerns. For the remaining 10% you can use Clojure macros, which, at a high level, can be thought of as a very powerful tool for aspect-oriented programming.

That's how I believe you can best decouple concerns in Clojure- Note that "objects" or "aspects" are not really necessary concepts in this approach.

like image 93
drcode Avatar answered Oct 12 '22 08:10

drcode