-
Notifications
You must be signed in to change notification settings - Fork 15
/
d_funcs.d
326 lines (259 loc) · 8.17 KB
/
d_funcs.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
Only exists to test the wrapping functionality Contains functions
with regular D types that will get wrapped so they can be called by
the spreadsheet.
*/
import xlld;
import std.datetime: DateTime;
import std.typecons: Tuple;
double[][] dlookup(string[][] haystack, string[] needles, double columnNumberD) nothrow
{
import std.exception;
import std.algorithm:map,countUntil;
import std.range:repeat,transposed;
import std.array:array;
import std.conv:to;
double toDouble(long pos)
{
return (pos==-1) ? double.nan : pos.to!double + 1.0;
}
try
{
auto columnNumber = columnNumberD.to!int;
auto haystackColumn = haystack.map!(row => row[columnNumber-1].to!string).array;
return needles.map!( needle => [toDouble(haystackColumn.countUntil(needle).to!long)]).array;
}
catch(Exception e)
{
return ([double.nan].repeat(needles.length)).array;
}
}
@Register(ArgumentText("Array to add"),
HelpTopic("https://github.com/symmetryinvestments/excel-d!0"),
FunctionHelp("Adds all cells in an array"),
ArgumentHelp(["The array to add"]))
double FuncAddEverything(double[][] args) nothrow @nogc {
import std.algorithm: fold;
import std.math: isNaN;
double ret = 0;
foreach(row; args)
ret += row.fold!((a, b) => b.isNaN ? 0.0 : a + b)(0.0);
return ret;
}
// @Dispose is used to tell the framework how to free memory that is dynamically
// allocated by the D function. After returning, the value is converted to an
// Excel type sand the D value is freed using the lambda defined here.
@Dispose!((ret) {
import std.experimental.allocator.mallocator: Mallocator;
import std.experimental.allocator: dispose;
Mallocator.instance.dispose(ret);
})
double[] FuncReturnArrayNoGc(scope double[] numbers) @nogc @safe nothrow {
import std.experimental.allocator.mallocator: Mallocator;
import std.experimental.allocator: makeArray;
import std.algorithm: map;
try {
// Allocate memory here in order to return an array of doubles.
// The memory will be freed after the call by calling the
// function in `@Dispose` above
return Mallocator.instance.makeArray(numbers.map!(a => a * 2));
} catch(Exception _) {
return [];
}
}
double[][] FuncTripleEverything(double[][] args) nothrow {
double[][] ret;
ret.length = args.length;
foreach(i; 0 .. args.length) {
ret[i].length = args[i].length;
foreach(j; 0 .. args[i].length)
ret[i][j] = args[i][j] * 3;
}
return ret;
}
double FuncAllLengths(string[][] args) nothrow @nogc {
import std.algorithm: fold;
double ret = 0;
foreach(row; args)
ret += row.fold!((a, b) => a + b.length)(0.0);
return ret;
}
double[][] FuncLengths(string[][] args) nothrow {
double[][] ret;
ret.length = args.length;
foreach(i; 0 .. args.length) {
ret[i].length = args[i].length;
foreach(j; 0 .. args[i].length)
ret[i][j] = args[i][j].length;
}
return ret;
}
string[][] FuncBob(string[][] args) nothrow {
string[][] ret;
ret.length = args.length;
foreach(i; 0 .. args.length) {
ret[i].length = args[i].length;
foreach(j; 0 .. args[i].length)
ret[i][j] = args[i][j] ~ "bob";
}
return ret;
}
double FuncDoubleSlice(double[] arg) nothrow @nogc {
return arg.length;
}
double FuncStringSlice(string[] arg) nothrow @nogc {
return arg.length;
}
double[] FuncSliceTimes3(double[] arg) nothrow {
import std.algorithm;
import std.array;
return arg.map!(a => a * 3).array;
}
string[] StringsToStrings(string[] args) nothrow {
import std.algorithm;
import std.array;
return args.map!(a => a ~ "foo").array;
}
string StringsToString(string[] args) nothrow {
import std.string;
return args.join(", ");
}
string StringToString(string arg) nothrow {
return arg ~ "bar";
}
private string shouldNotBeAProblem(string, string[]) nothrow {
return "";
}
string ManyToString(string arg0, string arg1, string arg2) nothrow {
return arg0 ~ arg1 ~ arg2;
}
double FuncThrows(double) {
throw new Exception("oops");
}
Any[][] FirstOfTwoAnyArrays(Any[][] lhs, Any[][] rhs) nothrow {
return lhs;
}
string[][] FirstOfTwoAnyArraysToString(Any[][] testarg, Any[][] rhs) nothrow {
import std.array, std.algorithm, std.conv;
try {
return testarg.map!(map!(to!string)).map!array.array;
} catch(Exception e) {
return [[e.msg]];
}
}
string DateTimeToString(DateTime dt) @safe {
import std.conv: text;
return text("year: ", dt.year, ", month: ", dt.month, ", day: ", dt.day,
", hour: ", dt.hour, ", minute: ", dt.minute, ", second: ", dt.second);
}
string DateTimesToString(scope DateTime[] dts) @safe {
import std.conv: text;
import std.algorithm: map;
import std.string: join;
return dts.map!(dt => text("year: ", dt.year, ", month: ", dt.month, ", day: ", dt.day,
", hour: ", dt.hour, ", minute: ", dt.minute, ", second: ", dt.second)).join("\n");
}
double FuncTwice(double d) @safe nothrow @nogc {
return d * 2;
}
@Async
double FuncTwiceAsync(double d) {
import core.thread;
Thread.sleep(5.seconds);
return d * 2;
}
double IntToDouble(int i) {
return i * 2;
}
int DoubleToInt(double d) {
return cast(int)(d * 2);
}
int IntToInt(int i) @safe nothrow @nogc {
return i * 2;
}
DateTime[] DateTimes(int year, int month, int day) {
return [
DateTime(year, month, day),
DateTime(year + 1, month + 1, day + 1),
DateTime(year + 2, month + 2, day + 2),
];
}
string FuncCaller() @safe {
import xlld.func.xlf: xlfCaller = caller;
import xlld.sdk.xlcall: XlType;
import std.conv: text;
auto caller = xlfCaller;
switch(caller.xltype) with(XlType) {
default:
return "Unknown caller type";
case xltypeSRef:
return text("Called from cell. Rows: ",
caller.val.sref.ref_.rwFirst, " .. ", caller.val.sref.ref_.rwLast,
" Cols: ", caller.val.sref.ref_.colFirst, " .. ", caller.val.sref.ref_.colLast);
case xltypeRef:
return "Called from a multi-cell array formula";
}
}
string FuncCallerAdjacent() @safe {
import xlld.func.xl: Coerced;
import xlld.func.xlf: caller;
import xlld.sdk.xlcall: XlType;
import std.exception: enforce;
auto res = caller;
enforce(res.xltype == XlType.xltypeSRef);
++res.val.sref.ref_.colFirst;
++res.val.sref.ref_.colLast;
auto coerced = Coerced(res);
return "Guy next to me: " ~ coerced.toString;
}
double[] Doubles() @safe {
return [33.3, 123.0];
}
enum MyEnum { foo, bar, baz }
string FuncEnumArg(MyEnum val) @safe {
import std.conv: text;
return "prefix_" ~ val.text;
}
MyEnum FuncEnumRet(int i) @safe {
return cast(MyEnum)i;
}
struct Point { int x, y; }
int FuncPointArg(Point point) @safe {
return point.x + point.y;
}
Point FuncPointRet(int x, int y) @safe {
return Point(x + 1, y + 2);
}
auto FuncSimpleTupleRet(double d, string s) @safe {
import std.typecons: tuple;
return tuple(d, s);
}
auto FuncComplexTupleRet(int d1, int d2) @safe {
import std.typecons: tuple;
return tuple([DateTime(2017, 1, d1), DateTime(2017, 2, d1)],
[DateTime(2018, 1, d1), DateTime(2018, 2, d2)]);
}
int FuncAddOptional(int i, int j = 42) @safe {
return i + j;
}
string FuncAppendOptional(scope string a, scope string b = "quux") @safe {
return a ~ b;
}
auto FuncVector(int i) @safe @nogc {
import automem.vector: vector;
import std.range: iota;
import std.experimental.allocator.mallocator: Mallocator;
return vector!Mallocator(i.iota);
}
auto FuncVector2D(int i) @safe @nogc {
import automem.vector: vector;
import std.range: iota;
import std.experimental.allocator.mallocator: Mallocator;
return vector!Mallocator(vector!Mallocator(i, i, i),
vector!Mallocator(i + 1, i + 1, i + 1));
}
auto FuncStringVector(int i) @safe @nogc {
import automem.vector: vector;
import std.experimental.allocator.mallocator: Mallocator;
return vector!Mallocator("hi");
}