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) ...
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]]
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...
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...
Getting the minimum of a sequence (iterable) is equivalent of accessing the first element of a sorted sequence:
min([2, 7, 5])
# Output: 2
sorted([2, 7, 5])[0]
# Output: 2
The maximum is a bit more complicated, because sorted keeps order and max returns the first encountered value. In case th...
What can be documented?
Examples of functions for various languages.
A brief introduction of each tag.
What is the difference between a question and a topic?
Topics have a broader scope than questions; documentation topics that are asked as a question can be closed because they are too bro...
The Promise.all() static method accepts an iterable (e.g. an Array) of promises and returns a new promise, which resolves when all promises in the iterable have resolved, or rejects if at least one of the promises in the iterable have rejected.
// wait "millis" ms, then resolve with "...
The Promise.race() static method accepts an iterable of Promises and returns a new Promise which resolves or rejects as soon as the first of the promises in the iterable has resolved or rejected.
// wait "milliseconds" milliseconds, then resolve with "value"
function resolve(va...
Arguments are defined in parentheses after the function name:
def divide(dividend, divisor): # The names of the function and its arguments
# The arguments are available by name in the body of the function
print(dividend / divisor)
The function name and its list of arguments are called...
Optional arguments can be defined by assigning (using =) a default value to the argument-name:
def make(action='nothing'):
return action
Calling this function is possible in 3 different ways:
make("fun")
# Out: fun
make(action="sleep")
# Out: sleep
# The argumen...
One can give a function as many arguments as one wants, the only fixed rules are that each argument name must be unique and that optional arguments must be after the not-optional ones:
def func(value1, value2, optionalvalue=10):
return '{0} {1} {2}'.format(value1, value2, optionalvalue1)
Wh...
Arbitrary number of positional arguments:
Defining a function capable of taking an arbitrary number of arguments can be done by prefixing one of the arguments with a *
def func(*args):
# args will be a tuple containing all values that are passed in
for i in args:
print(i)
fun...
A Left Outer Join (also known as a Left Join or Outer Join) is a Join that ensures all rows from the left table are represented; if no matching row from the right table exists, its corresponding fields are NULL.
The following example will select all departments and the first name of employees that ...
min, max, and sorted all need the objects to be orderable. To be properly orderable, the class needs to define all of the 6 methods __lt__, __gt__, __ge__, __le__, __ne__ and __eq__:
class IntegerContainer(object):
def __init__(self, value):
self.value = value
def __rep...
Closures (also known as blocks or lambdas) are pieces of code which can be stored and passed around within your program.
let sayHi = { print("Hello") }
// The type of sayHi is "() -> ()", aka "() -> Void"
sayHi() // prints "Hello"
Like other fun...
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...