This is the code for a simple console project, that prints "Hello, World!" to STDOUT, and exits with an exit code of 0
[<EntryPoint>]
let main argv =
printfn "Hello, World!"
0
Example breakdown Line-by-line:
[<EntryPoint>] - A .net Attribute that m...
Say you have a library website, and you want to have a custom post type named Books. It can be registered as
function create_bookposttype() {
$args = array(
'public' => true,
'labels' => array(
'name' => __( 'Books' ),
'singular_name' => ...
A factory decreases coupling between code that needs to create objects from object creation code. Object creation is not made explicitly by calling a class constructor but by calling some function that creates the object on behalf the caller. A simple Java example is the following one:
interface Ca...
Dim filename As String = "c:\path\to\file.txt"
If System.IO.File.Exists(filename) Then
Dim writer As New System.IO.StreamWriter(filename)
writer.Write("Text to write" & vbCrLf) 'Add a newline
writer.close()
End If
using System.Text;
using System.IO;
string filename = "c:\path\to\file.txt";
//'using' structure allows for proper disposal of stream.
using (StreamWriter writer = new StreamWriter(filename"))
{
writer.WriteLine("Text to Write\n");
}
The following program says hello to the user. It takes one positional argument, the name of the user, and can also be told the greeting.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('name',
help='name of user'
)
parser.add_argument('-g', '--greeting',
...
Every package requires a setup.py file which describes the package.
Consider the following directory structure for a simple package:
+-- package_name
| |
| +-- __init__.py
|
+-- setup.py
The __init__.py contains only the line def foo(): return 100.
The following setup.py...
Comparing sets
In R, a vector may contain duplicated elements:
v = "A"
w = c("A", "A")
However, a set contains only one copy of each element. R treats a vector like a set by taking only its distinct elements, so the two vectors above are regarded as the same:
set...
The %in% operator compares a vector with a set.
v = "A"
w = c("A", "A")
w %in% v
# TRUE TRUE
v %in% w
# TRUE
Each element on the left is treated individually and tested for membership in the set associated with the vector on the right (consisting of all its...
To find every vector of the form (x, y) where x is drawn from vector X and y from Y, we use expand.grid:
X = c(1, 1, 2)
Y = c(4, 5)
expand.grid(X, Y)
# Var1 Var2
# 1 1 4
# 2 1 4
# 3 2 4
# 4 1 5
# 5 1 5
# 6 2 5
The result is a data.frame with one...
unique drops duplicates so that each element in the result is unique (only appears once):
x = c(2, 1, 1, 2, 1)
unique(x)
# 2 1
Values are returned in the order they first appeared.
duplicated tags each duplicated element:
duplicated(x)
# FALSE FALSE TRUE TRUE TRUE
anyDuplicated(x) >...
To count how many elements of two sets overlap, one could write a custom function:
xtab_set <- function(A, B){
both <- union(A, B)
inA <- both %in% A
inB <- both %in% B
return(table(inA, inB))
}
A = 1:20
B = 10:30
xtab_set(A, B)
# inB
...
Imagine you have the following HTML:
<div>
<label>Name:</label>
John Smith
</div>
And you need to locate the text "John Smith" after the label element.
In this case, you can locate the label element by text and then use .next_sibling property:
from ...
BeautifulSoup has a limited support for CSS selectors, but covers most commonly used ones. Use select() method to find multiple elements and select_one() to find a single element.
Basic example:
from bs4 import BeautifulSoup
data = """
<ul>
<li class="item&quo...
Sets are unordered collections of distinct elements. But sometimes we want to work with unordered collections of elements that are not necessarily distinct and keep track of the elements' multiplicities.
Consider this example:
>>> setA = {'a','b','b','c'}
>>> setA
set(['a', 'c'...
You can restrict the valid types used in a generic class by bounding that type in the class definition. Given the following simple type hierarchy:
public abstract class Animal {
public abstract String getSound();
}
public class Cat extends Animal {
public String getSound() {
...