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 / Playground content
Created October 23, 2018 12:32
IteratorAndSequence.swift
//: Playground - noun: a place where people can play
import UIKit
/*:
## `Date` wrapping and convenience methods
*/
let calendar = Calendar.current
extension Int {
public var days: DateComponents {
@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 / CGSize+AspectCorrectResizing.swift
Created September 24, 2018 10:15
Aspect correct resizing
extension CGSize {
enum AspectMode {
case fit
case fill
}
enum Orientation {
case portrait
case landscape
}
@fewlinesofcode
fewlinesofcode / LoadFromNib.swift
Created July 16, 2018 05:29
Load UIView from NIB
class <#ClassName#>: UIView {
// Do not forget to wire to XIB
@IBOutlet var view: UIView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadFromNib()
}
init() {
import math
def to_deg(angle):
return angle * (180 / math.pi)
def to_rad(angle):
return angle * (math.pi / 180)
# Environment
airDensity = 1.2234 # kg/m^3
@fewlinesofcode
fewlinesofcode / locales.json
Created April 11, 2018 21:38
i18n locale identifiers in JSON format
[
{
"id": "af",
"description": "Afrikaans"
},
{
"id": "af-NA",
"description": "Afrikaans (Namibia)"
},
{
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1 /*Delay time from now*/) {
// Your needed function
}
@fewlinesofcode
fewlinesofcode / Bounded.swift
Last active January 25, 2017 12:41
Swift implementation of Haskell `Bounded` type
/// Sometimes we need to limit values
/// For instance, you are making application for the Tour De France, and
/// You have a variable depicting the Climb category (1..5)
/// Here is the place, where `Bounded` can be used
struct Bounded<T: Comparable> {
var minimum: T
var maximum: T
var value: T? {
willSet {