Created
July 30, 2015 20:22
-
-
Save wavebeem/187ecc982f631750b972 to your computer and use it in GitHub Desktop.
TC.js example translated into Squiggle
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
var TC = require('tc'); | |
var add = TC | |
.takes([TC.Number, TC.Number]) | |
.returns(TC.Number) | |
.by(function(a, b) { return a + b; }); | |
var getName = TC | |
.takes([TC.Object(TC.Any)]) | |
.returns(TC.String) | |
.by(function(obj) { return obj.name; }); | |
var max = TC([TC.Number, TC.Number], TC.Number, Math.max); | |
add(3, 2); // => 5 | |
add(3, 'x'); // => Error "wrong argument type" | |
add(); // => Error "wrong number of arguments" | |
getName({ name: 'Brian' }); // => 'Brian' | |
getName({ name: null }); // => Error "wrong return type" | |
max(3, 2); // => 3 | |
max(); // => Error "wrong number of arguments" |
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
let ( | |
TC = require("tc"), | |
add = TC | |
.takes([TC.Number, TC.Number]) | |
.returns(TC.Number) | |
.by(fn(a, b) a + b), | |
getName = TC | |
.takes([TC.object(TC.Any)]) | |
.returns(TC.String) | |
.by(fn(obj) obj.name), | |
max = TC([TC.Number, TC.Number], TC.Number, Math.max) | |
) do { | |
add(3, 2); # => 5 | |
add(3, "x"); # => TC type error | |
add(); # Squiggle arity error | |
getName({"name": "Brian"}); # => "Brian" | |
getName({"name": null}); # => TC return type error | |
max(3, 2); # => 3 | |
max(); # Squiggle arity error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment