From 3bd22a39f6c4cc1d547cf7c36a17e28fa50a7158 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 9 Feb 2009 16:26:56 -0600 Subject: [PATCH] effects: fix bug reported by Joe where printing { f } { } kills the listener; simpler implementation of 'shuffle'; add compiler transform to 'shuffle' if effect is constant --- basis/generalizations/generalizations.factor | 10 +++++++++- core/effects/effects-tests.factor | 6 +++++- core/effects/effects.factor | 16 +++++++--------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/basis/generalizations/generalizations.factor b/basis/generalizations/generalizations.factor index 9b2b2456c2..fe822f318c 100644 --- a/basis/generalizations/generalizations.factor +++ b/basis/generalizations/generalizations.factor @@ -2,7 +2,7 @@ ! Cavazos, Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: kernel sequences sequences.private math combinators -macros quotations fry ; +macros quotations fry stack-checker.transforms effects ; IN: generalizations << @@ -95,3 +95,11 @@ MACRO: nweave ( n -- ) [ narray concat ] dip like ; inline : nappend ( n -- seq ) narray concat ; inline + +: nths-quot ( indices -- quot ) + [ [ '[ _ swap nth ] ] map ] [ length ] bi + '[ _ cleave _ narray ] ; + +\ shuffle [ + shuffle-mapping nths-quot +] 1 define-transform \ No newline at end of file diff --git a/core/effects/effects-tests.factor b/core/effects/effects-tests.factor index c592ef6c92..316add54c0 100644 --- a/core/effects/effects-tests.factor +++ b/core/effects/effects-tests.factor @@ -9,9 +9,13 @@ USING: effects tools.test prettyprint accessors sequences ; [ 2 ] [ (( a b -- c )) in>> length ] unit-test [ 1 ] [ (( a b -- c )) out>> length ] unit-test - +[ "(( object -- object ))" ] [ { f } { f } unparse ] unit-test [ "(( a b -- c d ))" ] [ { "a" "b" } { "c" "d" } unparse ] unit-test [ "(( -- c d ))" ] [ { } { "c" "d" } unparse ] unit-test [ "(( a b -- ))" ] [ { "a" "b" } { } unparse ] unit-test [ "(( -- ))" ] [ { } { } unparse ] unit-test [ "(( a b -- c ))" ] [ (( a b -- c )) unparse ] unit-test + +[ { "x" "y" } ] [ { "y" "x" } (( a b -- b a )) shuffle ] unit-test +[ { "y" "x" "y" } ] [ { "y" "x" } (( a b -- a b a )) shuffle ] unit-test +[ { } ] [ { "y" "x" } (( a b -- )) shuffle ] unit-test \ No newline at end of file diff --git a/core/effects/effects.factor b/core/effects/effects.factor index 8a06653eb8..235c2bcc89 100644 --- a/core/effects/effects.factor +++ b/core/effects/effects.factor @@ -1,4 +1,4 @@ -! Copyright (C) 2006, 2008 Slava Pestov. +! Copyright (C) 2006, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: kernel math math.parser namespaces make sequences strings words assocs combinators accessors arrays ; @@ -24,6 +24,7 @@ TUPLE: effect in out terminated? ; GENERIC: effect>string ( obj -- str ) M: string effect>string ; +M: object effect>string drop "object" ; M: word effect>string name>> ; M: integer effect>string number>string ; M: pair effect>string first2 [ effect>string ] bi@ ": " glue ; @@ -45,8 +46,8 @@ M: effect effect>string ( effect -- string ) GENERIC: stack-effect ( word -- effect/f ) M: word stack-effect - { "declared-effect" "inferred-effect" } - swap props>> [ at ] curry map [ ] find nip ; + "inferred-effect" "declared-effect" + [ word-prop ] bi-curry@ bi or ; M: effect clone [ in>> clone ] [ out>> clone ] bi ; @@ -57,11 +58,8 @@ M: effect clone : split-shuffle ( stack shuffle -- stack1 stack2 ) in>> length cut* ; -: load-shuffle ( stack shuffle -- ) - in>> [ set ] 2each ; - -: shuffled-values ( shuffle -- values ) - out>> [ get ] map ; +: shuffle-mapping ( effect -- mapping ) + [ out>> ] [ in>> ] bi [ index ] curry map ; : shuffle ( stack shuffle -- newstack ) - [ [ load-shuffle ] keep shuffled-values ] with-scope ; + shuffle-mapping swap nths ;