Improve (groups)
This commit is contained in:
@@ -49,6 +49,8 @@ class Kiss {
|
|||||||
|
|
||||||
// Helpful aliases
|
// Helpful aliases
|
||||||
k.defAlias("print", Symbol("Prelude.print"));
|
k.defAlias("print", Symbol("Prelude.print"));
|
||||||
|
k.defAlias("groups", Symbol("Prelude.groups"));
|
||||||
|
|
||||||
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"));
|
||||||
|
|||||||
@@ -5,6 +5,13 @@ using Std;
|
|||||||
import kiss.Operand;
|
import kiss.Operand;
|
||||||
import haxe.ds.Either;
|
import haxe.ds.Either;
|
||||||
|
|
||||||
|
/** What functions that process Lists should do when there are more elements than expected **/
|
||||||
|
enum ExtraElementHandling {
|
||||||
|
Keep; // Keep the extra elements
|
||||||
|
Drop; // Drop the extra elements
|
||||||
|
Throw; // Throw an error
|
||||||
|
}
|
||||||
|
|
||||||
class Prelude {
|
class Prelude {
|
||||||
static function variadic(op:(Operand, Operand) -> Null<Operand>, comparison = false):(Array<Operand>) -> Dynamic {
|
static function variadic(op:(Operand, Operand) -> Null<Operand>, comparison = false):(Array<Operand>) -> Dynamic {
|
||||||
return (l:kiss.List<Operand>) -> switch (Lambda.fold(l.slice(1), op, l[0])) {
|
return (l:kiss.List<Operand>) -> switch (Lambda.fold(l.slice(1), op, l[0])) {
|
||||||
@@ -129,7 +136,7 @@ class Prelude {
|
|||||||
|
|
||||||
public static var areEqual = variadic(_areEqual, true);
|
public static var areEqual = variadic(_areEqual, true);
|
||||||
|
|
||||||
public static function groups<T>(a:Array<T>, size, keepRemainder = false) {
|
public static function groups<T>(a:Array<T>, size, extraHandling = Drop) {
|
||||||
var numFullGroups = Math.floor(a.length / size);
|
var numFullGroups = Math.floor(a.length / size);
|
||||||
var fullGroups = [
|
var fullGroups = [
|
||||||
for (num in 0...numFullGroups) {
|
for (num in 0...numFullGroups) {
|
||||||
@@ -138,9 +145,16 @@ class Prelude {
|
|||||||
a.slice(start, end);
|
a.slice(start, end);
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
if (a.length % size != 0 && keepRemainder) {
|
if (a.length % size != 0) {
|
||||||
fullGroups.push(a.slice(numFullGroups * size));
|
switch (extraHandling) {
|
||||||
|
case Throw:
|
||||||
|
throw 'groups was given a non-divisible number of elements: $a, $size';
|
||||||
|
case Keep:
|
||||||
|
fullGroups.push(a.slice(numFullGroups * size));
|
||||||
|
case Drop:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fullGroups;
|
return fullGroups;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -115,10 +115,10 @@
|
|||||||
(the Int 5))
|
(the Int 5))
|
||||||
|
|
||||||
(defun myGroups1 []
|
(defun myGroups1 []
|
||||||
(Prelude.groups [1 2 3 4] 2))
|
(groups [1 2 3 4] 2))
|
||||||
|
|
||||||
(defun myGroups2 []
|
(defun myGroups2 []
|
||||||
(Prelude.groups [1 2 3 4] 3 true))
|
(groups [1 2 3 4] 3 Keep))
|
||||||
|
|
||||||
(defun _testLet []
|
(defun _testLet []
|
||||||
(let [a 5
|
(let [a 5
|
||||||
|
|||||||
Reference in New Issue
Block a user