Re: Code Review: how can I make this socket code more functional?

2010-02-17 Thread Matt Culbreth
Cool, just what I needed Alan.  Thanks for the help.


On Feb 16, 4:30 pm, Alan Dipert alan.dip...@gmail.com wrote:
 Hi Matt,
 I think what you're looking for is 
 line-seq:http://richhickey.github.com/clojure/clojure.core-api.html#clojure.co...

 In your code, if you pass *in* to line-seq, you'll get back a seq of lines
 (the request headers).  Incidentally, I first ran into line-seq in the
 course of writing a Clojure web server as a first project.  You can see
 line-seq in action on line 52 of this gist, inside the 'handle-request'
 function:http://gist.github.com/203329

 I'm pretty new to Clojure myself so there might be a better way.  Hope
 this helps,

 Alan

 Excerpts from Matt Culbreth's message of 2010-02-16 16:17:57 -0500:



  Hello Group,

  I'm writing a web server in Clojure and I'd love to have a bit of help
  on a function I have.

  This function (and athttp://gist.github.com/305909) is used to read
  the HTTP request from a client and to then act on it:

  (defn handle-request
      [in out]
      (binding [*in* (BufferedReader. (InputStreamReader. in))]
          (let [client-out (OutputStreamWriter. out)]
              (loop [lines []]
                  (let [input (read-line)]
                      (if
                          (= (.length input) 0)
                          ;; 0 length line means it's time to serve the
  resource
                          (println lines)
                          ;; add to the lines vector and keep going
                          ;; note it makes the incoming request reversed
                          (recur (cons input lines

  The code works fine; I've left out the bit that actually does
  something and replaced it with a println call.  It's not very
  functional feeling though.  The loop/recur and the building of a list
  seems very imperative to me.  I'd much rather use something from
  clojure.contrib.io, probably using read-lines or something.

  Thanks for looking and for any suggestions!

  Matt

 --
 Alan Diperthttp://alan.dipert.org

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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


Re: Code Review: how can I make this socket code more functional?

2010-02-17 Thread Matt Culbreth
Thanks Wilson.  I'm actually not using the Java Servlet classes, and
am instead going down to the socket and streams level.

On Feb 16, 5:00 pm, Wilson MacGyver wmacgy...@gmail.com wrote:
 assuming you are doing this using java servlet.

 you may want to look at how compojure does this.

 http://github.com/weavejester/compojure/blob/master/src/compojure/htt...

 it turns the request into a map.

 and then various other parts of compojure can do different things
 based on the map.





 On Tue, Feb 16, 2010 at 4:17 PM, Matt Culbreth mattculbr...@gmail.com wrote:
  Hello Group,

  I'm writing a web server in Clojure and I'd love to have a bit of help
  on a function I have.

  This function (and athttp://gist.github.com/305909) is used to read
  the HTTP request from a client and to then act on it:

  (defn handle-request
     [in out]
     (binding [*in* (BufferedReader. (InputStreamReader. in))]
         (let [client-out (OutputStreamWriter. out)]
             (loop [lines []]
                 (let [input (read-line)]
                     (if
                         (= (.length input) 0)
                         ;; 0 length line means it's time to serve the
  resource
                         (println lines)
                         ;; add to the lines vector and keep going
                         ;; note it makes the incoming request reversed
                         (recur (cons input lines

  The code works fine; I've left out the bit that actually does
  something and replaced it with a println call.  It's not very
  functional feeling though.  The loop/recur and the building of a list
  seems very imperative to me.  I'd much rather use something from
  clojure.contrib.io, probably using read-lines or something.

  Thanks for looking and for any suggestions!

  Matt

  --
  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
  Note that posts from new members are moderated - please be patient with 
  your first post.
  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

 --
 Omnem crede diem tibi diluxisse supremum.

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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


Code Review: how can I make this socket code more functional?

2010-02-16 Thread Matt Culbreth
Hello Group,

I'm writing a web server in Clojure and I'd love to have a bit of help
on a function I have.

This function (and at http://gist.github.com/305909) is used to read
the HTTP request from a client and to then act on it:

(defn handle-request
[in out]
(binding [*in* (BufferedReader. (InputStreamReader. in))]
(let [client-out (OutputStreamWriter. out)]
(loop [lines []]
(let [input (read-line)]
(if
(= (.length input) 0)
;; 0 length line means it's time to serve the
resource
(println lines)
;; add to the lines vector and keep going
;; note it makes the incoming request reversed
(recur (cons input lines

The code works fine; I've left out the bit that actually does
something and replaced it with a println call.  It's not very
functional feeling though.  The loop/recur and the building of a list
seems very imperative to me.  I'd much rather use something from
clojure.contrib.io, probably using read-lines or something.

Thanks for looking and for any suggestions!

Matt

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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


Re: Code Review: how can I make this socket code more functional?

2010-02-16 Thread Alan Dipert
Hi Matt,
I think what you're looking for is line-seq:
http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/line-seq

In your code, if you pass *in* to line-seq, you'll get back a seq of lines
(the request headers).  Incidentally, I first ran into line-seq in the
course of writing a Clojure web server as a first project.  You can see
line-seq in action on line 52 of this gist, inside the 'handle-request'
function: http://gist.github.com/203329

I'm pretty new to Clojure myself so there might be a better way.  Hope
this helps,

Alan

Excerpts from Matt Culbreth's message of 2010-02-16 16:17:57 -0500:
 Hello Group,
 
 I'm writing a web server in Clojure and I'd love to have a bit of help
 on a function I have.
 
 This function (and at http://gist.github.com/305909) is used to read
 the HTTP request from a client and to then act on it:
 
 (defn handle-request
 [in out]
 (binding [*in* (BufferedReader. (InputStreamReader. in))]
 (let [client-out (OutputStreamWriter. out)]
 (loop [lines []]
 (let [input (read-line)]
 (if
 (= (.length input) 0)
 ;; 0 length line means it's time to serve the
 resource
 (println lines)
 ;; add to the lines vector and keep going
 ;; note it makes the incoming request reversed
 (recur (cons input lines
 
 The code works fine; I've left out the bit that actually does
 something and replaced it with a println call.  It's not very
 functional feeling though.  The loop/recur and the building of a list
 seems very imperative to me.  I'd much rather use something from
 clojure.contrib.io, probably using read-lines or something.
 
 Thanks for looking and for any suggestions!
 
 Matt
 
-- 
Alan Dipert
http://alan.dipert.org

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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


Re: Code Review: how can I make this socket code more functional?

2010-02-16 Thread Wilson MacGyver
assuming you are doing this using java servlet.

you may want to look at how compojure does this.

http://github.com/weavejester/compojure/blob/master/src/compojure/http/servlet.clj

it turns the request into a map.

and then various other parts of compojure can do different things
based on the map.

On Tue, Feb 16, 2010 at 4:17 PM, Matt Culbreth mattculbr...@gmail.com wrote:
 Hello Group,

 I'm writing a web server in Clojure and I'd love to have a bit of help
 on a function I have.

 This function (and at http://gist.github.com/305909) is used to read
 the HTTP request from a client and to then act on it:

 (defn handle-request
    [in out]
    (binding [*in* (BufferedReader. (InputStreamReader. in))]
        (let [client-out (OutputStreamWriter. out)]
            (loop [lines []]
                (let [input (read-line)]
                    (if
                        (= (.length input) 0)
                        ;; 0 length line means it's time to serve the
 resource
                        (println lines)
                        ;; add to the lines vector and keep going
                        ;; note it makes the incoming request reversed
                        (recur (cons input lines

 The code works fine; I've left out the bit that actually does
 something and replaced it with a println call.  It's not very
 functional feeling though.  The loop/recur and the building of a list
 seems very imperative to me.  I'd much rather use something from
 clojure.contrib.io, probably using read-lines or something.

 Thanks for looking and for any suggestions!

 Matt

 --
 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
 Note that posts from new members are moderated - please be patient with your 
 first post.
 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



-- 
Omnem crede diem tibi diluxisse supremum.

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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


Re: Code Review: how can I make this socket code more functional?

2010-02-16 Thread e
i'm interested in seeing how this progresses ... like how you'd spawn
handlers asynchronously to service the requests.  was thinking about doing
this exercise myself at some point.

On Tue, Feb 16, 2010 at 4:30 PM, Alan Dipert alan.dip...@gmail.com wrote:

 Hi Matt,
 I think what you're looking for is line-seq:

 http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/line-seq

 In your code, if you pass *in* to line-seq, you'll get back a seq of lines
 (the request headers).  Incidentally, I first ran into line-seq in the
 course of writing a Clojure web server as a first project.  You can see
 line-seq in action on line 52 of this gist, inside the 'handle-request'
 function: http://gist.github.com/203329

 I'm pretty new to Clojure myself so there might be a better way.  Hope
 this helps,

 Alan

 Excerpts from Matt Culbreth's message of 2010-02-16 16:17:57 -0500:
  Hello Group,
 
  I'm writing a web server in Clojure and I'd love to have a bit of help
  on a function I have.
 
  This function (and at http://gist.github.com/305909) is used to read
  the HTTP request from a client and to then act on it:
 
  (defn handle-request
  [in out]
  (binding [*in* (BufferedReader. (InputStreamReader. in))]
  (let [client-out (OutputStreamWriter. out)]
  (loop [lines []]
  (let [input (read-line)]
  (if
  (= (.length input) 0)
  ;; 0 length line means it's time to serve the
  resource
  (println lines)
  ;; add to the lines vector and keep going
  ;; note it makes the incoming request reversed
  (recur (cons input lines
 
  The code works fine; I've left out the bit that actually does
  something and replaced it with a println call.  It's not very
  functional feeling though.  The loop/recur and the building of a list
  seems very imperative to me.  I'd much rather use something from
  clojure.contrib.io, probably using read-lines or something.
 
  Thanks for looking and for any suggestions!
 
  Matt
 
 --
 Alan Dipert
 http://alan.dipert.org

 --
 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
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.comclojure%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en


-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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