Skip to content

Instantly share code, notes, and snippets.

@ilyannn
ilyannn / fold.py
Last active August 29, 2019 15:28
Almost one-liner fold
def fold(s, p = 0):
for i in [i+1 for (i, c), n in zip(enumerate(s), s[1:]+' ') if c != n]: p = print(s[p], i-p, sep="", end="") or i
@ilyannn
ilyannn / bangbang.md
Last active June 29, 2017 17:49 — forked from erica/bangbang.md

Instead of

assert(!array.isEmpty, "Array guaranteed to be non-empty because...")
let lastItem = array.last!

guard !array.isEmpty 
    else { fatalError("Array guaranteed to be non-empty because...") }

// ... etc ...
// TODO: insert guards to use open-source version of random() if necessary
func random(x: Int) -> Int {
return Int(arc4random_uniform(UInt32(endBoundary)))
}
extension CollectionType {
/// create a lazy succession of randomly ordered indices
///
@ilyannn
ilyannn / elvisOperatorStdLib.swift
Last active December 5, 2015 23:29 — forked from masters3d/elvisOperatorStdLib.swift
An experiment in removing tetriary expressions from stdlib (thanks to masters3d for original file)
//Filter.swift
public mutating func next() -> Base.Element? {
var n: Base.Element?
for/*ever*/;; {
n = _base.next()
if n != nil ? _predicate(n!) : true {
return n
}
}
@ilyannn
ilyannn / indents.swift
Last active November 24, 2015 19:05 — forked from erica/indents.swift
enum StringPaddingStyle {
case Left, Right
}
func padString(source: String,
with character: Character = " ",
fill target: Int,
style: StringPaddingStyle = .Left
) -> String {
@ilyannn
ilyannn / ShowPrices.swift
Last active August 29, 2015 14:22
Erica's code challenge
// For https://gist.github.com/erica/5477d5caccfbff04a802
// MARK: Generic collections
import Foundation
import Swift
infix operator ➜ { associativity left }
func ➜(source: [String], f: (String) -> ()) -> () {
for element in source {
@ilyannn
ilyannn / Timer.swift
Last active August 29, 2015 14:06
Timer with Alice, Bob, Carol, Daniel and Eva.
// -------------------------
// General-purpose functions. Created by Alice.
import Foundation
func <(lhs:NSDate, rhs:NSDate) -> Bool {
// This should be in the standard library. - Alice.
return lhs.compare(rhs) == .OrderedAscending
}
@ilyannn
ilyannn / utf8.swift
Last active August 29, 2015 14:05
UTF8 conversion routines test
#!/usr/bin/env xcrun swift -O3
//
// utf.swift
//
//
// Created by Ilya Nikokoshev on 8/22/14.
//
//
import Foundation
@ilyannn
ilyannn / Error.swift
Last active August 29, 2015 14:05
An example of Swift style
// A different style for an example code by David Owens.
// https://github.com/owensd/json-swift/blob/master/src/Error.swift
import Foundation
/** Creates a new type that is used to represent error information in Swift.
This is a new Swift-specific error type used to return error information.
The primary usage of this object is to return it as a `Failable` or
`FailableOf<T>` from function that could fail.
@ilyannn
ilyannn / StringElements.swift
Last active August 29, 2015 14:04
String elements in Swift
// Demonstrates some rather elementary points about code points vs elements
import Foundation
import Swift
extension String {
func canonize() -> [Character] {
return Array(precomposedStringWithCanonicalMapping)
}
static func decanonize(chars: [Character]) -> String {