tags:

views:

186

answers:

3

I want to keep a list of normalizing functions for a text. How do I store .toLowercase? I was thinking of something like this:

(def normalizing-functions (list remove-punctuations .toLowerCase))
+3  A: 

It looks like your making a list of functions to apply to something on a regular basis. the java method is not quite a clojure function in this sense though its really easy to wrap it up just like you would if you where going to feed it to the map function.

#(. tolowercase %)
Arthur Ulfeldt
That worked! Is there a documentation for these operations? Thanks.
kunjaan
This is an anomymous function, also sometimes called a lambda.#(. toLowerCase %)) is a reader macro that is shorthand for (fn [x] (.toLowerCase x)).
alanlcode
+1  A: 

Rather than keeping them in a list which you'll have to unpack some way later, it may just be easier to wrap .toLowerCase in a clojure function (edit: using my or Arthur's syntax) and compose it with the functions you're planning to use to normalize your data using comp:

user=> (defn remove-punctuation [st] ...removing puncutation mechanics...)

user=> (defn lower-case [st]
         (.toLowerCase st))

user=> ((comp remove-punctuation lower-case) "HELLO THERE!")
"hello there"

user=> (defn normalize-data [data]
         ((comp remove-punctuation lower-case) data))
Pinochle
Thanks but I cant do that for all of my functions. That is a nice hack anyways.
kunjaan
A: 

The memfn macro will do this in a more readable way.

(def f (memfn toLowerCase))
(f "Hello")

would return "hello". (doc memfn) has the details.

Pat Wallace