Skip to content

Instantly share code, notes, and snippets.

@glukianets
glukianets / SwiftUI.arm64e-apple-ios.swiftinterface.swift
Last active December 1, 2024 12:17
SwiftUI iOS18.1 module interface
This file has been truncated, but you can view the full file.
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 6.0 effective-5.10 (swiftlang-6.0.0.7.43 clang-1600.0.25.3)
// swift-module-flags: -target arm64e-apple-ios18.1 -enable-objc-interop -enable-library-evolution -swift-version 5 -enforce-exclusivity=checked -O -library-level api -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -enable-experimental-feature IsolatedAny2 -enable-upcoming-feature InferSendableFromCaptures -enable-bare-slash-regex -user-module-version 6.1.19.2.102 -module-name SwiftUI -package-name SwiftUI
import Combine
import CoreData
import CoreFoundation
@_exported import CoreGraphics
@_exported import CoreTransferable
@_exported import DeveloperToolsSupport
import Foundation
@glukianets
glukianets / 8Queens.swift
Created August 15, 2024 15:38
Naive 8Queens puzzle solution with bitmasks
func queen(atFile file: Int, rank: Int) -> UInt64 {
0b00000001_00000001_00000001_00000001_00000001_00000001_00000001_00000001 << (file) |
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_11111111 << (rank * 8) |
0b10000000_01000000_00100000_00010000_00001000_00000100_00000010_00000001 >> ((file - rank) * 8) |
0b00000001_00000010_00000100_00001000_00010000_00100000_01000000_10000000 >> ((7 - file - rank) * 8)
}
func occupiedIndices(in board: UInt64) -> some Sequence<(file: Int, rank: Int)> {
sequence(state: (board, 0)) { state in
let offset = state.0.trailingZeroBitCount
@glukianets
glukianets / JSONValue.swift
Last active March 23, 2025 18:38
Unstructured JSON Codable
public enum JSONValue: Codable & Hashable & Sendable & LosslessStringConvertible {
private struct ParseError: Error {}
static let decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.allowsJSON5 = true
return decoder
}()
static let encoder: JSONEncoder = {
@glukianets
glukianets / unescape-json.swift
Created March 18, 2024 14:50
A simple extension to unescape strings that were erroneusly double-escaped for JSON
extension String {
private static let excapesRegex: Regex = /\\(?<a>[bfnrtv0])|\\(?<x>x\H{2})|(?<u>(?:\\u\H{4})+)/
struct InvalidEscapeSequenceError: Error {
let value: Substring
}
func unescaped() throws -> String {
try self.replacing(Self.excapesRegex) { match in
if let a = match.a {
//
// RDSwipeView.h
// RDSwipeView
//
// Created by Gleb Lukianets on 31/03/16.
// Copyright © 2016 Gleb Lukianets. All rights reserved.
//
#import <UIKit/UIKit.h>
@glukianets
glukianets / Cloneable.swift
Created August 23, 2021 18:21
Poor man's lenses in swift
infix operator .=: AssignmentPrecedence
func .=<T, U>(_ kp: WritableKeyPath<T, U>, _ value: U) -> (inout T) -> () {
{ $0[keyPath: kp] = value }
}
protocol Cloneable { }
extension Cloneable {
func clone(with modifiers: (inout Self) -> ()...) -> Self {
@glukianets
glukianets / tuplesupport.cpp
Created June 1, 2020 12:59
common functional* operations on C++ tuples
#include <iostream>
#include <type_traits>
#include <tuple>
#include <limits>
#include <functional>
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
@glukianets
glukianets / flat_accumulate.cpp
Last active November 27, 2019 15:51
Performs any kind of accumulating operation over any amount of variably nested arrays and tuples. Can be extended to support more types.
namespace impl {
template<typename T>
struct FlatTypeTraits;
template<typename T, size_t L>
struct FlatTypeTraits<std::array<T, L>> {
template<typename AccT, typename CallableT>
static constexpr void acc(const std::array<T, L> &array, AccT &acc, CallableT &&callable) {
for (const T &element : array)
@glukianets
glukianets / RomanNumeralParser.swift
Created September 19, 2019 20:39
Roman numeral parser: a swift one-liner
let numerals: [Character:Int] = ["I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000]
func parse(romanNumeral numeral: String) -> Int {
return numeral.compactMap { numerals[$0] }.reversed().reduce((0, 0)) { $0.1 <= $1 ? ($0.0 + $1, $1) : ($0.0 - $1, 0) }.0
}
assert(parse(romanNumeral: "MMXIX") == 2019)
// Code golf: print factorial value of a highest digit in a number, function signature:
// int fact(int n)
int fact(int n) {return n>0?MAX(n%10*fact(n%10-1),fact(n/10)):1;}