Clojure Multi Method Chain with Partial

the code below:

  ((partial sort-by :Name))
  ((partial take items-per-page)))

Simple method chain taking advantage of partial methods. That might
as me.


  (range 20)
  ((partial drop 5))
  ((partial take 5))
  ((partial apply +)))

(Yes I used the word irks) is:

((partial ... )).





applied to it. Weeee!

should want too, is this:

  (range 20)
  (drop 5)
  (take 5)
  (apply +))


between a list, or code.

for the macro.


Don’t cry for them. Not them.

(defmacro |-> [original & items]

That way easy. Time to go home.

(defmacro |-> [original & items]

at expansion time.

Why not just:

(defmacro |-> [original & items]

not the (range 0 20) that it represents.
~original produces:

  (range 0 20)

original (with no ~) produces:


Next step is to figure out how to get this:

  ((partial drop 5))
  ((partial take 5))
  ((partial apply +))

from this:

  (drop 5)
  (take 5)
  (apply +)

done? Cons.

(cons `partial %)


(drop 5)

If “cons” the keyword partial to it I get:

('partial drop 5)

one. However, that doesn’t get what is needed.

((partial drop 5))

So guess what? Cons again.

(cons (cons 'partial %) '())


(('partial %))

converted items…



Now to put it all together:

(defmacro |-> [original & items]

map creates.

Now you do exciting things like:

  (range 0 20)
  (drop 5)
  (take 5)
  (map #(+ 1 %)))


Clojure: How to Chain Array Functions Using Partial


  (take items-per-page


take =    f(int list)     => list
drop =    f(int list)     => list
sort-by = f(keyword list) => list



function drop (numberToDrop listToUse)


drop(5 MathTestScores)
drop(5 EnglishTestScores)
drop(5 BiologyTestScores)


function drop5(list)


You might get something like this:

sum = drop5AndSum(MathTestScores 0)
sum = drop5AndSum(EnglishTestScores sum)
sum = drop5AndSum(BiologyScores sum)


sum = 
    (drop5AndSum EnglishTestScores 
     (drop5AndSum MathTestScores 0)))






partialEnglish = dropAndSum5(EnglishTestScores)
sum = drop5AndSum(MathScores, 0) -> partialEnglish


drop5AndSum = f(list, int) => int

Now look at partialEnglish




mathSum = drop5AndSum(MathTestScores);
englishSum = drop5AndSum(EnglishTestScores);
biologySum = drop5AndSum(BiologyTestScores);



  (take per-page
        (drop (* page 5)


  ((partial sort-by :Name))
  ((partial take items-per-page)))



Google Closure and How to Create a Batch File for Running ClojureBuilder



--jvm_flags="-d32" ^
//This is where all the closure files exist
--namespace="src.base.helper.arrayHelper" ^
--namespace="src.base.helper.constants" ^
--namespace="src.base.helper.domCreation" ^
--namespace="src.base.helper.domHelper" ^
--namespace="" ^
--namespace="src.base.control.gridBuilder" ^
--namespace="src.base.control.pager" ^
--namespace="" ^
--namespace="" ^
--namespace="" ^
--output_mode=compiled ^
//   it works with simple, but not advanced.
--compiler_flags="--compilation_level=ADVANCED_OPTIMIZATIONS" ^
//  jrm/jvm.
rem --jvm_flags="-d32" ^

As you can see, it’s actually not all that complicated.

Ruby, Sinatra, and static content (You know, JavaScript)

Sinatra: A quick look at how to set the default static content folder name to anything other than “public”.

Good news: If you were searching on how to server up static files with Sinatra, you’re in luck.

Bad news: I’m using Ruby…

As I stumble through the use of Sinatra (and to a lesser extent Ruby) one of the first issues I ran into was self loathing, followed by a need to have the server actually find my main JavaScript and CSS files. Well this is the current set up in a really poorly done folder rendition:

- content/script/live
 |-- final.js
- content/style/css
 |-- final.css
- view
 |-- main.html
- base.rb

Pretty simple.

At first main.html had what I thought would be the correct link to both:

  <link rel="stylesheet" href="content/style/css/final.css" type="text/css"/>
  <script type="text/javascript" src="content/script/live/final.js"></script>

Turns out, that wasn’t right. After a few attempts to get the server to actually do what it’s supposed to (and there by giving my powers of deduction too much credit), I went a huntin’.

So today I learned that Sinatra by default expects there to be a folder named “public” that would hold the /style, and /script folders. Go figure. Being stubborn as I am, I didn’t want to put them in a parent folder named ‘public’, I wanted it name ‘content’. Don’t hate. Note: the folder has to be on the same level as the file that was used to start the server.

So this is more than possible. Just takes one line it the base file (the one you used to start your server):

require 'sinatra'
require 'json'

set :bind, ''
set :public_folder, { File.join(File.dirname(__FILE__), 'content') } <--- HERE

Yes there are five lines… No the other four aren’t part of this problem… You done?

Ok so it’s now looking at that folder, but there’s still one more step. Remember those links?

  <link rel="stylesheet" href="/style/css/final.css" type="text/css" />
  <script type="text/javascript" src="/script/live/final.js"></script>

Just had to remove the content folder mention in the links, and everything is happy. Except me… because it’s Ruby.

Google Closure: An Enter Key Press

How to set up an event handler for when the enter key is pressed withing the Google Closure system.

Real quick thing which was getting the enter key to be handled on press in an input/textbox… though probably other things.,, function(a) {
  if(a.keycode === 13) {

This assumes that 13 is the universal id for the enter key. Not sure if there is a case of browser dependent key codes.

Clojure: Chaining Lamba Expressions that Act Like Partials

Really simple one. Say you have a chain like this:




  (retrieve-value result)
  (#(:username %))



The -> macro


  f() :string
  g(string) : string

I could call them like this:


With the -> operator I can do this:

f() -> g


The # macro:

This is used to shorthand the normal lambda expression syntax.

(fn [a] (:username a))

Is the same as:

#(:username %)


JavaScript DSL Because I’m Tired of Writing If.. If…If…

A little while ago a co-worker had expressed a dream of his to create some kind of language agnostic standard for validation. Without actually listening to what he meant by that, I set forth on doing this for JavaScript. I wanted to Lisp it up, as I am that kind of zealot. The idea was simple, provide a list like structure for describing the validation needs, but not make it a complete cluster *($#. I got the first part, not sure about the second. Essentially I wanted something like this:

 var rules =
      ('is not empty' 'Username is required.')
      ('is not longer than' 7 'Username is too long.'))

Where ‘Username’ is the name of the property needing the validation, and the ‘7’ is the maximum length, and the last part is the error. But, seeing as this was JavaScript, I had to do something a little more rigid.

Now for what the rules would look like:

 var rules =
      ['is not empty', 'Username is required.'],
      ['is not longer than', 7, 'Username is too long.']],
      ['is not empty', 'Name is required.']],
      ['length is between', 4, 6, 'Password is not acceptable.']]];

As you can see, the rules turn out to be very readable. On top of that, since it is only a list, converting a json based array to the rules should be pretty easy. The advantage of that would be the ability for a server response to contain the rules at which time the interpreter would match them to the methods. Thus providing a way to help with consistency of error messages, and constant values like in the password rule above. Something that is a pain in the #@@ some… all of the time.

The structure is fairly simple. [Property name [[validation need][extra parameters][error]…..[validation need][extra parameters][error]]] Essentially every property that is going to be validated can have n number of validation methods “attached” to it. Now one catch is that the object being validated has to have a matching “Property name”. Kind of falls into the “opinionated programming” side of things.

For this example, there will be three fairly generic validation methods. The methods either return null, or an error message. Over simplistic much?

 function isNotEmpty(toCheck, propertyName, error, rest) {
    return toCheck[propertyName] === '' ? error : null;

  function isNotLongerThan(toCheck, propertyName, error, rest) {
    return toCheck[propertyName].length > rest[0] ? error : null;

  function isBetween(toCheck, propertyName, error, rest) {
    var length = toCheck[propertyName].length;
    return (length < rest[0] || length > rest[1]) ? error : null;

And here is an example of how to attach the english to methods:

 var methods = [
    ['is not empty', isNotEmpty],
    ['is not longer than', isNotLongerThan],
    ['length is between', isBetween]];

I created a pseudo method look up table to match a much more “english” name for the validation method needed. Not exactly brilliant, and is sort of cheating. What would be nice is to actually read the english, break it down, and create the functions on the fly. BUT this works for now. That whole english breakdown I’ll attack at some point. After all, this was a sort of thought experiment.

The interpreter takes in the rules, matches the method, and send the extra values if needed. For example,


Is matched to:

  isBetween(toCheck, propertyName, error, rest)

Where “rest” is the 4 and 6. Rest is a carryover from Commmon Lisp’s &rest. It’s a collection of things that will be broken down by the validation method. Just think of them as optional values.

 var length = toCheck[propertyName].length;

There are two parts to the interpreter, but the first part is merely rolling through the rules, and making a call to this: = function(propertyName, methods, innerRule, find, car, cdr, peek, sink) {

 //Find the method that matches the english phrase
    var methodPair = find(methods, function(method) {
        return car(method) === car(innerRule);

 //In Closure the find method returns the whole matched "row", so I need the method which is the second column.
    var methodToUse = peek(methodPair);

    return function(obj) {
        var error = peek(innerRule);                           //error is the last index
        var values = sink(cdr(innerRule));                     //get everything but the error  
        return methodToUse(obj, propertyName, error, values);  //construct the validation call

car, cdr, sink, peek? The *(&^ are those? First two are carry overs from Common Lisp too. Car is just taking the first item of a list, if there is one.

  (car '(1 2 3)) ;;  1

Cdr returns a new list that is everything but the first item.

  (cdr '(1 2 3)) ;; '(2 3)

Haskell refers to them as Head and Tail. Clojure has First, and Rest. ect. ect. ect.

sink is used to get all but the last item in the list. Everything but the kitchen sink. Eh? Eh? Sigh. (The code for them can be seen here.).

peek is actually just a parameter cover for the goog.array.peek method. It gets the last element of an array. Remember, the structure is very specific. [validation need][extra parameters][error] Because of this, it’s easy to break down each rule into its respective parts using simple array manipulation.

Now at this point I only have created the method list creator (“Interpreter”), and haven’t created anything for running the validation. However, the code below is a rough estimate of how that would work.

 var result = interpret(rules, methods);

 var toValidate = {'Username': '12345678', 'Name': '', 'Password': '123'};
  for(var i = 0; i < result.length; i++) { something.innerText += '\n' + result[i](toValidate); };

The glaring issue with that is that I’m not removing empty strings. Other than that, it works. The working example is in the example folder for this repository. The removal would be nothing more than using a map -> filter type method chain.

 var validationMesssages =, function(item) { return item(toValidate); };
  return goog.array.filter(validationMesssages, function(message) { return !goog.string.isEmptySafe(message); });

Or something like that… probably not exactly like that unless you’re in with the Closure, or are stuck with Jsomethingry.

Common Lisp Macro to T-Sql Experiment


(select age)

And get:

"SELECT, age"


(select '


(select :user name)



(defmacro select (&rest args)


    -> ("TEST" "ME")


(defmacro select (&rest args)


    -> OOPS


(macroexpand-1 '(select test me))
    -> "TESTME"


(defmacro select (&rest args)
  `(concatenate 'string "SELECT "


(macroexpand-1 '(select age))

And there it is. Can’t get much better than that.

Common Lisp and Reader Macros… You Better Sit Down For This




And create this code:



  (defmacro ?> (conversion &rest args)






  #'(lambda (stream char1 char2)










        -> 15

Euler 3 Just Got Lisped Up



600851475143 / n = x where n 2 -> ?
600851475143 / 2 = x
600851475143 / 3 = x
600851475143 / 4 = x


    (/ start current)



  (if (= start currentDivisor)
      (if (= 0 (mod start currentDivisor))
