Hi everyone,
this is simply question for of course opinionated views of my approach.
I have some user input (nevertheless where it comes from, web form input, 
rich client... whatever).
Single field can be (as usual) "equipped" with couple of validations, 
empty, format, strength (if it is about password) ...etc

So in absence of objects :) my best friends are functions so, when input 
value is validated I need to come out with map like:

{
 :empty "Field empty"
 :format "Format not valid"
 :strength "Weak"
}

So i need "id" of validation (key) which produced message (value)

So, simply, I have vector containing arbitrary set of functions to be 
applied over value and simply reduce over them.
0-arity overload returns validation key of that validator, and 1-arity 
performs actual check.

To avoid excessive code here is "overnaive" example:

(let [fns [(fn ([] :empty) ([x] "x")) (fn ([] :format) ([y] "y"))] val "field 
value"]
  (reduce #(assoc % (%2) (%2 val)) {} fns))


this yields exactly what I need :
{:empty "x", :format "y"}
(real reducing function would check what validator returned and skip or 
assoc particular result into map).

Now, I am not really sure how functional this approach of "self contained" 
function is, where particular 0 arity is used 
to give key. My idea is rooted in fact that e.g. 0-arity + function gives 
neutral element for addition.

Personally, I like it :) but I would like 2nd opinion as I am maybe 
brutally misusing functional programming :)

Thanks! :)

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to