Skip to content

Instantly share code, notes, and snippets.

git
ghq
fzf
git-now
tig
tmux
gh
jq
fish
@to4iki
to4iki / game01.html
Created February 20, 2013 15:48
JavaScriptで神経衰弱のゲーム - dotinstall
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>jsの勉強03 tips</title>
</head>
<body onload="initCards();">
<h1>神経衰弱</h1>
<p>Your score: <span id="score"></span></p>
// MARK: Date + SwiftCheck
extension Date: RandomType {
public static func randomInRange<G : RandomGeneneratorType>(_ range : (Date, Date), gen : G) -> (Date, G) {
let (min, max) = range
let (t, gen2) = Double.randomInRange((min.timeIntervalSince1970, max.timeIntervalSince1970), gen: gen)
return (Date(timeIntervalSince1970: t), gen2)
}
}
@to4iki
to4iki / Singleton.ts
Created August 31, 2014 16:39
TypeScript - Singleton
class Singleton {
private static _instance:Singleton = null;
constructor() {
if(Singleton._instance){
throw new Error("must use the getInstance.");
}
Singleton._instance = this;
}
public static getInstance():Singleton {
if(Singleton._instance === null) {
@to4iki
to4iki / Reader.swift
Last active March 18, 2018 16:53
DI using `Reader` monad
/// See also:
/// - https://github.com/RxSwiftCommunity/Action
/// - https://github.com/ukitaka/RealmIO
/// - https://medium.com/@JorgeCastilloPr/kotlin-dependency-injection-with-the-reader-monad-7d52f94a482e
public class Reader<Input, Element> {
public typealias WorkFactory = (Input) -> Element
private let workFactory: WorkFactory
public init(_ workFactory: @escaping WorkFactory) {
@to4iki
to4iki / String+truncate.swift
Created March 14, 2018 17:37
string truncate extension
import Foundation
// https://qiita.com/risuke/items/cdd6a75f236faae3ce6b
// https://gist.github.com/budidino/8585eecd55fd4284afaaef762450f98e
extension String {
enum TruncationPosition {
case head
case middle
case tail
}
@to4iki
to4iki / mcake.swift
Created March 11, 2018 10:03
Minimal Cake Pattern
import Foundation
// See also: https://qiita.com/pab_tech/items/1c0bdbc8a61949891f1f
protocol Clock {
var now: Date { get }
}
struct SystemClock: Clock {
let now: Date = Date()
@to4iki
to4iki / DictionaryConverter.swift
Last active February 13, 2018 06:49
convert dictionary
import Foundation
typealias Parameters = [AnyHashable: Any]
protocol Converter {
func convert(_ input: Any) -> Any
}
struct BoolConverter: Converter {
func convert(_ input: Any) -> Any {
@to4iki
to4iki / Using.swift
Created November 27, 2017 15:23
Loan Pattern in Swift
/// See also
/// - http://www.ne.jp/asahi/hishidama/home/tech/scala/sample/using.html
/// - https://qiita.com/piyo7/items/c9be1f39bcfea43a778a
protocol Closer {
func close()
}
struct Loan {
@discardableResult