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); |
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
/// Consider that the JavaScript version contains far fewer assertions than the | |
/// Squiggle version. Squiggle automatically asserts that a key is an own | |
/// property of the object before retreiving it (e.g. x[0], foo.bar, keys must | |
/// exist or it throws). Also, functions will throw unless called with the | |
/// correct number of arguments. Arrays and objects created through Squiggle are | |
/// also implicitly frozen, so you can't accidentally mutate them. | |
var _ = require("lodash"); | |
var text = "2 3 4 * 3 - +"; |
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
// map = fn(f, xs) match (xs) { | |
// case [] => [] | |
// case [x, ...xs] => [f(x)] ++ map(f, xs) | |
// } | |
// This approach is going to be the best, but it requires the compiler to become | |
// aware of being inside functions, so it can expand things into statements, | |
// rather than being purely expression oriented. It's sort of amazing how far | |
// you can get only using expressions in JS, though. It will also need to | |
// rewrite variable usage, which might be a little tricky. |
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
-- Also note, it seems people generally reach for positional types unless the data has many fields. | |
-- data BTNode t = BTNode t (BTNode t) (BTNode t) | BTNil | |
-- as opposed to: | |
-- data BTNode t = BTNode { data :: t, left :: BTNode, right :: BTnode } | BTNil | |
data Car2 = Car2 { make :: Maybe String } deriving (Show) | |
-- Haskell automatically makes a function in this scope: | |
-- make :: Car2 -> Maybe String | |
-- So you can just say `make c1` to get the make of the Car2 called `c1`. |
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
(function(global) { | |
var foo = global.foo; | |
function bar1() { return 1; } | |
function bar2() { return 2; } | |
function bar3() { return 3; } | |
var api = { | |
bar1: bar1, | |
bar2: bar2, | |
bar3: bar3 | |
}; |
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
// squiggle> :set f = fn(n) match (n) { case 0 => 1 case x => x * f(x - 1) } | |
(function () { | |
'use strict'; | |
return (false || eval)('this')['f'] = sqgl$$freeze(function (n) { | |
if (arguments.length !== 1) { | |
throw new sqgl$$Error('expected 1 argument(s), got ' + arguments.length); | |
} | |
return function ($match) { | |
if (sqgl$$is($match, 0)) { | |
return function () { |
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
#!/usr/bin/env ruby | |
# | |
# Ruby, ruby, ruby | |
# | |
DATA.each{|line| puts "#{DATA.lineno}: #{line}"} | |
__END__ | |
Line one | |
Line two | |
Line three :D |
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
#!/usr/bin/ruby | |
# | |
# Allow the user to query the status of several running totals via multiple | |
# background threads | |
code = lambda do | |
Thread.current[:x] = 0 | |
loop do | |
Thread.current[:x] += 1 | |
Thread.pass |
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
#!/usr/bin/python2 | |
import sys | |
arg = sys.argv | |
if len(arg) != 3: | |
exit(1) | |
infile = arg[1] | |
outfile = arg[2] |
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
Ruby | |
real 0m2.820s | |
user 0m1.887s | |
sys 0m0.040s | |
------------------------------------------------------------------------------ | |
Perl | |
real 0m1.344s | |
user 0m1.047s |