application/octet-stream
•
4.74 KB
•
217 lines
;;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;;; This project uses the MIT license, a copy should be included under /LICENSE
;;;
;;; Some predicates that were simpler to do in nujel
(defn last? (a) :inline
"Return #t if a is the last pair in a list"
(nil? (cdr a)))
(defn pos? (a) :inline
"Return #t if a is positive"
(and (number? a)
(>= a 0.0)))
(defn zero-neg? (a) :inline
"Return #t if a is zero or negative"
(<= a 0.0))
(defn neg? (a) :inline
"Returns #t if a is negative"
(< a 0.0))
(defn odd? (a)
"Predicate that returns #t if a is odd"
(= (rem (int a) 2) 1))
(defn even? (a)
"Predicate that returns #t if a is even"
(= (mod/int (int a) 2) 0))
(defn not-zero? (val) :inline
"#t if VAL is not zero"
(not= 0 val))
(defn equal? (val-a val-b)
"Test whether two values are equal
Unlike = this is actually comparing the contents of compound data,
which can be very slow.
val-a: The first argument
val-b: The second argument
Whether the two arguments are equal?"
:cat :predicate
(def cur-type (:type-name val-a))
(if (not= cur-type (:type-name val-b))
#f
(case cur-type
(:array (array/equal? val-a val-b))
(:buffer (buffer/equal? val-a val-b))
(:tree (tree/equal? val-a val-b))
(:pair (list/equal? val-a val-b))
(otherwise (= val-a val-b)))))
(defn not-equal? (val-a val-b)
"Test whether two values are not equal
This is using equal? under the hood, meaning it can be quite slow since
it actually compares the contents of complex data structures.
val-a: The first argument
val-b: The second argument
Whether the two arguments are not equal?"
:cat :predicate
(not (equal? val-a val-b)))
(defn int? (val)
"Test whether val is an integer"
:cat :type-predicate
:inline
(= :int (:type-name val)))
(defn float? (val)
"Test whether val is a floating-point number"
:cat :type-predicate
:inline
(= :float (:type-name val)))
(defn number? (val)
"Test whether val is a number"
:cat :type-predicate
(or (int? val)
(float? val)))
(defn bool? (val)
"Test whether val is a boolean"
:cat :type-predicate
:inline
(= :bool (:type-name val)))
(defn pair? (val)
"Test whether val is a pair"
:cat :type-predicate
:inline
(= :pair (:type-name val)))
(defn array? (val)
"Test whether val is an array"
:cat :type-predicate
:inline
(= :array (:type-name val)))
(defn string? (val)
"Test whether val is a string"
:cat :type-predicate
:inline
(= :string (:type-name val)))
(defn symbol? (val)
"Test whether val is a symbol"
:cat :type-predicate
:inline
(= :symbol (:type-name val)))
(defn environment? (val)
"Test whether val is an environment"
:cat :type-predicate
:inline
(= :environment (:type-name val)))
(defn tree? (val)
"Test whether val is a binary tree"
:cat :type-predicate
:inline
(= :tree (:type-name val)))
(defn map? (val)
"Test whether val is a Map"
:cat :type-predicate
:inline
(= :map (:type-name val)))
(defn collection? (l)
"Test whether val is a collection"
:cat :type-predicate
:inline
(case (:type-name l)
((:pair :array :tree) #t)
(otherwise #f)))
(defn keyword? (v)
"Test whether val is a keyword"
:cat :type-predicate
:inline
(= :keyword (:type-name v)))
(defn macro? (val)
"Test whether val is a macro"
:cat :type-predicate
:inline
(= :macro (:type-name val)))
(defn lambda? (val)
"Test whether val is a function"
:cat :type-predicate
:inline
(or (= :lambda (:type-name val))))
(defn native? (val)
"Test whether val is a native function"
:cat :type-predicate
:inline
(= :native-function (:type-name val)))
(defn buffer? (v)
"Test whether val is a buffer"
:cat :type-predicate
:inline
(= :buffer (:type-name v)))
(defn bytecode-array? (v)
"Test whether val is a bytecode-array"
:cat :type-predicate
:inline
(= :bytecode-array (:type-name v)))
(defn procedure? (val)
"Test whether val is a procedure"
:cat :type-predicate
(or (lambda? val)
(native? val)))
(defn callable? (val)
"Test whether val is callable"
:cat :type-predicate
(or (macro? val)
(lambda? val)
(native? val)))
(defn in-range? (v min max)
(and (>= v min)
(<= v max)))