Skip to content

Instantly share code, notes, and snippets.

@plantpurecode
plantpurecode / itoa.c
Created February 14, 2021 15:42
A C implementation of stdlib's itoa function
//
// itoa.c
// itoa
//
// Created by Jacob Relkin on 14/02/2021.
//
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
self.val = val
self.left = left
self.right = right
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
@plantpurecode
plantpurecode / OptionalChecking.swift
Created February 23, 2020 07:45
A neat way to avoid nil checks with collections...
let array = ["a", "b", "c", "d"].map(Optional.init)
array.contains("a") // true
array.contains(Optional("a")) // true
array.contains("e") // false
array.contains(Optional("e")) // false
let otherArray = ["a"]
// `Array.first` is an Optional property, but we can use it as an argument to `Array.contains` since the array is actually of Optional<String> type...
array.contains(otherArray.first) // true
@plantpurecode
plantpurecode / gist:686908d873f9d210096b
Last active August 29, 2015 14:16
Threadsafe Mutable Dictionary
@interface JRMutableDictionary : NSMutableDictionary
@end
@implementation JRMutableDictionary {
dispatch_queue_t queue;
NSMutableDictionary *storage;
}
- (void)dealloc {
if(queue) {
@interface NSCountedSet (JRCountedSetAdditions)
- (NSArray *) objectsWithCount:(NSUInteger) count;
@end
typedef void (^JRIterationBlock)(NSUInteger index);
static inline void invokeBlockIterativelyUsingRange(JRIterationBlock block, NSRange rng) {
NSParameterAssert(block);
for(NSUInteger i = rng.location; i < NSMaxRange(rng); ++i) {
block(i);
}
}
@implementation NSNumber (BlockAdditions)
@plantpurecode
plantpurecode / NSLock+JRAdditions.h
Created September 20, 2012 22:54
Locking with blocks
@interface NSLock (JRAdditions)
- (void)lockWithBlock:(dispatch_block_t)block;
@end
@plantpurecode
plantpurecode / gist:3703384
Created September 12, 2012 00:57
Recursion, with blocks!
- (void)printRecursivelyUpTo:(NSUInteger)max {
__block dispatch_block_t block;
__block NSUInteger iterator = 0;
dispatch_block_t b = ^{
if(iterator == max) return;
NSLog(@"%u", iterator);
++iterator;
block();
};
@interface NSArray(JRAdditions)
- (id)jr_objectAtIndex:(long long)index {
NSUInteger realIndex = index;
if(index < 0) {
realIndex = [self count] - (+index);
}
return [self objectAtIndex:realIndex];
}