Paste: Swap characters in string
Author: | paldepind |
Mode: | factor |
Date: | Sat, 22 Dec 2012 15:49:38 |
Plain Text |
: swap-char ( char1 char2 char3 -- char' )
{
{ [ [ [ = ] 2keep ] dip 4 nrot ] [ [ 2drop ] dip ] }
{ [ [ swap ] dip [ = ] 2keep 3 nrot [ swap ] 2 ndip ] [ drop swap drop ] }
[ 2drop ]
} cond
;
: swap ( string char1 char2 -- string' )
[ swap-char ] 2curry map
;
Author: | noam |
Mode: | factor |
Date: | Sat, 22 Dec 2012 22:31:25 |
Plain Text |
: make-swap-assoc ( c1 c2 -- alist )
2dup swap [ 2array ] 2bi@ 2array ;
: swap-string ( s c1 c2 -- s' )
[ make-swap-assoc ?at drop ] 2curry map ;
:: swap-char ( c c1 c2 -- c' )
{
{ [ c c1 = ] [ c2 ] }
{ [ c c2 = ] [ c1 ] }
[ c ]
} cond ;
: swap-string ( s c1 c2 -- s' )
[ swap-char ] 2curry map ;
New Annotation