Skip to content

Instantly share code, notes, and snippets.

View VAndrJ's full-sized avatar

VAndrJ VAndrJ

  • Ukraine
  • 17:25 (UTC +03:00)
View GitHub Profile
@VAndrJ
VAndrJ / NumberAbbreviation.swift
Created February 12, 2021 09:11
Simple solution for numbers abbreviation to k m b t aa etc.
//
// NumberAbbreviation.swift
//
// Created by Volodymyr Andriienko.
//
import Foundation
public struct NumberAbbreviation {
// MARK: - Add more if needed, for example: (1_000_000_000, 1_000_000_000, "B"), etc.
class Option {
final int rawValue;
Option(this.rawValue);
Option.fromSet(Set<Option> set) : rawValue = set.rawValue;
Option.fromList(List<Option> set) : rawValue = set.rawValue;
Option operator |(Option option) {
@VAndrJ
VAndrJ / AsyncComposition.swift
Last active April 23, 2021 10:39
Simple drop-in example for macOS command line tool project.
import Foundation
infix operator ~>: MultiplicationPrecedence // Here we can create own group
// Composes two asynchronous functions
func ~> <T, U>(
_ first: @escaping (@escaping (Result<T, Error>) -> Void) -> Void,
_ second: @escaping (T, @escaping (Result<U, Error>) -> Void) -> Void) -> (@escaping (Result<U, Error>) -> Void
) -> Void {
return { result in
//
// ViewController.swift
// StackTest
//
// Created by Volodymyr Andriienko on 27.04.2021.
//
// Что здесь происходит:
// r - отправка на чтение (первый таймер).
// w - отправка на запись (второй таймер).
//
// Created by Volodymyr Andriienko on 18.05.2021.
// Copyright © 2021 VAndrJ. All rights reserved.
//
#if DEBUG
#if canImport(SwiftUI)
import SwiftUI
@available (iOS 13.0, *)
//
// ContentView.swift
// TestMapkit
//
// Created by Volodymyr Andriienko on 20.05.2021.
// Copyright © 2021 VAndrJ. All rights reserved.
//
import SwiftUI
import MapKit
@VAndrJ
VAndrJ / AppearanceImage.swift
Created October 3, 2021 08:01
Simple wrapper to support images programmatically for different userInterfaceStyle appearance.
#if canImport(UIKit)
import UIKit
@propertyWrapper
public struct AppearanceImage {
public var wrappedValue: UIImage { asset.image(with: .current) }
let asset: UIImageAsset
public init(light: UIImage, dark: UIImage) {
@VAndrJ
VAndrJ / Currying.swift
Created January 12, 2022 07:46
Function currying
/// Currying for function with 2 arguments.
/// - Returns: A curried function (A) -> (B) -> R
public func curry<A, B, R>(_ f: @escaping (A, B) -> R) -> (A) -> (B) -> R {
{ a in { b in f(a, b) } }
}
/// Currying for function with 3 arguments.
/// - Returns: A curried function (A) -> (B) -> (C) -> R
public func curry<A, B, C, R>(_ f: @escaping (A, B, C) -> R) -> (A) -> (B) -> (C) -> R {
{ a in { b in { c in f(a, b, c) } } }
@VAndrJ
VAndrJ / PartialApplication.swift
Created January 12, 2022 07:53
Partial function application
/// Partial application of a function with 2 arguments
/// - Returns: Partially applied function (A) -> R
public func partial<A, B, R>(_ f: @escaping (A, B) -> R, _ a: Deferred, _ b: B) -> (A) -> R {
{ a in f(a, b) }
}
/// Partial application of a function with 2 arguments
/// - Returns: Partially applied function (B) -> R
public func partial<A, B, R>(_ f: @escaping (A, B) -> R, _ a: A, _ b: Deferred) -> (B) -> R {
{ b in f(a, b) }
@VAndrJ
VAndrJ / FunctionComposition.swift
Created January 12, 2022 09:40
Function composition custom operators
infix operator |>: ForwardApplication
infix operator >>>: ForwardComposition
infix operator ~>: ForwardComposition
infix operator >=>: ForwardComposition
/// Function application
public func |> <A, R>(a: A, f: (A) -> R) -> R {