Skip to content

Instantly share code, notes, and snippets.

@yycking
yycking / audio.swift
Last active December 28, 2023 08:09
read/write audio file
let musicURL = try! FileManager.default.url(for: .musicDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let source = musicURL.appendingPathComponent("錄音/原始.wav")
let input = try! AVAudioFile(forReading: source)
let format = input.fileFormat
let destination = musicURL.appendingPathComponent("錄音/複製.wav")
let output = try! AVAudioFile(forWriting: destination, settings: format.settings, commonFormat: format.commonFormat, interleaved: format.isInterleaved)
while input.framePosition < input.length {
@yycking
yycking / 質數.pl
Created October 19, 2022 04:16
用 Prolog 判斷此數是否為質數
:- set_prolog_flag(verbose, silent).
:- initialization(main).
可整除(X, Y):-
0 is X mod Y.
有因數(X, Y):-
可整除(X, Y).
有因數(X, Y):-
X > Y+1,
@yycking
yycking / Codable+default.swift
Created June 15, 2022 10:29
Codable + default value
protocol Init {
init()
}
extension KeyedDecodingContainer {
func decode<T: Codable & Init>(_ type: T.Type,
forKey key: Key) throws -> T {
try decodeIfPresent(type, forKey: key) ?? .init()
}
}
@yycking
yycking / Prime.pl
Last active October 18, 2022 07:27
輸出18~399間的質數
:- set_prolog_flag(verbose, silent).
:- initialization(main).
非質數([X | _], Value):-
Mod is Value mod X,
Mod = 0.
非質數([_ | Other], Value):-
非質數(Other, Value).
@yycking
yycking / atm.pl
Created April 22, 2022 15:12
寫一程式仿金融卡提款,從鍵盤輸入帳號與密碼,若正確就顯示帳號與密碼正確可進行提款(預設帳號為你的學號,密碼為IMD12345),若錯誤則顯示帳號與密碼錯誤,還有幾次輸入機會(共有六次輸入之機會)。
:- set_prolog_flag(verbose, silent).
:- initialization(main).
帳號(學號). 密碼("IMD12345").
登入(Account, Password):-
帳號(Account), 密碼(Password),
write("帳號與密碼正確可進行提款"), nl.
登入:-
@yycking
yycking / 數字大寫.swift
Created July 19, 2021 08:13
數字轉中文大寫(最大單位只能換到萬)
extension String {
subscript(_ i: Int) -> String {
let idx1 = index(startIndex, offsetBy: i)
let idx2 = index(idx1, offsetBy: 1)
return String(self[idx1..<idx2])
}
subscript (r: Range<Int>) -> String {
let start = index(startIndex, offsetBy: r.lowerBound)
let end = index(startIndex, offsetBy: r.upperBound)
@yycking
yycking / version.swift
Last active May 28, 2021 08:15
Let Version compare as easy as float
import Combine
import Foundation
struct Version: Codable {
var stringValue: String
init(rawValue: String) {
stringValue = rawValue
}
init(from decoder: Decoder) throws {
@yycking
yycking / combine.swift
Created May 27, 2021 05:01
Creates a sequence of pairs built out of two underlying sequences.
func combine<Sequence1, Sequence2>(_ sequence1: Sequence1, _ sequence2: Sequence2) -> CombineSequence<Sequence1, Sequence2> {
return CombineSequence(sequence1, sequence2)
}
struct CombineSequence<Sequence1: Sequence, Sequence2: Sequence> {
let _sequence1: Sequence1
let _sequence2: Sequence2
init(_ sequence1: Sequence1, _ sequence2: Sequence2) {
(_sequence1, _sequence2) = (sequence1, sequence2)
}
@yycking
yycking / string~=.swift
Last active March 29, 2021 07:19
~= operator for String
import Cocoa
import Foundation
extension String {
static func ~= (lhs: String, rhs: String) -> Bool {
if lhs == rhs {
return true
}
if let range = rhs.range(of: lhs, options: .regularExpression) {
return rhs[range] == rhs
@yycking
yycking / fetchAll.swift
Created December 25, 2020 06:29
fetch all link from request
import Foundation
import PlaygroundSupport
import Combine
PlaygroundPage.current.needsIndefiniteExecution = true
enum RequestError: Error {
case sessionError(error: Error)
}