Conditional operator if on Clojure
-
Hello! At the very beginning of the Clojure study. Please.
I have a clojure-project. I use the hiccup template and Bootstrap style. It works. But I don't understand how the contingent operators work in my case.
Here's my code:
Fail project.clj:
(defproject yupppie "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "ссылка (ссылка словом потому что не позволяет репутация вставлять)" :license {:name "Eclipse Public License" :url "ссылка"} :dependencies [[org.clojure/clojure "1.7.0"] [ring "1.4.0"] [compojure "1.4.0"] [hiccup "1.0.5"]] :plugins [[lein-ring "0.9.7"]] :ring {:handler yupppie.core/app})
Fill yupppie.core:
(ns yupppie.core (:require [compojure.core :refer :all] [compojure.route :as route] [ring.middleware.params :refer [wrap-params]] [clojure.pprint :refer :all] [hiccup.core :refer :all] [hiccup.page :refer [include-css include-js]]))
(defn home []
(html
[:head (include-css "/bootstrap-3.3.6-dist/css/bootstrap.min.css")
(include-css "/styles.css")
(include-css "https://fonts.googleapis.com/icon?family=Material+Icons")
(include-js "/bootstrap-3.3.6-dist/js/bootstrap.min.js")][:body
[:div {:class "col-lg-6"}
[:div {:class "input-group"}
[:input {:type "text" :class "form-control" :placeholder "How old are you?"}
[:span {:class "input-group-btn"}
[:button {:class "btn btn-default" :type=" button"} "Go, baby!"]]]]]]));
(defroutes app
(route/resources "/")
(GET "/" [] (home))
(POST "/saveform" req
(with-out-str (clojure.pprint/pprint (:params req)))))
(def apps
(wrap-params app))
I've been studying it recently, so I don't really understand yet.
I have a simple form. And when you enter the field, the numbers are less than 18 and the button is pressed, one picture should be shown, and when the numbers are 18 or more, another picture. Is that how I use a contingent operator if it's working? Where to put the code and what to write?
-
I'm counting on you to figure out how to send a meaning in shape. I'm guessing that you've figured it out, and in the parameters of the request, you're getting the right meaning.
I'm sayin' that the row with the sent number is available in key parameters.
age
And that's why it could be bundled in Compojure like:(POST "/" [age] ???)
There's a line. We need a number.
You can't invent bikes and pull a function.
as-int
of https://weavejester.github.io/compojure/compojure.coercions.html ♪ It is true that a little change will be needed to describe the method:(POST "/" [age :<< as-int] ???)
Alternatives? They don't need to, https://github.com/weavejester/compojure/blob/master/src/compojure/coercions.clj#L4 Completely simple:
(try (Long/parseLong s) (catch NumberFormatException _ nil))
I tried to write a conversion code myself. The only difference
as-int
UseInteger/parseInt
♪ And it was wrong!Now we're thinking if the laser is old enough, and we'll report it to the template.
(let [old-enough? (and (-> age nil? not) ; если число не распарсилось, придёт nil (< 18 age))] (функция-шаблончик old-enough?))
In the box... well, just take one of the DOM knots.
if
♪(defn функция-шаблончик [old-enough?] (html [:body (if old-enough? "YEAH" "NOPE")]))
Examples, of course, are quite primitive. But you can develop them, right? :