From 521deebfd9a15a822bc2d595b5f8e1ff750817d5 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 7 Aug 2022 16:15:47 -0500 Subject: [PATCH] assocs: refactor collect-by to use collect-by! --- core/assocs/assocs-tests.factor | 11 +++++++++++ core/assocs/assocs.factor | 7 +++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/core/assocs/assocs-tests.factor b/core/assocs/assocs-tests.factor index 4b03479044..931da8f2e0 100644 --- a/core/assocs/assocs-tests.factor +++ b/core/assocs/assocs-tests.factor @@ -331,6 +331,17 @@ unit-test 10 [ 3 mod ] collect-by ] unit-test +{ + H{ + { 0 V{ 0 3 6 9 0 3 6 9 } } + { 1 V{ 1 4 7 1 4 7 } } + { 2 V{ 2 5 8 2 5 8 } } + } +} [ + 10 [ 3 mod ] collect-by + 10 [ 3 mod ] collect-by! +] unit-test + { H{ { 1 4 } } } [ H{ { 1 2 } } 1 over [ sq ] ?change-at ] unit-test { H{ { 1 2 } } } [ H{ { 1 2 } } 2 over [ sq ] ?change-at ] unit-test { H{ { 1 3 } } } [ H{ { 1 2 } } 3 1 pick [ drop dup ] ?change-at drop ] unit-test diff --git a/core/assocs/assocs.factor b/core/assocs/assocs.factor index bb9fd94c38..fdfacb8fcc 100644 --- a/core/assocs/assocs.factor +++ b/core/assocs/assocs.factor @@ -255,11 +255,14 @@ M: assoc unzip : zip-with ( ... seq quot: ( ... key -- ... value ) -- ... alist ) { } zip-with-as ; inline -: collect-by ( ... seq quot: ( ... obj -- ... key ) -- ... assoc ) - [ keep swap ] curry H{ } clone [ +: collect-by! ( ... assoc seq quot: ( ... obj -- ... key ) -- ... assoc ) + [ keep swap ] curry rot [ [ push-at ] curry compose each ] keep ; inline +: collect-by ( ... seq quot: ( ... obj -- ... key ) -- ... assoc ) + [ H{ } clone ] 2dip collect-by! ; inline + M: sequence at* search-alist [ second t ] [ f ] if ; -- 2.34.1