Live data from Hacker News

Developing and Deploying a Simple Clojure Web Application

mmcgrana.github.com

31–40 of 41 posts

Re: Developing and Deploying a Simple Clojure Web Application

#31
post #23

In Common Lisp ( without cheating or handwaving; this is it ): (defpackage :add-nums (:use :cl :hunchentoot :cl-who)) (in-package :add-nums) (defmacro with-html (&body body) `(with-html-output-to-string (*standard-output* nil :prologue t :indent t) ,@body)) (define-easy-handler (add-nums :uri "/add-nums") ((a :parameter-type 'integer) (b :parameter-type 'integer)) (with-html (:html (:head (:title "Add two numbers"))…

But you are cheating:

* you've omitted code to control the output content-type and the associated response headers (xhtml vs html)

* you've removed text, formatting from the form page

* you've discarded the results page altogether

* you aren't properly distinguishing between GET and POST

Do the same to the clojure code and it'll be about the same length as well.

Re: Developing and Deploying a Simple Clojure Web Application

#32
post #23

In Common Lisp ( without cheating or handwaving; this is it ): (defpackage :add-nums (:use :cl :hunchentoot :cl-who)) (in-package :add-nums) (defmacro with-html (&body body) `(with-html-output-to-string (*standard-output* nil :prologue t :indent t) ,@body)) (define-easy-handler (add-nums :uri "/add-nums") ((a :parameter-type 'integer) (b :parameter-type 'integer)) (with-html (:html (:head (:title "Add two numbers"))…

But you are cheating: * you've omitted code to control the output content-type and the associated response headers (xhtml vs html) * you've removed text, formatting from the form page * you've discarded the results page altogether * you aren't properly distinguishing between GET and POST Do the same to the clojure code and it'll be about the same length as well.

Your requests, when they're valid, are embarrassingly simple one-liners.

you've omitted code to control the output content-type and the associated response headers (xhtml vs html)

Add this single line to your WITH-HTML macro.

  (setf (html-mode) :xml)  ; for XML or :sgml for HTML.
http://weitz.de/cl-who/#html-mode

you've removed text, formatting from the form page

Add one line for a CSS file inclusion and style it to your heart's content. In real web apps I use html-template to template the code, and do not sloppily generate html like I did with the example; but I had to do what the clojure code did, in the same manner.

you've discarded the results page altogether

I am printing the results on the same page. Yeah, that's a better usability.

you aren't properly distinguishing between GET and POST

Says who? the DEFINE-EASY-HANDLER takes arguments, which correspond to the submitted form elements, via GET or POST. In fact, that macro also does some type conversion for you as well; since you declared your parameters to be integers they will be converted to integers when you get them, and not remain strings. This stuff is built into the web server.

http://weitz.de/hunchentoot/#define-easy-handler

If you're gonna implement a full REST then you have no business generating your display html from within your container.

What else do you think I omitted?

Re: Developing and Deploying a Simple Clojure Web Application

#33
post #32

Earlier quoted context omitted.

But you are cheating: * you've omitted code to control the output content-type and the associated response headers (xhtml vs html) * you've removed text, formatting from the form page * you've discarded the results page altogether * you aren't properly distinguishing between GET and POST Do the same to the clojure code and it'll be about the same length as well.

Your requests, when they're valid, are embarrassingly simple one-liners. you've omitted code to control the output content-type and the associated response headers (xhtml vs html) Add this single line to your WITH-HTML macro. (setf (html-mode) :xml) ; for XML or :sgml for HTML. http://weitz.de/cl-who/#html-mode you've removed text, formatting from the form page Add one line for a CSS file inclusion and style it to yo…

It would have been easier and no less accurate for you to write, "yes, my cl code was shorter only because I omitted a bunch of stuff." If you're going to start a dick-waving contest, at least man-up and write your code so it has the same behavior.

Re: Developing and Deploying a Simple Clojure Web Application

#34
post #24

Earlier quoted context omitted.

Huh. 45 LOC for templating, routing, and form processing and you don't need to configure a different webserver since jetty is production quality. Do you have links to something that takes less effort than what is being illustrated here that can be deployed immediately into production and perform well?

http://pastebin.com/PsecNBvB (To be honest, I think clojure has its purposes, but a one-off calculator page isn't one of them. Use the right tool for the right job and all that.)

What point are you trying to make? That one can write less code if one does less stuff?

Re: Developing and Deploying a Simple Clojure Web Application

#35

If this is an easy way to create a web app that adds two numbers on Clojure, I'd say Clojure is not ready for web development yet.

Huh. 45 LOC for templating, routing, and form processing and you don't need to configure a different webserver since jetty is production quality. Do you have links to something that takes less effort than what is being illustrated here that can be deployed immediately into production and perform well?

why are some people so lazy and short sighted.

"time to make trivial webapp" is NOT a good reason to choose anything.

Re: Developing and Deploying a Simple Clojure Web Application

#36
post #32

Earlier quoted context omitted.

Your requests, when they're valid, are embarrassingly simple one-liners. you've omitted code to control the output content-type and the associated response headers (xhtml vs html) Add this single line to your WITH-HTML macro. (setf (html-mode) :xml) ; for XML or :sgml for HTML. http://weitz.de/cl-who/#html-mode you've removed text, formatting from the form page Add one line for a CSS file inclusion and style it to yo…

It would have been easier and no less accurate for you to write, "yes, my cl code was shorter only because I omitted a bunch of stuff." If you're going to start a dick-waving contest, at least man-up and write your code so it has the same behavior.

Wow!

First sophistry and now this .. sophistication. I think I am out of my intellectual depth here, and you will have to excuse me for bidding you an early adieu!

[Edit:

I see you have only registered a day ago, welcome aboard! But can you please be a little gentle? (specially in sentences containing a 2nd person pronoun.)

Re: Developing and Deploying a Simple Clojure Web Application

#37
post #23

In Common Lisp ( without cheating or handwaving; this is it ): (defpackage :add-nums (:use :cl :hunchentoot :cl-who)) (in-package :add-nums) (defmacro with-html (&body body) `(with-html-output-to-string (*standard-output* nil :prologue t :indent t) ,@body)) (define-easy-handler (add-nums :uri "/add-nums") ((a :parameter-type 'integer) (b :parameter-type 'integer)) (with-html (:html (:head (:title "Add two numbers"))…

The equivalent Clojure code is actually a few lines shorter than your example:

    (ns add-nums
      (:use compojure.core [hiccup core page-helpers] ring.adapter.jetty))

    (defroutes handler
      (GET "/add-nums" [a b]
        (let [a (Integer/parseInt a)
              b (Integer/parseInt b)]
          (html
            [:html
              [:head [:title "Add two numbers"]]
              [:body
                (if (and a b) [:p (+ a b)])
                (form-to [:get "/add-nums"]
                  (text-field :a)
                  (text-field :b)
                  (submit-button))]])))

    (run-jetty handler {:port 8080})
Mark's tutorial code is a little more complete than your example, and doesn't aim for brevity. It also doesn't use any of the form functions from Hiccup (admittedly, they're not as well documented as they could be).

Re: Developing and Deploying a Simple Clojure Web Application

#38
post #26

Earlier quoted context omitted.

Er, Clojure has CL style macros so you can easily get to this level of brevity if that is desired.

The one custom macro I wrote is already in there. The rest is just the web server and cl-who. I have been screaming for ages telling Clojurists to rip CL APIs and implement them in clojure. They're more tasteful and cultured than that entrerprisey crap that java is smearing all over this nice Lisp dialect. The majority of open source clojure code that I have seen looks like Java FFI stubs; you need to tuck those loos…

What's so great about the CL APIs for web applications? Could you provide some examples?

Re: Developing and Deploying a Simple Clojure Web Application

#39
post #24

Earlier quoted context omitted.

http://pastebin.com/PsecNBvB (To be honest, I think clojure has its purposes, but a one-off calculator page isn't one of them. Use the right tool for the right job and all that.)

What point are you trying to make? That one can write less code if one does less stuff?

The parent asked for a link to something that takes less effort, I obliged. I don't think the amount of code is a particularly useful metric in most cases, but I would argue that the PHP+HTML solution is much clearer and easier to deploy, and taking into account everything you wish about such a simple web page (readability, maintainability, cross-server, security, etc.), takes less effort in the end. (I don't have any big issues with jetty or tomcat, since I use one or the other almost daily.) I'm sure there are larger use cases where clojure is simpler than raw PHP, but that's why PHP has frameworks.

Re: Developing and Deploying a Simple Clojure Web Application

#40
post #23

In Common Lisp ( without cheating or handwaving; this is it ): (defpackage :add-nums (:use :cl :hunchentoot :cl-who)) (in-package :add-nums) (defmacro with-html (&body body) `(with-html-output-to-string (*standard-output* nil :prologue t :indent t) ,@body)) (define-easy-handler (add-nums :uri "/add-nums") ((a :parameter-type 'integer) (b :parameter-type 'integer)) (with-html (:html (:head (:title "Add two numbers"))…

The equivalent Clojure code is actually a few lines shorter than your example: (ns add-nums (:use compojure.core [hiccup core page-helpers] ring.adapter.jetty)) (defroutes handler (GET "/add-nums" [a b] (let [a (Integer/parseInt a) b (Integer/parseInt b)] (html [:html [:head [:title "Add two numbers"]] [:body (if (and a b) [:p (+ a b)]) (form-to [:get "/add-nums"] (text-field :a) (text-field :b) (submit-button))]])))…

Finally, an actual programmer's response.

Thank you! It's good to know that verbosity was for the sake pedagogy and completeness, and not necessity.

Post reply on HN