Skip to content

Instantly share code, notes, and snippets.

@to4iki
to4iki / Sequence+.swift
Last active October 24, 2016 17:06
要素と位置(idx)をペアにしたコレクションを返す
import Foundation
extension Sequence {
func filterNot(_ isExcluded: (Self.Iterator.Element) throws -> Bool) rethrows -> [Self.Iterator.Element] {
var res: [Self.Iterator.Element] = []
for e in self where try !isExcluded(e) {
res.append(e)
}
return res
@to4iki
to4iki / MessagePackWrapper.swift
Last active September 16, 2016 07:48
Wrapper MessagePack.swift
import Foundation
import MessagePack
// used https://github.com/a2/MessagePack.swift
// MARK: - PackableI/F
protocol Packable {
func toPackValue() -> MessagePackValue
init?(packValue: MessagePackValue)
@to4iki
to4iki / Regex.swift
Created July 18, 2016 05:47
simple regex
import Foundation
/// See also
// http://qiita.com/tsuruchika/items/9ca9c4811e1f28b9417c
// http://qiita.com/moaible/items/85d80173bb8ed7f64b74
// http://fromatom.hatenablog.com/entry/2015/07/15/192328
public struct Regex {
private let pattern: String
@to4iki
to4iki / getIOSVersion.js
Last active June 9, 2017 12:20
Get iOS version via javascript
class Semver {
constructor(major, minner, patch) {
this.major = major;
this.minner = minner;
this.patch = patch;
}
toString() {
return `${this.major}_${this.minner}_${this.patch}`;
}
typealias ExecuteOnce = () -> Void
func executeOnce(f: () -> Void) -> ExecuteOnce {
var first = true
return {
if first {
first = false
f()
}
}
@to4iki
to4iki / foldl.swift
Last active June 4, 2016 06:10
Implement array function used foldl
import Foundation
extension Array {
var empty: Bool {
return count == 0
}
var nonEmpty: Bool {
return !empty
}
@to4iki
to4iki / InMemoryUserDefaults.swift
Created May 14, 2016 08:16
Swift NSUserDefaults InMemory for Testing
import Foundation
final class InMemoryUserDefaults: NSUserDefaults {
static let SuiteName = "InMemory"
private var data: [String: AnyObject?]
override init?(suiteName suitename: String?) {
data = [:]
@to4iki
to4iki / Regex.swift
Last active February 18, 2016 14:28
regex =~ operator for Swift
import Foundation
/// See also
/// - https://github.com/kasei/SwiftRegex
struct Regex {
private let pattern: String
private let internalRegexp: NSRegularExpression
@to4iki
to4iki / Euler.scala
Created October 24, 2015 09:18
projecteuler
object Euler {
// http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%205
def main(args: Array[String]) {
// val r1 = test(10)
val r = (1L to 20L).reduceLeft(lcm)
println(r)
}
// def test(n: Int) = (20 to Int.MaxValue).find(x => (2 to n).forall(x % _ == 0))
@to4iki
to4iki / ADT.scala
Created October 5, 2015 14:59
Algebraic Data Types
/**
* 代数的データ型
*/
object AlgebraicDataTypes extends App {
import Bool._
println(True.id) // 0
println(Bool(0)) // True
println(Bool.withName("True")) // True