Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
33
Task/Knuth-shuffle/Transd/knuth-shuffle.transd
Normal file
33
Task/Knuth-shuffle/Transd/knuth-shuffle.transd
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#lang transd
|
||||
|
||||
MainModule: {
|
||||
// Define an abstract type Vec to make the shuffling
|
||||
// function polymorphic
|
||||
Vec: typedef(Lambda<:Data Bool>(λ d :Data()
|
||||
(starts-with (_META_type d) "Vector<"))),
|
||||
|
||||
kshuffle: (λ v Vec() locals: rnd 0
|
||||
(for n in Range( (- (size v) 1) 0) do
|
||||
(= rnd (randr (to-Int n)))
|
||||
(with tmp (cp (get v n))
|
||||
(set-el v n (get v rnd))
|
||||
(set-el v rnd tmp))
|
||||
)
|
||||
(lout v)
|
||||
),
|
||||
_start: (λ
|
||||
(with v [10,20,30,40,50,60,70,80,90,100]
|
||||
(lout "Original:\n" v)
|
||||
(lout "Shuffled:")
|
||||
(kshuffle v))
|
||||
(lout "")
|
||||
(with v ["A","B","C","D","E","F","G","H"]
|
||||
(lout "Original:\n" v)
|
||||
(lout "Shuffled:")
|
||||
(kshuffle (cp v))
|
||||
// Transd has a built-in function that performs the same
|
||||
// kind of random shuffle
|
||||
(lout "Built-in shuffle:")
|
||||
(lout (shuffle v)))
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue