Skip to content

Instantly share code, notes, and snippets.

@dineybomfim
dineybomfim / UniqueSequence.swift
Last active August 19, 2023 13:26
Swift unique functionality for Sequences / Collections, option to keep the first or the last occurrence of the duplicated element.
public extension Sequence where Element : Hashable {
/// Returns an array containing the unique elements from the sequence.
///
/// - Parameter keepLast: A boolean value indicating whether to keep the last occurrence of each duplicated element.
/// If `true`, the last occurrence is kept. If `false`, the first occurrence is kept. The default value is `false`.
/// - Returns: An array containing the unique elements from the sequence based on the `keepLast` behavior.
/// - Complexity: O(n), where `n` is the length of the sequence.
func unique(keepLast: Bool = false) -> [Element] {
guard !keepLast else { return reversed().unique(keepLast: false).reversed() }
@dineybomfim
dineybomfim / httpbin
Created June 9, 2023 06:07
HTTP bin mock
{
"args": {},
"headers": {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
"Host": "httpbin.org",
"Referer": "https://httpbin.org/",
"Sec-Ch-Ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
"Sec-Ch-Ua-Mobile": "?0",
@dineybomfim
dineybomfim / PhoneNumberFormatter.swift
Last active January 16, 2024 11:56
The ultimate, free to use, no dependency, runtime automatically update, worldwide global Phone Number Formatter, works for any country and every single particular national phone number pattern, including carrier codes, international codes and country codes.
/*
* PhoneNumberFormatter.swift
*
* Created by Diney Bomfim on 12/12/22.
*
* Swift code that automatically loads the global phone format list, parses it and formats a given input.
* For everyone that believes Phone Number Formatting should be open source, global standard and easy to use.
* This is based on Google Project https://github.com/google/libphonenumber. Updated constantly by Google engineers.
*/
@dineybomfim
dineybomfim / phone_numbers_metadata
Created December 11, 2022 16:50
Worlwide phone patterns
{"phoneNumberMetadata": {"territories": {"territory": [{"id": "AC","countryCode": "247","internationalPrefix": "00","generalDesc": {"nationalNumberPattern": "(?:[01589]\\d|[46])\\d{4}"},"fixedLine": {"possibleLengths": {"national": "5"},"exampleNumber": "62889","nationalNumberPattern": "6[2-467]\\d{3}"},"mobile": {"possibleLengths": {"national": "5"},"exampleNumber": "40123","nationalNumberPattern": "4\\d{4}"},"uan": {"possibleLengths": {"national": "6"},"exampleNumber": "542011","nationalNumberPattern": "(?:0[1-9]|[1589]\\d)\\d{4}"}},{"id": "AD","countryCode": "376","internationalPrefix": "00","availableFormats": {"numberFormat": [{"pattern": "(\\d{3})(\\d{3})","leadingDigits": "[135-9]","format": "$1 $2"},{"pattern": "(\\d{4})(\\d{4})","leadingDigits": "1","format": "$1 $2"},{"pattern": "(\\d{3})(\\d{3})(\\d{3})","leadingDigits": "6","format": "$1 $2 $3"}]},"generalDesc": {"nationalNumberPattern": "(?:1|6\\d)\\d{7}|[135-9]\\d{5}"},"noInternationalDialling": {"possibleLengths": {"national": "8"},"nationalNum
@dineybomfim
dineybomfim / LocaleAlphaCode.swift
Last active June 12, 2025 15:09
Have you ever wondered how to convert and/or use alpha 2 and alpha 3 country codes seamlessly with native Swift Locale? Here is the answer: There is a tricky, very cost-efficient that can guarantee to find valid ISO Alpha-2 and Alpha-3 country codes (ISO 3166 international standard). NO EXTERNAL FILE IS NEEDED!
public extension Locale {
private static var availableRegions: [Locale] = { Locale.availableIdentifiers.map { Locale(identifier: $0) } }()
init?(isoCode: String, from: Locale = .autoupdatingCurrent) {
guard let locale = from.locale(isoCode: isoCode) else { return nil }
self = locale
}
func alpha2Code(from isoCode: String) -> String? {
@dineybomfim
dineybomfim / AnyCodable.swift
Last active December 12, 2022 12:32
Wondering how to parse dynamic JSON with Swift Codable without dealing with `init(from:Decoder)` and `encode(to: Encoder)`? The answer is `AnyCodable`
// Full Swift Codable object compatible with dynamic JSON structures. Able to encode and decode any kind of JSON.
//
// How to use it?
//
// Try it out on your playground at the end of the file:
public struct AnyCodable {
// MARK: - Properties
import Foundation
// MARK: - Definitions -
public typealias Vector<T : Hashable> = [T : Int]
public typealias Matrix<T : Hashable> = [T : Vector<T>]
public enum DecisionProcess {
case predict
func buildText(starting: String, length: Int, file: String) -> String {
var text = starting
let strings = loadTextFile(fileName: file)
.replacingOccurrences(of: "\n", with: " ")
.components(separatedBy: " ")
MarkovModel.process(transitions: strings) { matrix in
buildWords(with: &text, length: length, chain: matrix)
}
func buildWords(with text: inout String, length: Int, chain: Matrix<String>) {
var word = text
(0...length).forEach { _ in
if let next = chain.next(given: word, process: .weightedRandom) {
text.append(" \(next)")
word = next
}
}
}
let strings = loadTextFile(fileName: file)
.replacingOccurrences(of: "\n", with: " ")
.components(separatedBy: " ")
MarkovModel.process(transitions: strings) { matrix in
print(matrix)
}