Skip to content

Commit 4b26eb2

Browse files
authored
Merge pull request #6454 from JackStouffer/functional-docs
Added Params and Returns to std.functional merged-on-behalf-of: Petar Kirov <ZombineDev@users.noreply.github.com>
2 parents b2ca647 + 626cabc commit 4b26eb2

File tree

2 files changed

+100
-29
lines changed

2 files changed

+100
-29
lines changed

.dscanner.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ properly_documented_public_functions="-etc.c.odbc.sql,\
346346
-std.experimental.logger.core,\
347347
-std.experimental.logger.filelogger,\
348348
-std.format,\
349-
-std.functional,\
350349
-std.getopt,\
351350
-std.internal.cstring,\
352351
-std.internal.math.biguintcore,\

std/functional.d

Lines changed: 100 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,19 @@ private template needOpCallAlias(alias fun)
9393
}
9494

9595
/**
96-
Transforms a string representing an expression into a unary
97-
function. The string must either use symbol name `a` as
96+
Transforms a `string` representing an expression into a unary
97+
function. The `string` must either use symbol name `a` as
9898
the parameter or provide the symbol via the `parmName` argument.
99-
If `fun` is not a string, `unaryFun` aliases itself away to `fun`.
100-
*/
10199
100+
Params:
101+
fun = a `string` or a callable
102+
parmName = the name of the parameter if `fun` is a string. Defaults
103+
to `"a"`.
104+
Returns:
105+
If `fun` is a `string`, a new single parameter function
106+
107+
If `fun` is not a `string`, an alias to `fun`.
108+
*/
102109
template unaryFun(alias fun, string parmName = "a")
103110
{
104111
static if (is(typeof(fun) : string))
@@ -176,13 +183,20 @@ template unaryFun(alias fun, string parmName = "a")
176183
}
177184

