Skip to content

Instantly share code, notes, and snippets.

View kittinunf's full-sized avatar

Kittinun Vantasin kittinunf

View GitHub Profile
" Colemak stuff
nnoremap k h
nnoremap n j
nnoremap e k
nnoremap i l
nnoremap d v
nnoremap D V
nnoremap s d
nnoremap S D
//swift
protocol Addable {
func add(lhs: Self, rhs: Self) -> Self
}
extension String: Addable {
func add(lhs: String, rhs: String) -> String {
return lhs + rhs
}
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
public protocol Apply {}
extension Apply where Self: Any {
//let f = Foo().apply {
// $0.foo = 10
// $0.bar = "bar"
//}
public func apply(_ block: (inout Self) -> Void) -> Self {
var copy = self
sealed class Either<out L, out R> {
companion object {
fun <L> left(left: L): Left<L, Nothing> = Left(left)
fun <R> right(right: R): Right<Nothing, R> = Right(right)
}
fun left(): L? = when (this) {
is Left -> this.l
is Right -> null
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:
//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 -> {
@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
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
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()