Skip to content

Instantly share code, notes, and snippets.

View sforteln's full-sized avatar

Simon Fortelny sforteln

View GitHub Profile
@sforteln
sforteln / MemoizationWithGenerics
Created July 4, 2017 13:26
Simple Swift Function to memoize generic functions of the form T -> U
func memoize<T: Hashable,U>(function: @escaping (T) -> U) -> (T) -> U{
var cache : [T:U] = [:]
func memoWrapper(input: T) -> U {
if let cacheValue = cache[input] {
print("used cached result")
return cacheValue
}
let newVal = function(input)
cache[input] = newVal
print("new result")
@sforteln
sforteln / UIColorExtension.swift
Last active June 28, 2017 01:07
Shorten up UIColor creation
import UIKit
extension UIColor {
class func rgb(_ red: Int, _ green: Int, _ blue: Int) -> UIColor {
return UIColor.rgba(red,green,blue,1.0)
}
class func rgba(_ red: Int, _ green: Int, _ blue: Int, _ alpha: Float) -> UIColor {
return UIColor.init(colorLiteralRed: Float(red)/255.0, green: Float(green)/255.0, blue: Float(blue)/255.0, alpha:alpha)
}
}
@sforteln
sforteln / WatchKitAnimCompletion.swift
Created June 28, 2017 00:34
WatchKit animation with completion block
extension WKInterfaceController {
func animate(withDuration duration: TimeInterval, animations: @escaping () -> Swift.Void, completion: @escaping () -> Swift.Void) {
self.animate(withDuration: duration, animations: animations)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration, execute: completion)
}
}
@sforteln
sforteln / gist:e9fd7170a5126098af41
Last active August 29, 2015 14:21
Swift 1.2 : Confusing error message for closures that do not return value
import UIKit
import Foundation
//set up
extension Array {
func filterMap<U>(transform: (T) -> U?) -> [U] {
var mapped = [U]()
for item in self {
if let unwrapped = transform(item) {
mapped.append(unwrapped)
protocol ProtocolA {
init()
}
class classA : ProtocolA {
required init() {
}
}
@sforteln
sforteln / gist:158b72541d7240115fb3
Last active August 29, 2015 14:20
Swift generics and multiple protocols setup
//Given
protocol ProtocolA {
static var foo : String { get }
}
protocol ProtocolB {
static var bar : String { get }
}
//I want a function that will only accept types that impliment both protocols
class TypeA : ProtocolA, ProtocolB {
static var foo : String { get{ return "foo"} }
-(void) addFakeContacts{
ABAddressBookRef addressBook = ABAddressBookCreate();
// create 200 random contacts
for (int i = 201; i < 400; i++)
{
// create an ABRecordRef
ABRecordRef record = ABPersonCreate();
ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABStringPropertyType);
<?xml version="1.0" encoding="UTF-8"?\>
<!-- All runtime environments that use this package are required to supply--\>
<!-- real implementations of these interfaces at runtime--\>
<beans xmlns="http://www.springframework.org/schema/beans"\>
<bean id="datasource" class="test.mock.Datasource"
destroy-method="close" autowire-candidate="false"\>
</beans>
package test.spring;
import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
public class ApplicationContext {
static final String[] CONTEXT_FILES = new String[] {"datasource.xml","beans.xml"};
function searchManifests() {
for jarFile in \`ls $1/\*jar\`;
do echo $jarFile; unzip -p $jarFile META-INF/MANIFEST.MF |
perl -e '
while(){
if($\_ =\~ /$ARGV[0]:\\s([\^\\s]\*)\\s/){
print " " .$1 . "\\n"; exit;
}
}
print " no match \\n"; ' $2; done