Skip to content

Instantly share code, notes, and snippets.

View rosslebeau's full-sized avatar

Ross LeBeau rosslebeau

View GitHub Profile
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 / ramanujan.swift
Created August 13, 2016 18:28
Generate all Ramanujan numbers: (a^3) + (b^3) = (c^3) + (d^3), where 0 < a, b, c, d < n
// 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)]]()
@rosslebeau
rosslebeau / UnsafeInstanceMethods.swift
Last active August 28, 2016 21:08
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
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 / copy-on-write-test.swift
Created October 27, 2016 06:44
A test of a grouping function showing how copy-on-write can slow your code down if you aren't vigilant.
import Foundation
class SharedArray<T> {
var storage: [T] = []
func append(_ value: T) {
storage.append(value)
}
}
public extension Sequence {