Skip to content

Instantly share code, notes, and snippets.

Revolucent / Json.kt
Created Jan 31, 2022
Some combinators which implement a DSL for querying and deserializing Json from Strings. Extend or alter to your taste.
View Json.kt
typealias Deserializer<I, O> = (I) -> O?
typealias JsonDeserializer<I, O> = (Json) -> Deserializer<I, O>
typealias JsonElementDeserializer = JsonDeserializer<JsonElement, JsonElement>
private fun <I, O> const(deserializer: Deserializer<I, O>): JsonDeserializer<I, O> = {
infix fun <A, B, C> JsonDeserializer<A, B>.j(
next: JsonDeserializer<B, C>
Revolucent / KeyboardAvoidingViewController.swift
Last active Jun 8, 2020
Modern implementation of KeyboardAvoidingViewController
View KeyboardAvoidingViewController.swift
import UIKit
open class KeyboardAvoidingViewController: UIViewController {
private var observers: [NSObjectProtocol] = []
open override func viewWillAppear(_ animated: Bool) {
let nc = NotificationCenter.default
Revolucent / Api.hs
Last active Sep 21, 2019
A simple wrapper around Network.HTTP.Req to talk to an API.
View Api.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TupleSections #-}
View MapDispatchToProps.js
import parse from 'obj-parse'
In its simplest usage, mapStateToProps('foo', 'bar')
maps the state keys 'foo' and 'bar' to keys of the same
name in the props.
To alias, use an object literal: mapStateToProps('foo', {baz: ''}).
In this case, we're mapping state that's more deeply nested to the 'baz' prop.
The parse method of obj-parse takes care of this for us.
Revolucent / Family.swift
Last active Oct 28, 2018
Functions for recursively finding ancestor and descendants in Swift. Useful for creating extension methods.
View Family.swift
public func descendants<Descendant>(of parent: Descendant, in attribute: KeyPath<Descendant, [Descendant]>, where: (Descendant) throws -> Bool) rethrows -> [Descendant] {
var descendants: [Descendant] = []
for child in parent[keyPath: attribute] {
if try `where`(child) {
try descendants.append(contentsOf: MASCore.descendants(of: child, in: attribute, where: `where`))
return descendants
Revolucent / UIView+Rx.swift
Created Oct 5, 2018
isFirstResponder observable with RxSwift
View UIView+Rx.swift
import RxCocoa
import RxSwift
import UIKit
extension Reactive where Base: UIView {
var isFirstResponder: Observable<Bool> {
return Observable
Revolucent / RxReSwift.swift
Created Sep 24, 2018
Observe ReSwift subscriptions using RxSwift
View RxReSwift.swift
import Foundation
import ReSwift
import RxSwift
private class StoreObserver<SelectedState>: StoreSubscriber {
private let onNext: (SelectedState) -> Void
init(onNext: @escaping (SelectedState) -> Void) {
self.onNext = onNext
func newState(state: SelectedState) {
Revolucent / ThrowIfNil.swift
Created Jul 7, 2018
Throw if nil, otherwise return value
View ThrowIfNil.swift
infix operator ??!: NilCoalescingPrecedence
If the left-hand side in nil, the error on the right-hand side is thrown. Otherwise,
the unwrapped value is returned. For example:
// Instead of this…
guard let x = y else {
Revolucent / Semicircle.swift
Created May 20, 2018
Draw Semircle Between Any Two Arbirtary CGPoints
View Semicircle.swift
extension CGContext {
// via
func drawSemicircle(from: CGPoint, to: CGPoint, clockwise: Bool) {
let center = CGPoint(x: 0.5 * (from.x + to.x), y: 0.5 * (from.y + to.y))
let radius = 0.5 * hypot(to.x - from.x, to.y - from.y)
let startAngle = atan2(to.y - from.y, to.x - from.x)
let endAngle = startAngle + .pi
move(to: from)
addArc(center: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: clockwise)
Revolucent / Median.pgsql
Created Dec 2, 2017
Calculate a median
View Median.pgsql
FROM unnest($1) val
LIMIT 2 - MOD(array_upper($1, 1), 2)