-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f594fa1
commit 2dc3bab
Showing
4 changed files
with
34 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
(ns bean.operators | ||
(:require [bean.errors :as errors])) | ||
|
||
;; This is a hack to work around JS's float weirdnesses. | ||
(defn format-number | ||
[num] | ||
(if (.isInteger js/Number num) num (js/Number (.toFixed num 4)))) | ||
|
||
(defn bean-op-+ [left right] | ||
(if (and (int? left) (int? right)) | ||
(if (and (number? left) (number? right)) | ||
(+ left right) | ||
(errors/type-mismatch-+-op))) | ||
|
||
(defn bean-op-minus [left right] | ||
(if (and (number? left) (number? right)) | ||
(format-number (- left right)) | ||
(errors/type-mismatch-+-op))) | ||
|
||
(defn bean-op-div [left right] | ||
(if (and (number? left) (number? right)) | ||
(if (zero? right) | ||
(errors/divide-by-zero) | ||
(format-number (/ left right))) | ||
(errors/type-mismatch-+-op))) | ||
|
||
(defn bean-op-< [left right] | ||
(if (and (int? left) (int? right)) | ||
(if (and (number? left) (number? right)) | ||
(< left right) | ||
(errors/type-mismatch-<-op))) | ||
|
||
(defn bean-op-> [left right] | ||
(if (and (int? left) (int? right)) | ||
(if (and (number? left) (number? right)) | ||
(> left right) | ||
(errors/type-mismatch->-op))) | ||
|
||
(defn bean-op-= [left right] | ||
(if (and (or (string? left) | ||
(int? left)) | ||
(number? left)) | ||
(or (string? right) | ||
(int? right))) | ||
(number? right))) | ||
(= left right) | ||
(errors/type-mismatch-=-op))) | ||
|
||
(defn bean-op-* [left right] | ||
(if (and (int? left) (int? right)) | ||
(if (and (number? left) (number? right)) | ||
(* left right) | ||
(errors/type-mismatch-*-op))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters