2005-01-14 12:01:48 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
2005-01-29 14:18:28 -05:00
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-05-14 17:18:45 -04:00
|
|
|
IN: lists USING: kernel sequences ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-01-13 19:49:47 -05:00
|
|
|
: assoc* ( key alist -- [[ key value ]] )
|
2005-02-22 23:07:47 -05:00
|
|
|
#! Look up a key/value pair.
|
2005-07-16 22:16:18 -04:00
|
|
|
[ car = ] find-with nip ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-03-19 00:30:49 -05:00
|
|
|
: assoc ( key alist -- value ) assoc* cdr ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-10-16 21:55:13 -04:00
|
|
|
: remove-assoc ( key alist -- alist )
|
|
|
|
|
#! Remove all key/value pairs with this key.
|
2005-01-14 12:01:48 -05:00
|
|
|
[ car = not ] subset-with ;
|
2004-10-16 21:55:13 -04:00
|
|
|
|
2004-08-03 20:21:43 -04:00
|
|
|
: acons ( value key alist -- alist )
|
2004-10-16 21:55:13 -04:00
|
|
|
#! Adds the key/value pair to the alist. Existing pairs with
|
|
|
|
|
#! this key are not removed; the new pair simply shadows
|
|
|
|
|
#! existing pairs.
|
2004-08-03 20:21:43 -04:00
|
|
|
>r swons r> cons ;
|
|
|
|
|
|
2004-07-16 02:26:21 -04:00
|
|
|
: set-assoc ( value key alist -- alist )
|
2004-10-16 21:55:13 -04:00
|
|
|
#! Adds the key/value pair to the alist.
|
|
|
|
|
dupd remove-assoc acons ;
|
2004-08-21 02:55:37 -04:00
|
|
|
|
2004-08-21 03:30:52 -04:00
|
|
|
: assoc-apply ( value-alist quot-alist -- )
|
2004-08-21 02:55:37 -04:00
|
|
|
#! Looks up the key of each pair in the first list in the
|
|
|
|
|
#! second list to produce a quotation. The quotation is
|
|
|
|
|
#! applied to the value of the pair. If there is no
|
|
|
|
|
#! corresponding quotation, the value is popped off the
|
|
|
|
|
#! stack.
|
|
|
|
|
swap [
|
2005-01-19 21:01:47 -05:00
|
|
|
unswons rot assoc* dup [ cdr call ] [ 2drop ] ifte
|
2005-01-01 17:20:48 -05:00
|
|
|
] each-with ;
|