| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.functional; |
| 4 |
|
| 5 |
// Strings are compiled into functions: |
| 6 |
alias isEven = unaryFun!("(a & 1) == 0"); |
| 7 |
assert(isEven(2) && !isEven(1)); |
| 8 |
} |
| 9 |
|
| 10 |
@safe unittest |
| 11 |
{ |
| 12 |
import std.functional; |
| 13 |
|
| 14 |
alias less = binaryFun!("a < b"); |
| 15 |
assert(less(1, 2) && !less(2, 1)); |
| 16 |
alias greater = binaryFun!("a > b"); |
| 17 |
assert(!greater("1", "2") && greater("2", "1")); |
| 18 |
} |
| 19 |
|
| 20 |
pure @safe @nogc nothrow unittest |
| 21 |
{ |
| 22 |
import std.functional; |
| 23 |
|
| 24 |
assert(lessThan(2, 3)); |
| 25 |
assert(lessThan(2U, 3U)); |
| 26 |
assert(lessThan(2, 3.0)); |
| 27 |
assert(lessThan(-2, 3U)); |
| 28 |
assert(lessThan(2, 3U)); |
| 29 |
assert(!lessThan(3U, -2)); |
| 30 |
assert(!lessThan(3U, 2)); |
| 31 |
assert(!lessThan(0, 0)); |
| 32 |
assert(!lessThan(0U, 0)); |
| 33 |
assert(!lessThan(0, 0U)); |
| 34 |
} |
| 35 |
|
| 36 |
@safe unittest |
| 37 |
{ |
| 38 |
import std.functional; |
| 39 |
|
| 40 |
assert(!greaterThan(2, 3)); |
| 41 |
assert(!greaterThan(2U, 3U)); |
| 42 |
assert(!greaterThan(2, 3.0)); |
| 43 |
assert(!greaterThan(-2, 3U)); |
| 44 |
assert(!greaterThan(2, 3U)); |
| 45 |
assert(greaterThan(3U, -2)); |
| 46 |
assert(greaterThan(3U, 2)); |
| 47 |
assert(!greaterThan(0, 0)); |
| 48 |
assert(!greaterThan(0U, 0)); |
| 49 |
assert(!greaterThan(0, 0U)); |
| 50 |
} |
| 51 |
|
| 52 |
@safe unittest |
| 53 |
{ |
| 54 |
import std.functional; |
| 55 |
|
| 56 |
assert(equalTo(0U, 0)); |
| 57 |
assert(equalTo(0, 0U)); |
| 58 |
assert(!equalTo(-1, ~0U)); |
| 59 |
} |
| 60 |
|
| 61 |
@safe unittest |
| 62 |
{ |
| 63 |
import std.functional; |
| 64 |
|
| 65 |
alias gt = reverseArgs!(binaryFun!("a < b")); |
| 66 |
assert(gt(2, 1) && !gt(1, 1)); |
| 67 |
} |
| 68 |
|
| 69 |
@safe unittest |
| 70 |
{ |
| 71 |
import std.functional; |
| 72 |
|
| 73 |
int x = 42; |
| 74 |
bool xyz(int a, int b) { return a * x < b / x; } |
| 75 |
auto foo = &xyz; |
| 76 |
foo(4, 5); |
| 77 |
alias zyx = reverseArgs!(foo); |
| 78 |
assert(zyx(5, 4) == foo(4, 5)); |
| 79 |
} |
| 80 |
|
| 81 |
@safe unittest |
| 82 |
{ |
| 83 |
import std.functional; |
| 84 |
|
| 85 |
alias gt = reverseArgs!(binaryFun!("a < b")); |
| 86 |
assert(gt(2, 1) && !gt(1, 1)); |
| 87 |
int x = 42; |
| 88 |
bool xyz(int a, int b) { return a * x < b / x; } |
| 89 |
auto foo = &xyz; |
| 90 |
foo(4, 5); |
| 91 |
alias zyx = reverseArgs!(foo); |
| 92 |
assert(zyx(5, 4) == foo(4, 5)); |
| 93 |
} |
| 94 |
|
| 95 |
@safe unittest |
| 96 |
{ |
| 97 |
import std.functional; |
| 98 |
|
| 99 |
int abc(int a, int b, int c) { return a * b + c; } |
| 100 |
alias cba = reverseArgs!abc; |
| 101 |
assert(abc(91, 17, 32) == cba(32, 17, 91)); |
| 102 |
} |
| 103 |
|
| 104 |
@safe unittest |
| 105 |
{ |
| 106 |
import std.functional; |
| 107 |
|
| 108 |
int a(int a) { return a * 2; } |
| 109 |
alias _a = reverseArgs!a; |
| 110 |
assert(a(2) == _a(2)); |
| 111 |
} |
| 112 |
|
| 113 |
@safe unittest |
| 114 |
{ |
| 115 |
import std.functional; |
| 116 |
|
| 117 |
int b() { return 4; } |
| 118 |
alias _b = reverseArgs!b; |
| 119 |
assert(b() == _b()); |
| 120 |
} |
| 121 |
|
| 122 |
@safe unittest |
| 123 |
{ |
| 124 |
import std.functional; |
| 125 |
|
| 126 |
import std.algorithm.searching : find; |
| 127 |
import std.uni : isWhite; |
| 128 |
string a = " Hello, world!"; |
| 129 |
assert(find!(not!isWhite)(a) == "Hello, world!"); |
| 130 |
} |
| 131 |
|
| 132 |
@safe unittest |
| 133 |
{ |
| 134 |
import std.functional; |
| 135 |
|
| 136 |
int fun(int a, int b) { return a + b; } |
| 137 |
alias fun5 = partial!(fun, 5); |
| 138 |
assert(fun5(6) == 11); |
| 139 |
// Note that in most cases you'd use an alias instead of a value |
| 140 |
// assignment. Using an alias allows you to partially evaluate template |
| 141 |
// functions without committing to a particular type of the function. |
| 142 |
} |
| 143 |
|
| 144 |
@safe unittest |
| 145 |
{ |
| 146 |
import std.functional; |
| 147 |
|
| 148 |
// Overloads are resolved when the partially applied function is called |
| 149 |
// with the remaining arguments. |
| 150 |
struct S |
| 151 |
{ |
| 152 |
static char fun(int i, string s) { return s[i]; } |
| 153 |
static int fun(int a, int b) { return a * b; } |
| 154 |
} |
| 155 |
alias fun3 = partial!(S.fun, 3); |
| 156 |
assert(fun3("hello") == 'l'); |
| 157 |
assert(fun3(10) == 30); |
| 158 |
} |
| 159 |
|
| 160 |
pure @safe @nogc nothrow unittest |
| 161 |
{ |
| 162 |
import std.functional; |
| 163 |
|
| 164 |
int f(int x, int y, int z) |
| 165 |
{ |
| 166 |
return x + y + z; |
| 167 |
} |
| 168 |
auto cf = curry!f; |
| 169 |
auto cf1 = cf(1); |
| 170 |
auto cf2 = cf(2); |
| 171 |
|
| 172 |
assert(cf1(2)(3) == f(1, 2, 3)); |
| 173 |
assert(cf2(2)(3) == f(2, 2, 3)); |
| 174 |
} |
| 175 |
|
| 176 |
pure @safe @nogc nothrow unittest |
| 177 |
{ |
| 178 |
import std.functional; |
| 179 |
|
| 180 |
//works with callable structs too |
| 181 |
struct S |
| 182 |
{ |
| 183 |
int w; |
| 184 |
int opCall(int x, int y, int z) |
| 185 |
{ |
| 186 |
return w + x + y + z; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
S s; |
| 191 |
s.w = 5; |
| 192 |
|
| 193 |
auto cs = curry(s); |
| 194 |
auto cs1 = cs(1); |
| 195 |
auto cs2 = cs(2); |
| 196 |
|
| 197 |
assert(cs1(2)(3) == s(1, 2, 3)); |
| 198 |
assert(cs1(2)(3) == (1 + 2 + 3 + 5)); |
| 199 |
assert(cs2(2)(3) ==s(2, 2, 3)); |
| 200 |
} |
| 201 |
|
| 202 |
@safe unittest |
| 203 |
{ |
| 204 |
import std.functional; |
| 205 |
|
| 206 |
import std.typecons : Tuple; |
| 207 |
static bool f1(int a) { return a != 0; } |
| 208 |
static int f2(int a) { return a / 2; } |
| 209 |
auto x = adjoin!(f1, f2)(5); |
| 210 |
assert(is(typeof(x) == Tuple!(bool, int))); |
| 211 |
assert(x[0] == true && x[1] == 2); |
| 212 |
} |
| 213 |
|
| 214 |
@safe unittest |
| 215 |
{ |
| 216 |
import std.functional; |
| 217 |
|
| 218 |
import std.algorithm.comparison : equal; |
| 219 |
import std.algorithm.iteration : map; |
| 220 |
import std.array : split; |
| 221 |
import std.conv : to; |
| 222 |
|
| 223 |
// First split a string in whitespace-separated tokens and then |
| 224 |
// convert each token into an integer |
| 225 |
assert(compose!(map!(to!(int)), split)("1 2 3").equal([1, 2, 3])); |
| 226 |
} |
| 227 |
|
| 228 |
@safe unittest |
| 229 |
{ |
| 230 |
import std.functional; |
| 231 |
|
| 232 |
import std.conv : to; |
| 233 |
string foo(int a) { return to!(string)(a); } |
| 234 |
int bar(string a) { return to!(int)(a) + 1; } |
| 235 |
double baz(int a) { return a + 0.5; } |
| 236 |
assert(compose!(baz, bar, foo)(1) == 2.5); |
| 237 |
assert(pipe!(foo, bar, baz)(1) == 2.5); |
| 238 |
|
| 239 |
assert(compose!(baz, `to!(int)(a) + 1`, foo)(1) == 2.5); |
| 240 |
assert(compose!(baz, bar)("1"[]) == 2.5); |
| 241 |
|
| 242 |
assert(compose!(baz, bar)("1") == 2.5); |
| 243 |
|
| 244 |
assert(compose!(`a + 0.5`, `to!(int)(a) + 1`, foo)(1) == 2.5); |
| 245 |
} |
| 246 |
|
| 247 |
@safe nothrow unittest |
| 248 |
{ |
| 249 |
import std.functional; |
| 250 |
|
| 251 |
ulong fib(ulong n) @safe nothrow |
| 252 |
{ |
| 253 |
return n < 2 ? n : memoize!fib(n - 2) + memoize!fib(n - 1); |
| 254 |
} |
| 255 |
assert(fib(10) == 55); |
| 256 |
} |
| 257 |
|
| 258 |
@safe unittest |
| 259 |
{ |
| 260 |
import std.functional; |
| 261 |
|
| 262 |
ulong fact(ulong n) @safe |
| 263 |
{ |
| 264 |
return n < 2 ? 1 : n * memoize!fact(n - 1); |
| 265 |
} |
| 266 |
assert(fact(10) == 3628800); |
| 267 |
} |
| 268 |
|
| 269 |
@safe unittest |
| 270 |
{ |
| 271 |
import std.functional; |
| 272 |
|
| 273 |
ulong factImpl(ulong n) @safe |
| 274 |
{ |
| 275 |
return n < 2 ? 1 : n * factImpl(n - 1); |
| 276 |
} |
| 277 |
alias fact = memoize!factImpl; |
| 278 |
assert(fact(10) == 3628800); |
| 279 |
} |
| 280 |
|
| 281 |
@system unittest |
| 282 |
{ |
| 283 |
import std.functional; |
| 284 |
|
| 285 |
ulong fact(ulong n) |
| 286 |
{ |
| 287 |
// Memoize no more than 8 values |
| 288 |
return n < 2 ? 1 : n * memoize!(fact, 8)(n - 1); |
| 289 |
} |
| 290 |
assert(fact(8) == 40320); |
| 291 |
// using more entries than maxSize will overwrite existing entries |
| 292 |
assert(fact(10) == 3628800); |
| 293 |
} |
| 294 |
|
| 295 |
@safe unittest |
| 296 |
{ |
| 297 |
import std.functional; |
| 298 |
|
| 299 |
static int inc(ref uint num) { |
| 300 |
num++; |
| 301 |
return 8675309; |
| 302 |
} |
| 303 |
|
| 304 |
uint myNum = 0; |
| 305 |
auto incMyNumDel = toDelegate(&inc); |
| 306 |
auto returnVal = incMyNumDel(myNum); |
| 307 |
assert(myNum == 1); |
| 308 |
} |
| 309 |
|
| 310 |
@safe unittest |
| 311 |
{ |
| 312 |
import std.functional; |
| 313 |
|
| 314 |
import std.typecons : tuple; |
| 315 |
|
| 316 |
auto name = tuple("John", "Doe"); |
| 317 |
string full = name.bind!((first, last) => first ~ " " ~ last); |
| 318 |
assert(full == "John Doe"); |
| 319 |
} |
| 320 |
|
| 321 |
@safe unittest |
| 322 |
{ |
| 323 |
import std.functional; |
| 324 |
|
| 325 |
import std.algorithm.comparison : min, max; |
| 326 |
|
| 327 |
struct Pair |
| 328 |
{ |
| 329 |
int a; |
| 330 |
int b; |
| 331 |
} |
| 332 |
|
| 333 |
auto p = Pair(123, 456); |
| 334 |
assert(p.bind!min == 123); // min(p.a, p.b) |
| 335 |
assert(p.bind!max == 456); // max(p.a, p.b) |
| 336 |
} |
| 337 |
|
| 338 |
@safe unittest |
| 339 |
{ |
| 340 |
import std.functional; |
| 341 |
|
| 342 |
import std.algorithm.iteration : map, filter; |
| 343 |
import std.algorithm.comparison : equal; |
| 344 |
import std.typecons : tuple; |
| 345 |
|
| 346 |
auto ages = [ |
| 347 |
tuple("Alice", 35), |
| 348 |
tuple("Bob", 64), |
| 349 |
tuple("Carol", 21), |
| 350 |
tuple("David", 39), |
| 351 |
tuple("Eve", 50) |
| 352 |
]; |
| 353 |
|
| 354 |
auto overForty = ages |
| 355 |
.filter!(bind!((name, age) => age > 40)) |
| 356 |
.map!(bind!((name, age) => name)); |
| 357 |
|
| 358 |
assert(overForty.equal(["Bob", "Eve"])); |
| 359 |
} |
| 360 |
|
| 361 |
@safe unittest |
| 362 |
{ |
| 363 |
import std.functional; |
| 364 |
|
| 365 |
import std.math : abs; |
| 366 |
|
| 367 |
// No explicit `enum` needed. |
| 368 |
float result = ctEval!(abs(-3)); |
| 369 |
assert(result == 3); |
| 370 |
|
| 371 |
// Can be statically asserted. |
| 372 |
static assert(ctEval!(abs(-4)) == 4); |
| 373 |
static assert(ctEval!(abs( 9)) == 9); |
| 374 |
} |
| 375 |
|
| 376 |
@safe unittest |
| 377 |
{ |
| 378 |
import std.functional; |
| 379 |
|
| 380 |
import core.stdc.math : round; |
| 381 |
import std.conv : to; |
| 382 |
import std.math : abs, PI, sin; |
| 383 |
|
| 384 |
// `round` from the C standard library cannot be interpreted at compile |
| 385 |
// time, because it has no available source code. However the function |
| 386 |
// calls preceding `round` can be evaluated during compile time. |
| 387 |
int result = ctEval!(abs(sin(1.0)) * 180 / PI) |
| 388 |
.round() |
| 389 |
.to!int(); |
| 390 |
|
| 391 |
assert(result == 48); |
| 392 |
} |
| 393 |
|