Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor English improvements + code + query #337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions gems/compile-time-function-evaluation-ctfe.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Compile Time Function Evaluation (CTFE)

CTFE is a mechanism which allows the compiler to execute
functions at **compile time**. There is no special set of the D
functions at **compile time**. There is no special subset of the D
language necessary to use this feature - whenever
a function just depends on compile time known values
the D compiler might decide to interpret
it during compilation.
a function depends only on values known at compile time
the D compiler might decide to perform the computation
during compilation.

// result will be calculated at compile
// time. Check the machine code, it won't
Expand All @@ -27,10 +27,10 @@ it, and the same code can perfectly be shared:
One prominent example in D is the [std.regex](https://dlang.org/phobos/std_regex.html)
library. It provides the `ctRegex` type which uses
*string mixins* and CTFE to generate a highly optimized
regular expression automaton that is generated during
compilation. The same code base is re-used for
the run-time version `regex` that allows regular
expressions only available at run-time to be compiled.
regular expression automaton that is generated at
compile-time. The same code base is re-used for
the run-time version `regex` that handles regular
expressions which are only available at run-time to be compiled.

auto ctr = ctRegex!(`^.*/([^/]+)/?$`);
auto tr = regex(`^.*/([^/]+)/?$`);
Expand Down Expand Up @@ -69,7 +69,6 @@ auto sqrt(T)(T x) {
import std.math : abs;
// choose a good starting value.
T z = x*x, old = 0;
int iter;
while (abs(z - old) > GoodEnough) {
old = z;
z -= ((z*z)-x) / (2*z);
Expand Down