Created
November 30, 2010 23:12
-
-
Save pheuter/722611 to your computer and use it in GitHub Desktop.
Factor implementation of merge assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! With local bindings (7 loc) | |
: merge ( x y z -- z ) | |
[| a b c | a b c a length 0 > b length 0 > and ] | |
[| a b c | a b c 0 a nth 0 b nth over over < | |
[| a b c x y | 0 a remove-nth b c x over push ] | |
[| a b c x y | a 0 b remove-nth c y over push ] if ] | |
while | |
[| a b c | a b c b length 0 = [| a b c | c a append ] [| a b c | c b append ] if ] call ; | |
! Without local bindings (7 loc) | |
: merge ( x y z -- z ) | |
[ 2over length 0 > swap length 0 > and ] | |
[ 2over 0 swap nth swap 0 swap nth swap over over < | |
[ 2over swap push drop drop swapd swap 0 swap remove-nth swap swapd ] | |
[ swap 2over swap push drop drop swap swapd 0 swap remove-nth swapd swap ] if ] | |
while | |
swap dup length 0 = [ drop swap append ] [ append nip ] if ; | |
! Test | |
--- Data Stack: | |
V{ 1 3 5 7 9 } | |
V{ 2 4 6 } | |
V{ } | |
(scratchpad) : merge | |
--- Data Stack: | |
V{ 1 2 3 4 5 6 7 9 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment