Skip to content

Instantly share code, notes, and snippets.

Jaden Geller JadenGeller

Block or report user

Report or block JadenGeller

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@JadenGeller
JadenGeller / Bag.swift
Created Mar 11, 2020
Add numbers in a bag that only exposes flatMap and count
View Bag.swift
public struct Bag<Member> {
private var members: [Member] = []
public var count: Int {
members.count
}
public func flatMap<NewMember>(_ transform: (Member) throws -> Bag<NewMember>) rethrows -> Bag<NewMember> {
Bag<NewMember>(members: try members.flatMap({ member in try transform(member).members }))
@JadenGeller
JadenGeller / Combine-Denormalization-Example.swift
Created Mar 7, 2020
Denormalizing automatically using Combine
View Combine-Denormalization-Example.swift
import Combine
extension Publishers {
typealias Pair<Upstream> = Publishers.Map<Publishers.Scan<Upstream, (Upstream.Output?, Upstream.Output?)>, (Upstream.Output?, Upstream.Output)> where Upstream: Publisher
}
extension Publisher {
func pair() -> Publishers.Pair<Self> {
scan((nil as Output?, nil as Output?), { previousPair, value in
(previousPair.1, value)
}).map({ pair in
@JadenGeller
JadenGeller / Combine-Pair-Sketchy.swift
Last active Mar 7, 2020
Pair Publisher in Combine
View Combine-Pair-Sketchy.swift
import Combine
extension Publisher {
func pair() -> Publishers.Pair<Self> {
Publishers.Pair(upstream: self)
}
}
extension Publishers {
class Pair<Upstream: Publisher>: Publisher {
@JadenGeller
JadenGeller / Set.hs
Last active Oct 30, 2019
Correct-by-composition set operations that cannot branch on ordering
View Set.hs
{-# LANGUAGE FlexibleInstances, UndecidableInstances, LambdaCase, DeriveFunctor, MonadComprehensions #-}
import qualified Prelude
import Prelude (Show, Num, Functor, Bool(..), not, Int, Maybe(..), Either(..), Eq, (==), (.), ($), const, otherwise)
import Control.Monad (MonadPlus, guard, when, unless, (>>=), fmap, mzero, mplus, return)
import qualified Data.Foldable
import Data.Foldable (Foldable)
import Data.Maybe (maybe, isJust)
import Data.Functor.Foldable (Fix)
@JadenGeller
JadenGeller / rotate.py
Last active Jun 14, 2018
Rotate in place
View rotate.py
def transpose_matrix(size, matrix):
for y in range(size):
for x in range(y):
matrix[y][x], matrix[x][y] = matrix[x][y], matrix[y][x]
def flip_y_matrix(size, matrix):
for y in range(size // 2):
flip_y = size - y - 1
for x in range(size):
matrix[x][y], matrix[x][flip_y] = matrix[x][flip_y], matrix[x][y]
@JadenGeller
JadenGeller / Constructive.idr
Created Apr 27, 2018
Are constructive programs better?
View Constructive.idr
import Data.List
import Data.List.Views
import Data.Vect
import Data.Vect.Views
%default total
-- Constructive
rotate : Vect len elem -> Vect len elem
@JadenGeller
JadenGeller / checking.py
Created Apr 2, 2018
Dynamic Type Checking
View checking.py
#!/usr/bin/python
import inspect
def typeAsserting(f):
signature = inspect.signature(f)
def wrapper(*args, **kwargs):
bound_args = signature.bind(*args, **kwargs)
for name, parameter_value in bound_args.arguments.items():
expected_type = signature.parameters[name].annotation
@JadenGeller
JadenGeller / join_quoted_strs.py
Created Jan 24, 2018
A gross simple state machine
View join_quoted_strs.py
def join_quoted_strs(s):
r = ''
within_quotes = False
is_escaped = False
for c in s:
if within_quotes:
if is_escaped:
r += c
is_escaped = False
else:
View TypeDictionary.swift
private struct TypeDescriptor: Hashable, CustomStringConvertible {
private let type: Any.Type
internal init(_ type: Any.Type) {
self.type = type
}
public var hashValue: Int {
return String(describing: type).hashValue
}
public static func ==(lhs: TypeDescriptor, rhs: TypeDescriptor) -> Bool {
View Advent1.swift
public struct RotatingIterator<Base: IteratorProtocol>: Sequence, IteratorProtocol {
private var skippedElements: [Base.Element] = []
private var base: Base
internal init(_ base: Base, count: Int) {
self.base = base
for _ in 0..<count {
guard let element = self.base.next() else { break }
self.skippedElements.append(element)
}
You can’t perform that action at this time.