Skip to content

Instantly share code, notes, and snippets.

@HonmaMasaru
HonmaMasaru / ResultBuilderDemo.swift
Last active November 3, 2022 09:43
Build HTML with ResultBuilder
import Foundation
/// 要素
protocol Element {
/// HTMLを書き出すレンダー
/// - Returns: HTML
func render() -> String
}
require 'optparse'
inFile = 'base.jpg'
outFile = 'glitch.jpg'
e = 0.0
ARGV.options do |opt|
opt.on('-i [OPTIONAL]') {|v| inFile = v.to_s }
opt.on('-o [OPTIONAL]') {|v| outFile = v.to_s }
opt.on('-e [OPTIONAL]') {|v| e = v.to_f }
@HonmaMasaru
HonmaMasaru / ClampValue.swift
Last active April 2, 2022 03:31
値を範囲内に収める
import Foundation
/// 値を範囲内に収める
@propertyWrapper struct ClampValue<T> where T: Comparable {
private var value: T
init(_ range: ClosedRange<T>) {
value = range.lowerBound
projectedValue = range
}
import Foundation
struct Rates: Codable {
let quotes: [Rate]
struct Rate: Codable {
let bid: String
let currencyPairCode: String
}
}
import Foundation
@propertyWrapper struct RotationValue<T> where T: Numeric, T: Comparable {
private var value: T
private var length: T
init(_ range: ClosedRange<T>) {
value = range.lowerBound
projectedValue = range
import Foundation
extension Array where Element: Hashable {
var tally: [Element : Int] {
Dictionary(grouping: self) { $0 }.compactMapValues { $0.count }
}
}
let a = ["a", "b", "c", "d", "e", "a", "b", "c", "d", "a", "b", "c", "a", "b", "a"]
print(a.tally)
import Foundation
extension Array where Element: Equatable {
var unique: [Element] {
reduce([Element]()) { $0.contains($1) ? $0 : $0 + [$1] }
}
}
let a = ["a", "b", "c", "d", "e", "a", "b", "c", "d", "a", "b", "c", "a", "b", "a"]
print(a.unique)
import UIKit
extension CGPoint {
static func *<T: BinaryFloatingPoint>(lhs: CGPoint, rhs: T) -> CGPoint {
CGPoint(x: lhs.x * CGFloat(rhs), y: lhs.y * CGFloat(rhs))
}
static func *=<T: BinaryFloatingPoint>(left: inout CGPoint, right: T) {
left = CGPoint(x: left.x * CGFloat(right), y: left.y * CGFloat(right))
}
import Foundation
extension URLRequest {
enum Method: String {
case get = "GET"
case post = "POST"
case put = "PUT"
case delete = "DELETE"
}
import UIKit
protocol ConfigBase {
associatedtype Key: RawRepresentable, CaseIterable where Key.RawValue == String
var userDefaults: UserDefaults { get }
func set(_ value: Any?, forKey key: Key)
func set(_ value: Float, forKey key: Key)
func set(_ value: Double, forKey key: Key)