Tuples
A tuple is a collection which is ordered and immutable(cant be changed).
let laptopandprice = ("MacbookAir", 120000)
We can also type check the tuples.
let laptopandprice: (string, int) = ("MacbookAir", 120000)
Tuples can also carry more than two values
let laptoppriceandbacklit : (string, int, bool) = ("Macbookair", 120000, true)
We can also access values from tuple for instance if we wanted to get the backit prop of a tuple we can have
let (_,_, isMacbookBacklit) = laptoppriceandbacklit
the isMacBookBackit will be bound to the bool provided when the tuple was created.
As I said in the previous lessons, everything is a pattern
For instance lets say we wanted to match on the laptopandprice tuple and return a string based on the laptop type which could represent the apps one could build on the respective machines.
For Instance if its a MacbookAir or Macbookpro we can print ios apps, if its HP or lenovo we can print out android apps
Lets put that in practice.
let appsCapacity = laptopspecs => switch laptopspecs {
| ("Macbookair", _) => "Ios"
| ("Macbookpro", _) => "Ios"
| ("HP" | "Lenovo", _) => "Android"
| (_, _) => "Web Development"
};
appsCapacity(("HP", 20000)) // will return "Web development"
Now that we’ve seen how tuples work and how we can extract values from them and at the same time pattern match on them, we will proceed to records.