On Mon, Feb 16, 2009 at 9:18 PM, 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 should probably rework your Python logic too, you can make it a lot more
concise and understandable:

def make_table(cells, cols):
        spam = ""
    for i in xrange(0, len(cells), cols):
        spam += "<tr><td>" + "</td><td>".join(cells[i:i+cols]) + "</td></tr>
        return "<table>" + spam + "</table>"

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