Tutorial by Examples

Given the following CSV file: Id,Name 1,"Joel" 2,"Adam" 3,"Ryan" 4,"Matt" You can read the data with the following script: #r "FSharp.Data.dll" open FSharp.Data type PeopleDB = CsvProvider<"people.csv"> let people = Pe...
BEGIN UPDATE Employees SET PhoneNumber = '5551234567' WHERE Id = 1; UPDATE Employees SET Salary = 650 WHERE Id = 3; END
Create Subtree Add a new remote called plugin pointing to the plugin's repository: git remote add plugin https://path.to/remotes/plugin.git Then Create a subtree specifying the new folder prefix plugins/demo. plugin is the remote name, and master refers to the master branch on the subtree's rep...
String concatenation can be performed using the + operator. For example: String s1 = "a"; String s2 = "b"; String s3 = "c"; String s = s1 + s2 + s3; // abc Normally a compiler implementation will perform the above concatenation using methods involving a StringBui...
0.18.0 Prior to 0.18.0 you can create ranges like this: > range = [1..5] [1,2,3,4,5] : List number > > negative = [-5..3] [-5,-4,-3,-2,-1,0,1,2,3] : List number 0.18.0 In 0.18.0 The [1..5] syntax has been removed. > range = List.range 1 5 [1,2,3,4,5] : List number > &g...
> listOfNumbers = [1,4,99] [1,4,99] : List number > > listOfStrings = ["Hello","World"] ["Hello","World"] : List String > > emptyList = [] -- can be anything, we don't know yet [] : List a > Under the hood, List (linked list) is ...
> ourList = [1,2,3,4,5] [1,2,3,4,5] : List number > > firstElement = List.head ourList Just 1 : Maybe Int > > allButFirst = List.tail ourList Just [2,3,4,5] : Maybe (List Int) This wrapping into Maybe type happens because of the following scenario: What should List.head ret...
List.map : (a -> b) -> List a -> List b is a higher-order function that applies a one-parameter function to each element of a list, returning a new list with the modified values. import String ourList : List String ourList = ["wubba", "lubba", "dub",...
List.filter : (a -> Bool) -> List a -> List a is a higher-order function which takes a one-parameter function from any value to a boolean, and applies that function to every element of a given list, keeping only those elements for which the function returns True on. The function that List.f...
def multiply(factor: Int)(numberToBeMultiplied: Int): Int = factor * numberToBeMultiplied val multiplyBy3 = multiply(3)_ // resulting function signature Int => Int val multiplyBy10 = multiply(10)_ // resulting function signature Int => Int val sixFromCurriedCall = multiplyBy3(2) //6...
def numberOrCharacterSwitch(toggleNumber: Boolean)(number: Int)(character: Char): String = if (toggleNumber) number.toString else character.toString // need to explicitly specify the type of the parameter to be curried // resulting function signature Boolean => String val switchBetween3A...
def minus(left: Int, right: Int) = left - right val numberMinus5 = minus(_: Int, 5) val fiveMinusNumber = minus(5, _: Int) numberMinus5(7) // 2 fiveMinusNumber(7) // -2
To create a collection of n copies of some object x, use the fill method. This example creates a List, but this can work with other collections for which fill makes sense: // List.fill(n)(x) scala > List.fill(3)("Hello World") res0: List[String] = List(Hello World, Hello World, Hello...
Partial functions are often used to define a total function in parts: sealed trait SuperType case object A extends SuperType case object B extends SuperType case object C extends SuperType val pfA: PartialFunction[SuperType, Int] = { case A => 5 } val pfB: PartialFunction[SuperType,...
While partial function are often used as convenient syntax for total functions, by including a final wildcard match (case _), in some methods, their partiality is key. One very common example in idiomatic Scala is the collect method, defined in the Scala collections library. Here, partial functions ...
Scala has a special type of function called a partial function, which extends normal functions -- meaning that a PartialFunction instance can be used wherever Function1 is expected. Partial functions can be defined anonymously using case syntax also used in pattern matching: val pf: PartialFunction...
Partial functions are very common in idiomatic Scala. They are often used for their convenient case-based syntax to define total functions over traits: sealed trait SuperType // `sealed` modifier allows inheritance within current build-unit only case object A extends SuperType case object B exten...
For...Next loop is used for repeating the same action for a finite number of times. The statements inside the following loop will be executed 11 times. The first time, i will have the value 0, the second time it will have the value 1, the last time it will have the value 10. For i As Integer = 0 To...
Some JavaScript engines (for example, the current version of Node.js and older versions of Chrome before Ignition+turbofan) don't run the optimizer on functions that contain a try/catch block. If you need to handle exceptions in performance-critical code, it can be faster in some cases to keep the ...
To trim whitespace from the edges of a string, use String.prototype.trim: " some whitespaced string ".trim(); // "some whitespaced string" Many JavaScript engines, but not Internet Explorer, have implemented non-standard trimLeft and trimRight methods. There is a proposa...

Page 199 of 1336