! Copyright (C) 2012 John Benediktsson, Doug Coleman
! See https://factorcode.org/license.txt for BSD license
USING: arrays assocs assocs.private kernel math math.statistics
-sequences sequences.extras sets ;
+sequences sets ;
IN: assocs.extras
+: change-of ( ..a assoc key quot: ( ..a value -- ..b newvalue ) -- ..b assoc )
+ [ [ of ] dip call ] 2keepd rot set-of ; inline
+
: of* ( assoc key -- value/f ? ) swap at* ; inline
: of+ ( assoc key n -- assoc ) '[ 0 or _ + ] change-of ; inline
: rename-of ( assoc key newkey -- assoc )
[ delete-of* ] dip swap [ set-of ] [ 2drop ] if ;
-: at+* ( n key assoc -- old new ) [ 0 or [ + ] keep swap dup ] change-at ; inline
-
-: inc-at* ( key assoc -- old new ) [ 1 ] 2dip at+* ; inline
-
: inc-of ( assoc key -- assoc ) 1 of+ ; inline
: inc-of* ( assoc key -- assoc old new ) 1 of+* ; inline
-: change-of ( ..a assoc key quot: ( ..a value -- ..b newvalue ) -- ..b assoc )
- [ [ of ] dip call ] 2keepd rot set-of ; inline
-
: ?change-of ( ..a assoc key quot: ( ..a value -- ..b newvalue ) -- ..b assoc )
[ set-of ] compose [ 2dup ?of ] dip [ 2drop ] if ; inline
: deep-set-of ( assoc seq elt -- )
[ deep-of-but-last ] dip spin set-at ; inline
-: zip-longest-with ( seq1 seq2 fill -- assoc )
- pad-longest zip ;
-
-: zip-longest ( seq1 seq2 -- assoc )
- f zip-longest-with ;
-
: substitute! ( seq assoc -- seq )
substituter map! ;
+
USING: accessors arrays assocs combinators generalizations
grouping growable heaps kernel math math.order ranges sequences
sequences.private shuffle sorting splitting vectors ;
pick surround-as
] if-zero ;
+: zip-longest-with ( seq1 seq2 fill -- assoc )
+ pad-longest zip ;
+
+: zip-longest ( seq1 seq2 -- assoc )
+ f zip-longest-with ;
+
: change-nths ( ... indices seq quot: ( ... elt -- ... elt' ) -- ... )
[ change-nth ] 2curry each ; inline
: 0accumulate ( ... seq quot: ( ... prev elt -- ... next ) -- ... final newseq )
over 0accumulate-as ; inline
-: occurrence-count-by ( seq quot: ( elt -- elt' ) -- hash seq' )
- '[ nip @ over inc-at* drop ] [ H{ } clone ] 2dip 0accumulate ; inline
-
: nth-index ( n obj seq -- i )
[ = dup [ drop 1 - dup 0 < ] when ] with find drop nip ;
+: at+* ( n key assoc -- old new ) [ 0 or [ + ] keep swap dup ] change-at ; inline
+
+: inc-at* ( key assoc -- old new ) [ 1 ] 2dip at+* ; inline
+
: progressive-index-by-as ( seq1 seq2 quot exemplar -- hash seq' )
[
pick length '[