Minus the bad html, you'll want something like this:
(defn make-table
"Make an html table n rows wide from collection col."
[col n]
(let [make-row (fn [row]
(let [cont (map #(str "<td>" % "</td>") row)]
(apply str "<tr>" (conj (vec cont) "</tr>"))))
cont (map make-row (partition n col))]
(apply str "<table>" (conj (vec cont) "</table>"))))
(make-table ["fred" "mary" "sally" "joan"] 2)
On Mon, Feb 16, 2009 at 9:18 PM, Jesse Aldridge <[email protected]>wrote:
>
>
> I'm trying to port some Python code to Clojure. I'm new to functional
> programming and I hit a function that is causing my brain to melt.
> The python function looks something like this:
>
>
> def build_table():
> num_cols = 3
> selected_row = 0
> selected_col = 0
> strings = ["cat", "dog", "", "rabbit", "frog", "elephant",
> "gorilla"]
>
> row, col = 0, 0
> table_contents = "<table cellpadding=30 bgcolor=#6666aa>\n<tr>\n"
> for string in strings:
> if string == "":
> continue
>
> if col >= num_cols:
> row += 1
> col = 0
> table_contents += "</tr>\n<tr>"
>
> def new_cell():
> bg_color = "cfcfdf"
> if(col == selected_col and row == selected_row):
> bg_color = "d0e0ff"
> return "<td bgcolor=#" + bg_color + ">" + \
> "<font color=#0000cc>" + string[0] + \
> "</font>" + string[1:] + "</td>\n"
> table_contents += new_cell()
>
> col += 1
>
> table_contents += "</tr>\n</table>"
> return table_contents
>
> If someone could please demonstrate how to do this in Clojure, I think
> it would greatly help my understanding of the language and functional
> programming in general.
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---