From f214f3baa2f3b613f39ded5ac615a887603aa05c Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 16 Apr 2019 07:20:52 +0200 Subject: [PATCH] Add algorithm.iteration.foldl --- dub.json | 2 +- encoding/dub.json | 2 +- meta/dub.json | 2 +- middle/dub.json | 2 +- os/dub.json | 2 +- source/tanya/algorithm/iteration.d | 53 ++++++++++++++++++++++++++++++ sys/dub.json | 2 +- test/dub.json | 2 +- 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"] }