Part 8 — A complete beginner’s guide to Computer Programming with Clojure: Functions

Photo by Roman Mager on Unsplash
(+ 5 10)

Arity

(defn emptyfn [ ] (* 10 15))(emptyfn)150
(defn dublz [x] (* x 2))(dublz 10)20
(defn dublz2 [ x y ] ( * 2 x y))(deblz2 2 3)12
(defn dublz3 [x y] ( * 2 x ) ( * 2 y ))(dublz3 2 3)6
(dublz3 3 2)4
(defn dublz4 [ x y ] (println ( * 2 x ) ( * 2 y )))(dublz4 2 3)4 6

Variadic Functions

(defn mult [x & more] ( * x (apply * more)))(mult 3 3 3 3)81(mult 3 3 3 3 4)`324
(defn mult [x & y] ( * x (apply * y)))
(mult 7 8 9)504(* 7 8 9)504
(defn multy [x & more] ( mult x (apply mult more)))(multy 7 8 9)504
(apply * ‘(1 2 3 4 5 6))720
(apply * [1 2 3 4 5 6])720
(apply * [1 2 3 4 5 6])
(* 1 2 3 4 5 6)
(apply str ‘(“Star” “ Trek” “ — the Wrath of Khan”))“Star Trek — the Wrath of Khan”
(apply str [“Star” “ Trek” “ — the Wrath of Khan”])“Star Trek — the Wrath of Khan”
(str “Star” “ Trek” “ — the Wrath of Khan”)“Star Trek — the Wrath of Khan”
(reduce * ‘( 1 2 3 4 5 6))
(* (* (* (* (* 1 2) 3) 4) 5) 6)
(reduce str ‘(“Star” “ Trek” “ — the Wrath of Khan”))
(apply hash-map [:Title “The Caves of Steel” :Author “Isaac Asimov” :Year “1954”])
(hash-map :Title “The Caves of Steel” :Author “Isaac Asimov” :Year “1954”)
{:Title “The Caves of Steel”, :Year “1954”, :Author “Isaac Asimov”}
(reduce hash-map [:Title “The Caves of Steel” :Author “Isaac Asimov” :Year “1954”])
{{{{{:Title “The Caves of Steel”} :Author} “Isaac Asimov”} :Year} “1954”}

Function example

{:TITLE {“The Hobbit” {:AUTHOR “Tolkien”,                       :YEAR 1932                       :ISBN “9780788789823”                       :NOTES “ Prequel to The Lord of the Rings” },         “Star Trek” {:AUTHOR “Roddenberry”},        “Foundation” {:AUTHOR “Asimov”,                      :YEAR 1951}         }}
( def Library ( atom { } ) )
( def Title ( atom nil ) )( def Author ( atom nil ) )( def Isbn ( atom nil ) )( def Notes ( atom nil ) )( def Year ( atom nil ) )
(defn InsertAuth [ x y ](reset! Title x)(reset! Author y))
(defn InsertAuth [ x y ](reset! Title x)(reset! Author y)(swap! Library assoc-in [:TITLE @Title :AUTHOR ] @Author))
(defn AuthFn [ ](swap! Library assoc-in [:TITLE @Title :AUTHOR ] @Author))
(defn InsertAuth [ x y ]
(reset! Title x)(reset! Author y)(AuthFn))

Decision making

( if ( number? 5)( println “true” )( println “false” ) )true
( if ( number? “A”)( println “true” )( println “false” ) )false
(defn YearFn [ ](if (number? @Year )( swap! Library assoc-in [:TITLE @Title :YEAR ] @Year)(println “Please input a number without quotes.\n\n” (reset! Year nil) ) ) )(defn InsertYear [ x y ](reset! Title x)(reset! Year y)(YearFn))
#”^[0–9xX\-]+$”
(defn ISBNFn [ ](if (re-find #”^[0–9xX\-]+$” @Isbn )( swap! Library assoc-in [:TITLE @Title :ISBN ] @Isbn)(println “An ISBN number only has numbers and hyphens and, occasionally, the letter X .\n\n” (reset! Isbn nil) ) ) )(defn InsertISBN [ x y ](reset! Title x)(reset! Isbn y)(ISBNFn))
(count “12345”)5(count “12 45”)5
(if (< (count @Notes) 140)(println “True”)(println “False”))
( defn NotesFn [ ]( if (< ( count @Notes) 140 )( swap! Library assoc-in [:TITLE @Title :NOTES ] @Notes )( println “try again! There is a 140 character limit\n\n” ( reset! Notes nil ))))( defn InsertNotes [ x y ]( reset! Title x )( reset! Notes y )( NotesFn ))
#”^[12][0–9]{3}$”
(defn YearFn [ ](if (re-find #”^[12][0–9]{3}$” @Year )( swap! Library assoc-in [:TITLE @Title :YEAR ] @Year)(println “Please input a four digit year.\n\n” (reset! Year nil) ) ) )(defn InsertYear [ x y ](reset! Title x)(reset! Year y)(YearFn))

Find Functions

(defn FindBook [ x ](if (nil? (get-in @Library [:TITLE x ]))(println “Book not Found”)(println “The title “x “ has the following data stored “(get-in @Library [:TITLE x ]) )))(defn FindAuthor [ x ](if (nil? (get-in @Library [:TITLE x :AUTHOR]))(println “Nothing Found”)(get-in @Library [:TITLE x :AUTHOR])))(defn FindYear [ x ](if (nil? (get-in @Library [:TITLE x :YEAR]))(println “Nothing Found”)(get-in @Library [:TITLE x :YEAR])))(defn FindISBN [ x ](if (nil? (get-in @Library [:TITLE x :ISBN]))(println “Nothing Found”)(get-in @Library [:TITLE x :ISBN])))(defn FindNotes [ x ](if (nil? (get-in @Library [:TITLE x :NOTES]))(println “Nothing Found”)(get-in @Library [:TITLE x :NOTES])))

Delete Functions

(defn DelAuth [ x ](if (nil? (get-in @Library [:TITLE x :AUTHOR]))(println “Nothing to Delete”)(swap! Library update-in [:TITLE x ] dissoc :AUTHOR)))(defn DelYear [ x ](if (nil? (get-in @Library [:TITLE x :YEAR]))(println “Nothing to Delete”)(swap! Library update-in [:TITLE x ] dissoc :YEAR)))(defn DelISBN [ x ](if (nil? (get-in @Library [:TITLE x :ISBN]))(println “Nothing to Delete”)(swap! Library update-in [:TITLE x ] dissoc :ISBN)))(defn DelNotes [ x ](if (nil? (get-in @Library [:TITLE x :NOTES]))(println “Nothing to Delete”)(swap! Library update-in [:TITLE x ] dissoc :NOTES)))(defn DelBook [ x ](if (nil? (get-in @Library [:TITLE x ]))(println “Nothing to Delete”)(swap! Library update-in [:TITLE ] dissoc x)))

Saving the database as a file

(require ‘[clojure.java.io :as io])(require ‘[clojure.edn :as edn])
{:TITLE {“The Hobbit” {:AUTHOR “Tolkein”, 
:YEAR “1932”,
:ISBN “1932454–5644354–3234-X”,
:NOTES “Lorem ipsum …”},
“The Day of the Triffids” {:AUTHOR “Wyndham”}
}
}
(spit “Library.txt” (pr-str @Library))
user=> exitBye for now!$ lein repluser=>
(require ‘[clojure.java.io :as io])(require ‘[clojure.edn :as edn])
( def Library ( atom { } ) )
(reset! Library (edn/read-string (slurp “Library.txt”)))
user=> (pprint @Library){:TITLE{“The Hobbit”{:AUTHOR “Tolkein”,:YEAR “1932”,:ISBN “1932454–5644354–3234-X”,:NOTES “Lorem ipsum …”},“The Day of the Triffids” {:AUTHOR “Wyndham”}}}niluser=>

SUMMARY

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store