Created
April 9, 2016 03:55
-
-
Save nixpulvis/683366c61be9a95e6e4513cdd2fad43c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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