Skip to content

Instantly share code, notes, and snippets.

View zhigang1992's full-sized avatar
:octocat:
Focusing

Kyle Fang zhigang1992

:octocat:
Focusing
View GitHub Profile
@zhigang1992
zhigang1992 / scroll.swift
Last active March 14, 2016 10:36
Different scroll paging
import XCPlayground
import UIKit
let collectionCellId = "CollectionCell"
func newCollectionView() -> UICollectionView {
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 250, height: 300)
layout.scrollDirection = .Horizontal
let collectionView = UICollectionView(frame: CGRect(x: 0, y:0, width: 300, height: 500), collectionViewLayout: layout)
@zhigang1992
zhigang1992 / freer.swift
Last active March 22, 2016 14:03
Attemp on freer in swift
enum Freer<V, M, MV> {
case Pure(V)
case Impure(M, MV->Freer<V, M, MV>)
func flatMap<U>(f:V -> Freer<U, M, MV>) -> Freer<U, M, MV> {
switch self {
case .Pure(let v):
return f(v)
case .Impure(let v, let k):
return .Impure(v, { newV in
// (State, Action) -> State
class Store<State, Action> {
typealias Reducer = (State, Action) -> State
typealias Subscriber = State -> Void
typealias Disposable = () -> Void
private(set) var state: State
private let reducer:Reducer
private var subscribers:[Subscriber?] = []
typealias Disposable = () -> Void
protocol StoreType {
associatedtype StateType
associatedtype ActionType
var state:StateType { get }
func dispatch(action:ActionType)
func subscribe(subscriber:(StateType)->Void) -> Disposable
}
// originally from https://gist.github.com/cobbal/7562875ab5bfc6f0aed6
protocol Monad {
associatedtype V // Value Type
associatedtype U // Constraint Type
func bind<M : Monad where M.U == U>(f: V -> M) -> M
static func just(unit: V) -> Self
}
extension Monad {
import Foundation
let dictionary: NSDictionary = [
"env": [
"address": "Home"
],
"setting": [
"up": "Hello",
"spnining": false
func const<A, B>(value:A) -> B -> A {
return { _ in value }
}
func f<T: Equatable>(o: Optional<T> -> [T], i: T) -> Optional<T> -> [T] {
return { optional in
if optional == i {
return o(optional)
}
return o(i) + [i]
struct Async<T> {
let execution: (T->Void, ErrorType->Void) -> Void
static func unit(value:T) -> Async<T> {
return Async { $0.0(value) }
}
}
infix operator >>= { associativity left }
func >>=<A, B>(f: A->Async<B>, a:Async<A>) -> Async<B> {
@zhigang1992
zhigang1992 / Slides.swift
Last active June 5, 2016 14:04
Slide in Playground
import UIKit
infix operator |> { associativity left precedence 90 }
func |> <A, B> (l: A, r: A -> B) -> B {
return r(l)
}
infix operator <+> { associativity left precedence 110 }
func <+><A>(l: A->A, r: A->A) -> A->A {
return { v in
r(l(v))
@zhigang1992
zhigang1992 / git commit command.sh
Last active March 21, 2024 00:22
Git Previous and Next Commit
function n() {
git reset --hard HEAD
git log --reverse --pretty=%H master | grep -A 1 $(git rev-parse HEAD) | tail -n1 | xargs git checkout
}
function p() {
git reset --hard HEAD
git checkout HEAD^1
}