Skip to content

Instantly share code, notes, and snippets.

View kittinunf's full-sized avatar

Kittinun Vantasin kittinunf

View GitHub Profile
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}; };
//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
interface Fooable {
fun foo(): String
}
class FooImpl : Fooable {
override fun foo() = "I am fooImpl"
}
class AnotherFooImpl : Fooable {
override fun foo() = "I am anotherFooImpl"
@kittinunf
kittinunf / Json.swift
Last active May 27, 2016 06:25
JsonParser
struct Error : ErrorType {
let message: String
}
struct Json {
let dictionary: [String : AnyObject]
init?(dictionary: [String : AnyObject]?) {
guard let dictionary = dictionary else { return nil }
struct Parent {
let id: Int
let children: [Int]
}
struct Node {
let id: Int
let parent: Int
let children: [Int]
}
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()
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
@kittinunf
kittinunf / ObjectAlgebra.kt
Created August 11, 2016 16:28 — forked from norswap/ObjectAlgebra.kt
Object Algebras in Kotlin
// 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
//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 -> {
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: