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 / mac-install.sh
Last active June 20, 2016 03:42
Mac-install-script
#!/bin/sh
# install shit on mac osx
# author: mcor
# date: 2015-12-17
# install brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# install cask
brew tap caskroom/cask
// https://blog.gaplotech.com/content/images/2016/06/Screen-Shot-2016-06-22-at-22-09-29.png
// Result not consistent : 12291 false, 37709 true
for(var i=0; i<50000; ++i) {
console.log(typeof null === 'undefined')
}
// OR
for(var i=0; i<50000; ++i) {
console.log(typeof null == 'undefined')
}
@gaplo917
gaplo917 / NullSafe.kt
Last active December 7, 2016 15:36
Kotlin Null Safe Example
// Kotlin null safe example
// Java null risk example https://gist.github.com/gaplo917/5dc037bedea39a348854ae59ba89c063
fun doSth(str: String): String {
return str.toLowerCase()
}
fun doSthOptional(str: String?): String? {
// Optional chainning, similiar to Swift
return str?.toLowerCase()
@gaplo917
gaplo917 / NullRisk.java
Last active December 7, 2016 15:38
Java Null Risk Example
// Java null risk workaround example
// Kotlin null safe example https://gist.github.com/gaplo917/77d7ee9f48ed20f4a60bc20a1b39a2e6
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import static java.lang.System.*;
class NullRisk {
@gaplo917
gaplo917 / Verbose.java
Last active December 7, 2016 15:45
Java has no Type Inference that makes code become verbose
// Java 9
final Map<String,Integer> abc = Map.of("a",1, "b", 2, "c", 3);
// Java >= 5
final Map<String,Integer> abc = new HashMap<String, Integer>() {{
put("a",1);
put("b",2);
put("c",3);
}};
@gaplo917
gaplo917 / WithoutNamedDefaultArgument.java
Last active December 7, 2016 15:50
Java without Named and Default arguments makes code verbose and increase the chance of human error
// Java
// Ugly work around to save time (compared to method overload)
void doSomething(
@NotNull String fname,
@NotNull String lname,
@Nullable String addr,
@Nullable Gender gender
) {
requireNonNull(fname);
requireNonNull(lname);
@gaplo917
gaplo917 / NamedDefaultArgument.kt
Last active December 7, 2016 15:57
Kotlin with Named and Default Argument help to write clean code
// Kotlin
// Default argument
fun doSomething(
fname: String,
lname: String,
addr: String = "N/A",
gender: Gender = Gender.Unknown
){
// do something that really need fname,lname, addr, gender..
}
@gaplo917
gaplo917 / UtilsPattern.java
Last active December 7, 2016 16:10
Java Utils Pattern is not Human friendly or IDE friendly
// Java
// please focus on the function name & declaration
// As we all know, String is a final class and we can't extend
class StringUtils{
public static TypeA toTypeA(String str)
}
// Assume TypeA is from third party library that you can't extend
class TypeAUtils {
@gaplo917
gaplo917 / Extensions.kt
Last active December 7, 2016 16:27
Kotlin Extension solve Java Utils Patterns
// Kotlin
// extension, give toTypeA() functionality to String
fun String.toTypeA(): TypeA {
return TypeA(str = this)
}
// give calculateResult() functionality to TypeA
fun TypeA.calculateResult(): Double {
return this.str.length + 0.123456789
@gaplo917
gaplo917 / DataClassExample.kt
Last active December 7, 2016 17:11
Kotlin Data class is handy
// Kotlin
data class User(
// primary construtor
val id: UUID,
val username: String,
val firstName: String? = null,
val lastName: String? = null,
val address: String? = null,
val isEmailVerified: Boolean = false