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
#!/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 |
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
// 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') | |
} |
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://stackoverflow.com/questions/40398072/singleton-with-parameter-in-kotlin | |
class TasksLocalDataSource private constructor(context: Context) : TasksDataSource { | |
private val mDbHelper: TasksDbHelper | |
init { | |
// You cannot pass null in kotlin unless you are using `context: Context?` | |
// therefore, checking null is useless | |
// checkNotNull(context) |
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 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 { |
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 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() |
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 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); | |
}}; |
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 abc: Map<String,Int> = mapOf("a" to 1, "b" to 2, "c" to 3) | |
// the actual type of the Map is obvious | |
// thus we can skip Map<String,Int> and let kotlin compiler to make type inference | |
val abc = mapOf("a" to 1, "b" to 2, "c" to 3) | |
val abcd = abc + ("d" to 4) // ok! | |
val abce = abc + ("e" to "5") // compile error: Type mismatch | |
// val c: Int? = abc["c"] | |
var c = abc["c"] |
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 | |
// 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); |
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 | |
// 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.. | |
} |
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 | |
// 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 { |
OlderNewer