Skip to content

Instantly share code, notes, and snippets.

View moaible's full-sized avatar

Hiromi Motodera moaible

View GitHub Profile
@moaible
moaible / typedreact.js
Last active April 14, 2017 07:54
Typed React Component
/* @flow */
import React from 'react'
export interface ReactComponentable<T, U> {
props: T;
state: U;
configureStateFor(props: T): ?U;
}
export class TypedReactComponent<T, U> extends React.Component<*, *, *> implements ReactComponentable<T, U> {
@moaible
moaible / StoryboardInstantiate.swift
Last active November 13, 2017 23:26
StoryboardInstantiate.swift
import UIKit
protocol StoryboardInstantiate {
static var storyboardIdentifier: String { get }
static var storyboardName: String { get }
}
extension UIStoryboard {
static func instantiateViewController<ViewController: UIViewController>() -> ViewController where ViewController: StoryboardInstantiate {
@moaible
moaible / Nibable.swift
Created April 18, 2017 15:35
Nibable.swift
import UIKit
protocol Nibable {
static var nibName: String { get }
}
extension UINib {
convenience init<View: UIView>(nibableClass: View.Type) where View: Nibable {
self.init(nibName: nibableClass.nibName, bundle: nil)
@moaible
moaible / Sort.js
Last active May 30, 2017 14:16
(JavaScript+Flowtype).Sort
type SortDirection = "ascending" | "descending"
reverseDirection(sortDirection: SortDirection): SortDirection {
return sortDirection == "ascending" ? "descending" : "ascending"
}
sortStatement(lhs: any, rhs: any, sortDirection: SortDirection): number {
if (lhs === rhs) {
return 0
@moaible
moaible / phantom.kt
Created July 10, 2017 13:46
Kotlin phantom type
fun main(args: Array<String>) {
Distributer<Initial>().hoge()
// Distributer<Fetch>().hoge() ->
// Type mismatch: inferred type is Distributer<Fetch> but Distributer<Initial> was expected
}
interface DistributeStatusable {
}
@moaible
moaible / Array+Average.swift
Last active November 6, 2017 22:46
PaizaQuiz Swift Programs
extension Array where Element == Int {
/// Returns the sum of all elements in the array
var total: Element {
return reduce(0, +)
}
/// Returns the average of all elements in the array
var average: Double {
return isEmpty ? 0 : Double(reduce(0, +)) / Double(count)
}
}
@moaible
moaible / SlideView.swift
Last active November 1, 2017 22:48
SlideView.swift
final class SlideView: UIView {
// MARK: - Property
private lazy var scrollView = UIScrollView()
private var contentViews: [UIView] = []
// MARK: - Initialize
@moaible
moaible / ButtonView.swift
Last active November 8, 2017 23:15
ButtonView.swift
/// UIButtonライクにボタンの振る舞いをするViewコンポーネント
class ButtonView: UIView {
// MARK: - Property
// MARK: Public
override var tintColor: UIColor! {
willSet {
backgroundColor = newValue
@moaible
moaible / InfinityRotateAnimation.swift
Last active November 13, 2017 23:10
InfinityRotateAnimation.swift
import UIKit
/// viewのlayerとして追加するとクルクル回るようにするコンポーネント
class InfinityRotateAnimation: CABasicAnimation {
override init() {
super.init()
self.keyPath = "transform"
self.repeatCount = .infinity
self.duration = 0.5
@moaible
moaible / BlinkView.swift
Created November 13, 2017 23:36
[WIP]BlinkView.swift
class BlinkView: UIView {
// MARK: - Property
private var contentViews: [UIView] = []
private(set) var displayView: UIView? {
willSet {
displayView?.removeFromSuperview()
if let displayView = newValue {