Last active
August 29, 2015 14:00
-
-
Save mythmon/11166016 to your computer and use it in GitHub Desktop.
This is some crazy ideas about Monte modules. I don't think you're going to like this. (the numbers are just to control the Gist's order)
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
# This is a file that doesn't define any modules, and has one thing that is a | |
# main function. Clearly this is meant to run in the IO scope, and the IO scope | |
# by default will execute a function named main in the file it executes. | |
fn main(): | |
# addModules is a configuration step that is only available in IO scope. | |
# It could be in a separate configuration, or inline as it is here. It | |
# defines modules, the sums they should match (or the special value | |
# `M.linker.any`, which allows for any content), and the file path that | |
# module can be found in. | |
M.linker.addModules([ | |
["cards", sha1sum`fe35e3eca6d189fed3755633fd1679b961d9ed3b`, "./cards.mt"], | |
["deck", sha1sum`a677f1c766ee89673e5adbdb89b314b76a6f68f4`, "./deck.mt"], | |
["hand", M.linker.any, "./hand.mt"], | |
]) | |
# I'll explain this elsewhere. | |
def {makeDeck} := require("deck") | |
def {makeHand} := require("hand") | |
traceln("Let's play blackjack!") | |
def deck := makeDeck() | |
deck.shuffle() | |
def hand := makeHand([deck.draw() for _ in 1..2]) | |
traceln("My hand is: ") | |
for card in hand.cards: | |
trace(`${card.pretty}, `) | |
traceln("It's score is ${hand.score}) |
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
# This file defines the "hand" module, which does not export any names. | |
module("hand", {}) | |
# This is a syntax I made up that plucks the names on the left out of the thing | |
# on the right and creates slots with those names. It has a similar use when | |
# defining exports too, seen in cards.mt and deck.mt. | |
def {suits, ranks} := require("cards") | |
def {makeDeck} := require("deck") | |
fn makeHand(cards): | |
to cards(): | |
return card | |
to score(): | |
# I'd love some sort of clean reduce syntax, but I can't think of one. | |
var sum := 0 | |
for card in cards: | |
sum += card.rank.value | |
return sum |
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
# This file defines the "cards" module, which does export some things. Here we | |
# see the return of these funny braces simpson is going to hate. Used as a | |
# literal here, they form an object with methods that return the contents by | |
# name. | |
module("cards", { | |
makeCard, | |
suits, | |
ranks, | |
}) | |
# I really should invent a name for this brace thing. These next few lines | |
# create an object named "suits", which has 4 getter methods, one for each suit. | |
def makeSuit(name): | |
return object `$name`: | |
to name(): | |
return name | |
def suits := { | |
makeSuit("hearts"), | |
makeSuit("spades"), | |
makeSuit("diamonds"), | |
makeSuit("clubs"), | |
} | |
# The above is similar to the below, but easier to type. | |
# | |
# def _suitsMaker(): | |
# def hearats := makeSuit("hearats") | |
# def spades := makeSuit("spades") | |
# def diamonds := makeSuit("diamonds") | |
# def clubs := makeSuit("clubs") | |
# return object suits: | |
# to hearts: | |
# return hearts | |
# to spades: | |
# return spades | |
# to diamonds: | |
# return diamonds | |
# to clubs: | |
# return clubs | |
# More of the same, | |
def makeRank(name, value): | |
return object `$name`: | |
to name: | |
return name | |
to value: | |
return value | |
def ranks := { | |
makeRank("ace", 1), | |
makeRank("two", 2), | |
makeRank("three", 3), | |
makeRank("four", 4), | |
makeRank("five", 5), | |
makeRank("six", 6), | |
makeRank("seven", 7), | |
makeRank("eight", 8), | |
makeRank("nine", 9), | |
makeRank("ten", 10), | |
makeRank("jack", 11), | |
makeRank("queen", 12), | |
makeRank("king", 13), | |
} | |
fn makeCard(rank, suit): | |
return object card: | |
to rank(): | |
return rank | |
to suit(): | |
return suit | |
to prettyPrint(): | |
return `${name(rank)} of ${name(suit)}` | |
to op__cmp(other): | |
def rankCmp := ranks.compare(rank, other.rank) | |
def suitCmp := suits.compare(suit, other.suit) | |
if rankCmp.aboveZero: | |
return 1 | |
else if rankCmp.belowZero: | |
return -1 | |
else if suitCmp.aboveZero: | |
return 1 | |
else if suitCmp.belowZero: | |
return -1 | |
else: | |
return 0 | |
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
# I don't think there is anything new in this file that wasn't described earlier. | |
module("deck", { | |
makeDeck, | |
}) | |
def {suits, ranks, makeCard} := require("cards") | |
def {product} := require("itertools") | |
def makeDeck(): | |
def cards := [].diverge() | |
for [rank, suit] in product(ranks, suits): | |
cards.push(makeCard(rank, suit)) | |
return object deck: | |
to draw(): | |
return cards.pop() | |
to add(c): | |
cards.push(c) | |
to shuffle(): | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment