Skip to content

Instantly share code, notes, and snippets.

View fewlinesofcode's full-sized avatar
🏠
Working from home

Oleksandr Glagoliev fewlinesofcode

🏠
Working from home
View GitHub Profile
@fewlinesofcode
fewlinesofcode / StructCompareUsingMirror.swift
Created April 2, 2020 17:56
Example of structure comparison in Swift
import Foundation
protocol KeyPathListable {
var allKeyPaths: [String: PartialKeyPath<Self>] { get }
var keyPathsList: [PartialKeyPath<Self>] { get }
}
extension KeyPathListable {
private subscript(checkedMirrorDescendant key: String) -> Any {
Mirror(reflecting: self).descendant(key)!
@fewlinesofcode
fewlinesofcode / PageIndicator.swift
Last active September 11, 2023 09:02
SwiftUI Page Indicator implementation
//
// PageIndicator.swift
//
// Created by Oleksandr Glagoliev on 6/24/20.
// Copyright © 2020 Oleksandr Glagoliev. All rights reserved.
//
import SwiftUI
// MARK: - Dot Indicator -
import XCTest
extension XCTestCase {
func trackForMemoryLeaks(_ instance: AnyObject, file: StaticString = #filePath, line: UInt = #line) {
addTeardownBlock { [weak instance] in
XCTAssertNil(instance, "Instance should have been deallocated. Potential memory leak.", file: file, line: line)
}
}
}
@fewlinesofcode
fewlinesofcode / AugmentedIntevalTree.swift
Last active February 15, 2023 14:18
Augmented Interval Search tree
//
// Created by @fewlinesofcode on 9/6/18.
// Copyright (c) 2018 Oleksandr Glagoliev. All rights reserved.
//
public class Interval<T: Comparable> {
private (set) var start: T
private (set) var end: T
var max: T
import UIKit
public extension CGPoint {
func distance(to point: CGPoint) -> CGFloat {
let xDist = x - point.x
let yDist = y - point.y
return (xDist * xDist + yDist * yDist).squareRoot()
}
}
@fewlinesofcode
fewlinesofcode / ClosuresCheatSheetPG.swift
Created October 22, 2018 07:53
Playground contents for "Closures cheatsheet"
import Foundation
/*
> Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages. Swift’s closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-free syntax in common scenarios.
For better understanding, I strongly recommend you to read the [Swift functions cheatsheet]({% post_url 2016-02-12-swift-functions-cheatsheet %}) and it's sources.
# Closure syntax
Since any function is the special case of closure, they are pretty the same. Basic difference is the way of writing and the use purpose. Closures syntax is optimized to be convenient for *inlining*, *passing as parameter* and *using as return type* of other functions.
The general form of the Swift closure is:
{ (parameters) -> ReturnType in
@fewlinesofcode
fewlinesofcode / CGVectorArithmetics.swift
Last active April 12, 2021 18:02
2d Vector Arithmetics Swift 5
//
//
// CGVectorArithmetics.swift
//
// Created by fewlinesofcode.com on 2/6/19.
// Copyright © 2019 fewlinesofcode.com All rights reserved.
//
import Foundation
import CoreGraphics
@fewlinesofcode
fewlinesofcode / Vector2D.swift
Last active April 12, 2021 17:57
Some useful operations on 2d vectors
//
// Vector2D.swift
//
// Created by fewlinesofcode.com on 2/6/19.
// Copyright © 2019 fewlinesofcode.com All rights reserved.
//
import Foundation
struct Vector2D {
/**
* The $1 Unistroke Recognizer
*
* Jacob O. Wobbrock, Ph.D.
* The Information School
* University of Washington
* Seattle, WA 98195-2840
* wobbrock@uw.edu
*
* Andrew D. Wilson, Ph.D.
@fewlinesofcode
fewlinesofcode / GLSL-Noise.md
Created October 11, 2020 17:12 — forked from patriciogonzalezvivo/GLSL-Noise.md
GLSL Noise Algorithms

Generic 1,2,3 Noise

float rand(float n){return fract(sin(n) * 43758.5453123);}

float noise(float p){
	float fl = floor(p);
  float fc = fract(p);
	return mix(rand(fl), rand(fl + 1.0), fc);
}