(ns html-table
  (:use clojure.contrib.prxml)
  (:use clojure.contrib.seq-utils))

(defn print-table [grid selected]
  (prxml
    [:table {:cellpadding 30, :bgcolor "#6666aa"}
      (for [[x row] (indexed grid)]
        [:tr
          (for [[y cell] (indexed row)]
            [:td {:bgcolor (if (= selected [x y])
                             "#d0e0ff"
                             "#cfcfdf")}
              [:font {:color "#0000cc"} (subs cell 0 1)]
              (subs cell 1)])])]))

(defn make-grid [col-size data]
  (partition col-size
    (filter #(not= % "") data)))

(print-table
  (make-grid 3 ["cat" "dog" "" "rabbit" "frog" "elephant" "gorilla"])
  [0 0])

(println)

On Feb 17, 2:18 am, Jesse Aldridge <jessealdri...@gmail.com> 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 clojure@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to