This file contains 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 | |
// XORShiftのRandomNumberGenerator | |
// 参考: https://ja.wikipedia.org/wiki/Xorshift | |
struct XORShift: RandomNumberGenerator { | |
private var y: UInt64 | |
init(seed: UInt64 = 2463534242) { | |
y = seed > 0 ? seed : 2463534242 |
This file contains 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 SwiftUI | |
import PlaygroundSupport | |
struct NowPlayingIcon: View { | |
var body: some View { | |
ZStack(alignment: .bottom) { | |
HStack(spacing: 12) { | |
ForEach(0..<5) { i in | |
Bar(delay: Double(i) * 0.2) // (1.0秒 / 5.0本) | |
} |
This file contains 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
// times.swift.gyb | |
%{ | |
# https://yoshinblog.com/blogs/426E9F5D-8CA4-49A7-AC6D-54AC22429B46 | |
# curl "https://raw.githubusercontent.com/apple/swift/master/utils/gyb.py" -o "gyb.py" | |
# ./gyb.py times.swift.gyb -o times.swift --line-directive= | |
}% | |
% for sign in ['','U']: | |
% for intType in ['','8','16','32','64']: | |
/// Extension that adds a few additional functionalities to ${sign}Int${intType} |
This file contains 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 UIKit | |
import PlaygroundSupport | |
final class ViewController: UIViewController { | |
private let colors: [UIColor] = [.cyan, .magenta, .yellow] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let img = makeTile(colors: colors, size: 50)! | |
.resizableImage(withCapInsets: .zero, resizingMode: .tile) |
This file contains 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 SwiftUI | |
import PlaygroundSupport | |
/// ネットから画像を取得 | |
struct NetImage: View { | |
@ObservedObject private var observer: Observer | |
init(url: URL) { | |
observer = Observer(url) | |
} |
This file contains 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
// ↓ こっちの方が良い | |
// https://gist.github.com/sumitokamoi/22b8f30c2c1a3ef93cb1f03d4a7e8066 | |
func divide<Element>(array: Array<Element>, partition: Int) -> [Array<Element>] { | |
var r: [Array<Element>] = [] | |
stride(from: 0, to: array.count, by: partition).forEach { i in | |
let e = i + partition < array.count ? i + partition : array.count | |
r.append(Array(array[i..<e])) | |
} | |
return r |
This file contains 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 | |
// エラトステネスの篩 | |
// https://ja.wikipedia.org/?curid=147374 | |
// パターン1 | |
func sieve1(_ max: Int) -> [Int] { | |
let max = max + 1 | |
This file contains 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 UIKit | |
import UserNotifications | |
extension UNNotificationAttachment { | |
static func build(identifier: String, image: UIImage?, options: [AnyHashable: Any]? = nil) -> UNNotificationAttachment? { | |
guard let data = image?.pngData() else { return nil } | |
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(identifier).png") | |
do { try data.write(to: url) } catch { return nil } | |
return try? UNNotificationAttachment(identifier: identifier, url: url, options: options) | |
} |
This file contains 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 UIKit | |
protocol SegueKey: RawRepresentable where RawValue == String {} | |
extension UIViewController { | |
func performSegue<Key: SegueKey>(withIdentifier identifier: Key, sender: Any?) { | |
performSegue(withIdentifier: identifier.rawValue, sender: sender) | |
} | |
} |
This file contains 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 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) |