Skip to content

Instantly share code, notes, and snippets.

@superlopuh
superlopuh / ShapesView.swift
Last active March 21, 2018 21:35
Sugar for populating a UIView with a bunch of CAShapeLayers from models.
extension CAShapeLayer {
struct Model {
let path: UIBezierPath
let strokeColor: UIColor?
let fillColor: UIColor
let lineWidth: CGFloat

Keybase proof

I hereby claim:

  • I am superlopuh on github.
  • I am superlopuh (https://keybase.io/superlopuh) on keybase.
  • I have a public key ASDG9sV7x-2PNGdsF17xpvgC82Y6bwruku27vlEgnJEG8go

To claim this, I am signing this object:

@superlopuh
superlopuh / BitSet.swift
Created February 4, 2018 12:16
Implementation of a flexible size bit set in Swift.
extension BidirectionalCollection {
public func last(where predicate: (Element) throws -> Bool) rethrows -> Element? {
for element in self.reversed() where try predicate(element) {
return element
}
return nil
}
}
@superlopuh
superlopuh / Random.swift
Created September 15, 2017 09:07
Random Number Generation in Swift
import Darwin
// MARK: Integers
extension UInt32 {
/// Returns a random number in `0...UInt32.max`
static func random() -> UInt32 {
return arc4random()
}
@superlopuh
superlopuh / animations.py
Created March 31, 2017 09:55
Animation Curves Python
# MIT License
#
# Copyright (c) 2017 Snips
#
# 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:
@superlopuh
superlopuh / ArrayPatternMatching.swift
Last active December 21, 2015 23:11
Example of matching for any value in array.
let myString: String? = nil
func ~=<T: Equatable>(values: [T?], value: T?) -> Bool {
return values.contains({$0 == value})
}
if [nil, ""] ~= myString {
...
}
@superlopuh
superlopuh / Smashable Protocol
Created June 22, 2015 00:44
Written as an attempted solution to Brent Simmons's blog post.
// Written by Alexandre Lopoukhine
protocol Value: Equatable {}
protocol Smashable {
typealias SmashValue
func valueBySmashingOtherValue<T: Value>(value: T) -> SmashValue
}
struct Bar: Value {
import Cocoa
func smallestPrimeFactorOf(integer: Int, var withMinGuess minGuess: Int = 2) -> Int? {
let upperBound = Int(sqrt(Double(integer)))
while minGuess <= upperBound {
if integer % minGuess == 0 {
return minGuess
}
minGuess++
#!/usr/bin/env xcrun swift
import Cocoa
func smallestPrimeFactorOf(integer: Int, andMinGuess minGuess: Int = 2) -> Int? {
let upperBound = Int(sqrt(Double(integer)))
var attempt = minGuess
while attempt <= upperBound {
if integer % attempt == 0 {
#!/usr/bin/env xcrun swift
import Cocoa
func smallestPrimeFactorOf(integer: Int) -> Int? {
let upperBound = Int(sqrt(Double(integer)))
var attempt = 2
while attempt <= upperBound {
if integer % attempt == 0 {