Skip to content

Instantly share code, notes, and snippets.

View alskipp's full-sized avatar

Al Skipp alskipp

View GitHub Profile
@alskipp
alskipp / findFirst.swift
Created October 21, 2015 17:35
Swift: Find the first element of a Sequence that matches an element of another Sequence
extension SequenceType {
func find(predicate: Generator.Element -> Bool) -> Generator.Element? {
for x in self {
if predicate(x) { return x }
}
return .None
}
}
infix operator <|> {}
@alskipp
alskipp / FizzBuzzTizzWuzz.hs
Created November 21, 2015 17:49
The FizzBuzzTizzWuzz challenge
import Data.Monoid
import Control.Applicative
import Data.Maybe
main = mapM_ putStrLn $ mapMaybe fizzy [1..315]
fizzy :: Int -> Maybe String
fizzy n = fizz <> buzz <> tizz <> wuzz <|> pure (show n)
where
fizz = modReplace 3 "Fizz"
@alskipp
alskipp / ArrayExtensions.swift
Last active February 17, 2019 07:59
A few Array extensions
extension Array {
/*
'span' takes a predicate and returns a tuple where first element is longest prefix (possibly empty)
of elements of self that satisfy 'predicate' and second element is the remainder of self:
-- > [1,2,3,4,1,2,3,4].span { $0 < 3 } == ([1,2],[3,4,1,2,3,4])
-- > [1,2,3].span { $0 < 9 } == ([1,2,3],[])
-- > [1,2,3].span { $0 < 0 } == ([],[1,2,3])
*/
@alskipp
alskipp / protocol-oo-inheritance-conflict.swift
Created December 1, 2015 23:01
Example of the incompatibility between OO inheritance and protocol oriented Swift
/*
A silly example demonstrating how object-oriented inhertitance can conflict with protocol oriented Swift
*/
class Animal {
func respire() { print("Yum, oxygen") }
}
class Wolf: Animal {
func growl() { print("Grrrr!") }
@alskipp
alskipp / ignoreNil.swift
Created May 6, 2016 00:04
How to ignore `nil` values using RxSwift without using❗️
// Create an `Observable` of Optional<Int>
let values: Observable<Int?> = [1, 2, .None, 3, .None, 4, 5].toObservable()
// Method 1: using a free function
// Requires passing the function to `flatMap`
func ignoreNil<A>(x: A?) -> Observable<A> {
return x.map { Observable.just($0) } ?? Observable.empty()
}
@alskipp
alskipp / vid_to_gif.sh
Created July 29, 2020 16:14
Mangle your .mp4 files into .gif
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p ffmpeg gifsicle
scale="${3:-320}"
fps="${4:-15}"
palette="/tmp/palette.png"
filters="fps=$fps,scale=$scale:-1:flags=lanczos"
ffmpeg -v warning -i "$1" -vf "$filters,palettegen=stats_mode=diff" -y $palette
ffmpeg -i "$1" -i $palette -lavfi "$filters,paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle" -y -f gif - \