machx / konkyo Goto Github PK
View Code? Open in Web Editor NEWA Foundation Swift Package for my projects
License: Apache License 2.0
A Foundation Swift Package for my projects
License: Apache License 2.0
@propertyWrapper public struct Preferences<Value: Codable> {
public let key: String
public let defaultValue: Value
public init(key: String, defaultValue: Value) {
self.key = key
self.defaultValue = defaultValue
}
public var wrappedValue: Value {
get { UserDefaults.standard.object(forKey: key) as? Value ?? defaultValue }
set { UserDefaults.standard.setValue(newValue, forKey: key) }
}
}
Should probably use dependency injection or something like that to make the value testable from within the package more easily.
Idea is that you may just want something that keeps track of the last x number of key value pairs added modified. So if you do
dict.capacity = 2
dict[a] = 5 // [(a, 5)]
dict[b] = 3 // [(a,5),(b,3)]
dict[c] = 6 // [(b,3), (a,5)]
When official Xcode 12.5 is released then the platform iOS settings are updated and include definitions for iOS 14 and macOS 11 then update the package to require those and get rid of the @Availability api's.
Being in a struct it seems that Swift has been resistant to returning the dictionary keys. Need to find a way (for unit tests among other things) to expose the keys as part of the struct
// roughly equivalent to
// lock.lock()
// if shouldDoThing == true {
// shouldDoThing = false
// print(shouldDoThing)
// }
// lock.unlock
if AtomicCompareAndSwap(shouldDoThing, expecting: true, replaceWith: false) {
print(shouldDoThing) // false
}
import Foundation
//FIXME: Need to expand on this code or delete it...
public protocol MVVM {
associatedtype T
var value: T { get set }
}
Currently this is considered as a candidate for deletion if nothing else useful is added here.
private mutating func filterExcessEntries() {
while _keys.count > _limit {
if let value = _dictionary.removeValue(forKey: _keys[0]) {
//FIXME: Remove when Swift gets the ability to specify macOS 11 in Package.swift
print("Removing Key/value pair (\(_keys[0]), \(value)")
_keys.remove(at: 0)
}
}
}
Thread atomics could have their own framework or have a section in the package like
import Konkyo.Thread
func testDictionaryLiteralAndSetLimit() {
var dictionary: RollingDictionary = ["A": 1, "B": 2]
XCTAssertNotNil(dictionary)
XCTAssertEqual(dictionary["A"], 1)
XCTAssertEqual(dictionary["B"], 2)
dictionary.setLimit(1)
XCTAssertEqual(dictionary.getKeyLimit(), 1)
//
XCTAssertNil(dictionary["A"]) // <-- FAIL HERE
XCTAssertNotNil(dictionary["B"]) // <-- FAIL HERE
}
The test randomly fails. I'm guessing the key orders aren't preserved or something like that.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.