Skip to content

Instantly share code, notes, and snippets.

View Francescu's full-sized avatar

Francescu Santoni Francescu

View GitHub Profile
inline fun <T>tryOrNull(action: () -> T): T? {
var ret: T?
try {
ret = action()
} catch (e: Exception) {
ret = null
// TODO: log error
}
return ret
}
import Foundation
infix operator ..
func ..<T:AnyObject>(lhs: T, rhs:(T) -> Void) -> T {
rhs(lhs)
return lhs
}
import UIKit
class Foo {
var bar: Int
init(bar: Int) {
self.bar = bar
}
}
1> import Foundation
2> let test = String(contentsOfFile: "/test.txt")
fatal error: init(contentsOfFile:usedEncoding:) is not yet implemented: file Foundation/NSString.swift, line 1304
test: String = {
_core = {
_baseAddress = <extracting data from value failed>
_countAndFlags = <extracting data from value failed>
_owner = <extracting data from value failed>
String(contentsOfFile: "/test.txt")
/* OUTPUTS:
main.swift:2:17: note: overloads for 'String' exist with these partially matching parameter lists: (_StringCore), (Character), (String.CharacterView), (_builtinUnicodeScalarLiteral: Int32), (unicodeScalarLiteral: String), (extendedGraphemeClusterLiteral: String), (stringLiteral: String), (_storage: _StringBuffer), (S), (stringInterpolation: String...), (stringInterpolationSegment: T), (stringInterpolationSegment: String), (stringInterpolationSegment: Character), (stringInterpolationSegment: UnicodeScalar), (stringInterpolationSegment: Bool), (stringInterpolationSegment: Float32), (stringInterpolationSegment: Float64), (stringInterpolationSegment: UInt8), (stringInterpolationSegment: Int8), (stringInterpolationSegment: UInt16), (stringInterpolationSegment: Int16), (stringInterpolationSegment: UInt32), (stringInterpolationSegment: Int32), (stringInterpolationSegment: UInt64), (stringInterpolationSegment: Int64), (stringInterpolationSegment: UInt), (stringInterpola
@Francescu
Francescu / Hook.swift
Last active December 23, 2015 14:09
Segmentation fault 11
protocol Hook {
var matchingPaths: [Int : String] { get }
var matchingVars: [Int : String] { get }
func match(pathComponents: [String]) -> String?
}
extension Hook {
func match(pathComponents: [String], queryString: String) -> String? {
@Francescu
Francescu / gist:5003a61570cd9d2f5cc7
Created March 15, 2015 15:36
Simple Circle Mask over Image in Canvas
scratchCanvas = document.createElement('canvas')
scratchCanvas.width = size
scratchCanvas.height = size
scratchCtx = scratchCanvas.getContext('2d')
scratchCtx.clearRect(0, 0, scratchCanvas.width, scratchCanvas.height)
scratchCtx.globalCompositeOperation = 'source-over'
scratchCtx.drawImage(image, 0, 0, size, size)
@Francescu
Francescu / gist:2dfe97f9bc2f4470c2ba
Created March 9, 2015 15:09
URL Query String from Params in CoffeeScript
queryString = (params) ->
_.map params, (value, key) ->
encodeURIComponent(key) + "=" + encodeURIComponent(value)
.join("&").replace(/%20/g, "+")
extension Array {
func mapSome<R>(transform: T -> R?) -> [R] {
return self.reduce([R]()) {
if let unwrappedValue = transform($1) {
return $0 + [unwrappedValue]
}
return $0
}
}
}
@Francescu
Francescu / gist:97706ce4e22d8d9078eb
Last active August 29, 2015 14:12
Self in property initial value
class A {
let b = "TEST"
var c = self.b //no error
}
class D {
let e = "TEST"
let f = self.e //error
}