Tutorial by Examples: apt

var tasks = Enumerable.Range(1, 5).Select(n => new Task<int>(() => { Console.WriteLine("I'm task " + n); return n; })).ToArray(); foreach(var task in tasks) task.Start(); Task.WaitAll(tasks); foreach(var task in tasks) Console.WriteLine(task.Result); ...
If you need to extract a part of string from the input string, we can use capture groups of regex. For this example, we'll start with a simple phone number regex: \d{3}-\d{3}-\d{4} If parentheses are added to the regex, each set of parentheses is considered a capturing group. In this case, we a...
When desired XML format differs from Java object model, an XmlAdapter implementation can be used to transform model object into xml-format object and vice versa. This example demonstrates how to put a field's value into an attribute of an element with field's name. public class XmlAdapterExample { ...
class MyClass { func sayHi() { print("Hello") } deinit { print("Goodbye") } } When a closure captures a reference type (a class instance), it holds a strong reference by default: let closure: () -> Void do { let obj = MyClass() // Captures a strong re...
Events fired on DOM elements don't just affect the element they're targeting. Any of the target's ancestors in the DOM may also have a chance to react to the event. Consider the following document: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </hea...
A module map can simply import mymodule by configuring it to read C header files and make them appear as Swift functions. Place a file named module.modulemap inside a directory named mymodule: Inside the module map file: // mymodule/module.modulemap module mymodule { header "defs.h&q...
In this example, we have an array containing some data. We capture the output buffer in $items_li_html and use it twice in the page. <?php // Start capturing the output ob_start(); $items = ['Home', 'Blog', 'FAQ', 'Contact']; foreach($items as $item): // Note we're about to step &q...
If you specify the variable's name in the capture list, the lambda will capture it by value. This means that the generated closure type for the lambda stores a copy of the variable. This also requires that the variable's type be copy-constructible: int a = 0; [a]() { return a; // Ok, 'a' ...
C++14 Lambdas can capture expressions, rather than just variables. This permits lambdas to store move-only types: auto p = std::make_unique<T>(...); auto lamb = [p = std::move(p)]() //Overrides capture-by-value of `p`. { p->SomeFunc(); }; This moves the outer p variable into th...
If you precede a local variable's name with an &, then the variable will be captured by reference. Conceptually, this means that the lambda's closure type will have a reference variable, initialized as a reference to the corresponding variable from outside of the lambda's scope. Any use of the v...
By default, local variables that are not explicitly specified in the capture list, cannot be accessed from within the lambda body. However, it is possible to implicitly capture variables named by the lambda body: int a = 1; int b = 2; // Default capture by value [=]() { return a + b; }; // OK;...
A group is a section of a regular expression enclosed in parentheses (). This is commonly called "sub-expression" and serves two purposes: It makes the sub-expression atomic, i.e. it will either match, fail or repeat as a whole. The portion of text it matched is accessible in the remai...
Since Groups are "numbered" some engines also support matching what a group has previously matched again. Assuming you wanted to match something where two equals strings of length three are divided by a $ you'd use: (.{3})\$\1 This would match any of the following strings: "abc$...
// Java: Arrays.stream(new int[] {1, 2, 3}) .map(n -> 2 * n + 1) .average() .ifPresent(System.out::println); // 5.0 // Kotlin: arrayOf(1,2,3).map { 2 * it + 1}.average().apply(::println)
// Java: Stream.of("a1", "a2", "a3") .map(s -> s.substring(1)) .mapToInt(Integer::parseInt) .max() .ifPresent(System.out::println); // 3 // Kotlin: sequenceOf("a1", "a2", "a3") .map { it.substring(1) } ...
// Java: IntStream.range(1, 4) .mapToObj(i -> "a" + i) .forEach(System.out::println); // a1 // a2 // a3 // Kotlin: (inclusive range) (1..3).map { "a$it" }.forEach(::println)
// Java: Stream.of(1.0, 2.0, 3.0) .mapToInt(Double::intValue) .mapToObj(i -> "a" + i) .forEach(System.out::println); // a1 // a2 // a3 // Kotlin: sequenceOf(1.0, 2.0, 3.0).map(Double::toInt).map { "a$it" }.forEach(::println)
A lambda expression evaluated in a class' member function is implicitly a friend of that class: class Foo { private: int i; public: Foo(int val) : i(val) {} // definition of a member function void Test() { auto lamb = [](Foo &foo, int val) ...
Some regular expression flavors allow named capture groups. Instead of by a numerical index you can refer to these groups by name in subsequent code, i.e. in backreferences, in the replace pattern as well as in the following lines of the program. Numerical indexes change as the number or arrangemen...
Given the flavors, the named capture group may looks like this: (?'name'X) (?<name>X) (?P<name>X) With X being the pattern you want to capture. Let's consider the following string: Once upon a time there was a pretty little girl... Once upon a time there was a unicorn with an h...

Page 1 of 6