Tutorial by Examples: c

3.0 To run tasks on a dispatch queue, use the sync, async, and after methods. To dispatch a task to a queue asynchronously: let queue = DispatchQueue(label: "myQueueName") queue.async { //do something DispatchQueue.main.async { //this will be called in main t...
Arrays are objects, but their type is defined by the type of the contained objects. Therefore, one cannot just cast A[] to T[], but each A member of the specific A[] must be cast to a T object. Generic example: public static <T, A> T[] castArray(T[] target, A[] array) { for (int i = 0; i...
Letters 3.0 let letters = CharacterSet.letters let phrase = "Test case" let range = phrase.rangeOfCharacter(from: letters) // range will be nil if no letters is found if let test = range { print("letters found") } else { print("letters not found") }...
A common question is how to juxtapose (combine) physically separate geographical regions on the same map, such as in the case of a choropleth describing all 50 American states (The mainland with Alaska and Hawaii juxtaposed). Creating an attractive 50 state map is simple when leveraging Google Maps...
The + symbol marks a type parameter as covariant - here we say that "Producer is covariant on A": trait Producer[+A] { def produce: A } A covariant type parameter can be thought of as an "output" type. Marking A as covariant asserts that Producer[X] <: Producer[Y] prov...
By default all type parameters are invariant - given trait A[B], we say that "A is invariant on B". This means that given two parametrizations A[Cat] and A[Animal], we assert no sub/superclass relationship between these two types - it does not hold that A[Cat] <: A[Animal] nor that A[Ca...
The - symbol marks a type parameter as contravariant - here we say that "Handler is contravariant on A": trait Handler[-A] { def handle(a: A): Unit } A contravariant type parameter can be thought of as an "input" type. Marking A as contravariant asserts that Handler[X] &l...
Because collections are typically covariant in their element type*, a collection of a subtype may be passed where a super type is expected: trait Animal { def name: String } case class Dog(name: String) extends Animal object Animal { def printAnimalNames(animals: Seq[Animal]) = { anima...
The static keyword means 2 things: This value does not change from object to object but rather changes on a class as a whole Static properties and methods don't require an instance. public class Foo { public Foo{ Counter++; NonStaticCounter++; } public st...
The "static" keyword when referring to a class has three effects: You cannot create an instance of a static class (this even removes the default constructor) All properties and methods in the class must be static as well. A static class is a sealed class, meaning it cannot be inherite...
// Obtain an instance of JavaScript engine ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); //String to be evaluated String str = "3+2*4+5"; //Value after doing Arithmetic operation with operator preceden...
A struct can simply be copied using assignment. type T struct { I int S string } // initialize a struct t := T{1, "one"} // make struct copy u := t // u has its field values equal to t if u == t { // true fmt.Println("u and t are equal") // Prints: &qu...
The if construct (called a block IF statement in FORTRAN 77) is common across many programming languages. It conditionally executes one block of code when a logical expression is evaluated to true. [name:] IF (expr) THEN block [ELSE IF (expr) THEN [name] block] [ELSE [name] block] ...
Suppose you have a simple Customer model: class Customer(models.Model): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) is_premium = models.BooleanField(default=False) You register it in the Django admin and add search field by first_name...
In this example we aim to accomplish one of the most common tasks: I have a small DC motor laying around, how do I use my Arduino to control it? Easy, with PWM and serial communication, using the function analogWrite() and the Serial library. The basics Pulse Width Modulation or PWM for short is a...
Interceptors are used to intercept OkHttp calls. The reason to intercept could be to monitor, rewrite and retry calls. It can be used for outgoing request or incoming response both. class LoggingInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) thr...
To perform actions in Django using commandline or other services (where the user/request is not used), you can use the management commands. Django modules can be imported as needed. For each command a separate file needs to be created: myapp/management/commands/my_command.py (The management and...
When you want a complete copy of an object (i.e. the object properties and the values inside those properties, etc...), that is called deep cloning. 5.1 If an object can be serialized to JSON, then you can create a deep clone of it with a combination of JSON.parse and JSON.stringify: var existing...
If you want to calculate with BigDecimal you have to use the returned value because BigDecimal objects are immutable: BigDecimal a = new BigDecimal("42.23"); BigDecimal b = new BigDecimal("10.001"); a.add(b); // a will still be 42.23 BigDecimal c = a.add(b); // c will be ...
The most simple data structure available in R is a vector. You can make vectors of numeric values, logical values, and character strings using the c() function. For example: c(1, 2, 3) ## [1] 1 2 3 c(TRUE, TRUE, FALSE) ## [1] TRUE TRUE FALSE c("a", "b", "c") ## ...

Page 125 of 826