Hello,
I wanted to try it too, so I grabbed Timothy's version, and did some
improvements (I hope they are, feedback welcome ! :-) over it.
Something I corrected is the removal of null or blank items, as in the first
python version.
I also got rid of the assoc by using update-in.
And I felt that there was too much use of apply, but this may be subjective.
Question: In order to use update-in (or assoc in the previous version), one
has to transform the seqs returned by partition into a vector of vectors,
thus the use of 'into.
It still seems a little bit ugly to me. Is there a more concise (if not
idiomatic) way to do that ?
Here is the new version:
(ns html.table)
(defn make-table [column-count selected-row selected-column words]
(let [remove-blank (partial remove #(or (nil? %) (-> % .trim .isEmpty)))
table (into [] (map (partial into []) (partition column-count
(remove-blank words))))
table-with-selected (update-in table [selected-row selected-column]
(partial list :bold))]
(cons :table
(map (fn [r] (cons :tr (map (fn [i] (list :item i)) r)))
table-with-selected))))
(defn test-mt []
(make-table 3 1 0 ["cat" "dog" "rabbit" "cat" "dog" "rabbit" "frog"
"elephant" "gorilla"]))
Regards,
--
Laurent
2009/2/17 Timothy Pratley <[email protected]>
>
> I found this question interesting for two reasons:
>
> (1) A selected item has been specified which needs to be handled
> differently - at first glance suggests an iterative solution.
> (2) There is good support for html in clojure via libraries, so you
> should avoid string concatenation of tags.
>
> So here is my solution:
> http://groups.google.com/group/clojure/web/layout.clj
>
> Notably it bolds the selected item without any iteration, and returns
> a list representation of the html instead of a string. I'm not
> familiar with the html libraries to render this representation so
> forgive any inconsistencies there, but the output is:
>
> (:table (:tr (:item (:bold "cat")) (:item "dog") (:item "")) (:tr
> (:item "rabbit") (:item "frog") (:item "elephant")))
> Which will be validated by the rendering library, giving an additional
> security that it is correct.
>
> In order to deal with the 'selected item' I had to jump through some
> hoops to treat collections as vectors and use assoc on that. Is there
> perhaps a more elegant way? Overall though I hope it is a good example
> of how and why it is a good idea to avoid string catting and instead
> think of the problem in terms of creating a structure.
>
> Really you are building a tree based upon some inputs. I'd be
> interested in other ways the tree could be constructed.
>
>
> Regards,
> Tim.
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---