Tutorial by Examples: f

Before C++17, having pointers with a value of nullptr commonly represented the absence of a value. This is a good solution for large objects that have been dynamically allocated and are already managed by pointers. However, this solution does not work well for small or primitive types such as int, w...
Before C++17, a function typically represented failure in one of several ways: A null pointer was returned. e.g. Calling a function Delegate *App::get_delegate() on an App instance that did not have a delegate would return nullptr. This is a good solution for objects that have been dynamicall...
This illustrates that union members shares memory and that struct members does not share memory. #include <stdio.h> #include <string.h> union My_Union { int variable_1; int variable_2; }; struct My_Struct { int variable_1; int variable_2; }; int main (void) { ...
Assert.That(actual, Is.EqualTo(expected));
6 When using arrow functions this takes the value from the enclosing execution context's this (that is, this in arrow functions has lexical scope rather than the usual dynamic scope). In global code (code that doesn't belong to any function) it would be the global object. And it keeps that way, eve...
let sel = document.getSelection(); let myNode = document.getElementById('element-to-select'); let range = document.createRange(); range.selectNodeContents(myNode); sel.addRange(range); It may be necessary to first remove all the ranges of the previous selection, as most browsers don't s...
let sel = document.getSelection(); let text = sel.toString(); console.log(text); // logs what the user selected Alternatively, since the toString member function is called automatically by some functions when converting the object to a string, you don't always have to call it yourself. console...
Let's extend our template from above and include content from header.php and footer.php Including header: We will include header right after Template name comment There are two common ways to do this. Both are right and work same, it's just about your style and how code looks First way: <?ph...
Values can be given names using let: # let a = 1;; val a : int = 1 You can use similar syntax to define a function. Just provide additional parameters for the arguments. # let add arg1 arg2 = arg1 + arg2;; val add : int -> int -> int = <fun> We can call it like this: # add 1...
The function keyword automatically has pattern matching when you define the body of your function. Observe it below: # let foo = function 0 -> "zero" | 1 -> "one" | 2 -> "couple" | 3 -> "few" | _ -> "many";; val foo : int -&gt...
A defer statement in Go is simply a function call marked to be executed at a later time. Defer statement is an ordinary function call prefixed by the keyword defer. defer someFunction() A deferred function is executed once the function that contains the defer statement returns. Actual call to th...
[1, 2, [[3, 4], [5]], 6].flatten # => [1, 2, 3, 4, 5, 6] If you have a multi-dimensional array and you need to make it a simple (i.e. one-dimensional) array, you can use the #flatten method.
If targeting Flash Player 11+, the built-in removeChildren method is the best way to remove all children: removeChildren(); //a start and end index can be passed For legacy applications, the same can be accomplished with a loop: while (numChildren > 0) { removeChildAt(0); }
Enums in Swift are much more powerful than some of their counterparts in other languages, such as C. They share many features with classes and structs, such as defining initialisers, computed properties, instance methods, protocol conformances and extensions. protocol ChangesDirection { mutati...
Using functions that take in and execute closures can be extremely useful for sending a block of code to be executed elsewhere. We can start by allowing our function to take in an optional closure that will (in this case) return Void. func closedFunc(block: (()->Void)? = nil) { print("...
Note: Everything below applies to the str.format method, as well as the format function. In the text below, the two are interchangeable. For every value which is passed to the format function, Python looks for a __format__ method for that argument. Your own custom class can therefore have th...
Select the .xcdatamodeld file. You will notice you have no entities. You will have to create one yourself. At the bottom of Xcode you will notice a button that says "Add Entity" click it and you will have a new entity for you to work with on the project. In this step there are ...
We define the type of boolean expressions whose atoms are identified by strings as type expr = | Atom of string | Not of expr | And of expr * expr | Or of expr * expr and can evaluate these expressions using an oracle : string -> bool giving the values of the atoms we find as follows: le...
Pattern matching allows to deconstruct complex values and it is by no way limited to the “outer most” level of the representation of a value. To illustrate this, we implement the function transforming a boolean expression into a boolean expression where all negations are only on atoms, the so calle...
Pattern matching can be used to deconstruct records. We illustrate this with a record type representing locations in a text file, e.g. the source code of a program. type location = { filename : string; line: int; column: int; offset: int; } A value x of type location can be deconstr...

Page 115 of 457