Mind boggling card trick: Difference between revisions

→‎{{header|Javascript}}: Wrapped the swap of n cards in an exchange function
(→‎{{header|Haskell}}: Wrapped the swap of n cards in an exchange function)
(→‎{{header|Javascript}}: Wrapped the swap of n cards in an exchange function)
Line 344:
const main = () => {
const
// DEALT
[rs_, bs_, discards] = threeStacks(
map(n =>
even(n) ? (
Line 357:
// SWAPPED
nSwap = randomRInt(1, min(rs_.length, bs_.length)),
[rs, bs] = takeexchange(nSwap, rs_).concat(drop(nSwap, bs_)),
rs = take(nSwap, bs_).concat(drop(nSwap, rs_)),
 
// CHECKED
Line 382 ⟶ 381:
return 0 < lng ? (
1 < lng ? (() => {
const [x, y] = take(2, xs),
[x, y] = take(2, xs),
ds_ = cons(x, ds);
return (
Line 389 ⟶ 387:
go([cons(y, rs), bs, ds_])
) : go([rs, cons(y, bs), ds_])
)(drop(2, xs));
})() : [rs, bs, ds_]
) : [rs, bs, ds];
Line 398 ⟶ 396:
[]
])(cards);
};
 
// exchange :: Int -> [a] -> [a] -> ([a], [a])
const exchange = (n, xs, ys) => {
const [xs_, ys_] = map(splitAt(n), [xs, ys]);
return [
rs = takefst(nSwap, bs_ys_).concat(dropsnd(nSwap, rs_xs_)),
fst(xs_).concat(snd(ys_))
];
};
 
Line 447 ⟶ 454:
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
 
// fst :: (a, b) -> a
const fst = tpl => tpl[0];
 
// iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a]
Line 467 ⟶ 477:
(Math.random() * ((high - low) + 1))
);
 
// snd :: (a, b) -> b
const snd = tpl => tpl[1];
 
// splitAt :: Int -> [a] -> ([a],[a])
const splitAt = n => xs => Tuple(xs.slice(0, n), xs.slice(n));
 
// take :: Int -> [a] -> [a]
const take = (n, xs) => xs.slice(0, n);
 
// Tuple (,) :: a -> b -> (a, b)
const Tuple = (a, b) => ({
type: 'Tuple',
'0': a,
'1': b,
length: 2
});
 
// unlines :: [String] -> String
9,659

edits