Tutorial by Examples

Consider a database with the following two tables. Employees table: IdFNameLNameDeptId1JamesSmith32JohnJohnson4 Departments table: IdName1Sales2Marketing3Finance4IT Simple select statement * is the wildcard character used to select all available columns in a table. When used as a substitute f...
The JSONSerialization class is built into Apple's Foundation framework. 2.2 Read JSON The JSONObjectWithData function takes NSData, and returns AnyObject. You can use as? to convert the result to your expected type. do { guard let jsonData = "[\"Hello\", \"JSON\"]&q...
An enum provides a set of related values: enum Direction { case up case down case left case right } enum Direction { case up, down, left, right } Enum values can be used by their fully-qualified name, but you can omit the type name when it can be inferred: let dir = Dire...
Enum cases can contain one or more payloads (associated values): enum Action { case jump case kick case move(distance: Float) // The "move" case has an associated distance } The payload must be provided when instantiating the enum value: performAction(.jump) performA...
Normally, enums can't be recursive (because they would require infinite storage): enum Tree<T> { case leaf(T) case branch(Tree<T>, Tree<T>) // error: recursive enum 'Tree<T>' is not marked 'indirect' } The indirect keyword makes the enum store its payload with...
Enums without payloads can have raw values of any literal type: enum Rotation: Int { case up = 0 case left = 90 case upsideDown = 180 case right = 270 } Enums without any specific type do not expose the rawValue property enum Rotation { case up case right cas...
This example assumes Ruby and Ruby on Rails have already been installed properly. If not, you can find how to do it here. Open up a command line or terminal. To generate a new rails application, use rails new command followed by the name of your application: $ rails new my_app If you want to c...
Logical OR (||), reading left to right, will evaluate to the first truthy value. If no truthy value is found, the last value is returned. var a = 'hello' || ''; // a = 'hello' var b = '' || []; // b = [] var c = '' || undefined; // c = undefined var d = 1 |...
Standard usage for (var i = 0; i < 100; i++) { console.log(i); } Expected output: 0 1 ... 99 Multiple declarations Commonly used to cache the length of an array. var array = ['a', 'b', 'c']; for (var i = 0; i < array.length; i++) { console.log(array[i]); } Expect...
Standard While Loop A standard while loop will execute until the condition given is false: var i = 0; while (i < 100) { console.log(i); i++; } Expected output: 0 1 ... 99 Decremented loop var i = 100; while (i > 0) { console.log(i); i--; /* equivalent to i...
Breaking out of a while loop var i = 0; while(true) { i++; if(i === 42) { break; } } console.log(i); Expected output: 42 Breaking out of a for loop var i; for(i = 0; i < 100; i++) { if(i === 42) { break; } } console.log(i); Expected o...
Continuing a "for" Loop When you put the continue keyword in a for loop, execution jumps to the update expression (i++ in the example): for (var i = 0; i < 3; i++) { if (i === 1) { continue; } console.log(i); } Expected output: 0 2 Continuing a While...
Place the following code into a file name hello.go: package main import "fmt" func main() { fmt.Println("Hello, 世界") } Playground When Go is installed correctly this program can be compiled and run like this: go run hello.go Output: Hello, 世界 Once you are...
Using the def statement is the most common way to define a function in python. This statement is a so called single clause compound statement with the following syntax: def function_name(parameters): statement(s) function_name is known as the identifier of the function. Since a function def...
Functions can return a value that you can use directly: def give_me_five(): return 5 print(give_me_five()) # Print the returned value # Out: 5 or save the value for later use: num = give_me_five() print(num) # Print the saved returned value # Out: 5 or use the value f...
Decorators augment the behavior of other functions or methods. Any function that takes a function as a parameter and returns an augmented function can be used as a decorator. # This simplest decorator does nothing to the function being decorated. Such # minimal decorators can occasionally be used ...
As mentioned in the introduction, a decorator is a function that can be applied to another function to augment its behavior. The syntactic sugar is equivalent to the following: my_func = decorator(my_func). But what if the decorator was instead a class? The syntax would still work, except that now m...
Decorators normally strip function metadata as they aren't the same. This can cause problems when using meta-programming to dynamically access function metadata. Metadata also includes function's docstrings and its name. functools.wraps makes the decorated function look like the original function b...
A decorator takes just one argument: the function to be decorated. There is no way to pass other arguments. But additional arguments are often desired. The trick is then to make a function which takes arbitrary arguments and returns a decorator. Decorator functions def decoratorfactory(message): ...
The math module contains the math.sqrt()-function that can compute the square root of any number (that can be converted to a float) and the result will always be a float: import math math.sqrt(9) # 3.0 math.sqrt(11.11) # 3.3331666624997918 math.sqrt(Decimal('6.25')) ...

Page 30 of 1336