Tutorial by Examples

Sometimes you don't want to have your function accessible/stored as a variable. You can create an Immediately Invoked Function Expression (IIFE for short). These are essentially self-executing anonymous functions. They have access to the surrounding scope, but the function itself and any internal va...
When you define a function, it creates a scope. Everything defined within the function is not accessible by code outside the function. Only code within this scope can see the entities defined inside the scope. function foo() { var a = 'hello'; console.log(a); // => 'hello' } consol...
The then method of a promise returns a new promise. const promise = new Promise(resolve => setTimeout(resolve, 5000)); promise // 5 seconds later .then(() => 2) // returning a value from a then callback will cause // the new promise to resolve with this value .then...
A Promise object represents an operation which has produced or will eventually produce a value. Promises provide a robust way to wrap the (possibly pending) result of asynchronous work, mitigating the problem of deeply nested callbacks (known as "callback hell"). States and control flow ...
The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. It is also a trivial way to achieve an asynchronous operation. In this example calling the wait function resolves the promise after the time specified as first argument: function wait(ms) ...
Integers in PHP can be natively specified in base 2 (binary), base 8 (octal), base 10 (decimal), or base 16 (hexadecimal.) $my_decimal = 42; $my_binary = 0b101010; $my_octal = 052; $my_hexadecimal = 0x2a; echo ($my_binary + $my_octal) / 2; // Output is always in decimal: 42 Integers are 3...
A traditional for-loop A traditional for loop has three components: The initialization: executed before the look block is executed the first time The condition: checks a condition every time before the loop block is executed, and quits the loop if false The afterthought: performed every time a...
In addition to the built-in round function, the math module provides the floor, ceil, and trunc functions. x = 1.55 y = -1.55 # round to the nearest integer round(x) # 2 round(y) # -2 # the second argument gives how many decimal places to round to (defaults to 0) round(x, 1) ...
git push will push your code to your existing upstream. Depending on the push configuration, it will either push code from you current branch (default in Git 2.x) or from all branches (default in Git 1.x). Specify remote repository When working with git, it can be handy to have multiple remote ...
Sometimes, when you have local changes incompatible with remote changes (ie, when you cannot fast-forward the remote branch, or the remote branch is not a direct ancestor of your local branch), the only way to push your changes is a force push. git push -f or git push --force Important not...
Tk is the standard graphical user interface (GUI) for Ruby. It provides a cross-platform GUI for Ruby programs. Example code: require "tk" TkRoot.new{ title "Hello World!" } Tk.mainloop The result: Step by Step explanation: require "tk" Load the tk package...
Open a new blank document in the MATLAB Editor (in recent versions of MATLAB, do this by selecting the Home tab of the toolstrip, and clicking on New Script). The default keyboard shortcut to create a new script is Ctrl-n. Alternatively, typing edit myscriptname.m will open the file myscriptname.m ...
for loops iterate over a collection of items, such as list or dict, and run a block of code with each element from the collection. for i in [0, 1, 2, 3, 4]: print(i) The above for loop iterates over a list of numbers. Each iteration sets the value of i to the next element of the list. So f...
import random shuffle() You can use random.shuffle() to mix up/randomize the items in a mutable and indexable sequence. For example a list: laughs = ["Hi", "Ho", "He"] random.shuffle(laughs) # Shuffles in-place! Don't do: laughs = random.shuffle(laughs) p...
import random randint() Returns a random integer between x and y (inclusive): random.randint(x, y) For example getting a random number between 1 and 8: random.randint(1, 8) # Out: 8 randrange() random.randrange has the same syntax as range and unlike random.randint, the last value is no...
jQuery is the starting point for writing any jQuery code. It can be used as a function jQuery(...) or a variable jQuery.foo. $ is an alias for jQuery and the two can usually be interchanged for each other (except where jQuery.noConflict(); has been used - see Avoiding namespace collisions). Assumi...
git log will display all your commits with the author and hash. This will be shown over multiple lines per commit. (If you wish to show a single line per commit, look at onelineing). Use the q key to exit the log. By default, with no arguments, git log lists the commits made in that reposito...
git log --oneline will show all of your commits with only the first part of the hash and the commit message. Each commit will be in a single line, as the oneline flag suggests. The oneline option prints each commit on a single line, which is useful if you’re looking at a lot of commits. - sou...
About Protocols A Protocol specifies initialisers, properties, functions, subscripts and associated types required of a Swift object type (class, struct or enum) conforming to the protocol. In some languages similar ideas for requirement specifications of subsequent objects are known as ‘interfaces...
Protocols may define associated type requirements using the associatedtype keyword: protocol Container { associatedtype Element var count: Int { get } subscript(index: Int) -> Element { get set } } Protocols with associated type requirements can only be used as generic constraints:...

Page 31 of 1336