{-# LANGUAGE QuasiQuotes #-}
module Language.Javascript.JMacro.Prelude where
import Language.Javascript.JMacro.Base
import Language.Javascript.JMacro.QQ
jmPrelude :: JStat
jmPrelude = [jmacro|
fun withHourglass f {
document.body.style.cursor="wait";
setTimeout(\ {
try {f();} catch(e) {console.log(e);}
document.body.style.cursor="default";},
400);
};
fun confirmIt n f {
var r = confirm("Are you sure you want to " + n + "?");
if(r) {
f();
}
return false;
};
fun memo f {
var tbl = {};
return \x {
var v0 = tbl[x];
if( v0 == null )
{ var v1 = f x;
tbl[x] = v1;
return v1
}
else { return v0 }
}
};
fun mySplit str xs -> map $.trim (xs.split str);
fun unquote open close x {
var special = ["[","]","(",")"];
fun escape c -> (elem c special ? "\\" : "") + c;
var rgx = new RegExp("^"+escape open+"(.*)"+escape close+"$"),
res = x.match(rgx);
if( res != null ) {return res[1]} else {return x}
};
fun head xs -> xs[0];
fun tail xs -> xs.slice(1);
fun init xs {xs.pop(); return xs;};
fun last xs -> xs[xs.length - 1];
fun cons x xs {
xs.unshift(x);
return xs;
};
fun id x -> x;
fun konst x -> \_ -> x;
fun isEmpty x -> x.length == 0;
fun notNull x -> x != null;
fun nullDef def x -> x == null ? def : x;
fun bindNl x f -> x == null ? null : f x;
fun tryNull x notNull isNull -> x != null ? notNull x : isNull();
//
//
//
fun objFoldlEnumLbl f v xs {
var acc = v;
for( var i in xs ) {
if (xs[i] != null) {
var res = f i acc xs[i];
acc = res[0];
if( !res[1] ) { break; }
};
};
return acc;
};
//
fun objFoldlEnum f v xs -> objFoldlEnumLbl (\_ acc x -> f acc x) v xs;
//
fun objFoldlLbl f v xs {
var acc = v;
for( var i in xs) {if (xs[i] != null) {acc = f i acc xs[i]};};
return acc;
};
fun objFoldl f v xs -> objFoldlLbl (\_ acc x -> f acc x) v xs;
fun mapObjVals f xs -> objFoldlLbl (\lbl acc x {acc[lbl] = f x; return acc}) {} xs
fun objLength xs -> objFoldl (\n _ -> n + 1) 0 xs;
fun objIter f xs -> objFoldl (\_ x {f x; return null}) null xs;
fun objIterLbl f xs -> objFoldlLbl (\l _ v {f l v; return null}) null xs;
fun objUnion xs ys {
var res = {};
for (var i in xs) {res[i] = xs[i]};
for (var i in ys) {res[i] = ys[i]};
return res;
};
//
fun foldlEnumIdx f v xs {
var acc = v;
for( var i = 0; i < xs.length; i = i + 1) {
var res = f i acc xs[i];
acc = res[0];
if( !res[1] ) { break; }
};
return acc;
};
fun foldlEnum f v xs -> foldlEnumIdx (\_ acc x -> f acc x) v xs;
fun foldl f v xs { //
var acc = v;
for( var i = 0; i < xs.length; i++) {acc = f acc xs[i];};
return acc;
};
fun foldl2 f v xs ys {
var acc = v;
for( var i = 0; i < xs.length && i < ys.length; i++) {acc = f acc xs[i] ys[i];};
return acc;
};
fun foldr f v xs {
var res = v;
for( var i = xs.length - 1; i >= 0; i = i - 1) {res = f xs[i] res;};
return res;
};
//
fun strFoldr f v xs {
var res = v;
for (var i = xs.length - 1; i >= 0; i = i - 1) {res = f xs.charAt(i) res};
return res;
};
fun max x y -> x > y ? x : y;
fun min x y -> x < y ? x : y;
fun maximumOrNull xs -> (xs.length == 0) ? null : foldl max (head xs) (tail xs);
fun minimumOrNull xs -> (xs.length == 0) ? null : foldl min (head xs) (tail xs);
fun sum x -> foldl (\a b -> a + b) 0 x;
//
fun addIndex xs {
var res = [];
for( var i = 0; i < xs.length; i++) { res.push([xs[i],i]) };
return res;
};
//
fun minimumBy cmp xs -> foldl (\x y -> cmp x y ? x : y) (xs[0]) xs.slice(1);
fun zipWith f xs ys {
var res = [],
l = min xs.length ys.length;
for(var i = 0; i < l; i++) {
res.push(f xs[i] ys[i]);
}
return res;
};
fun zip xs ys -> zipWith (\x y -> [x, y]) xs ys;
fun zip3 xs ys zs {
var res = [],
l = min (min xs.length ys.length) zs.length;
for(var i = 0; i < l; i++) {
res.push([xs[i], ys[i], zs[i]]);
}
return res;
};
fun getTblHash tbl {
var cols = $("th", $(tbl)).map(\_ x -> $(x).text());
return map (\r -> foldl2 (\acc c v {acc[c] = $(v).text(); return acc}) {} cols $("td",$(r))) $("tbody tr", $(tbl))
};
//
fun eq x y {
var x1 = typeof(x) == "string" ? x.toLowerCase() : x,
y1 = typeof(y) == "string" ? y.toLowerCase() : y;
return x1 == y1;
};
//
fun equals x y {
if(x===y) {return true;}
if(typeof x != typeof y) {return false;}
if($.isArray x && $.isArray y) {
for(var n in x) {
if (!(equals x[n] y[n])) {return false;}
}
return true;
}
return x == y;
}
fun map f xs -> foldl (\acc x {acc.push(f x); return acc}) [] xs;
fun filter p xs -> foldl (\acc x {if (p x) {acc.push(x)}; return acc}) [] xs
fun mapFilter p f xs -> foldl (\acc x {if (p x) {acc.push(f x)}; return acc}) [] xs
fun concat xs -> foldl (\acc x -> acc.concat(x) ) [] xs
fun toList xs -> map id xs; //
fun all p xs -> foldlEnum (\_ x -> [p x, p x]) true xs;
fun findWithIdx p xs -> foldlEnumIdx (\i failure x -> p i x ? [x, false] : [failure, true]) null xs;
fun findIdx p xs -> foldlEnumIdx (\i failure x -> p x ? [i, false] : [failure, true]) null xs;
fun find p xs -> findWithIdx (\_ x -> p x) xs;
fun elem x xs -> tryNull (find (\y -> x == y) xs) (konst true) (konst false);
fun isPrefixOf x xs -> xs.search(new RegExp("^"+x)) != -1;
//
fun sortOn f arr {
fun cmpFun x y {
var xv = f x,
yv = f y;
if (xv == yv) {return 0};
if (xv == null) {return 1};
if (yv == null) {return -1};
return xv > yv ? 1 : -1
};
arr.sort(cmpFun);
return arr;
}
fun hashOn f xs {
var hash = {};
fun pushAttr x {
var atr = f x;
if( atr != null ) {
if( hash[atr] == null ) { hash[atr] = [] };
hash[atr].push(x)
};
};
map pushAttr xs;
return hash;
}
fun groupOn f xs {
var hash = hashOn f xs;
return objFoldl (\acc x {acc.push x; return acc;}) [] hash;
}
fun chunkBy x ys -> x >= ys.length ? [ys]
: cons (ys.slice(0,x)) (chunkBy x (ys.slice(x)));
fun transpose xxs {
if (xxs.length == 0) {return []};
if (xxs[0].length == 0) {return transpose (tail xxs);};
var x = xxs[0][0],
xs = xxs[0].slice(1),
xss = xxs.slice(1);
return cons (cons x (map head xss)) (transpose (cons xs (map tail xss))) //
}
//
fun multiIdxSortGen idxs cmpFun modFun xs {
var f = \x y -> \i acc idxsi {
var cmp = cmpFun (modFun x)[i] (modFun y)[i];
return (cmp != 0) ? [idxsi[1] ? cmp : cmp * (-1), false] : [acc, true]
};
xs.sort( \x y -> foldlEnumIdx (f x y) 0 idxs );
};
//
//
fun ols xs ys {
if (xs.length != ys.length) {return null};
var n = xs.length,
sx = sum xs,
sx2 = sum (map (\x -> x*x) xs),
sy = sum ys,
sxy = sum (zipWith (\x y -> x * y) xs ys),
bot = n * sx2 - sx * sx,
m = (n * sxy - sy*sx) / bot,
b = (sy * sx2 - sx * sxy) / bot;
return [m,b];
};
//
fun doRegress xs {
var xvs = map (\x -> x[0]) xs,
yvs = map (\x -> x[1]) xs,
regressres = ols xvs yvs,
m = regressres[0],
b = regressres[1],
yvs1 = map (\x -> m*x+b) xvs,
ymean = mean yvs,
sstot = sum (map (\y -> Math.pow (y - ymean) 2) yvs),
sserr = sum (zipWith (\y f -> Math.pow (y - f) 2) yvs yvs1),
xsNew = zipWith (\x y -> [x,y,x,y.toPrecision(2),""]) xvs yvs1;
return [xsNew, (1 - (sserr/sstot)).toPrecision(4)];
};
fun mean xs {
var res = xs[0];
for (var i = 1; i < xs.length; i++) {
var x = xs[i];
var delta = x - res;
var sweep = i + 1.0;
res = res + (delta / sweep);
//
}
return res;
};
fun stdev xs { //
var n = 0,
mean = 0,
s = 0;
for (var i = 0; i < xs.length; i++) {
n = n + 1;
var delta = xs[i] - mean;
mean = mean + delta/n;
s = s + delta*(xs[i] - mean); //
};
return [Math.sqrt (s/(n - 1)), mean, n];
};
|]