This file contains 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
//: Playground - noun: a place where people can play | |
struct Post { | |
let isLiked: Bool | |
let timestamp: Int64 | |
let content: String | |
} | |
extension Post: Equatable { | |
static func == (lhs: Post, rhs: Post) -> Bool { |
This file contains 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
# gdate required to `brew install coreutils` | |
start=`gdate +%s%N` | |
for run in {1..10} | |
do | |
dig blog.gaplotech.com NS @$1 > /dev/null | |
done | |
end=$(gdate +%s%N) | |
diff=$((end-start)) | |
echo "Total Query Time = $((diff/1000000)) ms"; |
This file contains 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
// http://54.169.33.162:8088/GobeeBike/app/ | |
function checkMyCreditCard(phone, password) { | |
fetch('login?industryType=3&requestType=10001&phone=' + phone + '&password=' + password, { | |
method: 'get' | |
}) | |
.then(resp => resp.json()) | |
.then(response => { | |
let token = response.data.token | |
if(!token) throw 'You have entered a wrong phone/password!' |
This file contains 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
// Scala | |
// pattern matching in Option Type | |
val usernameOpt: Option[String] = Some("gary") | |
usernameOpt match { | |
case Some("gary") => println("matched gary!") | |
case Some("peter") => println("matched peter!") | |
case Some(username) => println("found ${username}, but no username is matched") | |
case None => println("No username is found") | |
} |
This file contains 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
// Kotlin NullableType is different from java.util.Optional or scala.Option | |
// it is NOT a Value Type | |
var str: String? = null | |
str == null // Compile OK! => true | |
str = "gary" // Compile OK! | |
str == null // Compile OK! => false |
This file contains 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
// Java | |
// Optional Type Overhead | |
Optional<String> strOpt = null; // Compile OK | |
Optional<String> strOpt = Optional.empty(); // Compile OK | |
strOpt = null; // Compile OK | |
strOpt == null; // Compile OK => true |
This file contains 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
// Every Thing is an expression | |
val input = 1 | |
val result = if(input == 1) "Equal to one" else "Not Equal to One" | |
val result2 = when(input) { | |
1 -> "Equal to one" | |
else -> "Not equal to one" | |
} |
This file contains 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
// Kotlin - currying function example | |
fun doCurrying(first: Int): (Int) -> ((Int, Int) -> Int) -> Int { | |
return { second -> { f -> f(first,second) } } | |
} | |
val add = { a: Int, b: Int -> a + b } | |
val multiply = { a: Int, b: Int -> a * b } | |
val minus = { a: Int, b: Int -> a - b } |
This file contains 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
data class User(val id: Int, val name: String, val address: String) | |
val user = User(id = 1, name = "GARY LO", address = "Hong Kong") | |
// Destructing order is import!! | |
val (userId, userName, userAddress) = user | |
println("id = $userId, name = $userName, address = $userAddress") | |
// id = 1, name = GARY LO, address = Hong Kong |
This file contains 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
val list = listOf(1,2,3,4,5) | |
// destructing the list | |
val (a,b,c) = list | |
println("a = $a, b = $b, c = $c") | |
// a = 1, b = 2, c = 3 | |
// already include in kotlin std-lib | |
val head = list.head |