astring = 'thisisashorttext'
astring.count('t')
# Out: 4
This works even for substrings longer than one character:
astring.count('th')
# Out: 1
astring.count('is')
# Out: 2
astring.count('text')
# Out: 1
which would not be possible with collections.Counter which only counts single char...
var favoriteColors: Set = ["Red", "Blue", "Green"]
//favoriteColors = {"Blue", "Green", "Red"}
You can use the contains(_:) method to check whether a set contains a value. It will return true if the set contains that value.
if favorite...
Anonymous functions are functions that are defined but not assigned a name.
The following is an anonymous function that takes in two integers and returns the sum.
(x: Int, y: Int) => x + y
The resultant expression can be assigned to a val:
val sum = (x: Int, y: Int) => x + y
Anonymous...
To load jQuery from the official CDN, go to the jQuery website. You'll see a list of different versions and formats available.
Now, copy the source of the version of jQuery, you want to load. Suppose, you want to load jQuery 2.X, click uncompressed or minified tag which will show you something li...
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; /...
When a function is declared, variables in the context of its declaration are captured in its scope. For example, in the code below, the variable x is bound to a value in the outer scope, and then the reference to x is captured in the context of bar:
var x = 4; // declaration in outer scope
funct...
What is hoisting?
Hoisting is a mechanism which moves all variable and function declarations to the top of their scope. However, variable assignments still happen where they originally were.
For example, consider the following code:
console.log(foo); // → undefined
var foo = 42;
console.log(fo...
Importing using base R
Comma separated value files (CSVs) can be imported using read.csv, which wraps read.table, but uses sep = "," to set the delimiter to a comma.
# get the file path of a CSV included in R's utils package
csv_path <- system.file("misc", "exDIF.csv&q...
By default, a Date object is created as local time. This is not always desirable, for example when communicating a date between a server and a client that do not reside in the same timezone. In this scenario, one doesn't want to worry about timezones at all until the date needs to be displayed in lo...
A very simple way to import data from many common file formats is with rio. This package provides a function import() that wraps many commonly used data import functions, thereby providing a standard interface. It works simply by passing a file name or URL to import():
import("example.csv"...
Java SE 7
As the try-catch-final statement example illustrates, resource cleanup using a finally clause requires a significant amount of "boiler-plate" code to implement the edge-cases correctly. Java 7 provides a much simpler way to deal with this problem in the form of the try-with-res...
Employ the EAFP coding style and try to open it.
import errno
try:
with open(path) as f:
# File exists
except IOError as e:
# Raise the exception if it is not ENOENT (No such file or directory)
if e.errno != errno.ENOENT:
raise
# No such file or directory
...
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 */
}
...
The and-operator (&&) and the or-operator (||) employ short-circuiting to prevent unnecessary work if the outcome of the operation does not change with the extra work.
In x && y, y will not be evaluated if x evaluates to false, because the whole expression is guaranteed to be false....
Introduction
Package is a term used by npm to denote tools that developers can use for their projects. This includes everything from libraries and frameworks such as jQuery and AngularJS to task runners such as Gulp.js. The packages will come in a folder typically called node_modules, which will a...
# 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...