pairs, memoize
This commit is contained in:
@@ -46,6 +46,8 @@ class Kiss {
|
|||||||
k.defAlias("print", Symbol("Prelude.print"));
|
k.defAlias("print", Symbol("Prelude.print"));
|
||||||
k.defAlias("groups", Symbol("Prelude.groups"));
|
k.defAlias("groups", Symbol("Prelude.groups"));
|
||||||
k.defAlias("zip", Symbol("Prelude.zip"));
|
k.defAlias("zip", Symbol("Prelude.zip"));
|
||||||
|
k.defAlias("pairs", Symbol("Prelude.pairs")); // TODO test pairs
|
||||||
|
k.defAlias("memoize", Symbol("Prelude.memoize")); // TODO test memoize
|
||||||
k.defAlias("map", Symbol("Lambda.map"));
|
k.defAlias("map", Symbol("Lambda.map"));
|
||||||
k.defAlias("filter", Symbol("Lambda.filter")); // TODO use truthy as the default filter function
|
k.defAlias("filter", Symbol("Lambda.filter")); // TODO use truthy as the default filter function
|
||||||
k.defAlias("has", Symbol("Lambda.has"));
|
k.defAlias("has", Symbol("Lambda.has"));
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using Std;
|
|||||||
|
|
||||||
import kiss.Operand;
|
import kiss.Operand;
|
||||||
import haxe.ds.Either;
|
import haxe.ds.Either;
|
||||||
|
import haxe.Constraints;
|
||||||
|
|
||||||
/** What functions that process Lists should do when there are more elements than expected **/
|
/** What functions that process Lists should do when there are more elements than expected **/
|
||||||
enum ExtraElementHandling {
|
enum ExtraElementHandling {
|
||||||
@@ -158,7 +159,7 @@ class Prelude {
|
|||||||
return fullGroups;
|
return fullGroups;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function zip<A, B>(a:Array<A>, b:Array<B>, extraHandling = Drop):Array<Array<Dynamic>> {
|
public static function zip(a:Array<Dynamic>, b:Array<Dynamic>, extraHandling = Drop):kiss.List<kiss.List<Dynamic>> {
|
||||||
var max = Math.floor(if (a.length != b.length) {
|
var max = Math.floor(if (a.length != b.length) {
|
||||||
switch (extraHandling) {
|
switch (extraHandling) {
|
||||||
case Throw:
|
case Throw:
|
||||||
@@ -186,6 +187,16 @@ class Prelude {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function pairs(l:kiss.List<Dynamic>, loopAround = false):kiss.List<kiss.List<Dynamic>> {
|
||||||
|
var l1 = l.slice(0, l.length - 1);
|
||||||
|
var l2 = l.slice(1, l.length);
|
||||||
|
if (loopAround) {
|
||||||
|
l1.push(l[-1]);
|
||||||
|
l2.unshift(l[0]);
|
||||||
|
}
|
||||||
|
return zip(l1, l2);
|
||||||
|
}
|
||||||
|
|
||||||
// Ranges with a min, exclusive max, and step size, just like Python.
|
// Ranges with a min, exclusive max, and step size, just like Python.
|
||||||
public static function range(min, max, step):Iterator<Int> {
|
public static function range(min, max, step):Iterator<Int> {
|
||||||
if (step <= 0)
|
if (step <= 0)
|
||||||
@@ -224,6 +235,23 @@ class Prelude {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Based on: http://old.haxe.org/doc/snip/memoize
|
||||||
|
public static function memoize(func:Function, ?caller:Dynamic):Function {
|
||||||
|
var argMap = new Map<String, Dynamic>();
|
||||||
|
var f = (args:Array<Dynamic>) -> {
|
||||||
|
var argString = args.join('|');
|
||||||
|
return if (argMap.exists(argString)) {
|
||||||
|
argMap[argString];
|
||||||
|
} else {
|
||||||
|
var ret = Reflect.callMethod(caller, func, args);
|
||||||
|
argMap[argString] = ret;
|
||||||
|
ret;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
f = Reflect.makeVarArgs(f);
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
public static function print<T>(v:T) {
|
public static function print<T>(v:T) {
|
||||||
#if (sys || hxnodejs)
|
#if (sys || hxnodejs)
|
||||||
Sys.println(v);
|
Sys.println(v);
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
(defun differences [:kiss.List<Int> ratings]
|
(defun differences [:kiss.List<Int> ratings]
|
||||||
(for idx (range (- ratings.length 1))
|
(for pair (pairs ratings) (- 0 (apply - pair))))
|
||||||
(- (nth ratings (+ idx 1)) (nth ratings idx))))
|
|
||||||
|
|
||||||
(defun distribution [:kiss.List<Int> numbers]
|
(defun distribution [:kiss.List<Int> numbers]
|
||||||
(let [:Map<Int,Int> dist (new Map)]
|
(let [:Map<Int,Int> dist (new Map)]
|
||||||
|
|||||||
Reference in New Issue
Block a user