Login
7 branches 0 tags
Ben (X13/Arch) Made module test order deterministic 574184c 3 years ago 745 Commits
nujel / stdlib / core / predicates.nuj
;; 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 numeric? [a]
      "Return #t if a is a number"
      [or [int? a] [float? a] [vec? a]]]

[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"
      [>= 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? [a b]
      "High level equality comparator, can also recursively test lists/arrays for equivalence, can be slow."
      [def cur-type [type-of a]]
      [if [not= cur-type [type-of b]]
          #f
          [case cur-type
                [:array [array/equal? a b]]
                [:tree [tree/equal? a b]]
                [:pair [list/equal? a b]]
                [otherwise [= a b]]]]]

[defn inequal? [a b]
      "High level inequality comparator"
      [not [equal? a b]]]

[defn int? [val]
      [= :int [type-of val]]]

[defn float? [val]
      [= :float [type-of val]]]

[defn vec? [val]
      [= :vec [type-of val]]]

[defn bool? [val]
      [= :bool [type-of val]]]

[defn pair? [val]
      [= :pair [type-of val]]]

[defn array? [val]
      [= :array [type-of val]]]

[defn string? [val]
      [= :string [type-of val]]]

[defn symbol? [val]
      [= :symbol [type-of val]]]

[defn object? [val]
      [= :object [type-of val]]]

[defn tree? [val]
      [= :tree [type-of val]]]

[defn keyword? [v]
      [= :keyword [type-of v]]]

[defn macro? [val]
      [= :macro [type-of val]]]

[defn lambda? [val]
      [or [= :lambda [type-of val]]]]

[defn native? [val]
      [= :native-function [type-of val]]]

[defn procedure? [val]
      [or [lambda? val] [native? val]]]

[defn buffer? [v]
      [= :buffer [type-of v]]]

[defn buffer-view? [v]
      [= :buffer-view [type-of v]]]

[defn bytecode-array? [v]
      [= :bytecode-array [type-of v]]]

[defn bytecode-op? [v]
      [= :bytecode-op [type-of v]]]

[defn in-range? [v min max]
      [and [>= v min] [<= v max]]]