Skip to content

Instantly share code, notes, and snippets.

View noahsark769's full-sized avatar

Noah Gilmore noahsark769

View GitHub Profile
protocol KeyPathUpdatable {}
extension KeyPathUpdatable {
func updating<LeafType>(_ keyPath: WritableKeyPath<Self, LeafType>, to value: LeafType) -> Self {
var copy = self
copy[keyPath: keyPath] = value
return copy
}
}
protocol KeyPathUpdatable {}
extension KeyPathUpdatable {
func updating<LeafType>(_ keyPath: WritableKeyPath<Self, LeafType>, to value: LeafType) -> Self {
var copy = self
copy[keyPath: keyPath] = value
return copy
}
}
@noahsark769
noahsark769 / test.swift
Created November 29, 2019 18:51
CombineLatestCollection crash
import XCTest
import Combine
extension Collection where Element: Publisher {
/// Combine the array of publishers to give a single publisher of an array
/// of their outputs.
public func combineLatest() -> CombineLatestCollection<Self> {
return CombineLatestCollection(self)
}
@noahsark769
noahsark769 / README.md
Created March 29, 2019 18:58
Local PSQL configuration for Django projects

I keep forgetting how to do initial local setup of postgres for django projects, so here are the steps I generally follow:

  1. Install postgres locally (for mac I like postgres.app). If you get a return value from which psql, you should be good to go
  2. Set up your local database:
$ psql
postgres=# CREATE USER myproject_user WITH PASSWORD 'myproject_user';
CREATE ROLE
postgres=# CREATE DATABASE myproject_main;
@noahsark769
noahsark769 / UIImageView+ColorWheel.swift
Created February 27, 2019 16:47
Get the point on a color wheel image for a given color
extension UIImageView {
func pointOnColorWheel(for color: UIColor) -> CGPoint? {
guard let image = self.image else { return nil }
var hue: CGFloat = 0
var saturation: CGFloat = 0
var brightness: CGFloat = 0
color.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: nil)
let width = self.frame.size.width
@noahsark769
noahsark769 / UIColor+Hex.swift
Created February 6, 2019 17:48
Instantiate a UIColor from a hex string with 6 or 8 chars
extension UIColor {
// Adapted from https://gist.github.com/yannickl/16f0ed38f0698d9a8ae7, modified to accept alpha
// values. Works with strings with or without "#", 6 char strings like "a735b5", 8 char strings
// with alpha value like "836be64"
convenience init(hexString: String) {
let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
let scanner = Scanner(string: hexString)
if (hexString.hasPrefix("#")) {
scanner.scanLocation = 1
@noahsark769
noahsark769 / EitherView.swift
Last active February 3, 2019 03:46
UIView subclass which displays one of a given set of views at a time
final class EitherView: UIView {
private let views: [UIView]
init(views: [UIView]) {
self.views = views
super.init(frame: .zero)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
@noahsark769
noahsark769 / kth_smallest.py
Created December 18, 2018 04:55
kth smallest
def kthSmallest(root, k):
"""
:type root: TreeNode
:type k: int
:rtype: Optional[int]
"""
def recurse(node, k):
if not node:
return None, k
@noahsark769
noahsark769 / apns.py
Created December 11, 2018 19:40
APNSService for calling APNS
"""Apple Push Notification Service Library.
Documentation is available on the iOS Developer Library:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
This is from django_push_notifications, but I cleaned it up and edited it to
work with custom settings.
"""
import json
@noahsark769
noahsark769 / .pre-commit-config.yaml
Created March 12, 2018 15:51
Python pre-commit setup
- repo: git://github.com/pre-commit/pre-commit-hooks
sha: v1.2.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-case-conflict
- id: check-docstring-first
- id: check-merge-conflict
- id: check-symlinks
- id: double-quote-string-fixer