Skip to content

Instantly share code, notes, and snippets.

Ross LeBeau rosslebeau

Block or report user

Report or block rosslebeau

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
@rosslebeau
rosslebeau / copy-on-write-test.swift
Created Oct 27, 2016
A test of a grouping function showing how copy-on-write can slow your code down if you aren't vigilant.
View copy-on-write-test.swift
import Foundation
class SharedArray<T> {
var storage: [T] = []
func append(_ value: T) {
storage.append(value)
}
}
public extension Sequence {
@rosslebeau
rosslebeau / UnsafeInstanceMethods.swift
Last active Aug 28, 2016
Shows how Swift's instance methods actually all capture self, due to the underlying implementation. Read the whole explanation: http://rosslebeau.com/2016/sneaky-reference-cycles-swift-instance-methods
View UnsafeInstanceMethods.swift
func weakify<A: AnyObject, B>(obj: A, target: ((A) -> (B) -> Void)?) -> ((B) -> Void) {
return { [weak obj] value in
guard let obj = obj else { return }
target?(obj)(value)
}
}
class Actor {
var action: () -> Void
@rosslebeau
rosslebeau / ramanujan.swift
Created Aug 13, 2016
Generate all Ramanujan numbers: (a^3) + (b^3) = (c^3) + (d^3), where 0 < a, b, c, d < n
View ramanujan.swift
// Generate all Ramanujan numbers: (a^3) + (b^3) = (c^3) + (d^3)
// Where 0 < a, b, c, d < n
let n = 1000
let cubes = (0..<n).map({$0*$0*$0})
let cubeCount = cubes.count
var sumsOfPairs = [Int: (Int, Int)]()
var ramanujans = [Int: [(Int, Int)]]()
View fizzbuzz-functional-no-conditionals.rb
def fizzbuzz start, limit
(start..limit).to_a.map { |n|
printFizzBuzzForInput n
}
end
def printFizzBuzzForInput n
print "#{'fizz' * f(n)}"
print "#{'buzz' * g(n)}"
print "#{n.to_s * h(n)}"
@rosslebeau
rosslebeau / ruby-fizzbuzz-wat.rb
Last active Jun 12, 2016
An insane, one-line solution to FizzBuzz in Ruby, using no conditionals.
View ruby-fizzbuzz-wat.rb
def fizzbuzz(start, limit) loop do puts "#{'fizz' * (((start % 3 / (start % 3 + 1.0)).ceil - 1) * (-1))}#{'buzz' * (((start % 5 / (start % 5 + 1.0)).ceil - 1) * (-1))}#{start.to_s * ((start % 3 / (start % 3 + 1.0)).ceil * ((start % 5) / ((((start += ((limit + 1) - start)/((limit + 1) - start)) - 1) % 5) + 1.0)).ceil)}" end end
View swift-rewrite-redux.swift
extension CGSize {
func scaled(byFactor factor: CGFloat) -> CGSize {
return CGSize(width: width * factor,
height: height * factor)
}
}
extension CGSize {
func scaled(toSize: CGSize, mode: UIImage.ScaleMode) -> CGSize {
let (widthFactor, heightFactor) = (toSize.width / width, toSize.height / height)
View ios-build-script.rb
#!/usr/bin/env ruby
require 'fileutils'
require 'optparse'
require 'json'
IPA_EXPORT_DIR = 'WFIPAExport'
ARCHIVE_DIR = 'WFArchive'
XCARCHIVE_NAME = 'WF-iOS.xcarchive'
IPA_NAME = 'WF-iOS.ipa'
PLIST_NAME = 'WF-iOS.plist'
View scale-crop-image.swift
func scaleAndCropImage(_ image: UIImage, toSize newSize: CGSize) -> UIImage {
if image.size.equalTo(newSize) {
return image
}
// Find the ratios of the desired dimensions to the current dimensions,
// and use the larger one to scale the image to fill the desired size
let widthFactor = newSize.width / image.size.width
let heightFactor = newSize.height / image.size.height
let scaleFactor = max(widthFactor, heightFactor)
View CustomExportSizes.sketchplugin
// (command shift a)
if (selection.length() > 0){
for (var i=0; i < selection.length(); i++) {
addExportSizes(selection[i]);
}
[doc showMessage:"Export Options Added"];
} else {
var app = [NSApplication sharedApplication];
@rosslebeau
rosslebeau / gist:7e2265990fb3ed997a9c
Last active Aug 29, 2015
How to make a signed request URL for Google Maps for Work API
View gist:7e2265990fb3ed997a9c
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>
+ (NSString *)signedUrlFromUrl:(NSString *)fullURL {
fullURL = [fullURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *unmodifiedKey = [@"Your Google Maps for Work API Key" stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
unmodifiedKey = [unmodifiedKey stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
NSData *keyData = [[NSData alloc] initWithBase64EncodedData:[unmodifiedKey dataUsingEncoding:NSUTF8StringEncoding] options:0];
NSData *urlData = [fullURL dataUsingEncoding:NSUTF8StringEncoding];
You can’t perform that action at this time.