From: Doug Coleman Date: Sun, 20 Feb 2022 01:28:13 +0000 (-0600) Subject: assocs.extras: Add collect-key-by and collect-value-by X-Git-Tag: 0.99~1498 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=bc3f412e1b6e1fa5c1f76dca5d286be994b1ae94 assocs.extras: Add collect-key-by and collect-value-by --- diff --git a/extra/assocs/extras/extras.factor b/extra/assocs/extras/extras.factor index addd66b5fd..9a6ba7553e 100644 --- a/extra/assocs/extras/extras.factor +++ b/extra/assocs/extras/extras.factor @@ -222,3 +222,11 @@ PRIVATE> : counts ( seq elts -- counts ) [ histogram ] dip intersect-keys ; + +: collect-key-by ( ... seq quot: ( ... obj -- ... key ) -- ... assoc ) + [ keep swap ] curry H{ } clone + [ '[ @ [ first ] dip _ push-at ] each ] keep ; inline + +: collect-value-by ( ... seq quot: ( ... obj -- ... key ) -- ... assoc ) + [ keep swap ] curry H{ } clone + [ '[ @ [ second ] dip _ push-at ] each ] keep ; inline