The Predicates =?, >? and Friends.

;===============================================================
; Predicates =?, >? and friends

(dolist (predicate '(< > = <= >= !=))
  (set 'left (sym (append (string predicate) "?")))
  (set 'right (expand (lambda(x)
                         (expand (lambda(y)
                                     (predicate y x))
                                 'x))
                      'predicate))
  (set left right))
               
(println (filter (<=? 3) '(1 2 3 4 5 1 2 3 4 5)))
(println (map (=? 4)     '(1 2 3 4 5 1 2 3 4 5)))
(println (clean (>? 1)   '(1 2 3 4 5 1 2 3 4 5)))
(exit)

RESULTS:

(1 2 3 1 2 3)
(nil nil nil true nil nil nil nil true nil)
(1 1)








4 comments:

  1. I like watching you program, Kasimir! creative, like a musician...

    a less creative way:

    (find-all 3 '(1 2 3 4 5 1 2 3 4 5) $0 >=)
    ;-> (1 2 3 1 2 3)

    (find-all 1 '(1 2 3 4 5 1 2 3 4 5) $0 >=)

    ;-> (1 1)

    ReplyDelete
  2. You have a crazy beard, and Lisp derivatives breed like rabbits.

    ReplyDelete
  3. Built-in curry is much faster then handmade one:

    (map (curry = 3) '(1 2 3 4 5 6))

    But I like this technique of generating the "postfixed" versions of existing functions, too! My favorite is here (the surronding text is in Russian, bud you don't need to read it: newlisp itself is enough! ;-)

    ReplyDelete
  4. Yes, your right that curry is faster than =?. At least their results (=? 3) and (curry = 3) are about equally fast!

    Good code that one with not& etc. I didn't know it is so simple.

    ReplyDelete