1 ! Copyright (C) 2008 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
10 : invert-comparison ( <=> -- <=>' )
11 #! Can't use case, index or nth here
12 dup +lt+ eq? [ drop +gt+ ] [ +eq+ eq? +eq+ +lt+ ? ] if ;
14 GENERIC: <=> ( obj1 obj2 -- <=> )
16 M: real <=> 2dup < [ 2drop +lt+ ] [ number= +eq+ +gt+ ? ] if ;
18 GENERIC: before? ( obj1 obj2 -- ? )
19 GENERIC: after? ( obj1 obj2 -- ? )
20 GENERIC: before=? ( obj1 obj2 -- ? )
21 GENERIC: after=? ( obj1 obj2 -- ? )
23 M: object before? ( obj1 obj2 -- ? ) <=> +lt+ eq? ;
24 M: object after? ( obj1 obj2 -- ? ) <=> +gt+ eq? ;
25 M: object before=? ( obj1 obj2 -- ? ) <=> +gt+ eq? not ;
26 M: object after=? ( obj1 obj2 -- ? ) <=> +lt+ eq? not ;
28 M: real before? ( obj1 obj2 -- ? ) < ;
29 M: real after? ( obj1 obj2 -- ? ) > ;
30 M: real before=? ( obj1 obj2 -- ? ) <= ;
31 M: real after=? ( obj1 obj2 -- ? ) >= ;
33 : min ( x y -- z ) [ before? ] most ; inline
34 : max ( x y -- z ) [ after? ] most ; inline
36 : between? ( x y z -- ? )
37 pick after=? [ after=? ] [ 2drop f ] if ; inline
39 : [-] ( x y -- z ) - 0 max ; inline
41 : compare ( obj1 obj2 quot -- <=> ) bi@ <=> ; inline