178185
/**
179-
Transforms a string representing an expression into a binary function. The
180-
string must either use symbol names `a` and `b` as the parameters or
186+
Transforms a `string` representing an expression into a binary function. The
187+
`string` must either use symbol names `a` and `b` as the parameters or
181188
provide the symbols via the `parm1Name` and `parm2Name` arguments.
182-
If `fun` is not a string, `binaryFun` aliases itself away to
183-
`fun`.
184-
*/
185189
190+
Params:
191+
fun = a `string` or a callable
192+
parm1Name = the name of the first parameter if `fun` is a string.
193+
Defaults to `"a"`.
194+
parm2Name = the name of the second parameter if `fun` is a string.
195+
Defaults to `"b"`.
196+
Returns:
197+
If `fun` is not a string, `binaryFun` aliases itself away to
198+
`fun`.
199+
*/
186200
template binaryFun(alias fun, string parm1Name = "a",
187201
string parm2Name = "b")
188202
{
@@ -531,8 +545,13 @@ alias equalTo = safeOp!"==";
531545
assert(!equalTo(-1, ~0U));
532546
}
533547
/**
534-
N-ary predicate that reverses the order of arguments, e.g., given
535-
$(D pred(a, b, c)), returns $(D pred(c, b, a)).
548+
N-ary predicate that reverses the order of arguments, e.g., given
549+
$(D pred(a, b, c)), returns $(D pred(c, b, a)).
550+
551+
Params:
552+
pred = A callable
553+
Returns:
554+
A function which calls `pred` after reversing the given parameters
536555
*/
537556
template reverseArgs(alias pred)
538557
{
@@ -600,10 +619,15 @@ template reverseArgs(alias pred)
600619

601620
// @@@DEPRECATED_2.089@@@
602621
/**
603-
Binary predicate that reverses the order of arguments, e.g., given
604-
$(D pred(a, b)), returns $(D pred(b, a)).
622+
Binary predicate that reverses the order of arguments, e.g., given
623+
$(D pred(a, b)), returns $(D pred(b, a)).
605624
606-
$(RED DEPRECATED: Use $(LREF reverseArgs))
625+
$(RED DEPRECATED: Use $(LREF reverseArgs))
626+
627+
Params:
628+
pred = A callable
629+
Returns:
630+
A function which calls `pred` after reversing the given parameters
607631
*/
608632
deprecated("Use `reverseArgs`. `binaryReverseArgs` will be removed in 2.089.")
609633
template binaryReverseArgs(alias pred)
@@ -637,6 +661,12 @@ deprecated
637661

638662
/**
639663
Negates predicate `pred`.
664+
665+
Params:
666+
pred = A string or a callable
667+
Returns:
668+
A function which calls `pred` and returns the logical negation of its
669+
return value.
640670
*/
641671
template not(alias pred)
642672
{
@@ -677,6 +707,12 @@ template not(alias pred)
677707
/**
678708
$(LINK2 http://en.wikipedia.org/wiki/Partial_application, Partially
679709
applies) $(D_PARAM fun) by tying its first argument to $(D_PARAM arg).
710+
711+
Params:
712+
fun = A callable
713+
arg = The first argument to apply to `fun`
714+
Returns:
715+
A new function which calls `fun` with `arg` plus the passed parameters.
680716
*/
681717
template partial(alias fun, alias arg)
682718
{
@@ -811,10 +847,13 @@ template partial(alias fun, alias arg)
811847
}
812848

813849
/**
814-
Takes multiple functions and adjoins them together. The result is a
815-
$(REF Tuple, std,typecons) with one element per passed-in function. Upon
816-
invocation, the returned tuple is the adjoined results of all
817-
functions.
850+
Takes multiple functions and adjoins them together.
851+
852+
Params:
853+
F = the call-able(s) to adjoin
854+
Returns:
855+
A new function which returns a $(REF Tuple, std,typecons). Each of the
856+
elements of the tuple will be the return values of `F`.
818857
819858
Note: In the special case where only a single function is provided
820859
($(D F.length == 1)), adjoin simply aliases to the single passed function
@@ -906,10 +945,12 @@ if (F.length > 1)
906945
}
907946

908947
/**
909-
Composes passed-in functions $(D fun[0], fun[1], ...) returning a
910-
function `f(x)` that in turn returns $(D
911-
fun[0](fun[1](...(x)))...). Each function can be a regular
912-
functions, a delegate, or a string.
948+
Composes passed-in functions $(D fun[0], fun[1], ...).
949+
950+
Params:
951+
fun = the call-able(s) or `string`(s) to compose into one function
952+
Returns:
953+
A new function `f(x)` that in turn returns $(D fun[0](fun[1](...(x)))...).
913954
914955
See_Also: $(LREF pipe)
915956
*/
@@ -957,6 +998,11 @@ template compose(fun...)
957998
lead to more readable code in some situation because the order of
958999
execution is the same as lexical order.
9591000
1001+
Params:
1002+
fun = the call-able(s) or `string`(s) to compose into one function
1003+
Returns:
1004+
A new function `f(x)` that in turn returns $(D fun[0](fun[1](...(x)))...).
1005+
9601006
Example:
9611007
9621008
----
@@ -970,6 +1016,7 @@ int[] a = pipe!(readText, split, map!(to!(int)))("file.txt");
9701016
*/
9711017
alias pipe(fun...) = compose!(Reverse!(fun));
9721018

1019+
///
9731020
@safe unittest
9741021
{
9751022
import std.conv : to;
@@ -1009,10 +1056,16 @@ unittest
10091056
}
10101057
----
10111058
1012-
Technically the memoized function should be pure because `memoize` assumes it will
1013-
always return the same result for a given tuple of arguments. However, `memoize` does not
1014-
enforce that because sometimes it
1015-
is useful to memoize an impure function, too.
1059+
Params:
1060+
fun = the call-able to memozie
1061+
maxSize = The maximum size of the GC buffer to hold the return values
1062+
Returns:
1063+
A new function which calls `fun` and caches its return values.
1064+
1065+
Note:
1066+
Technically the memoized function should be pure because `memoize` assumes it will
1067+
always return the same result for a given tuple of arguments. However, `memoize` does not
1068+
enforce that because sometimes it is useful to memoize an impure function, too.
10161069
*/
10171070
template memoize(alias fun)
10181071
{
@@ -1329,6 +1382,11 @@ private struct DelegateFaker(F)
13291382
* Convert a callable to a delegate with the same parameter list and
13301383
* return type, avoiding heap allocations and use of auxiliary storage.
13311384
*
1385+
* Params:
1386+
* fp = a function pointer or an aggregate type with `opCall` defined.
1387+
* Returns:
1388+
* A delegate with the context pointer pointing to nothing.
1389+
*
13321390
* Example:
13331391
* ----
13341392
* void doStuff() {
@@ -1492,7 +1550,13 @@ if (isCallable!(F))
14921550
}
14931551

14941552
/**
1495-
Forwards function arguments with saving ref-ness.
1553+
Forwards function arguments while keeping `out`, `ref`, and `lazy` on
1554+
the parameters.
1555+
1556+
Params:
1557+
args = a parameter list or an $(REF AliasSeq,std,meta).
1558+
Returns:
1559+
An `AliasSeq` of `args` with `out`, `ref`, and `lazy` saved.
14961560
*/
14971561
template forward(args...)
14981562
{
@@ -1528,11 +1592,19 @@ template forward(args...)
15281592
static int foo(int n) { return 1; }
15291593
static int foo(ref int n) { return 2; }
15301594
}
1595+
1596+
// with forward
15311597
int bar()(auto ref int x) { return C.foo(forward!x); }
15321598

1533-
assert(bar(1) == 1);
1599+
// without forward
1600+
int baz()(auto ref int x) { return C.foo(x); }
1601+
15341602
int i;
1603+
assert(bar(1) == 1);
15351604
assert(bar(i) == 2);
1605+
1606+
assert(baz(1) == 2);
1607+
assert(baz(i) == 2);
15361608
}
15371609

15381610
///

0 commit comments

Comments
 (0)