Tutorial by Examples: cas

From the official documentation: Gradle. dependencies { compile "com.squareup.picasso:picasso:2.5.2" } Maven: <dependency> <groupId>com.squareup.picasso</groupId> <artifactId>picasso</artifactId> <version>2.5.2</version> </dep...
A single case discriminated union is like any other discriminated union except that it only has one case. // Define single-case discriminated union type. type OrderId = OrderId of int // Construct OrderId type. let order = OrderId 123 // Deconstruct using pattern matching. // Parentheses used...
Incorrect usage: In the following snippet, the last match will never be used: let x = 4 match x with | 1 -> printfn "x is 1" | _ -> printfn "x is anything that wasn't listed above" | 4 -> printfn "x is 4" prints x is anything that wasn't listed abov...
The function toupper will convert a string to upper case (capital letters). For example: BEGIN { greeting = "hello" loud_greeting = toupper(greeting) print loud_greeting } This code will output "HELLO" when run.
Given the following definitions : public interface IMyInterface1 { string GetName(); } public interface IMyInterface2 { string GetName(); } public class MyClass : IMyInterface1, IMyInterface2 { string IMyInterface1.GetName() { return "IMyInterface1"...
Sometimes it is useful to create union types with only one case to implement record-like types: type Point = Point of float * float let point1 = Point(0.0, 3.0) let point2 = Point(-2.5, -4.0) These become very useful because they can be decomposed via pattern matching in the same way as tu...
Suppose that you have a pointer to an object of a polymorphic class: Shape *ps; // see example on defining a polymorphic class ps = get_a_new_random_shape(); // if you don't have such a function yet, you // could just write ps = new Square...
In comparison to regular classes – case classes notation provides several benefits: All constructor arguments are public and can be accessed on initialized objects (normally this is not the case, as demonstrated here): case class Dog1(age: Int) val x = Dog1(18) println(x.age) // 18 (success!...
This type of association allows an ActiveRecord model to belong to more than one kind of model record. Common example: class Human < ActiveRecord::Base has_one :address, :as => :addressable end class Company < ActiveRecord::Base has_one :address, :as => :addressable end cl...
The Scala compiler prefixes every argument in the parameter list by default with val. This means that, by default, case classes are immutable. Each parameter is given an accessor method, but there are no mutator methods. For example: case class Foo(i: Int) val fooInstance = Foo(1) val j = fooIn...
The following code broadcasts the contents in buffer among all the processes belonging to the MPI_COMM_WORLD communicator (i.e. all the processes running in parallel) using the MPI_Bcast operation. int rank; int res; res = MPI_Comm_rank (MPI_COMM_WORLD, &rank); if (res != MPI_SUCCESS) { ...
For example, if you are sending an email to a customer after starting a task, it's best to immediately redirect the user to the next page while queuing the email to be sent in the background. This will speed up the load time for the next page, since sending an email can sometimes take several second...
extern crate serde; extern crate serde_json; #[macro_use] extern crate serde_derive; #[derive(Serialize)] struct Person { #[serde(rename="firstName")] first_name: String, #[serde(rename="lastName")] last_name: String, } fn main() { let person = ...
You can test if a string matches several regular expressions using a switch statement. Example case "Ruby is #1!" when /\APython/ puts "Boooo." when /\ARuby/ puts "You are right." else puts "Sorry, I didn't understand that." end This w...
If you know that a value is of a specific type, you can explicitly cast it to that type in order to use it in a context where that type is needed. object value = -1; int number = (int) value; Console.WriteLine(Math.Abs(number)); If we tried passing value directly to Math.Abs(), we would get a ...
If you aren't sure whether a value is of the type you think it is, you can safely cast it using the as operator. If the value is not of that type, the resulting value will be null. object value = "-1"; int? number = value as int?; if(number != null) { Console.WriteLine(Math.Abs(nu...
A value will automatically be cast to the appropriate type if the compiler knows that it can always be converted to that type. int number = -1; object value = number; Console.WriteLine(value); In this example, we didn't need to use the typical explicit casting syntax because the compiler knows...
If you need to know whether a value's type extends or implements a given type, but you don't want to actually cast it as that type, you can use the is operator. if(value is int) { Console.WriteLine(value + "is an int"); }
The following example is an introduction to: Template compilation using underscore Accessing variables in a template Creating a view Rendering a view Showing a view <html> <head> <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script> ...
Given a file sample: hello Hello HELLO_there A normal grep for "hello" returns: $ grep "hello" sample hello Using -i allows to ignore case and match any "hello": $ grep -i "hello" sample hello Hello HELLO_there

Page 4 of 14