Skip to content

Instantly share code, notes, and snippets.

//mutable
int x = 1;
var y = new Dictionary<string, string>();
//immutable
const int a = 1;
const var b = new Dictionary<string, string>(); // Won't compile
let x = 123;
let mut y = "Hello";
final Person frank = new Person("Frank", "Walker");
frank.setFirstName("Bob");
System.out.println(frank.getFirstName()) // prints "Bob"
let num = if input > 0 then true else false
def factorial(n: Int) : Int = {
@tailrec
def factorialWithAccumulator(acc: Int, n: Int) : Int = {
if (number == 1) acc
else factorialWithAccumulator(accumulator * n, n - 1)
}
factorialWithAccumulator(1, n)
}
String constructionFirm = person.getResidence().getAddress().getConstructionFirm();
//Can't do this because residence, address or constructionFirm could be null.
String constructionFirm = null;
if(person != null){
Residence residence = person.getResidence();
if(residence != null){
Address address = residence.getAddress();
if(address != null){
constructionFirm = adress.getConstructionFirm();
def constructionFirm = person?.residence?.address?.constructionFirm
constructionFirm = person&.residence&.address&.constructionFirm
personOption |> Option.bind (fun p -> p.residence)
|> Option.bind (fun r -> r.address)
|> Option.map (fun a -> a.constructionFirm)
Optional<String> firm = personOption
.flatMap(Person::getResidence)
.flatMap(Residence::getAddress)
.map(Address::getConstructionFirm)