Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuebert committed Feb 9, 2024
2 parents 988168c + 19d772c commit 5e932c6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion curriculum/Example Gallery.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@

;; #### Snake!

;; [Move the snake with your arrow keys! 🐍](https://www.maria.cloud/gist/bba60540e714b31dcac02eb7cdf02fbc?eval=true)
;; [Move the snake with your arrow keys! 🐍](https://www.maria.cloud/gist/33d3a88ae35d8b5698c84d1672dd13a9?eval=true)

;; #### Music!

Expand Down
2 changes: 1 addition & 1 deletion editor2/src/main/maria/curriculum/example_gallery.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@

;; #### Snake!

;; [Move the snake with your arrow keys! 🐍](https://www.maria.editor.cloud/gist/bba60540e714b31dcac02eb7cdf02fbc?eval=true)
;; [Move the snake with your arrow keys! 🐍](https://www.maria.editor.cloud/gist/33d3a88ae35d8b5698c84d1672dd13a9?eval=true)

;; #### Music!

Expand Down
49 changes: 44 additions & 5 deletions shapes/src/shapes/core.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns shapes.core)
(ns shapes.core
(:require [clojure.string :as string]))

;; TODO add spec annotations!
;; TODO re-implement this mess using a transform matrix
Expand Down Expand Up @@ -38,23 +39,57 @@

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; helpers for param checks

(defn type-to-name
"Return a string representation of the type indicated by the symbol `thing`."
[thing]
(cond
(string? thing) "string"
(number? thing) "number"
(list? thing) "list"
(vector? thing) "vector"
(keyword? thing) "keyword"
(map? thing) "map"
(string/includes? (str thing) "function") "function"
:else thing))

(defn assert-number [message x]
(if (js/isNaN x)
(if (nil? x)
(throw (js/Error. message))
(js/parseFloat x)))
(if (js/isNaN x)
(throw (js/Error. message))
(js/parseFloat x))))

(defn assert-number-range [message x-min x-max x]
(let [x-parsed (assert-number message x)]
(if (<= x-min x-parsed x-max)
x-parsed
(throw (js/Error. message)))))

(defn assert-positive [message x]
(let [x-parsed (assert-number message x)]
(if (pos? x)
x-parsed
(throw (js/Error. message)))))

(defn say-why-not-num
;; note: This function still does not gracefully handle functions as bad-input, e.g (circle odd?)
[fname bad-input]
(if (nil? bad-input)
(str " The function `" fname "` can't use `nil`"
" because `nil` is not a number.")
(str " The function `" fname "` can't use '" bad-input
"' because '" bad-input "' is a " (type-to-name bad-input) ".")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; basic types

(defn circle
"Returns a circle of `radius`."
[radius]
(assert-number "radius must be a number!" radius)
(assert-number (str "Argument `radius` has to be a number."
(say-why-not-num "circle" radius)) radius)
(assert-positive (str "Argument `radius` has to be a positive number."
" The function `circle` can't use '" radius
"' because it is not positive.") radius)
(map->Shape {:kind :circle
:r radius
:cx radius
Expand Down Expand Up @@ -92,7 +127,11 @@
(defn square
"Returns a square of dimension `side`."
[side]
(assert-number "side must be a number!" side)
(assert-number (str "Argument `side` has to be a number."
(say-why-not-num "square" side)) side)
(assert-positive (str "Argument `side` has to be a positive number."
" The function `square` can't use '" side
"' because it is not positive.") side)
(rectangle side side))

(defn text
Expand Down

0 comments on commit 5e932c6

Please sign in to comment.