object HelloWorld extends App {
println("Hello, world!")
}
Live demo
By extending the App trait, you can avoid defining an explicit main method. The entire body of the HelloWorld object is treated as "the main method".
2.11.0
Delayed Initialization
Per the official d...
When working with dictionaries, it's often necessary to access all the keys and values in the dictionary, either in a for loop, a list comprehension, or just as a plain list.
Given a dictionary like:
mydict = {
'a': '1',
'b': '2'
}
You can get a list of keys using the keys() method:
...
Conditional comments can be used to customize code for different versions of Microsoft Internet Explorer. For example, different HTML classes, script tags, or stylesheets can be provided. Conditional comments are supported in Internet Explorer versions 5 through 9. Older and newer Internet Explorer ...
An if statement checks whether a Bool condition is true:
let num = 10
if num == 10 {
// Code inside this block only executes if the condition was true.
print("num is 10")
}
let condition = num == 10 // condition's type is Bool
if condition {
print("num is 10&...
Optionals must be unwrapped before they can be used in most expressions. if let is an optional binding, which succeeds if the optional value was not nil:
let num: Int? = 10 // or: let num: Int? = nil
if let unwrappedNum = num {
// num has type Int?; unwrappedNum has type Int
print(&quo...
To allow the use of in for custom classes the class must either provide the magic method __contains__ or, failing that, an __iter__-method.
Suppose you have a class containing a list of lists:
class ListList:
def __init__(self, value):
self.value = value
# Create a set of al...
Import the ElementTree object, open the relevant .xml file and get the root tag:
import xml.etree.ElementTree as ET
tree = ET.parse("yourXMLfile.xml")
root = tree.getroot()
There are a few ways to search through the tree. First is by iteration:
for child in root:
print(child.ta...
(Note: All examples using let are also valid for const)
var is available in all versions of JavaScript, while let and const are part of ECMAScript 6 and only available in some newer browsers.
var is scoped to the containing function or the global space, depending when it is declared:
var x = 4; /...
Final classes
When used in a class declaration, the final modifier prevents other classes from being declared that extend the class. A final class is a "leaf" class in the inheritance class hierarchy.
// This declares a final class
final class MyFinalClass {
/* some code */
}
...
# Set the repository for the scope "myscope"
npm config set @myscope:registry http://registry.corporation.com
# Login at a repository and associate it with the scope "myscope"
npm adduser --registry=http://registry.corporation.com --scope=@myscope
# Install a package &quo...
The $_SESSION variable is an array, and you can retrieve or manipulate it like a normal array.
<?php
// Starting the session
session_start();
// Storing the value in session
$_SESSION['id'] = 342;
// conditional usage of session values that may have been set in a previous session
if(!i...
Use Case
CASE can be used in conjunction with SUM to return a count of only those items matching a pre-defined condition. (This is similar to COUNTIF in Excel.)
The trick is to return binary results indicating matches, so the "1"s returned for matching entries can be summed for a count o...
A useful feature of namespaces is that you can expand them (add members to it).
namespace Foo
{
void bar() {}
}
//some other stuff
namespace Foo
{
void bar2() {}
}
To create a new branch, while staying on the current branch, use:
git branch <name>
Generally, the branch name must not contain spaces and is subject to other specifications listed here. To switch to an existing branch :
git checkout <name>
To create a new branch and switch to it...
jQuery code is often wrapped in jQuery(function($) { ... }); so that it only runs after the DOM has finished loading.
<script type="text/javascript">
jQuery(function($) {
// this will set the div's text to "Hello".
$("#myDiv").text("Hello"...
These are all equivalent, the code inside the blocks will run when the document is ready:
$(function() {
// code
});
$().ready(function() {
// code
});
$(document).ready(function() {
// code
});
Because these are equivalent the first is the recommended form, the following is a ...
var='0123456789abcdef'
# Define a zero-based offset
$ printf '%s\n' "${var:3}"
3456789abcdef
# Offset and length of substring
$ printf '%s\n' "${var:3:4}"
3456
4.2
# Negative length counts from the end of the string
$ printf '%s\n' "${var:3:-5}"
3456789a...