Last active
January 5, 2023 18:17
-
-
Save bobzhang/9f27a5a0bd730e8d3503bf5d058a58a7 to your computer and use it in GitHub Desktop.
Type safe Alt-JS language comparison
This file contains hidden or 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
A list of languages which compile to JS (Elm, Purescript, OCaml) | |
(Inspired by this thread: https://groups.google.com/forum/#!topic/elm-discuss/Um7WIBTq9xU) | |
They all support curry calling convention by default. | |
Some interesting results: | |
1. `min` is curried function, only OCaml(BuckleScript) managed to optimize this overhead. | |
2. All optimize the self tail call | |
3. Only BuckleScript and PureScript type-specialized comparison functoin (>=) and inlined | |
4. PureScript generate a raise function which does not make sense | |
5. Only BuckleScript and PureScript generates module by module | |
Try websites: | |
http://elm-lang.org/try | |
http://try.purescript.org/ | |
http://bloomberg.github.io/bucklescript/js-demo/ | |
Only BuckleScript work offline (the compiler is compiled into JS too) |
This file contains hidden or 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
range : Int | |
range = 1000000000 | |
testProg : Int -> Int | |
testProg n = -- do some work | |
let lmt = min (n + 100000000) range in | |
let loop i = | |
if i >= lmt then i else | |
loop (i + 1) in loop n |
This file contains hidden or 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 _user$project$Temp1482759649866537$range = 1000000000; | |
var _user$project$Temp1482759649866537$testProg = function (n) { | |
var lmt = A2(_elm_lang$core$Basics$min, n + 10000000000, _user$project$Temp1482759649866537$range); | |
var loop = function (i) { | |
loop: | |
while (true) { | |
if (_elm_lang$core$Native_Utils.cmp(i, lmt) > -1) { | |
return i; | |
} else { | |
var _v0 = i + 1; | |
i = _v0; | |
continue loop; | |
} | |
} | |
}; | |
return loop(n); | |
}; |
This file contains hidden or 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 range = 1000000000 | |
let testProg n = | |
let lmt = Pervasives.min (n + 100000000) range | |
in | |
let rec loop i = | |
if i >= lmt | |
then i | |
else loop (i + 1) | |
in | |
loop n |
This file contains hidden or 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
// Generated by BUCKLESCRIPT VERSION 1.2.1 , PLEASE EDIT WITH CARE | |
'use strict'; | |
var Pervasives = require("stdlib/pervasives"); | |
function testProg(n) { | |
var lmt = Pervasives.min(n + 100000000 | 0, 1000000000); | |
var _i = n; | |
while(true) { | |
var i = _i; | |
if (i >= lmt) { | |
return i; | |
} | |
else { | |
_i = i + 1 | 0; | |
continue ; | |
} | |
}; | |
} | |
var range = 1000000000; | |
exports.range = range; | |
exports.testProg = testProg; |
This file contains hidden or 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
module Main where | |
import Prelude | |
range = 1000000000 | |
testProg n = -- do some work | |
let lmt = min (n + 100000000) range | |
in | |
let loop i = | |
if i >= lmt | |
then i | |
else loop (i + 1) | |
in | |
loop n |
This file contains hidden or 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
"use strict"; | |
var Prelude = require("../Prelude"); | |
var Data_Ord = require("../Data.Ord"); | |
var Data_Semiring = require("../Data.Semiring"); | |
var range = 1000000000; | |
var testProg = function (n) { | |
var lmt = Data_Ord.min(Data_Ord.ordInt)(n + 100000000 | 0)(range); | |
var loop = function (__copy_i) { | |
var i = __copy_i; | |
tco: while (true) { | |
var $0 = i >= lmt; | |
if ($0) { | |
return i; | |
}; | |
if (!$0) { | |
var __tco_i = i + 1 | 0; | |
i = __tco_i; | |
continue tco; | |
}; | |
throw new Error("Failed pattern match at Main line 9, column 8 - line 11, column 26: " + [ $0.constructor.name ]); | |
}; | |
}; | |
return loop(n); | |
}; | |
module.exports = { | |
range: range, | |
testProg: testProg | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Continue: Specialize constructor, test and projector of
Bool
andMaybe
.