Tutorial by Examples: f

You define a class like this: class Dog {} A class can also be a subclass of another class: class Animal {} class Dog: Animal {} In this example, Animal could also be a protocol that Dog conforms to.
Classes are reference types, meaning that multiple variables can refer to the same instance. class Dog { var name = "" } let firstDog = Dog() firstDog.name = "Fido" let otherDog = firstDog // otherDog points to the same Dog instance otherDog.name = "Rover" // modifying otherDog also...
Templating can also be applied to functions (as well as the more traditional structures) with the same effect. // 'T' stands for the unknown type // Both of our arguments will be of the same type. template<typename T> void printSum(T add1, T add2) { std::cout << (add1 + add2) &...
Factorials can be computed at compile-time using template metaprogramming techniques. #include <iostream> template<unsigned int n> struct factorial { enum { value = n * factorial<n - 1>::value }; }; template<> struct factorial<0> { ...
Swift let label = UILabel() Objective-C UILabel *label = [[UILabel alloc] init]; or UILabel *label = [UILabel new]; // convenience method for calling alloc-init Change the default font's size Swift label.font = UIFont.systemFontOfSize(17) Swift 3 label.font = UIFont.systemFont(ofSi...
INSERT INTO Customers (FName, LName, Email, PreferredContact) VALUES ('Zack', 'Smith', '[email protected]', 'EMAIL'); This statement will insert a new row into the Customers table. Data will only be inserted into the columns specified - note that no value was provided for the PhoneNumber column. ...
The second step to creating a subscription for a user is to create and execute a billing agreement, based on an existing activated billing plan. This example assumes that you have already gone through and activated a billing plan in the previous example, and have an ID for that billing plan to refer...
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 ...
In helloWorld.sh #!/bin/bash # Define a function greet greet () { echo "Hello World!" } # Call the function greet greet In running the script, we see our message $ bash helloWorld.sh Hello World! Note that sourcing a file with functions makes them available in your ...
$.ajax({ url: 'https://api.dropboxapi.com/2/sharing/add_folder_member', type: 'POST', processData: false, data: JSON.stringify({"shared_folder_id": "84528192421","members": [{"member": {".tag": "email","email":...
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&...
var favoriteColors: Set = ["Red", "Blue", "Green"] //favoriteColors = {"Blue", "Green", "Red"} You can use the insert(_:) method to add a new item into a set. favoriteColors.insert("Orange") //favoriteColors = {"Red&q...
from collections import Counter c = Counter(["a", "b", "c", "d", "a", "b", "a", "c", "d"]) c # Out: Counter({'a': 3, 'b': 2, 'c': 2, 'd': 2}) c["a"] # Out: 3 c[7] # not in the list (7 oc...
alist = [1, 2, 3, 4, 1, 2, 1, 3, 4] alist.count(1) # Out: 3 atuple = ('bear', 'weasel', 'bear', 'frog') atuple.count('bear') # Out: 2 atuple.count('fox') # Out: 0
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...
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...
(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; /...
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...

Page 21 of 457