Instantly share code, notes, and snippets.

View AppStoreImagesGenerator.sh
#!/bin/bash
set -e
SRC_FILE="$1"
DST_PATH="$2"
VERSION=1.0.0
info() {
View Scanl1.swift
// same as scanl but without initial
extension Array {
func scanl(combine: (Element, Element) -> Element) -> [Element] {
guard let first = self.first else { return [] }
return Array(self.dropFirst()).scanl(initial:first, combine:combine)
}
}
View ArrayToOccurrencesDictionary.swift
extension Array where Element: Hashable {
var occurrencesCount :[Element : Int] {
var map = [Element : Int]()
forEach { element in
if let val = map[element]{
map[element] = val+1
} else {
map[element] = 1
}
View Partition.swift
extension Array {
func partition(predicate: (Element) -> Bool) -> ([Element],[Element]) {
var res = (satisfy:[Element](),notSatisfy:[Element]())
self.forEach { element in
if predicate(element){
res.satisfy.append(element)
} else {
res.notSatisfy.append(element)
}
View Scanl.swift
extension Array {
func scanl<T>(initial: T, combine:@noescape (Element, T) -> T) -> [T] {
guard let first = self.first else { return [initial] }
return [initial] + Array(self.dropFirst()).scanl(initial: combine(first, initial), combine: combine)
}
}
View LensType.swift
struct Lens<A,B> {
let get: (A) -> B
let set: (B,A) -> A
}
extension Lens {
func compose<C>(other: Lens<B, C>) -> Lens<A, C> {
return Lens<A, C>(
get: { whole in
let part = self.get(whole)
View JsonFromFile.swift
func JSONFromFile(file: String) -> AnyObject? {
return NSBundle(forClass: self.dynamicType).pathForResource(file, ofType: "json")
.flatMap { NSData(contentsOfFile: $0) }
.flatMap(JSONObjectWithData)
}
private func JSONObjectWithData(data: NSData) -> AnyObject? {
return try? NSJSONSerialization.JSONObjectWithData(data, options: [])
}
View NSURL+Query.swift
extension NSURL {
func getQueryItemValueForKey(key: String) -> String? {
guard let components = NSURLComponents(URL: self, resolvingAgainstBaseURL: false) else {
return nil
}
guard let queryItems = components.queryItems else { return nil }
return queryItems.filter {
$0.name == key
}.first?.value
View UIColor+Hex.swift
extension UIColor {
convenience init?(hexString userHexString: String, alpha: CGFloat) {
var hexString:String = userHexString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
if (hexString.hasPrefix("#")) {
hexString = hexString.substringFromIndex(hexString.startIndex.advancedBy(1))
}
let hexNum = CGFloat(strtoul(hexString, nil, 16))
View toDictionary.swift
extension CollectionType {
@warn_unused_result
func toDictionary<K, V>
(@noescape transform:(element: Self.Generator.Element) -> (key: K, value: V)?) -> [K : V] {
return self.reduce([:]) { (var dictionary, e) in
if let (key, value) = transform(element: e){
dictionary[key] = value
}
return dictionary
}