Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
Created April 9, 2016 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixpulvis/683366c61be9a95e6e4513cdd2fad43c to your computer and use it in GitHub Desktop.
Save nixpulvis/683366c61be9a95e6e4513cdd2fad43c to your computer and use it in GitHub Desktop.
// NOTE:
//
// Matthias's code below and inline.
//
// (define (<-card c1 c2)
// (match-define (card f1 t1) c1)
// (match-define (card f2 t2) c2)
// (or (<-trait t1 t2) (and (equal? t1 t2) (< f1 f2))))
// (define (<-card c1 c2)
//
// Here the notion of `<-card` is encoded in the type system as
// `impl Ord for Card` and that trait requires a a function named `cmp`,
// which takes in two `&Card`s.
impl Ord for Card {
fn cmp(&self, other: &Card) -> Ordering {
// (match-define (card f1 t1) c1)
// (match-define (card f2 t2) c2)
//
// This is a straightforward translation. `let` in Rust destructures
// just as `match-define` does. In fact our data models for the game
// must be practically identical.
let Card(f1, t1) = *self;
let Card(f2, t2) = *other;
// This line I'll line up to show how cool it is that these two
// different languages encode the idea of order in a similar, yet
// different way.
//
// (or (<-trait t1 t2) (and (equal? t1 t2) (< f1 f2))))
match t1.cmp(&t2) { Equal => f1.cmp(&f2), o => o }
// Rust's is shorter :P
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment