summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2019-04-16 07:20:52 +0200
committerEugen Wissner <belka@caraus.de>2019-04-16 07:20:52 +0200
commitf214f3baa2f3b613f39ded5ac615a887603aa05c (patch)
tree77a23a89e8d327a015f18f4f58ac07fa1fc8e9ec
parentf66935f40d58e33c2882f5899186dce743bc91ad (diff)
downloadtanya-f214f3baa2f3b613f39ded5ac615a887603aa05c.tar.gz
Add algorithm.iteration.foldl
-rw-r--r--dub.json2
-rw-r--r--encoding/dub.json2
-rw-r--r--meta/dub.json2
-rw-r--r--middle/dub.json2
-rw-r--r--os/dub.json2
-rw-r--r--source/tanya/algorithm/iteration.d53
-rw-r--r--sys/dub.json2
-rw-r--r--test/dub.json2
8 files changed, 60 insertions, 7 deletions
diff --git a/dub.json b/dub.json
index f0186df..1e1b393 100644
--- a/dub.json
+++ b/dub.json
@@ -79,7 +79,7 @@
}
],
- "dflags": ["-dip1000"],
+ "dflags-dmd": ["-dip1000"],
"libs-windows": ["advapi32"],
"libs-windows-x86_mscoff": ["iphlpapi"],
diff --git a/encoding/dub.json b/encoding/dub.json
index 441f29d..717656e 100644
--- a/encoding/dub.json
+++ b/encoding/dub.json
@@ -13,5 +13,5 @@
"importPaths": [
"."
],
- "dflags": ["-dip1000"]
+ "dflags-dmd": ["-dip1000"]
}
diff --git a/meta/dub.json b/meta/dub.json
index beca5d5..4b7651b 100644
--- a/meta/dub.json
+++ b/meta/dub.json
@@ -9,5 +9,5 @@
"importPaths": [
"."
],
- "dflags": ["-dip1000"]
+ "dflags-dmd": ["-dip1000"]
}
diff --git a/middle/dub.json b/middle/dub.json
index f490141..d712f7f 100644
--- a/middle/dub.json
+++ b/middle/dub.json
@@ -19,5 +19,5 @@
"importPaths": [
"."
],
- "dflags": ["-dip1000"]
+ "dflags-dmd": ["-dip1000"]
}
diff --git a/os/dub.json b/os/dub.json
index 31537a1..18fe17a 100644
--- a/os/dub.json
+++ b/os/dub.json
@@ -13,5 +13,5 @@
"importPaths": [
"."
],
- "dflags": ["-dip1000"]
+ "dflags-dmd": ["-dip1000"]
}
diff --git a/source/tanya/algorithm/iteration.d b/source/tanya/algorithm/iteration.d
index f759e1d..0e91c5d 100644
--- a/source/tanya/algorithm/iteration.d
+++ b/source/tanya/algorithm/iteration.d
@@ -727,3 +727,56 @@ auto singleton(E)(return ref E element)
singleChar.popFront();
assert(singleChar.empty);
}
+
+/**
+ * Accumulates all elements of a range using a function.
+ *
+ * $(D_PSYMBOL foldl) takes a function, an input range and the initial value.
+ * The function takes this initial value and the first element of the range (in
+ * this order), puts them together and returns the result. The return
+ * type of the function should be the same as the type of the initial value.
+ * This is than repeated for all the remaining elements of the range, whereby
+ * the value returned by the passed function is used at the place of the
+ * initial value.
+ *
+ * $(D_PSYMBOL foldl) accumulates from left to right.
+ *
+ * Params:
+ * F = Callable accepting the accumulator and a range element.
+ */
+template foldl(F...)
+if (F.length == 1)
+{
+ /**
+ * Params:
+ * R = Input range type.
+ * T = Type of the accumulated value.
+ * range = Input range.
+ * init = Initial value.
+ *
+ * Returns: Accumulated value.
+ */
+ T foldl(R, T)(R range, auto ref T init)
+ if (isInputRange!R && !isInfinite!R)
+ {
+ if (range.empty)
+ {
+ return init;
+ }
+ else
+ {
+ auto acc = F[0](init, range.front);
+ range.popFront;
+ return foldl(range, acc);
+ }
+ }
+}
+
+///
+@nogc nothrow pure @safe unittest
+{
+ int[3] range = [1, 2, 3];
+ const actual = foldl!((acc, x) => acc + x)(range[], 0);
+
+ assert(actual == 6);
+}
diff --git a/sys/dub.json b/sys/dub.json
index 3db3888..9f7099d 100644
--- a/sys/dub.json
+++ b/sys/dub.json
@@ -9,5 +9,5 @@
"importPaths": [
"."
],
- "dflags": ["-dip1000"]
+ "dflags-dmd": ["-dip1000"]
}
diff --git a/test/dub.json b/test/dub.json
index e0fc8af..c988bd0 100644
--- a/test/dub.json
+++ b/test/dub.json
@@ -13,5 +13,5 @@
"importPaths": [
"."
],
- "dflags": ["-dip1000"]
+ "dflags-dmd": ["-dip1000"]
}