Skip to content

Instantly share code, notes, and snippets.

@sketchytech
sketchytech / PercentageCircle.html
Created August 23, 2016 11:36
Percentage Circle in HTML Canvas
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
// Types work but are not optimal - no error handling
// Demo in response to https://gist.github.com/sketchytech/029c00a74a4217a89797
extension CollectionType {
func reorder<T>(sorter: [(index: Self.Index, element: T)]) -> Array<Self.Generator.Element> {
return sorter.map({self[$0.index]})
}
}
func sortColumns<T0, T1>(_ col0: Array<T0>, _ col1: Array<T1>, _ cmp: (T0,T0)->Bool) -> (Array<T0>, Array<T1>) {
//
// SimpleScrollingStack.swift
// A super-simple demo of a scrolling UIStackView in iOS 9
//
// Created by Paul Hudson on 10/06/2015.
// Learn Swift at www.hackingwithswift.com
// @twostraws
//
import UIKit
@natecook1000
natecook1000 / openRanges.swift
Last active May 27, 2021 19:37
Open-ended range operators for Swift
// Open-ended range operators
//
// 100... is equivalent to 100...Int.max
// ...-100 is equivalent to Int.min...-100
// ..<3 is equivalent to Int.min..<3
import Swift
/// Conforming types provide static `max` and `min` constants.
protocol MinMaxType {
@alskipp
alskipp / uniq.swift
Last active August 29, 2015 14:06
Generic uniq function in Swift
// uniq func for Swift 1.2
func uniq<C: ExtensibleCollectionType where C.Generator.Element: Hashable>(collection: C) -> C {
var seen:Set<C.Generator.Element> = Set()
return reduce(collection, C()) { result, item in
if seen.contains(item) {
return result
}
@rnapier
rnapier / gist:067156ac404cc42f17b6
Created September 7, 2014 18:20
Version 2 of Flattenin' Your Mappenin'
//
// Version 2 of pagesFromData from Flattenin' Your Mappenin'
// http://robnapier.net/flatmap
//
import Foundation
infix operator >>== {}
func >>== <T,U>(x: T, f:T -> Result<U>) -> Result<U> {
return x.flatMap(f)
@natecook1000
natecook1000 / Set.swift
Last active July 13, 2024 21:09
Creating a Set Type in Swift
// The MIT License (MIT)
//
// Copyright (c) 2014 Nate Cook
//
// 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:
@rnapier
rnapier / gist:0f5611d0bf89a9645713
Last active August 29, 2015 14:05
Version 3 of Functional Wish Fulfillment
//
// Version 3 of pagesFromData from Functional Wish Fulfillment
// http://robnapier.net/functional-wish-fulfillment
//
import Foundation
func pagesFromData(data: NSData) -> Result<[Page]> {
return continueWith(asJSON(data)) {
continueWith(asJSONArray($0)) {
@krzyzanowskim
krzyzanowskim / shift_bits_with_truncation
Last active October 9, 2019 20:17
Bit shifting with overflow protection using overflow operator "&"
//
// IntExtension.swift
// CryptoSwift
//
// Created by Marcin Krzyzanowski on 12/08/14.
// Copyright (C) 2014 Marcin Krzyżanowski <[email protected]>
// This software is provided 'as-is', without any express or implied warranty.
//
// In no event will the authors be held liable for any damages arising from the use of this software.
//
@airspeedswift
airspeedswift / RemoveDuplicates.swift
Last active January 22, 2021 22:34
Removing duplicates
// removes all but first occurrence from a sequence, returning an array.
// requires elements to be hashable, not just equatable, but the alternative
// of using contains is very inefficient
// alternatively, could require comparable, sort, and remove adjacent dupes
func uniq<S: SequenceType, E: Hashable where E==S.Generator.Element>(seq: S) -> [E] {
var seen: [S.Generator.Element:Int] = [:]
return filter(seq) { !seen.updateValue(1, forKey: $0).hasValue }
}
// TODO: a version that takes a custom comparator function, say for lexicographic deduping