Skip to content

Instantly share code, notes, and snippets.

@nanoxd
nanoxd / run.sh
Last active June 10, 2019 19:44
[Xcode Diet] Clean up after Xcode's voracious hard drive appetite
#!/usr/bin/env sh
set -e
fancy_echo() {
local fmt="$1"; shift
printf "\n$fmt\n" "$@"
}
@nanoxd
nanoxd / PermutationIterator.swift
Created December 10, 2018 00:49
[PermutationIterator] Use of Heap's Algorithm to create permutations
public struct PermutationIterator<T>: IteratorProtocol {
private var hasReturnedInitial = false
private var a: [T]
private var c: [Int]
private let n: Int
private var i = 0
public init<C: Collection>(_ values: C) where C.Element == T {
a = Array(values)
n = a.count
@nanoxd
nanoxd / Collection+anySatisfy.swift
Created December 10, 2018 12:17
[Collection.anySatisfy] With the introduction of allSatisfy, we can cleanly represent the inverse.
extension Collection {
func anySatisfy(_ p: (Element) -> Bool) -> Bool {
return !self.allSatisfy { !p($0) }
}
}
@nanoxd
nanoxd / HelloWorldViewModel.swift
Created December 16, 2018 02:34
[ViewModelType] Provide a clear contract between inputs/outputs desired in a view model
final class HelloWorldViewModel: ViewModelType {
let input: Input
let output: Output
struct Input {
let name: Anyobserver<String>
}
struct Output {
let greeting: Driver<String>
@nanoxd
nanoxd / Either.swift
Created December 22, 2018 03:16
[Either] A type representing an alternative of one of two types.
/// A type representing an alternative of one of two types.
///
/// By convention, and where applicable, `Left` is used to indicate failure, while `Right` is used to indicate success. (Mnemonic: “right” is a synonym for “correct.”)
///
/// Otherwise, it is implied that `Left` and `Right` are effectively unordered alternatives of equal standing.
public enum Either<Left, Right> {
case left(Left)
case right(Right)
/// Returns the value of `Left` instances, or `nil` for `Right` instances.
@nanoxd
nanoxd / main.swift
Created February 7, 2019 23:56
[Assert Main Queue] Assert you're on main queue. Note that main thread and main queue are not always the same thing http://blog.benjamin-encz.de/post/main-queue-vs-main-thread/
dispatchPrecondition(
condition: DispatchPredicate.onQueue(DispatchQueue.main)
)
@nanoxd
nanoxd / Request+Result.swift
Last active March 30, 2019 21:49
Request+Result.swift #swift
extension Request where Response: Decodable {
func handle(
response: Result<Data, Error>,
completion: (Result<Response, Error>) -> Void) {
completion(Result {
try JSONDecoder().decode(Response.self, from: response.get())
})
}
}
@nanoxd
nanoxd / Pendulum.swift
Created March 30, 2019 21:55
A wrapper around a repeating timer that does not require invalidation.
/// A wrapper around a repeating timer that does not require invalidation.
final class Pendulum {
let timer: Timer
init(seconds: TimeInterval, closure: @escaping () -> ()) {
timer = Timer.scheduledTimer(
withTimeInterval: seconds,
repeats: true,
block: { _ in
closure();
@nanoxd
nanoxd / rbenv.plugin.zsh
Created August 19, 2019 10:51
Plugin for rbenv on ZSH
found_rbenv=''
rbenvdirs=("$HOME/.rbenv" "$HOME/.local/rbenv" "/usr/local/opt/rbenv" "/usr/local/rbenv" "/opt/rbenv")
for rbenvdir in "${rbenvdirs[@]}" ; do
if [ -z "$found_rbenv" ] && [ -d "$rbenvdir/versions" ]; then
found_rbenv=true
if [ -z "$RBENV_ROOT" ]; then
RBENV_ROOT=$rbenvdir
export RBENV_ROOT
fi
import RxRelay
import RxSwift
@propertyWrapper
public struct BehaviorRelayWrapping<T> {
private let subject: BehaviorRelay<T>
// MARK: PropertyWrapper
public let wrappedValue: Observable<T>