Skip to content

Instantly share code, notes, and snippets.

View Exey's full-sized avatar

Exey Panteleev Exey

View GitHub Profile
@inlinable func trampolineToMain(_ function: @escaping @autoclosure ()->()) -> Bool {
if Thread.isMainThread {
return false
} else {
DispatchQueue.main.async {
function()
}
return true
}
}
@Exey
Exey / count_ios_source_rows.sh
Created September 18, 2019 09:28
Count rows in iOS App sources
find . -type d \( -path ./Pods -o -path ./Vendor \) -prune -o \( -iname \*.m -o -iname \*.mm -o -iname \*.h -o -iname \*.swift \) -print0 | xargs -0 wc -l
@Exey
Exey / BenchSwiftGuardWithIf.swift
Last active September 23, 2019 12:51
Benchmark comparing guard vs. if let
import Foundation
var RUNS = 10
var COUNT = 10_000_000
// Extensions for prints
extension Sequence where Element: BinaryFloatingPoint {
func average() -> Element {
var i: Element = 0
@Exey
Exey / DebouncerPlayground.swift
Last active November 27, 2019 22:45
Debouncer with Argument
import Foundation
// Debouncer
func debounce(delay: TimeInterval, action: @escaping (String)->Void) -> (String)->Void {
let callback = DebounceCallback(action)
var timer: Timer?
return { arg in
if let timer = timer { timer.invalidate() } // invalidate the last timer
timer = Timer(timeInterval: delay,
target: callback,
@Exey
Exey / MostFrequentNumber.swift
Created February 7, 2020 19:36
Most Frequent Number Task
// Utils for bit operations
enum Bit {
case zero, one
func asInt() -> Int { self == .one ? 1 : 0}
}
// to bytes(UInt8)
func bitsToBytes(bits: [Bit]) -> [UInt8] {
let numBits = bits.count
let numBytes = (numBits + 7)/8
var bytes = [UInt8](repeating: 0, count: numBytes)
//
// UUID4.swift
//
// Created by exey on 17.03.2020.
// Copyright © 2020 Exey Panteleev. All rights reserved.
//
extension UInt8 {
func hexString(padded:Bool = true) -> String {
let dict:[Character] = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]
//
// Date.swift
//
// Created by Exey Panteleev on 29.08.2019.
// Copyright © 2019 Exey Panteleev. All rights reserved.
//
import Foundation
let ANY_VALID_DATE: String = #"(\b(0?[1-9]|[12]\d|30|31)[^\w\d\r\n:](0?[1-9]|1[0-2])[^\w\d\r\n:](\d{4}|\d{2})\b)|(\b(0?[1-9]|1[0-2])[^\w\d\r\n:](0?[1-9]|[12]\d|30|31)[^\w\d\r\n:](\d{4}|\d{2})\b)"#
import SwiftUI
private struct LazyView<Content: View>: View {
let build: () -> Content
init(_ build: @autoclosure @escaping () -> Content) {
self.build = build
}
var body: Content {
build()
extension UITabBar {
/// // TabBar Hide Mechanism
/// @Published var hideTabBar = false
/// var tabBarSubscriberCancellable: AnyCancellable?
open override func setNeedsDisplay() {
super.setNeedsDisplay()
// tricky subscribe
let coreApp: CoreAppService = ServiceLocator.shared.inject()
if coreApp.tabBarSubscriberCancellable == nil {
#include <metal_stdlib>
using namespace metal;
struct VertexIn {
float3 position [[attribute(0)]];
float3 normal [[attribute(1)]];
};
struct VertexOut {
float4 position [[position]];