Skip to content

Instantly share code, notes, and snippets.

View gaplo917's full-sized avatar
🎯
Focusing on Kotlin

Gary Lo gaplo917

🎯
Focusing on Kotlin
View GitHub Profile
@gaplo917
gaplo917 / SortedListQuestion.swift
Created May 20, 2018 18:10
Swift Sorted List Question
//: 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 {
@gaplo917
gaplo917 / dig_test.sh
Created April 1, 2018 17:17
Time the dig test in MacOS
# 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";
@gaplo917
gaplo917 / check.js
Created April 21, 2017 20:01
GoBee.bike Credit Card check script
// 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!'
@gaplo917
gaplo917 / ScalaOptionTypePatternMatching.scala
Last active January 15, 2017 16:57
Scala Option Type Pattern matching
// 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")
}
@gaplo917
gaplo917 / KotlinNullableTypeZeroOverhead.kt
Last active January 15, 2017 17:21
Kotlin Nullable Type zero overhead
// 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
@gaplo917
gaplo917 / JavaOptionalOverhead.java
Last active January 15, 2017 17:19
Java Optional overhead
// 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
@gaplo917
gaplo917 / KotlinFunctionalSupport5.kt
Created January 15, 2017 15:19
Kotlin Functional Support 5 - Every Thing is an expression
// 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"
}
@gaplo917
gaplo917 / KotlinFunctionalSupport4.kt
Last active January 15, 2017 17:52
Kotlin Function Support 4
// 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 }
@gaplo917
gaplo917 / KotlinFunctionalSupport3.kt
Last active January 15, 2017 14:24
Kotlin Functional Support 3
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
@gaplo917
gaplo917 / KotlinFunctionalSupport2.kt
Last active January 15, 2017 17:35
Kotlin Functional Support 2
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