Tutorial by Examples

#include <stdio.h> /* increment: take number, increment it by one, and return it */ int increment(int i) { printf("increment %d by 1\n", i); return i + 1; } /* decrement: take number, decrement it by one, and return it */ int decrement(int i) { printf("...
#include <stdio.h> enum Op { ADD = '+', SUB = '-', }; /* add: add a and b, return result */ int add(int a, int b) { return a + b; } /* sub: subtract b from a, return result */ int sub(int a, int b) { return a - b; } /* getmath: return the appropriate m...
Using typedef It might be handy to use a typedef instead of declaring the function pointer each time by hand. The syntax for declaring a typedef for a function pointer is: typedef returnType (*name)(parameters); Example: Posit that we have a function, sort, that expects a function pointer to ...
A basic "Hello, World!" program in Haskell can be expressed concisely in just one or two lines: main :: IO () main = putStrLn "Hello, World!" The first line is an optional type annotation, indicating that main is a value of type IO (), representing an I/O action which "...
from module_name import * for example: from math import * sqrt(2) # instead of math.sqrt(2) ceil(2.7) # instead of math.ceil(2.7) This will import all names defined in the math module into the global namespace, other than names that begin with an underscore (which indicates that the wri...
You can compare multiple items with multiple comparison operators with chain comparison. For example x > y > z is just a short form of: x > y and y > z This will evaluate to True only if both comparisons are True. The general form is a OP b OP c OP d ... Where OP represents ...
Finding the minimum/maximum of a sequence of sequences is possible: list_of_tuples = [(0, 10), (1, 15), (2, 8)] min(list_of_tuples) # Output: (0, 10) but if you want to sort by a specific element in each sequence use the key-argument: min(list_of_tuples, key=lambda x: x[0]) # Sorting ...
You can't pass an empty sequence into max or min: min([]) ValueError: min() arg is an empty sequence However, with Python 3, you can pass in the keyword argument default with a value that will be returned if the sequence is empty, instead of raising an exception: max([], default=42) ...
Create a file hello.html with the following content: <!DOCTYPE html> <html> <head> <title>Hello, World!</title> </head> <body> <div> <p id="hello">Some random text</p> </div> <script src=...
Arrays can be created by enclosing a list of elements in square brackets ([ and ]). Array elements in this notation are separated with commas: array = [1, 2, 3, 4] Arrays can contain any kind of objects in any combination with no restrictions on type: array = [1, 'b', nil, [3, 4]]
Arrays of strings can be created using ruby's percent string syntax: array = %w(one two three four) This is functionally equivalent to defining the array as: array = ['one', 'two', 'three', 'four'] Instead of %w() you may use other matching pairs of delimiters: %w{...}, %w[...] or %w<...&...
2.0 array = %i(one two three four) Creates the array [:one, :two, :three, :four]. Instead of %i(...), you may use %i{...} or %i[...] or %i!...! Additionally, if you want to use interpolation, you can do this with %I. 2.0 a = 'hello' b = 'goodbye' array_one = %I(#{a} #{b} world) array_tw...
An empty Array ([]) can be created with Array's class method, Array::new: Array.new To set the length of the array, pass a numerical argument: Array.new 3 #=> [nil, nil, nil] There are two ways to populate an array with default values: Pass an immutable value as second argument. P...
The groupingBy(classifier, downstream) collector allows the collection of Stream elements into a Map by classifying each element in a group and performing a downstream operation on the elements classified in the same group. A classic example of this principle is to use a Map to count the occurrence...
This is the basic use of the <a> (anchor element) element: <a href="http://example.com/">Link to example.com</a> It creates a hyperlink, to the URL http://example.com/ as specified by the href (hypertext reference) attribute, with the anchor text "Link to example...
If you ignore files by using a pattern but have exceptions, prefix an exclamation mark(!) to the exception. For example: *.txt !important.txt The above example instructs Git to ignore all files with the .txt extension except for files named important.txt. If the file is in an ignored folder, y...
struct Repository { let identifier: Int let name: String var description: String? } This defines a Repository struct with three stored properties, an integer identifier, a string name, and an optional string description. The identifier and name are constants, as they've been decla...
In order to access the value of an Optional, it needs to be unwrapped. You can conditionally unwrap an Optional using optional binding and force unwrap an Optional using the ! operator. Conditionally unwrapping effectively asks "Does this variable have a value?" while force unwrapping sa...
Relational operators check if a specific relation between two operands is true. The result is evaluated to 1 (which means true) or 0 (which means false). This result is often used to affect control flow (via if, while, for), but can also be stored in variables. Equals "==" Checks whether...

Page 33 of 1336