Skip to content

Commit

Permalink
demo: add a fun deck-shuffling example
Browse files Browse the repository at this point in the history
Discovered this today. It uses the newly implemented binary ? operator,
"deal".
  • Loading branch information
robpike committed Jan 30, 2020
1 parent 7ef67b9 commit 75f2a67
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions demo/demo.ivy
Expand Up @@ -235,5 +235,14 @@ op primes N = (not T in T o.* T) sel T = 1 drop iota N
# The assignment to T gives 2..N. We use outer product to build an array of all products.
# Then we find all elements of T that appear in the product matrix, invert that, and select from the original.
primes 100
# A final trick.
# The binary ? operator "deals": x?y selects at random x distinct integers from 1..y inclusive.
5?10
# We can use this to shuffle a deck of cards. The suits are ♠♡♣♢, the values
# A234567890JQK (using 0 for 10, for simplicity).
# Create the deck using outer product with the ravel operator:
"A234567890JQK" o., "♠♡♣♢"
# To shuffle it, ravel into into a vector and index that by 1 through 52, shuffled.
(, "A234567890JQK" o., "♠♡♣♢")[52?52]
# That's it! Have fun.
# For more information visit https://godoc.org/robpike.io/ivy
15 changes: 15 additions & 0 deletions demo/demo.out
Expand Up @@ -291,3 +291,18 @@ wroo
46
5
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
7 4 9 5 2
A♠ A♡ A♣ A♢
2♠ 2♡ 2♣ 2♢
3♠ 3♡ 3♣ 3♢
4♠ 4♡ 4♣ 4♢
5♠ 5♡ 5♣ 5♢
6♠ 6♡ 6♣ 6♢
7♠ 7♡ 7♣ 7♢
8♠ 8♡ 8♣ 8♢
9♠ 9♡ 9♣ 9♢
0♠ 0♡ 0♣ 0♢
J♠ J♡ J♣ J♢
Q♠ Q♡ Q♣ Q♢
K♠ K♡ K♣ K♢
J♡ Q♠ Q♢ 8♡ K♢ 4♠ 6♡ 8♣ 7♠ 8♢ 2♢ 4♡ 4♢ 0♣ A♠ 9♣ 7♣ 7♡ 5♣ Q♣ 3♣ 5♢ 2♡ J♣ J♢ 0♠ A♢ 9♡ J♠ 6♢ 9♠ K♣ 5♡ K♠ 3♡ 0♢ 9♢ 7♢ 6♠ A♡ 5♠ 3♢ A♣ 6♣ Q♡ 2♣ K♡ 2♠ 8♠ 3♠ 4♣ 0♡

0 comments on commit 75f2a67

Please sign in to comment.