factor/core/classes/intersection/intersection.factor

63 lines
1.8 KiB
Factor
Raw Normal View History

2010-01-20 02:26:47 -05:00
! Copyright (C) 2004, 2010 Slava Pestov.
2008-05-10 19:09:05 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs classes classes.algebra
classes.algebra.private classes.builtin classes.private
combinators kernel make sequences words ;
2008-05-10 19:09:05 -04:00
IN: classes.intersection
PREDICATE: intersection-class < class
"metaclass" word-prop intersection-class eq? ;
<PRIVATE
2008-05-10 19:09:05 -04:00
: intersection-predicate-quot ( members -- quot )
2008-09-06 18:10:32 -04:00
[
[ drop t ]
2008-05-10 19:09:05 -04:00
] [
unclip predicate-def swap [
predicate-def [ dup ] [ not ] surround
2008-05-10 19:09:05 -04:00
[ drop f ]
] { } map>assoc alist>quot
2008-09-06 18:10:32 -04:00
] if-empty ;
2008-05-10 19:09:05 -04:00
: define-intersection-predicate ( class -- )
dup class-participants intersection-predicate-quot define-predicate ;
2008-05-10 19:09:05 -04:00
M: intersection-class update-class define-intersection-predicate ;
M: intersection-class rank-class drop 5 ;
M: intersection-class instance?
"participants" word-prop [ instance? ] with all? ;
2008-07-05 18:08:01 -04:00
M: anonymous-intersection instance?
participants>> [ instance? ] with all? ;
2010-01-20 02:26:47 -05:00
M: intersection-class normalize-class
class-participants <anonymous-intersection> normalize-class ;
2010-01-20 02:26:47 -05:00
2008-07-05 18:08:01 -04:00
M: intersection-class (flatten-class)
class-participants <anonymous-intersection> (flatten-class) ;
! Horribly inefficient and inaccurate
: intersect-flattened-classes ( seq1 seq2 -- seq3 )
! Only keep those in seq1 that intersect something in seq2.
[ [ classes-intersect? ] with any? ] curry filter ;
M: anonymous-intersection (flatten-class)
participants>> [ full-cover ] [
[ flatten-class keys ]
[ intersect-flattened-classes ] map-reduce
2012-07-19 12:50:09 -04:00
[ dup ,, ] each
] if-empty ;
M: anonymous-intersection class-name
participants>> [ class-name ] map " " join ;
PRIVATE>
: define-intersection-class ( class participants -- )
[ [ f f ] dip intersection-class define-class ]
[ drop update-classes ]
2bi ;