Files
honore-beaugrand/Cours 4.ipynb
T
2018-09-17 18:08:24 -04:00

8.7 KiB

Classo numero quatro

Some small remarks

Note on immutability

In [2]:
(def lst [1 2 3 4 5 6 7 8])

(defn times-2 [n]
    (* 2 n))

(def lst-2 (map times-2 lst))
(print lst)
[1 2 3 4 5 6 7 8]

Note on lazyness

In [3]:
(def all-numbers (range))
(def all-even-numbers (map times-2 all-numbers))
(take 10 all-even-numbers)
Out [3]:
(0 2 4 6 8 10 12 14 16 18)

Note on comments

In [6]:
(def persen {:name "steph"}) ;:age 34
(def foo 2) ;; this is a comment.  We call it a "line comment"

(comment (println "This is what we call a block comment, rarely used"))


#_(println "This is another type of block comment, more often used.  (used by david nolen)")

Hyperfiddle

This is the coolest thing ever! http://www.hyperfiddle.net/

if and when

Sometime, we only want to do "stuff" when something happen. This is moment to use if and when

In [8]:
(defn print-odd-or-even [n]
    (if (odd? n)
        (println n " is odd")
        (println n " is even")))

(defn custom-hello [gender]
    (if (= gender :male)
        "his"
        (if (= gender :female)
            "her"
            "hex")))

(defn custom-hello-2 [gender]
    (when (= gender :male)
     (println  "his")))
    


(print-odd-or-even 99)
99  is odd
In [1]:
;;  the bellow line "imports" the functions required to display html
;;  in the jupyter notebook
(require '[clojupyter.misc.display :as display]) ;; https://github.com/clojupyter/clojupyter
In [2]:
(display/html "<h3 style='color:yellow'>We can very easely print stuff from code</h3>") 
Out [2]:

We can very easely print stuff from code

In [4]:

(defn wrap-html [title tag]
    "Wraps a 'title' in a html 'tag'.  this function
    main goal is to show how hard and boring it is to
    surround a simple text by html tags programatically."
    (str "<" tag ">" title "</" tag))

(defn html-h1 [title]
    "wraps title in html h1 tags"
    (wrap-html title "b"))

(display/html (html-h1 "borris"))
Out [4]:
borris

James Reeves made a fantastic, database, library for html called hiccup.

In [6]:
;;  Hiccup is THE way to create html content from clojure
(display/hiccup-html [:h3 "hiccup is much better"])
Out [6]:

hiccup is much better

In [7]:
(defn hiccup-h1 [title]
    [:h1 title])
(display/hiccup-html (hiccup-h1 "ok"))
Out [7]:

ok

Dodgeball data

In [8]:
(def dodgeball-data
    [{:team-1 :drummonville :team-2 :montreal :points-1 4 :points-2 5}
     {:team-1 :quebec :team-2 :montreal :points-1 12 :points-2 2}
     {:team-1 :gatineau :team-2 :montreal :points-1 7 :points-2 5}
     {:team-1 :amos :team-2 :montreal :points-1 1 :points-2 12}])
     
                        
                        
Out [8]:
#'user/dodgeball-data
In [10]:
(def bold {:style "font-weight: bold;"})
(def not-bold nil)

(defn team-1-style [game]
    (if (> (game :points-1) (game :points-2))
     bold
     not-bold))

(defn team-2-style [game]
    (if (< (game :points-1) (game :points-2))
     bold
     not-bold))



(defn create-row [game]
    [:tr [:td (team-1-style game ) (:team-1 game)]
         [:td (:points-1 game)]
         [:td (team-2-style game ) (:team-2 game)]
         [:td (:points-2 game)]])

(defn summary-table [games]
    [:table
     [:thead
      [:tr [:td "Ville"]
           [:td "Resultat"]
           [:td "Ville"]
           [:td "Resultat"]]]
     (map create-row games)])

(display/hiccup-html (summary-table dodgeball-data))
Out [10]:
VilleResultatVilleResultat
drummonville4montreal5
quebec12montreal2
gatineau7montreal5
amos1montreal12
In [ ]: