Tutorial by Examples: l

Similar to the while loop, only the control statement is evaluated after the loop. Therefore, the loop will always execute at least once. var i: Int = 0 repeat { print(i) i += 1 } while i < 3 // 0 // 1 // 2
A while loop will execute as long as the condition is true. var count = 1 while count < 10 { print("This is the \(count) run of the loop") count += 1 }
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...
When you need to remove a specific value from an array, you can use the following one-liner to create a copy array without the given value: array.filter(function(val) { return val !== to_remove; }); Or if you want to change the array itself without creating a copy (for example if you write a fun...
The operator for an "exclusive or" (for short XOR) is: ^ This operator returns true when one, but only one, of the supplied bools are true. true ^ false // Returns true false ^ true // Returns true false ^ false // Returns false true ^ true // Returns false
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'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...
index.html <!doctype html> <html> <head> <title>D3 Sample</title> </head> <body> <!-- This will serve as a container for our chart. This does not have to be a div, and can in fact, just be the body if you want. --> <div id=...
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...
git shortlog summarizes git log and groups by author If no parameters are given, a list of all commits made per committer will be shown in chronological order. $ git shortlog Committer 1 (<number_of_commits>): Commit Message 1 Commit Message 2 ... Committer 2 (<number_of_...
The flow of execution of a Ruby block may be controlled with the break, next, and redo statements. break The break statement will exit the block immediately. Any remaining instructions in the block will be skipped, and the iteration will end: actions = %w(run jump swim exit macarena) index = 0 ...
When assigning named methods to delegates, they will refer to the same underlying object if: They are the same instance method, on the same instance of a class They are the same static method on a class public class Greeter { public void WriteInstance() { Console.Write...
The following syntax creates a delegate type with name NumberInOutDelegate, representing a method which takes an int and returns an int. public delegate int NumberInOutDelegate(int input); This can be used as follows: public static class Program { static void Main() { Number...
The System namespace contains Func<..., TResult> delegate types with between 0 and 15 generic parameters, returning type TResult. private void UseFunc(Func<string> func) { string output = func(); // Func with a single generic type parameter returns that type Console.WriteLine...
Named methods can be assigned to delegates with matching signatures: public static class Example { public static int AddOne(int input) { return input + 1; } } Func<int,int> addOne = Example.AddOne Example.AddOne takes an int and returns an int, its signature ...

Page 93 of 861