Card tricks with Ruby

M

Matthew Moss

Yah, [x..y], [x..y), (x..y], (x..y) would be sweetness. It's slightly =
less
Can you really stand to look at that? It gives me the same feeling as
putting my shoes on the wrong feet :)

As part-mathematician, I whole-heartedly say, "Yes!" It's a compact,
consistent and clear representation of closed [ and open ( ends of
ranges.

As part-programmer, I recognize that it would be a bitch to parse,
considering how often we make use of braces for all sorts of other
needs (array access, expression grouping, function calls, etc, etc,
etc).

I'm not sure what it is about the range operators that bothers you.
Are you finding it hard to remember which is which? My way of
remembering is: every range is the same width, in relation to the
operators. Since the ... operator is wider, the end-point gets pushed
outside the range:

v v
a..b
a...b
^ ^

This is basically how I now remember the difference between the two,
and so having both operators doesn't bother me as much anymore. But I
will admit that at first I could never remember the difference, and
can see it as confusing to newbs.
 
M

Matthew Moss

How would you code riffle?

Start with a merge sort:

def merge(left, right, &pred)
if right.empty?
left.dup
elsif left.empty?
right.dup
elsif pred.call(left.first, right.first)
[left.first] + merge(left[1..-1], right, &pred)
else
[right.first] + merge(left, right[1..-1], &pred)
end
end

And then riffle!

def riffle(deck)
n =3D deck.size / 2
left, right =3D deck[0...n], deck[n..-1]
merge(left, right) { rand < 0.5 }
end

Or, to simulate one side's cards being more likely given the other
side's card just went down:

def riffle(deck)
n =3D deck.size / 2
left, right =3D deck[0...n], deck[n..-1]
p =3D 0.5
merge(left, right) do
res =3D (rand < p)
p =3D res ? p*p : Math.sqrt(p)
res
end
end
 
B

Benjohn Barnes

Matthew said:
deck = deck[x..-1] + deck[0...x] # notice differing amounts of dots

I find myself constantly pointing this out too.. is anyone else
bothered by it? Has Matz weighed in on this in the past?

I like being able to express either. I often don't remember which is
which though. It feels like they're the wrong way round because three
dots ought to go a little bit further than two :)
 
J

Jim Weirich

Benjohn said:
I like being able to express either. I often don't remember which is
which though. It feels like they're the wrong way round because three
dots ought to go a little bit further than two :)

The dots *do* go a bit further ... far enough to push the last value
right out of the range ;)

-- Jim Weirich
 
S

Stephen Waits

I'm not sure what it is about the range operators that bothers you.
Are you finding it hard to remember which is which? My way of
remembering is: every range is the same width, in relation to the
operators. Since the ... operator is wider, the end-point gets pushed
outside the range:

That you had to invent that mnemonic device just to remember what they
is what I don't like about them. That they differ by a single '.' is
error-prone. It just one of the few things I've run across in Ruby that
doesn't feel like the rest of Ruby (very straightforward, POLS, etc.).

I don't want to start a whole thing, though, which is why I inquired as
to whether Matz has discussed this publicly in the past.

Thanks,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top