Re: User input validations, design doubt

2017-02-10 Thread Rastko Soskic
Hey, thanks, I like your idea of utilizing closure as poor's man object (or 
vice versa if you like :) ) to encapsulate 
particular validators.

However, one note is that I was opting for a more dynamism/flexibility like 
simply associating field 
with set of arbitrary composed set of validators. But of course given 
re-occurring pattern for multiple
fields composing them as you suggest is next logical step.

And... about performance...that is not an issue currently at all :)

Good tips, thanks!

On Thursday, February 9, 2017 at 7:01:36 AM UTC+1, Herwig Hochleitner wrote:
>
> Of course, for performance you can optimize that apply+hash-map+interleave 
> (or even your reduce) into reduce+transients, e.g. by using map-vals 
> 
> .​
>

-- 
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.


Re: User input validations, design doubt

2017-02-08 Thread Herwig Hochleitner
Of course, for performance you can optimize that apply+hash-map+interleave
(or even your reduce) into reduce+transients, e.g. by using map-vals

.​

-- 
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.


Re: User input validations, design doubt

2017-02-08 Thread Herwig Hochleitner
2017-02-08 23:14 GMT+01:00 Rastko Soskic :
> (let [fns [(fn ([] :empty) ([x] "x")) (fn ([] :format) ([y] "y"))] val
> "field value"]
>   (reduce #(assoc % (%2) (%2 val)) {} fns))

The advantage in this approach is that you can get the keys from a set
of validators, but you still need some outside logic to get at the
result (the reduce).

Since in map validators one wouldn't expect to read any mutable state
to get at the keys (which would necessitate a possibly zero-arg fn
call), you could compile the map validator into a closure and keep the
keys as values in metadata:

(defn juxt-vals [& {:as kvs}]
  (with-meta
(let [ks (keys kvs)
  vs (vals kvs)]
  (fn [& args]
(apply hash-map
   (interleave ks (map #(apply % args) vs)
{:validator/map kvs}))

(def ef-validator
  (juxt-vals
:empty (fn [x] "x")
:format (fn [y] "y")))

(ef-validator nil) => {:format "y", :empty "x"}
(meta ef-validator) => #:validator{:map {:format
#function[user/fn--21690], :empty #function[user/fn--21688]}}

-- 
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.


User input validations, design doubt

2017-02-08 Thread Rastko Soskic
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.