This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Reverse a list in Haskell is cool (Pseudo syntax): | |
-- reverse = foldLeft (flip (:.)) Nil | |
-- or in formal haskell syntax: | |
reverse = foldl (flip (:)) [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ways to transform List<Optional<T>> to List<T> (excluding empty ones) | |
// 1. Using filter() | |
List<String> filteredList = listOfOptionals.stream() | |
.filter(Optional::isPresent) | |
.map(Optional::get) | |
.collect(Collectors.toList()); | |
// 2. Using flatMap() | |
List<String> filteredList = listOfOptionals.stream() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage of apply: do something with an object and return it | |
fun getDeveloper(): Developer { | |
return Developer().apply { | |
developerName = "Amit Shekhar" | |
developerAge = 22 | |
} | |
} | |
// Usage of with: transform A -> B | |
fun getPersonFromDeveloper(developer: Developer): Person { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Typed Tuple | |
let error: [number, string] = [123, 'Some Message']; | |
// Both correctly typed | |
let [code, message] = error; // code is number and message is string | |
let anotherCode = error[0]; // anotherCode is number | |
let anotherMessage = error[1]; // anotherMessage is string | |
// 2. String Literal Type |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myRevealingModule = (function () { | |
var privateVar = "Ben Cherry", | |
publicVar = "Hey there!"; | |
function privateFunction() { | |
console.log( "Name:" + privateVar ); | |
} | |
function publicSetName( strName ) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Foo { def bar: Int } | |
object F1 extends Foo { def bar = util.Random.nextInt(33) } // ok | |
class F2(val bar: Int) extends Foo // ok | |
object F3 extends Foo { | |
lazy val bar = { // ok | |
Thread.sleep(5000) // really heavy number crunching | |
42 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The difference is scoping. var is scoped to the nearest function block and let is scoped to the nearest enclosing block | |
// (both are global if outside any block), which can be smaller than a function block. | |
// Also, variables declared with let are not visible before they are declared in their enclosing block. | |
// In the example below, let is only visible in the for() loop and var is visible to the whole function. | |
function allyIlliterate() { | |
//tuce is *not* visible out here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait U { | |
self => | |
val name = "outer" | |
val b = new AnyRef { | |
val name = "inner" | |
println(name) | |
println(this.name) | |
println(self.name) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// when passing parameters to method calls. You may replace parenthesis with curly braces if, and only if, | |
// the method expects a single parameter. For example: | |
List(1, 2, 3).reduceLeft{_ + _} // valid, single Function2[Int,Int] parameter | |
List{1, 2, 3}.reduceLeft(_ + _) // invalid, A* vararg parameter | |
// Why multiple parameters lists scala ? | |
// First: you can have multiple var args: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Consider the method tabulate which forms an array from the results of applying a given function f on a range of numbers from 0 until a given length. | |
// Up to Scala 2.7, tabulate could be written as follows: | |
def tabulate[T](len: Int, f: Int => T) = { | |
val xs = new Array[T](len) | |
for (i <- 0 until len) xs(i) = f(i) | |
xs | |
} | |
// In Scala 2.8 this is no longer possible, because runtime information is necessary to create the right representation of Array[T]. |