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
std::unordered_map<std::string,std::string> m{{"1", "2"}, {"3", "4"}, {"5", "6"}}; | |
std::unordered_map<std::string,std::string> ms; | |
std::transform(std::begin(m), std::end(m), std::inserter(ms, std::begin(ms)), [](const auto& p) { return {p.first, p.first}; }; |
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
//a somewhat contrived example of how reactive programming can be useful | |
//some real concerns that are omitted, but can be easily accomplished using rxswift are | |
//error handling, displaying loading indicators, etc | |
// A result object that comes from the network. | |
// The contents are irrelevant for this example. | |
struct Result { | |
let text: String | |
let someOtherThing: String |
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
interface Fooable { | |
fun foo(): String | |
} | |
class FooImpl : Fooable { | |
override fun foo() = "I am fooImpl" | |
} | |
class AnotherFooImpl : Fooable { | |
override fun foo() = "I am anotherFooImpl" |
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
struct Error : ErrorType { | |
let message: String | |
} | |
struct Json { | |
let dictionary: [String : AnyObject] | |
init?(dictionary: [String : AnyObject]?) { | |
guard let dictionary = dictionary else { return nil } |
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
struct Parent { | |
let id: Int | |
let children: [Int] | |
} | |
struct Node { | |
let id: Int | |
let parent: Int | |
let children: [Int] | |
} |
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
fun doSomething(path: String) { | |
val content: String //instead of declare as `var content: String? = null` | |
val br = BufferedReader(FileReader(path)) | |
var currentLine = br.readLine() | |
content = buildString { | |
while (currentLine != null) { | |
appendln(currentLine) | |
currentLine = br.readLine() |
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
interface SingleAssignType<T> { | |
operator fun getValue(thisRef: Any?, property: KProperty<*>): T | |
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) | |
} | |
class SingleAssign<T> : SingleAssignType<T> { | |
private var initialized = false | |
private var _value: Any? = null |
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
// Basic setup | |
interface Exp | |
data class Lit(val x: Int): Exp | |
data class Add(val x: Exp, val y: Exp): Exp | |
interface IntAlg<A> | |
{ | |
fun lit(x: Int): A | |
fun add(x: A, y: A): A |
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
//without phantom type | |
enum class Currency { JPY, THB } | |
data class Money(val amount: Double, val currency: Currency) | |
class IllegalCurrencyException : Throwable("Wrong currency") | |
fun convertJPYToTHB(jpy: Money): Money { | |
//check whether we have right format | |
when(jpy.currency) { | |
Currency.JPY -> { |
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
class MyTestClass { | |
companion object { | |
init { | |
// things that may need to be setup before companion class member variables are instantiated | |
} | |
// variables you initialize for the class just once: | |
val someClassVar = initializer() | |
// variables you initialize for the class later in the @BeforeClass method: |