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...
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...
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...
Assigns the value of the right-hand operand to the storage location named by the left-hand operand, and returns the value.
int x = 5; /* Variable x holds the value 5. Returns 5. */
char y = 'c'; /* Variable y holds the value 99. Returns 99
* (as the character 'c' is repr...
Logical AND
Performs a logical boolean AND-ing of the two operands returning 1 if both of the operands are non-zero. The logical AND operator is of type int.
0 && 0 /* Returns 0. */
0 && 1 /* Returns 0. */
2 && 0 /* Returns 0. */
2 && 3 /* Returns 1. */
Lo...
Grouping the key-value pairs of a dictionary by the value with itemgetter:
from itertools import groupby
from operator import itemgetter
adict = {'a': 1, 'b': 5, 'c': 1}
dict((i, dict(v)) for i, v in groupby(adict.items(), itemgetter(1)))
# Output: {1: {'a': 1, 'c': 1}, 5: {'b': 5}}
which ...
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...
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...
In Python, variables inside functions are considered local if and only if they appear in the left side of an assignment statement, or some other binding occurrence; otherwise such a binding is looked up in enclosing functions, up to the global scope. This is true even if the assignment statement is ...
A for loop iterates over a sequence, so altering this sequence inside the loop could lead to unexpected results (especially when adding or removing elements):
alist = [0, 1, 2]
for index, value in enumerate(alist):
alist.pop(index)
print(alist)
# Out: [1]
Note: list.pop() is being used t...
You can define a new class using the class keyword.
class MyClass
end
Once defined, you can create a new instance using the .new method
somevar = MyClass.new
# => #<MyClass:0x007fe2b8aa4a18>
We have three methods:
attr_reader: used to allow reading the variable outside the class.
attr_writer: used to allow modifying the variable outside the class.
attr_accessor: combines both methods.
class Cat
attr_reader :age # you can read the age but you can never change it
attr_writer...
math.log(x) gives the natural (base e) logarithm of x.
math.log(math.e) # 1.0
math.log(1) # 0.0
math.log(100) # 4.605170185988092
math.log can lose precision with numbers close to 1, due to the limitations of floating-point numbers. In order to accurately calculate logs close to 1, ...