average
function box
A list consists of:
Example:
(+ 3 5)
You may edit the code to try other numbers and operators. Press CTRL-ENTER to evaluate the code.
Here are your arithmetic functions:
+
, -
, *
, /
,
quot
, and rem
()
All of these except the last two take multiple arguments. The last two are for integer division and remainder.
Translate 3 + 4 * 5
to ClojureScript:
(+ 3 (* 4 5))
How about 3 * 4 + 5
?
()
Photo Credit: Angela P
x = 5
y = x · 3
Photo Credit: Angela P
x = 5
y = x · 3
x = 6
Here is the model:
(def symbol value)
(def x 27)
In ClojureScript, you bind a symbol to a value.
In other languages, variables do not work as in algebra. Instead of binding a symbol to a value, a variable refers to a location in memory that can be updated at any time.
The model:
(def symbol (fn [parameters] function-body))
(def average (fn [a b] (/ (+ a b) 2.0)))
(def cube (fn [n] (* n n n)))
What if I wanted to do the Pythagorean distance formula? I would need to do a square root...
(sqrt (+ (* 3 3) (* 4 4)))
What if I wanted to do the Pythagorean distance formula? I would need to do a square root...
(sqrt (+ (* 3 3) (* 4 4)))
Task | ClojureScript | Example |
---|---|---|
Call a method | (.method js/object argument ...) |
(.max js/Math 3 5 7 2) |
Get a property’s value | (.-property js/object) |
(.-title js/document) |
Create a new object | (js/object.) |
(js/Date.) |
Set a property’s value | (set! (.-property js/object) value) |
(set! (.-title js/document) "New Title") |
Try these!
()
(def square (fn [x] (* x x))) ⇒ (defn square [x] (* x x))
(.sqrt js/Math 5) ⇒ (js/Math.sqrt 5) (.parseFloat js/window "12.3") ⇒ (js/window.parseFloat "12.3") ⇒ (js/parseFloat "12.3")
()
(def distance (fn [a b]
(js/Math.sqrt (+ (* a a) (* b b)))))
<p> a = <input type="text" id="a" size="5"/> b = <input type="text" id="b" size="5"/> c = <span id="c"></span> </p> <p> <input type="button" value="Calculate" id="calculate"/> </p>
Which looks like this...
a = b = c =
Let’s go to a real development environment to complete this example.
(js/document.getElementById "some-id")
(.-value element)
(set! (.-innerHTML element) "content")
Here is the formula for monthly payment on a loan:
We want a function that takes:
It would be convenient to have temporary variables for:
let
(defn payment [principal apr years]
(let [rate (/ (/ apr 100) 12) ; convert to monthly decimal
n (* years 12) ; convert to months
sub-formula (js/Math.pow (+ 1 rate) n)]
(* principal (/ (* rate sub-formula) (- sub-formula 1)))))
(payment 5000 7.25 10)
if
The model:
(if condition true-expr false-expr)
Conditional functions:
=
, not=
, <
,
>
, <=
, >=
Conditional operator functions: and
, or
, not
(defn tester [condition]
(if condition "yes" "no"))
(tester (< 3 5))
cond
(defn test-cond [qty]
(let [price 3.95
subtotal (* price qty)]
(cond
(< qty 10) subtotal
(< qty 20) (* subtotal .90)
(< qty 50) (* subtotal .85)
:else (* subtotal .75))))
(test-cond 10)
(list 1 2 (- 8 5))
first
returns the first item in the collectionrest
returns a sequence of all the items except the first onelast
returns the last item in the collectioncount
gives you the number of items in the collectionlist?
returns true if its argument is a list, false otherwiseconj
takes a collection and an item; returns a new collection with that item addednth
takes a collection and an index, and returns the item at that index(def data (list 10 11 12 13 14))
(first data)
[1 2 3]
(vector 1 2 3)
(vec (list 1 2 3))
(into [] (list 1 2 3))
[1 2 (- 8 5)]
first
returns the first item in the collectionrest
returns a sequence of all the items except the first onelast
returns the last item in the collectioncount
gives you the number of items in the collectionvector?
returns true if its argument is a vector, false otherwiseconj
takes a collection and an item; returns a new collection with that item addednth
takes a collection and an index, and returns the item at that index(def data [10 11 12 13 14])
(first data)
Consider this code to calculate a 10% discount on a price:
(defn discount [price]
(* price 0.90))
With this vector of prices:
(def price-vector [3.95 6.80 2.49 5.33 1.99])
map
functionThe map
function applies a function to each element of a list,
generating a new sequence of values:
(defn discount [price]
(* price 0.90))
(def price-vector [3.95 6.80 2.49 5.33 1.99])
(map discount price-vector)
map
1map
2map
3map
4map
5map
6(defn discount [price]
(* price 0.90))
(def price-vector [3.95 6.80 2.49 5.33 1.99])(map discount price-vector)
(def price-vector [3.95 6.80 2.49 5.33 1.99])
(for [price price-vector]
(* price 0.90))
Given a vector of numbers:
[2 3 5]
How do I get the sum of the squares of the numbers?
reduce
function(defn sumsq [result value]
(+ result (* value value)))
(reduce sumsq 0 [2 3 5])
reduce
(defn sumsq [result value]
(+ result (* value value)))
accumulated result of reduction
individual value from collection
reduce
1reduce
2reduce
3reduce
4Develop a web page to calculate mean and standard deviation.
map
function(def capitals {:paris "France" :london "England" :washington-dc "USA"})
(get map key)
(key map)
if key is a symbol(keys map)