Skip to content

Instantly share code, notes, and snippets.

View rnapier's full-sized avatar

Rob Napier rnapier

View GitHub Profile
@rnapier
rnapier / gist:b639da64d5890a0340d5
Last active October 26, 2015 20:50
Simple FFT example
import Accelerate
func spectrumForValues(signal: [Double]) -> [Double] {
// Find the largest power of two in our samples
let log2N = vDSP_Length(log2(Double(signal.count)))
let n = 1 << log2N
let fftLength = n / 2
// This is expensive; factor it out if you need to call this function a lot
let fftsetup = create_fftsetupD(log2N, FFTRadix(kFFTRadix2))
@rnapier
rnapier / gist:478465d1b15e95b98b42
Last active August 29, 2015 14:04
Spooky action at a distance when you don't use self
// Try this in a commandline app vs a playground. The behaviors are different.
class MyClass {
func doSomething() {
refresh() // I wonder which refresh we'll get?
}
}
// Not only does this change the behavior of doSomething()... (see below)
// extension MyClass {
@rnapier
rnapier / gist:4213dc64206b17df6935
Last active December 13, 2015 09:41
Even more shenanigans when self is not required
// MyClass.swift
class MyClass {
func doSomething() {
refresh()
}
}
private func refresh() {
println("I'm the global refresh function.")
func ==<A:Equatable, B:Equatable>(lhs:(A,B), rhs:(A,B)) -> Bool {
return lhs.0 == rhs.0
&& lhs.1 == rhs.1
}
func ==<A:Equatable, B:Equatable, C:Equatable>(lhs:(A,B,C), rhs:(A,B,C)) -> Bool {
return lhs.0 == rhs.0
&& lhs.1 == rhs.1
&& lhs.2 == rhs.2
}
@rnapier
rnapier / gist:399676bd389c270e7b66
Last active October 15, 2015 16:08
FFT in Swift (protecting against deallocation)
//
// FFT.swift
// Signal
//
// Created by Rob Napier on 7/24/14.
// Copyright (c) 2014 Rob Napier. All rights reserved.
//
import Accelerate
@rnapier
rnapier / gist:0d24ca74e4b4c5bfb3b4
Last active August 29, 2015 14:04
withExtendedLifetimes (arbitrary number of parameters)
// You call this as:
// withExtendedLifetimes([x,y,z]) { ... }
// I haven't been able to replace the array with variadic. The function
// will be absorbed into the list (even if it's curried).
func withExtendedLifetimes(args: [Any], f: () -> ()) {
var rest = args
let last:Any = rest.removeLast()
if rest.count > 0 {
Swift.withExtendedLifetime(last) { () -> () in withExtendedLifetimes(rest, f) }
}
@rnapier
rnapier / gist:e757aed05afc1811a6cc
Last active August 29, 2015 14:04
Tail-recursive reverse
func reverseR<T>(result: [T], curList: Slice<T>) -> [T] {
switch curList.first {
case .None: return result
case .Some(let cur): return reverseR([cur] + result, dropFirst(curList))
}
}
func reverseTailRecursive<T>(ls: [T]) -> [T] {
return reverseR([], Slice(ls))
}
@rnapier
rnapier / gist:8eda179689d9d61c2bfb
Last active August 29, 2015 14:04
And autoclosure saves(?) the day for generic recursive enums
// Creating a generic recursive data structure with autoclosure. (READ ALL NOTES; THIS MAY NOT DO WHAT YOU WANT.)
// Closures are reference types, so the size is known (? I think ?)
// Note that this is just because of unimplemented features in the compiler in Beta5
// There's no reason to think this is a long-term requirement.
// IMPORTANT: the closure will run every time you access this value, so if that has
// side effects, this won't work. It's only possible on pure value types.
// But the fact that this works as expected is actually kind of incredible.
// Think about what is required for it to work out the type for NestedList.Elem("first").
@rnapier
rnapier / gist:2c2bccc40b24fb9d54fc
Last active August 29, 2015 14:05
Version 2 of Functional Wish Fulfillment
//
// Version 2 of pagesFromData from Functional Wish Fulfillment
// http://robnapier.net/functional-wish-fulfillment
//
import Foundation
func pagesFromData(data: NSData) -> Result<[Page]> {
// 1. Parse the NSData into a JSON object
@rnapier
rnapier / gist:9e8f92a1ce6be5c3d295
Created August 18, 2014 18:28
Version 1 of Functional Wish Fulfillment
//
// Version 1 of pagesFromData from Functional Wish Fulfillment
// http://robnapier.net/functional-wish-fulfillment
//
import Foundation
func pagesFromData(data: NSData) -> PageListResult {
// 1. Parse the NSData into a JSON object