Skip to content

Instantly share code, notes, and snippets.

@practicalli-johnny
Last active May 2, 2019 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save practicalli-johnny/28fd518eeb652b1ebf561d2ef480669e to your computer and use it in GitHub Desktop.
Save practicalli-johnny/28fd518eeb652b1ebf561d2ef480669e to your computer and use it in GitHub Desktop.
The Learning path for Maria.cloud
;; # Colours and Shapes in Clojure
;; Clojure is a functional programming language. The behaviour of your code is expressed by calling one or more functions.
;; Each function has a particular thing it does when you call it. We will discover functions that:
;; * draw particular shapes
;; * add a colour to a shape
;; * help you combine shapes
;; ## Calling a function
;; A function is called by putting its name within a `list`. A `list` is represented by an open and close round bracket: `()`
;; We have create several functions that will draw a shape for you in this web page. You just need to call the function and tell it how big a shape you want.
;; For example, when we call the `circle` function, we need to tell it what size of circle to draw.
;; Put the cursor at the end of the code below (in the white box) and press `Ctrl+Enter` on a PC or `Cmd+Enter` on a Mac
(circle 25)
;; #### Exercise: Create a circle of size 50 (or any other size you like)
()
;; ## Arguments
;; A function can take zero or more arguments. Arguments affect the behaviour of a function.
;; So if you give a bigger number as an argument to the `circle` function, then a bigger circle is drawn. The argument represents the radius of the circle.
;; What happens if we give `circle` more that one argument
(circle)
(circle 20 30)
;; Arguments must match the same number as the argument takes
;; Different functions can take a different number of arguments
(rectangle 20 10)
;; ## Functions return a value
;; func returns a value
(+ 1 2 3 4 5)
;; because functions take a value, then you can use func as an argument
(circle (* 5 5))
;; Make a rectangle that is half high as it is wide
(rectangle 40 (/ 40 2))
;; Adding Colours
;; We can give our shapes colours by using the `colorize` function
;; A colour is a string
(colorize "green" (circle 25))
(colorize "blue" (circle (* 5 5)))
;; Creating a rectangle
(rectangle 250 100)
;; Now colourise the rectangle
(colorize "blue" (rectangle 250 100))
;; # Collections
;; So far we have seen simple values, such as 1, "blue", ....
["red" "orange" "yellow" "green" "blue" "indigo" "violet"]
;; rather than type it out all over gain, give it a name
(def rainbow ["red" "orange" "yellow" "green" "blue" "indigo" "violet"])
rainbow
;; Periodic table
;; We can group simple values together in collections.
;; We can group collections of collections in collections...
(def periodic-table-of-elements
[["gold" "Au"] ["silver" "Ag"] ["platinum" "Pt"] ["palladium" "Pd"] ["Tennessine" "Ts"]])
;; ## What colours are available
color-names
;; so how did we get the coloured shapes in the collection of colours?
;; Calling a function returns a value, so we can also include a function call in a collection.
;; Lets see this by defining our favourite colours
;; simple representation
(def my-fav-colours-simple ["blue" "green"])
my-fav-colours-simple
;; We also want to see what the colour looks like, so we can include a function that will draw a shape with that particular colour
(def my-fav-colours
[["blue" (colorize "blue" (square 25))]
["green" (colorize "green" (square 25))]])
my-fav-colours
;; To avoid duplication then we can use a function to iterate over a collection
(mapv (fn [color-name]
[color-name (colorize color-name (square 25))])
my-fav-colours-simple)
;; ## filter
;; a special case of map that only returns values that match a certain criteria..
;; Clojure has a function called odd? that will tell you if a number is odd or even. It returns true if the number is odd and false if it is not.
(odd? 1)
;; Odd? only takes one value, so if we want to process lots of values in a collection, we need to use another function with odd.
(filter odd? [1 2 3 4 5 6 7 8 9])
(filter even? [1 2 3 4 5 6 7 8 9])
;; adding to our fav colours
(def more-fav-colours-simple ["blue" "lightskyblue" "darkslateblue" "midnightblue"
"green" "springgreen"])
;; Here is a helper function that will determine if a colour is a kind of blue, if you evalute this funciton, then we can use it in our filter
(defn is-blue? [color]
(clojure.string/includes? color "blue"))
(filter is-blue? more-fav-colours-simple)
;; There is a handy function we have written for you called
(colors-named "green")
;; # Defining functions
;; Defining functions allows you to create your own specific behaviour.
;; We have seen a function definition with the `is-blue?` function, so lets have a look at this in more detail
;; #### nice picture of the syntax of a function
;; ## fix bugs using structural editing
;; given a piece of code, can you spot the bug and fix it using structural editing (barf, slurp, raise)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment