Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
D (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Functional==== D supports [[functional programming]] features such as [[anonymous function|function literals]], [[Closure (computer science)|closures]], recursively-immutable objects and the use of [[higher-order function]]s. There are two syntaxes for anonymous functions, including a multiple-statement form and a "shorthand" single-expression notation:<ref name="short">{{cite web |title=Expressions |url=http://dlang.org/expression.html#Lambda |publisher=Digital Mars |access-date=27 December 2012}}</ref> <syntaxhighlight lang="D"> int function(int) g; g = (x) { return x * x; }; // longhand g = (x) => x * x; // shorthand </syntaxhighlight> There are two built-in types for function literals, <code>function</code>, which is simply a pointer to a stack-allocated function, and <code>delegate</code>, which also includes a pointer to the relevant [[stack frame]], the surrounding ‘environment’, which contains the current local variables. Type inference may be used with an anonymous function, in which case the compiler creates a <code>delegate</code> unless it can prove that an environment pointer is not necessary. Likewise, to implement a closure, the compiler places enclosed local variables on the [[Heap (data structure)|heap]] only if necessary (for example, if a closure is returned by another function, and exits that function's scope). When using type inference, the compiler will also add attributes such as <code>pure</code> and <code>nothrow</code> to a function's type, if it can prove that they apply. Other functional features such as [[currying]] and common higher-order functions such as [[map (higher-order function)|map]], [[filter (higher-order function)|filter]], and [[fold (higher-order function)|reduce]] are available through the standard library modules <code>std.functional</code> and <code>std.algorithm</code>. <syntaxhighlight lang="D"> import std.stdio, std.algorithm, std.range; void main() { int[] a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; int[] a2 = [6, 7, 8, 9]; // must be immutable to allow access from inside a pure function immutable pivot = 5; int mySum(int a, int b) pure nothrow /* pure function */ { if (b <= pivot) // ref to enclosing-scope return a + b; else return a; } // passing a delegate (closure) auto result = reduce!mySum(chain(a1, a2)); writeln("Result: ", result); // Result: 15 // passing a delegate literal result = reduce!((a, b) => (b <= pivot) ? a + b : a)(chain(a1, a2)); writeln("Result: ", result); // Result: 15 } </syntaxhighlight> Alternatively, the above function compositions can be expressed using Uniform function call syntax (UFCS) for more natural left-to-right reading: <syntaxhighlight lang="D"> auto result = a1.chain(a2).reduce!mySum(); writeln("Result: ", result); result = a1.chain(a2).reduce!((a, b) => (b <= pivot) ? a + b : a)(); writeln("Result: ", result); </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)