Skip to content

Instantly share code, notes, and snippets.

View haldun's full-sized avatar

Haldun Bayhantopcu haldun

  • Berlin, Germany
View GitHub Profile
// Proving 2x2 = 4 without numbers
indirect enum set: Equatable {
case empty
case notEmpty(element: set, rest: set = .empty)
static func == (lhs: set, rhs: set) -> Bool {
switch (lhs, rhs) {
case (.empty, .empty): return true
case (_, .empty): return false
@haldun
haldun / MyTextLabel.swift
Created August 15, 2020 20:46 — forked from CrystDragon/MyTextLabel.swift
Most basic custom UITextInput conformance, without selection interaction, no UI elements but only pure texts.
import UIKit
class MyTextLabel: UIView {
var textLayer = CATextLayer()
var textStorage: String = "" {
didSet {
textLayer.string = textStorage
}
}
@haldun
haldun / random.swift
Created December 13, 2016 12:00
Random for collections
extension RandomAccessCollection {
func random() -> Iterator.Element? {
guard count > 0 else { return nil }
let offset = arc4random_uniform(numericCast(count))
let i = index(startIndex, offsetBy: numericCast(offset))
return self[i]
}
}
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"
import scala.collection._
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.{Builder, MapBuilder}
case class Word(value: String) extends AnyVal
class WordMap extends PrefixMap[Word]
class PrefixMap[T]
extends mutable.Map[String, T]
class PrefixMatcher {
private var set = new scala.collection.immutable.TreeSet[String]()
private def successor(s: String) = s.take(s.length - 1) + (s.charAt(s.length - 1) + 1).toChar
def add(s: String) = set += s
def findMatches(prefix: String): Set[String] =
if (prefix.isEmpty) set
else set.range(prefix, successor(prefix))
}
import UIKit
final class MasonaryFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
guard let attrs = super.layoutAttributesForItemAtIndexPath(indexPath) else { return nil }
if indexPath.item == 0 {
return attrs
}
@haldun
haldun / hash.swift
Created April 6, 2016 09:13
Generic hash value computation for swift objects
func hashValue<T: Hashable>(objects: T?...) -> Int {
var result = 31
for object in objects {
result = (result &* 17) &+ object.hashValue
}
return result
}
func hashValue<T: Hashable>(objects: T...) -> Int {
var result = 31
return jsonBuilder()
.startObject()
.startObject(TYPE_NAME)
.startObject("properties")
.startObject("id")
.field("type", "string")
.field("index", "not_analyzed")
.endObject()
.startObject("title")
.field("type", "string")
//
// RootViewController.swift
// Filters
//
// Created by Haldun Bayhantopcu on 03/03/16.
// Copyright © 2016 Haldun Bayhantopcu. All rights reserved.
//
import UIKit