Last active
August 29, 2015 14:04
-
-
Save dignifiedquire/c6be2457afa6b41d5c50 to your computer and use it in GitHub Desktop.
TypeScript Patternmatching
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
// Translation of functions.ts using sparkler | |
// https://github.com/natefaubion/sparkler | |
function pickCard(x) { | |
[...{ suit @ String, card @ number }] => { | |
var pickedCard = Math.floor(Math.random() * x.length); | |
return pickedCard; | |
} | |
x @ Number => { | |
var pickedSuit = Math.floor(x / 13); | |
return { suit: suits[pickedSuit], card: x % 13 }; | |
} | |
} |
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
// Pattern matching on functions | |
function pickCard { | |
(x: {suit: string; card: number; }[]): number { | |
var pickedCard = Math.floor(Math.random() * x.length); | |
return pickedCard; | |
} | |
(x: number): {suit: string; card: number; } { | |
var pickedSuit = Math.floor(x / 13); | |
return { suit: suits[pickedSuit], card: x % 13 }; | |
} | |
} |
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
// Pattern matching using match translated | |
function whoami (x) { | |
match x { | |
case { name @ String, type: 'person' }: | |
return 'Person: ' + name; | |
case { name @ String, type: 'animal' }: | |
return 'Animal: ' + name; | |
default: | |
return 'Unkown you'; | |
} | |
} |
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
// Pattern matching using match | |
function whoami (x: any): string { | |
match x { | |
case { name: string, type: 'person' }: | |
return 'Person: ' + name; | |
case { name: string, type: 'animal' }: | |
return 'Animal: ' + name; | |
default: | |
return 'Unkown you'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment