This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MyClass.swift | |
class MyClass { | |
func doSomething() { | |
refresh() | |
} | |
} | |
private func refresh() { | |
println("I'm the global refresh function.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FFT.swift | |
// Signal | |
// | |
// Created by Rob Napier on 7/24/14. | |
// Copyright (c) 2014 Rob Napier. All rights reserved. | |
// | |
import Accelerate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |