Skip to content

Instantly share code, notes, and snippets.

View Allan-Gong's full-sized avatar

Allan Gong Allan-Gong

  • Facebook
  • Seattle, US
View GitHub Profile
@Allan-Gong
Allan-Gong / reverse.hs
Created March 22, 2018 05:19
Reverse a list in Haskell
-- Reverse a list in Haskell is cool (Pseudo syntax):
-- reverse = foldLeft (flip (:.)) Nil
-- or in formal haskell syntax:
reverse = foldl (flip (:)) []
@Allan-Gong
Allan-Gong / flatmap_optionals.java
Created January 18, 2018 05:01
Options to flatMap a list of Optionals in Java8
// 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()
@Allan-Gong
Allan-Gong / apply_with.kt
Created January 9, 2018 22:23
apply V.S with in Kotlin
// 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 {
@Allan-Gong
Allan-Gong / notes.ts
Created December 13, 2016 01:22
Notes for typescript
// 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
@Allan-Gong
Allan-Gong / RevealingModulePattern.js
Created November 15, 2016 13:09
Javascript's Revealing Module Pattern by example
var myRevealingModule = (function () {
var privateVar = "Ben Cherry",
publicVar = "Hey there!";
function privateFunction() {
console.log( "Name:" + privateVar );
}
function publicSetName( strName ) {
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
@Allan-Gong
Allan-Gong / let_or_var.js
Created August 22, 2016 01:58
JS: let or var
// 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
trait U {
self =>
val name = "outer"
val b = new AnyRef {
val name = "inner"
println(name)
println(this.name)
println(self.name)
}
}
@Allan-Gong
Allan-Gong / tricks.scala
Created May 3, 2016 14:21
Tricks in scala language
// 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:
@Allan-Gong
Allan-Gong / context_bound.scala
Created May 3, 2016 06:09
Context bound in scala
// 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].