Skip to content

Instantly share code, notes, and snippets.

/**
* A Javascript object to encode and/or decode html characters using HTML or Numeric entities that handles double or partial encoding
* Author: R Reid
* source: http://www.strictly-software.com/htmlencode
* Licences: GPL, The MIT License (MIT)
* Copyright: (c) 2011 Robert Reid - Strictly-Software.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABI
extension CollectionType {
/// Return a lazy SequenceType containing pairs *(i, x)*,
/// where *i*s are the sequential indices and *x*s are the elements of `base`.
func enumerateWithIndex() -> AnySequence<(Index, Generator.Element)> {
var index = startIndex
return AnySequence {
return anyGenerator {
guard index != self.endIndex else { return nil }
return (index, self[index++])
}
protocol ContextError : ErrorType {
mutating func addContext<T>(type: T.Type)
}
protocol Contextualizable {}
extension Contextualizable {
func addContext(var error: ContextError) -> ContextError {
error.addContext(self.dynamicType)
return error
}
public struct Error: ErrorType {
let source: String; let reason: String
public init(_ source: String = __FILE__, _ reason: String) {
self.reason = reason; self.source = source
}
}
protocol Contextualizable {}
extension Contextualizable {
func functionContext(function : String = __FUNCTION__) -> String {
@zyg-github
zyg-github / error.swift
Last active December 7, 2015 02:50 — forked from erica/error.swift
//http://swift.gg/2015/11/27/implementing-printing-versions-of-try-and-try-on-steroids-in-swiftlang/
import Foundation
// Generic Error
public struct Error: ErrorType {let reason: String}
/**
Printing version of try? Call either with standard or autoclosure approach
```
let contents = attempt{try NSFileManager.defaultManager().contentsOfDirectoryAtPath(fakePath)}