Skip to content

Instantly share code, notes, and snippets.

Jake Bromberg jakebromberg

Block or report user

Report or block jakebromberg

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
View Threadsafe.swift
import Foundation
final class Threadsafe<Value> {
init(_ value: Value) {
self._value = value
}
var value: Value {
return queue.sync { return _value }
}
@jakebromberg
jakebromberg / AlmostIncreasingSequence.swift
Last active Jan 9, 2019 — forked from jakehawken/AlmostIncreasingSequence.swift
For the "almostIncreasingSequence" problem on CodeFights:
View AlmostIncreasingSequence.swift
extension Sequence where Element: Comparable {
func isAlmostIncreasingSequence() -> Bool {
var foundMisplacedElement = false
for (a, b) in zip(self, self.dropFirst()) where a >= b {
guard !foundMisplacedElement else { return false }
foundMisplacedElement = true
}
@jakebromberg
jakebromberg / ThreadSafe.h
Last active Aug 24, 2018
Inspired by Chris Lattner's concurrency manifesto, this defines a generic thread-safe object in Objective-C, similar to how Chris describes the behavior of actors.
View ThreadSafe.h
@import Foundation;
@interface ThreadSafe<Value>: NSProxy
- (instancetype)initWithValue:(Value)value;
@property (nonatomic, strong, readonly) Value value;
@end
@jakebromberg
jakebromberg / DisableNaglesAlgorithm.m
Last active Jun 12, 2018
Disables Nagle's Algorithm on a TCP socket
View DisableNaglesAlgorithm.m
// Nagle's algorithm is a means of improving the efficiency of TCP/IP networks by reducing the number of packets that need to
// be sent over the network.
// https://en.wikipedia.org/wiki/Nagle%27s_algorithm
#import <netinet/in.h>
#import <netinet/tcp.h>
@implementation NSStream (DisableNaglesAlgorithm)
- (void)disableNaglesAlgorithmWithError:(NSError **)error {
View Observable.swift
final class WeakBox<A: AnyObject> {
weak var unbox: A?
init(_ value: A) {
unbox = value
}
}
typealias Callback<T> = (T) -> ()
final class Observation<T> {
View ExecuteOnce.swift
final class ExecuteOnce {
typealias Work = () -> ()
private var work: Work?
init(_ work: @escaping Work) {
self.work = work
}
func execute() {
work?()
@jakebromberg
jakebromberg / SingleThreadedOperationQueue.swift
Last active May 2, 2018
Operation and dispatch queues on their own don't guarantee any single thread of execution. Here's one way to fix that.
View SingleThreadedOperationQueue.swift
import Foundation
@objc public final class SingleThreadedOperationQueue: Thread {
public typealias Operation = () -> ()
public func addOperation(_ operation: @escaping Operation) {
enqueueLock.lock()
defer {
enqueueLock.unlock()
}
@jakebromberg
jakebromberg / Array+EachCons.swift
Created Feb 17, 2018 — forked from khanlou/Array+EachCons.swift
each_cons from Ruby in Swift as a function on Sequence
View Array+EachCons.swift
extension Collection {
func eachConsecutive(_ size: Int) -> Array<SubSequence> {
let droppedIndices = indices.dropFirst(size - 1)
return zip(indices, droppedIndices)
.map { return self[$0...$1] }
}
}
View NearestKeyDictionary.swift
struct NearestKeyDictionary<Key: Comparable & Hashable, Value: Equatable>: ExpressibleByDictionaryLiteral {
// MARK: Type aliases
typealias Values = Dictionary<Key, Value>.Values
// MARK: Public properties
var allValues: Values {
return dictionary.values
}
View gist:313cd471147c740437ad9e8d65f68c8c
// One note before we start: if the inhabitants of Value are a closed set,
// then making Value an enum is going to be a clearer model than using a
// protocol like this. I'm assuming you need to be able to retroactively add
// new members of Value.
// Can't use Equatable directly because of its Self requirement.
protocol HeteroEquatable {
func isEqualTo(_ value: HeteroEquatable) -> Bool
}
You can’t perform that action at this time.