1 /**
2 Capture variables with value semantics, useful in nogc code
3 */4 moduleddash.lang.capture;
5 6 ///7 @nogcunittest {
8 importstd.range: only;
9 importstd.algorithm: map;
10 autoxs = only(1, 2, 3);
11 inta = 2, b = 3;
12 xs.capture(a, b).map!(unpack!((x, a, b) => x * a * b));
13 }
14 15 /**
16 The capture function takes a range as the first argument and a list of arguments you
17 want to capture by value to pass along to another range
18 19 Since:
20 - 0.0.1
21 */22 autocapture(Range, Captures...)(autorefRangerange, autorefCapturescaptures) {
23 stringmix() {
24 importstd.conv: to;
25 strings = "zip(range, ";
26 staticforeach (i; 0 .. Captures.length) {
27 s ~= "repeat(captures[" ~ i.to!string ~ "]),";
28 }
29 s ~= ")";
30 returns;
31 }
32 importstd.range: repeat, zip, only;
33 returnmixin(mix());
34 }
35 36 /**
37 Complements the `capture` function by allowing you to unpack a capture tuple in the function
38 you want to call it from.
39 40 Since:
41 - 0.0.1
42 */43 templateunpack(aliasfunc) {
44 importstd.typecons: isTuple;
45 autounpack(TupleType)(TupleTypetup) if (isTuple!TupleType) {
46 returnfunc(tup.expand);
47 }
48 }