Skip to content

Some asserts from the Functions section (and one typo) #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/cljlab/flowcontrol.clj
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
;; **
;;; ## `when`
;;;
;;; `when` is a one-armed `if`. It checks a condition and then evaluates any number of statements as a body (so no `do` is required). The value of the last expression is returned. If the condition is false, nil is returned.
;;; `when` is a one-armed `if`. It checks a condition and then evaluates any number of expressions as a body (so no `do` is required). The value of the last expression is returned. If the condition is false, nil is returned.
;;;
;;; `when` communicates to a reader that there is no "else" branch.
;; **
Expand Down Expand Up @@ -514,15 +514,15 @@

;; **
;;; ## Euclid's algorithm
;;; [Euclid's algorithm](http://en.wikipedia.org/wiki/Euclidean_algorithm) finds the greatest common divisor of two integers using only substraction. In imperative pseudo-code, it looks like this:
;;; [Euclid's algorithm](http://en.wikipedia.org/wiki/Euclidean_algorithm) finds the greatest common divisor of two integers using only subtraction. In imperative pseudo-code, it looks like this:
;;;
;;; ```
;;; function gcd(A, B):
;;; do loop:
;;; if A == 0
;;; return B
;;; if B == 0
;;; return B
;;; return A
;;; if A > B
;;; A := A - B
;;; else
Expand Down
19 changes: 18 additions & 1 deletion src/cljlab/functions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ vector

;; @@
(defn do-nothing [x] ___)

(assert (= 42 (do-nothing 42)))
(assert (= "wtf"(do-nothing "wtf")))
;; @@

;; **
Expand All @@ -404,6 +407,10 @@ vector

;; @@
(defn always-thing [__] ___)

(assert (= :thing (always-thing)))
(assert (= :thing (always-thing 42)))
(assert (= :thing (always-thing "wtf" 42)))
;; @@

;; **
Expand Down Expand Up @@ -515,6 +522,8 @@ vector
;; @@
(defn http-get [url]
___)

(assert (.contains (http-get "http://www.w3.org") "html"))
;; @@

;; **
Expand All @@ -531,6 +540,9 @@ vector
;; @@
(defn one-less-arg [f x]
(fn [& args] ___))

(defn add-two-numbers [x y] (+ x y))
(assert (= 3 ((one-less-arg add-two-numbers 1) 2)))
;; @@

;; **
Expand All @@ -546,8 +558,13 @@ vector
;; @@
(defn two-fns [f g]
___)
;; @@

(defn add-two [x] (+ 2 x))
(defn times-ten [x] (* 10 x))

(assert (= 12 ((two-fns add-two times-ten) 1)))
(assert (= 30 ((two-fns times-ten add-two) 1)))
;; @@
;; **
;;; # Lab Solutions
;;;
Expand Down
2 changes: 1 addition & 1 deletion src/cljlab/polymorphism.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
;; **
;;; ## Motivation
;;;
;;; There are many situations where it is useful to provide behavior that varies based on type or value. Clojure provides two different mechanisms to for conditional (polymorphic) behavior: protocols and multimethods.
;;; There are many situations where it is useful to provide behavior that varies based on type or value. Clojure provides two different mechanisms for conditional (polymorphic) behavior: protocols and multimethods.
;;;
;;; Also, Clojure provides two constructs that allow you to create new data "types": records (which we saw in collections earlier) and types.
;;;
Expand Down