Skip to content

Instantly share code, notes, and snippets.

View regnerjr's full-sized avatar

John Regner regnerjr

View GitHub Profile
@regnerjr
regnerjr / Microfiche.Swift
Created March 15, 2015 14:41
A set of functions for Archiving Swift Collections.
import Swift
import Foundation
// convert input Collection<T> into an NSMutableArray<NSData>
// works with Array<T>, Set<T>, Dictionary<T,U>
func convertCollectionToArrayOfData<T: CollectionType>(collection: T) -> NSMutableArray {
return NSMutableArray(array: map(collection){
var mutableItem = $0
return NSData(bytes: &mutableItem, length: sizeof(T.Generator.Element.self))
})
@regnerjr
regnerjr / roundClock.swift
Last active August 29, 2015 14:17
A playground for calculating hours and minutes into fractional hours i.e. 4 hours and 12 minutes should be 4.2 hours.
/// A function for calculating the time in 0.1 hour increments
/// i.e. 1 hour and 6 min is 1.1hours, 3 hours 12 minutes is 3.2 hours
func calc(hours: Int, min: Int) -> Double {
let dbl = Double(min)
let roundedMin = (dbl / 60.0)
let flr = floor(roundedMin * 10 ) / 10
return Double(hours) + floor(roundedMin * 10) / 10
}
/// How to round a number?
//Turns out in swift Bool is a struct!
/// From the Docs. `import Swift`
/// A value type whose instances are either `true` or `false`.
struct Bool {
/// Default-initialize Boolean value to `false`.
init()
}
@regnerjr
regnerjr / uniq.swift
Created March 27, 2015 00:52
A simple rewriting of the uniq command line tool.
#!/usr/bin/env xcrun swift -F $(xcode-select)/Platforms/MacOSX.platform/Developer/Library/Frameworks
import Foundation
//need to process any arguements here
//
// uniq [-c | -d | -u] [-i] [-f num] [-s chars] [input_file [output_file]]
// MARK: Standard In , Standard Out
func getStandardInput() -> String? {
@regnerjr
regnerjr / getDateFor.swift
Last active August 29, 2015 14:17
A Function for returning the next NSDate which matches given Day, Hour, Minute, Useful for getting an NSDate to represent Sunday at 4:00 am. This works no matter what the date is now.
import UIKit
import Swift
import XCPlayground
// we want to schedule a timer to fire on Sunday at 4:00am.
// NSTimer takes a fire date.
// NSTimer.init(fireDate date: NSDate, interval seconds: NSTimeInterval, target target: AnyObject, selector aSelector: Selector, userInfo userInfo: AnyObject?, repeats repeats: Bool)
//
// So how do we get the date for the next coming Sunday at 4:00am ?
// Date Components of course!
@regnerjr
regnerjr / uniqueChars.c
Created May 8, 2015 17:57
Given a string, returns only the unique characters. i.e. abacb -> abc
#include <stdio.h>
#include <stdlib.h>
int in(char value, char* list){
for (char *i = list; *i != '\0'; i++){
if ( *i == value ){
return 1;
}
}
return 0;
@regnerjr
regnerjr / intToBinary.swift
Created June 2, 2015 16:06
A couple functions for doing Integer to Binary Conversion in swift.
import UIKit
func intToBin(num: Int, padding: Bool = true ) -> String {
var binString = String(num, radix:2)
if padding {
binString = padStringTo4DigitChunks(binString)
}
return binString
}
@regnerjr
regnerjr / BoolExtensions.swift
Created June 28, 2015 22:48
Method chaining and reducing if statements
extension BooleanLiteralType {
func if_true(block: () -> () ) -> BooleanLiteralType{
if self {
block()
}
return self
}
func if_false(block: () -> ()) -> BooleanLiteralType {
if !self {
block()
xcode_plugin_update_uuid () {
local xcode plugin
for xcode in /Applications/Xcode*.app
do
uuid=$(defaults read "$xcode/Contents/Info" DVTPlugInCompatibilityUUID)
echo "$xcode - $uuid"
for plugin in ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/*.xcplugin
do
echo "Updating $plugin"
defaults write "$plugin/Contents/Info" DVTPlugInCompatibilityUUIDs -array-add $uuid

for simplifications

Q: How can we simplify this code?

let items = ["one", "two", "three", "four"]
for (_, thing) in items.enumerate() {
    print(thing.characters.count)
}