Login
7 branches 0 tags
Ben (Win10) Fixed disassembly of [$apply] 9c49de0 4 years ago 364 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 predica tes that were simpler to do in nujel

[defun numeric? [a]
        "Return #t if a is a number"
        [or [int? a] [float? a] [vec? a]]]

[defun last? [a]
        "Return #t if a is the last pair in a list"
        [nil? [cdr a]]]

[defun pos? [a]
        "Return #t if a is positive"
        [and [numeric? a] [>= [float a] 0.0]]]

[defun zero-neg? [a]
        "Return #t if a is zero or negative"
        [and [numeric? a] [<= [float a] 0.0]]]

[defun neg? [a]
        "Returns #t if a is negative"
        [and [numeric? a] [< [float a] 0.0]]]

[defun odd? [a]
        "Predicate that returns #t if a is odd"
        [== [% [int a] 2] 1]]

[defun even? [a]
        "Predicate that returns #t if a is even"
        [== [mod/int [int a] 2] 0]]

[defun zero? [val]
        "#t if VAL is zero"
        [== 0 val]]

[defun not-zero? [val]
        "#t if VAL is not zero"
        [!= 0 val]]

[defun list-equal? [a b]
        "#t if A and B are equal"
        [when [== [type-of a] [type-of b]]
              [if [pair? a]
                  [and [list-equal? [car a] [car b]]
                      [list-equal? [cdr a] [cdr b]]]
                  [== a b]]]]

[defun there-exists? [l pred]
        "Applies predicate to each element and return #t if it holds true for any element, otherwise #f"
        [cond [[nil? l] #f]
              [[pred [car l]] #t]
              [#t [there-exists? [cdr l] pred]]]]

[defun for-all? [l pred]
        "Applies predicate to each element returns #t if it holds true for every element, otherwise #f"
        [cond [[nil? l] #t]
              [[not [pred [car l]]] #f]
              [#t [for-all? [cdr l] pred]]]]

[defun int? [val]
        "#t if VAL is a integer"
        [== :int [type-of val]]]

[defun float? [val]
        "#t if VAL is a floating-point number"
        [== :float [type-of val]]]

[defun vec? [val]
        "#t if VAL is a vector"
        [== :vec [type-of val]]]

[defun bool? [val]
        "#t if VAL is a boolean"
        [== :bool [type-of val]]]

[defun pair? [val]
        "#t if VAL is a pair"
        [== :pair [type-of val]]]

[defun arr? [val]
	"#t if VAL is an array"
	[== :array [type-of val]]]

[defun string? [val]
        "#t if VAL is a string"
        [== :string [type-of val]]]

[defun symbol? [val]
        "#t if VAL is a symbol"
        [== :symbol [type-of val]]]

[defun object? [val]
        "#t if VAL is an object"
        [== :object [type-of val]]]

[defun tree? [val]
        "#t if VAL is an object"
        [== :tree [type-of val]]]

[defun macro? [val]
        "#t if VAL is an object"
        [== :macro [type-of val]]]

[defun lambda? [val]
        "#t if VAL is a lambda"
        [or [== :lambda [type-of val]] [== :dynamic [type-of val]]]]

[defun native? [val]
        "#t if VAL is a native function"
        [== :native-function [type-of val]]]

[defun special-form? [val]
        "#t if VAL is a native function"
        [== :special-form [type-of val]]]

[defun procedure? [val]
        "#t if VAL is a native or lisp function"
        [or [lambda? val] [native? val]]]

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

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

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