Skip to content

Instantly share code, notes, and snippets.

@sinelaw
Last active November 5, 2015 23:21
Show Gist options
  • Save sinelaw/e9b394cade8642e4a9c6 to your computer and use it in GitHub Desktop.
Save sinelaw/e9b394cade8642e4a9c6 to your computer and use it in GitHub Desktop.
/*jsl:option explicit*/
"use strict";
// /////////////////////////////////////////////////////////////////////
// // JQUERY EXTENSIONS
/* Removed this part.. */
/////////////////////////////////////////////////////////////////////
// STRING FORMATTING
// showTime : (Number -> String)
function /* export */ showTime(x) // :: Double -> String
{
// digits : (d -> String)
// s : String
function digits(x){var s = String(x); return s.length == 1 ? "0" + s : s;}
if (x >= 3600)
{
x = Math.round(x / 60);
return String(Math.floor(x / 60)) + "h" + digits(x % 60) + "m";
}
else if (x >= 60)
{
x = Math.round(x);
return String(Math.floor(x / 60)) + "m" + digits(x % 60) + "s";
}
else
return x.toFixed(2) + "s";
}
// showPerc : (Number -> String)
function /* export */ showPerc(x) // :: Double -> String
{
return (x*100).toFixed(2) + "%";
}
// plural : ((Number,String,String) -> String)
function /* export */ plural(n,not1,is1) // :: Int -> Maybe String -> Maybe String -> String
{
return n == 1
? (!Boolean(is1) ? "" : is1)
: (!Boolean(not1) ? "s" : not1);
}
/////////////////////////////////////////////////////////////////////
// MISC
/* sum : ( { get length: Number
, get =[]: Rnr.(Number -> Number) | l} -> Number) */
function sum(xs) // :: Num a => [a] -> a
{
// res : Mut Number
var res = 0;
// i : Mut Number
var i = 0;
for (i = 0; i < xs.length; i++)
res += xs[i];
return res;
}
/* testRegExp : (( { get test: Roo.(o -> p) | q}
,o) -> p) */
function testRegExp(r, s)
{
// type-casing not supported
// if (typeof r == "string")
// return s.indexOf(r) != -1;
// else
return r.test(s);
}
/* execRegExp : (( { get exec: Rpi.(t -> u) | v}
,t) -> u) */
function execRegExp(r, s)
{
// type-casing not supported
// if (typeof r == "string")
// return s.indexOf(r) == -1 ? null : [];
// else
return r.exec(s);
}
/* listEq : (( { get length: Number
, get =[]: { get length: Number
, get =[]: Rnr.(Number -> a`) | y}.(Number -> a`) | y}
, { get length: Number
, get =[]: { get length: Number
, get =[]: Rnr.(Number -> a`) | aa}.(Number -> a`) | aa}) -> Boolean) */
function listEq(xs, ys) // :: Eq a => [a] -> [a] -> Bool
{
if (xs.length != ys.length) return false;
// i : Mut Number
var i = 0;
for (i = 0; i < xs.length; i++)
{
if (xs[i] != ys[i])
return false;
}
return true;
}
/* cache : (((ae -> String)
,(ae -> Boolean)) -> (ae -> Boolean)) */
function cache(str, f) // :: (k -> String) -> (k -> v) -> (k -> v)
{
// cache : StringMap Boolean
var cache = {};
return function(k){
// s : String
var s = str(k);
if (!cache[s])
// if (!(s in cache))
cache[s] = f(k);
return cache[s];
};
}
/* recordEq : (( { get =[]: Raab.(String -> ao) | an}
, { get =[]: { get =[]: Raab.(String -> ao) | ap}.(String -> ao) | ap}) -> Boolean) */
function recordEq(xs, ys) // :: Record -> Record -> Bool
{
/* f : (( { get =[]: Raab.(String -> ak) | aj}
, { get =[]: { get =[]: Raab.(String -> ak) | al}.(String -> ak) | al}) -> Boolean) */
function f(a,b)
{
// s : Mut String
var s = '';
for (s in a)
{
if (a[s] != b[s]) return false;
}
return true;
}
return f(xs,ys) && f(ys,xs);
}
// recordCopy : ( { get =[]: { get =[]: Raab.(String -> as) | at}.(String -> as) | at} -> StringMap as)
function recordCopy(xs) // :: Record -> Record
{
// res : StringMap as
var res = {};
// s : Mut String
var s = '';
for (s in xs)
res[s] = xs[s];
return res;
}
/* recordUnion : (( { get =[]: { get =[]: Raab.(String -> ax) | ay}.(String -> ax) | ay}
, { get =[]: { get =[]: Raab.(String -> ax) | aw}.(String -> ax) | aw}) -> StringMap ax) */
function recordUnion(xs,ys) // :: Record -> Record -> Record -- left biased
{
// res : StringMap ax
var res = recordCopy(ys);
// s : Mut String
var s = '';
for (s in xs)
res[s] = xs[s];
return res;
}
/* concatNub : ( { get length: Number
, get =[]: { get length: Number
, get =[]: Rnr.(Number -> { get length: Number
, get =[]: { get length: Number
, get =[]: Rnr.(Number -> String) | bc}.(Number -> String) | bc}) | bb}.(Number -> { get length: Number
, get =[]: { get length: Number
, get =[]: Rnr.(Number -> String) | bc}.(Number -> String) | bc}) | bb} -> [String]) */
function concatNub(xs) // :: Eq a => [[a]] -> [a]
{
// res : [String]
var res = [];
// seen : StringMap Boolean
var seen = {};
// i : Mut Number
var i = 0;
// j : Mut Number
var j = 0;
for (i = 0; i < xs.length; i++)
{
/* x : { get length: Number
, get =[]: { get length: Number
, get =[]: Rnr.(Number -> String) | bc}.(Number -> String) | bc} */
var x = xs[i];
for (j = 0; j < x.length; j++)
{
// e : String
var e = x[j];
if (!(seen[e]))
// if (!(e in seen))
{
seen[e] = true;
res.push(e);
}
}
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment