Given the following HTML file:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>React Tutorial</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.2.1/react.js"></script>...
<link rel="alternate stylesheet" href="path/to/style.css" title="yourTitle">
Some browsers allow alternate style sheets to apply if they are offered. By default they will not be applied, but usually they can be changed through the browser settings:
Firefox l...
Unlike classes, structures cannot inherit:
class MyView: NSView { } // works
struct MyInt: Int { } // error: inheritance from non-protocol type 'Int'
Structures, however, can adopt protocols:
struct Vector: Hashable { ... } // works
If a case class has exactly two values, its extractor can be used in infix notation.
case class Pair(a: String, b: String)
val p: Pair = Pair("hello", "world")
val x Pair y = p
//x: String = hello
//y: String = world
Any extractor that returns a 2-tuple can work this way....
The for-in loop allows you to iterate over any sequence.
Iterating over a range
You can iterate over both half-open and closed ranges:
for i in 0..<3 {
print(i)
}
for i in 0...2 {
print(i)
}
// Both print:
// 0
// 1
// 2
Iterating over an array or set
let names = [&quo...
You can use the Enumerable class alongside Linq queries to convert for loops into Linq one liners.
Select Example
Opposed to doing this:
var asciiCharacters = new List<char>();
for (var x = 0; x < 256; x++)
{
asciiCharacters.Add((char)x);
}
You can do this:
var asciiCharacter...
Composer tracks which versions of packages you have installed in a file called composer.lock, which is intended to be committed to version control, so that when the project is cloned in the future, simply running composer install will download and install all the project's dependencies.
Composer de...
A lot of the power of ReactJS is its ability to allow nesting of components. Take the following two components:
var React = require('react');
var createReactClass = require('create-react-class');
var CommentList = reactCreateClass({
render: function() {
return (
<div className...
It's possible to declare protocol name without methods:
@protocol Person;
use it your code (class definition, etc):
@interface World : NSObject
@property (strong, nonatomic) NSArray<id<some>> *employees;
@end
and later define protocol's method somewhere in your code:
@protocol...
It is possible to bind values to names using @:
struct Badger {
pub age: u8
}
fn main() {
// Let's create a Badger instances
let badger_john = Badger { age: 8 };
// Now try to find out what John's favourite activity is, based on his age
match badger_john.age {
...
// Create a boolean value
let a = true;
// The following expression will try and find a pattern for our value starting with
// the topmost pattern.
// This is an exhaustive match expression because it checks for every possible value
match a {
true => println!("a is true"),
...
It's possible to treat multiple, distinct values the same way, using |:
enum Colour {
Red,
Green,
Blue,
Cyan,
Magenta,
Yellow,
Black
}
enum ColourModel {
RGB,
CMYK
}
// let's take an example colour
let colour = Colour::Red;
let model = match ...
Patterns can be matched based on values independent to the value being matched using if guards:
// Let's imagine a simplistic web app with the following pages:
enum Page {
Login,
Logout,
About,
Admin
}
// We are authenticated
let is_authenticated = true;
// But we aren't admins...
To select the children of an element you can use the children() method.
<div class="parent">
<h2>A headline</h2>
<p>Lorem ipsum dolor sit amet...</p>
<p>Praesent quis dolor turpis...</p>
</div>
Change the color of all the ...
// View to hold the CAGradientLayer.
let view: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 320))
// Initialize gradient layer.
let gradientLayer: CAGradientLayer = CAGradientLayer()
// Set frame of gradient layer.
gradientLayer.frame = view.bounds
// Color at...
By default the Python random module use the Mersenne Twister PRNG to generate random numbers, which, although suitable in domains like simulations, fails to meet security requirements in more demanding environments.
In order to create a cryptographically secure pseudorandom number, one can use Syst...
public class Singleton
{
private readonly static Singleton instance = new Singleton();
private Singleton() { }
public static Singleton Instance => instance;
}
This implementation is thread-safe because in this case instance object is initialized in the static constructor. The ...
This thread-safe version of a singleton was necessary in the early versions of .NET where static initialization was not guaranteed to be thread-safe. In more modern versions of the framework a statically initialized singleton is usually preferred because it is very easy to make implementation mistak...
To avoid repetition in nested routes, concerns provide a great way of sharing common resources that are reusable. To create a concern use the method concern within the routes.rb file. The method expects a symbol and block:
concern :commentable do
resources :comments
end
While not creating a...