Skip to content

Instantly share code, notes, and snippets.

View apparentsoft's full-sized avatar

Jacob Gorban apparentsoft

View GitHub Profile
add1 xs n c
| c < 0 = reverse $ add1 (reverse xs) n (abs c)
| c > 0 = add1' xs n c
| c == 0 = add1' xs n (length xs)
where
add1' [] _ _ = []
add1' xs n 0 = xs
add1' (x:xs) n c
| x == n = x+1:(add1' xs n (c-1))
| otherwise = x:add1' xs n c
@apparentsoft
apparentsoft / README.txt
Last active December 26, 2015 09:09
I think I found a stupid bug in NSURL when trying to read Mavericks Tags associated with the URL, along with other information for that URL resources.
The output for the code below is as following, on my Mac. So, the first time that I ask to read the Tags it won't return localized filename or the UTI. But it will on a next invocation of the same code. Duh!
2013-10-24 00:10:26.780 Untitled 3[39076:507] Attributes WITHOUT Tags: {
NSURLContentAccessDateKey = "2013-10-23 12:38:49 +0000";
NSURLContentModificationDateKey = "2013-10-23 10:33:50 +0000";
NSURLLocalizedNameKey = Applications;
NSURLTypeIdentifierKey = "public.folder";
}
2013-10-24 00:10:26.781 Untitled 3[39076:507] Attributes WITH Tags: {
@apparentsoft
apparentsoft / gist:95707a39f00f444ac9b8
Last active August 29, 2015 14:02
An operator similar to how I'd image ?: to work. Check option and provide a specified (right-hand-side) value if it's nil.
operator infix ||| {associativity left precedence 100}
func ||| <T>(lhs:T?, rhs: @auto_closure () -> T) -> T {
if let alhs = lhs {
return alhs
} else {
return rhs()
}
}
@apparentsoft
apparentsoft / gist:6ab276fe2befa7151fb6
Created November 19, 2014 15:27
IfLetTuples.swift
func noNils(optionals:[Any?]) -> Bool {
for item in optionals {
if item == nil {
return false
}
}
return true
}
func allSome<A,B>(a:A?, b:B?) -> (A,B)? {
func catOptionals<T,S where S:SequenceType, S.Generator.Element == Optional<T>>(optionals: S) -> [T] {
return reduce(optionals, []) { (acc, value: T?) -> [T] in
switch value {
case let .Some(v): return acc + [v]
default: return acc
}
}
}