This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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. | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// MARK: - Definitions - | |
public typealias Vector<T : Hashable> = [T : Int] | |
public typealias Matrix<T : Hashable> = [T : Vector<T>] | |
public enum DecisionProcess { | |
case predict |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let strings = loadTextFile(fileName: file) | |
.replacingOccurrences(of: "\n", with: " ") | |
.components(separatedBy: " ") | |
MarkovModel.process(transitions: strings) { matrix in | |
print(matrix) | |
} |
NewerOlder