Skip to content

Instantly share code, notes, and snippets.

@marcoconti83
marcoconti83 / Guard-catch.swift
Created October 29, 2015 23:46
Guard-catch in swift?
/**
# QUESTION
Is there a way to combine `guard` and `try-catch`, so that we can
exploit the `guard let` to have a immutable value, but have an early
return in case of errors?
*/
@marcoconti83
marcoconti83 / Collections+Clusters.swift
Created October 28, 2015 08:25
A function to group a collection into clusters of equal size
enum CollectionClusteringError : ErrorType {
case SizeIsNotMultipleOfClusterSize
}
extension CollectionType where Index.Distance == Int, Index == Int {
/**
Returns an array of elements from the collection grouped in clusters of the given size,
grouped according to the original order in the collection
public func p004()->Int {
// 999 * 993 = 992007 sum: 1992
// 998 * 994 = 992012 sum: 1992
// 997 * 995 = 992015 sum: 1992
// 996 * 996 = 992016 sum: 1992
// 999 * 994 = 993006 sum: 1993
// 998 * 995 = 993010 sum: 1993
// 997 * 996 = 993012 sum: 1993
// 999 * 995 = 994005 sum: 1994