1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences sequences.private arrays vectors fry
4 math math.order namespaces assocs locals ;
7 : flattener ( seq quot -- seq vector quot' )
12 dup [ array? ] [ vector? ] bi or
13 [ _ push-all ] [ _ push ] if
18 : flattening ( seq quot combinator -- seq' )
19 [ flattener ] dip dip { } like ; inline
21 : map-flat ( seq quot -- seq' ) [ each ] flattening ; inline
23 : 2map-flat ( seq quot -- seq' ) [ 2each ] flattening ; inline
27 yield-hook [ [ ] ] initialize
29 : alist-most ( alist quot -- pair )
30 [ [ ] ] dip '[ [ [ second ] bi@ @ ] most ] map-reduce ; inline
32 : alist-min ( alist -- pair ) [ before? ] alist-most ;
34 : alist-max ( alist -- pair ) [ after? ] alist-most ;
36 : penultimate ( seq -- elt ) [ length 2 - ] keep nth ;
38 :: compress-path ( source assoc -- destination )
39 [let | destination [ source assoc at ] |
40 source destination = [ source ] [
41 [let | destination' [ destination assoc compress-path ] |
42 destination' destination = [
43 destination' source assoc set-at