Skip to content

Instantly share code, notes, and snippets.

View alskipp's full-sized avatar

Al Skipp alskipp

View GitHub Profile
@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 - \
@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 / 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 / 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 / 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 / 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 / List_monoid_foldable.swift
Last active February 3, 2018 22:58
Foldable built on a bedrock of Monoids
/*
If `Monoid` and `Foldable` are predefined, it's possible to create data structures that
receive many default methods, simply by implementing the `Monoid` and `Foldable` protocols.
These data structures could be Trees or Lists, or the built in Array.
Here's an example with a basic implementation of a List:
*/
enum List<Element> {
@alskipp
alskipp / monoid.swift
Last active February 3, 2018 22:56
Swiftly towards the monoid
// erm… this Num protocol is useless, but let's overlook this bit for now, shall we?
protocol Num: IntegerLiteralConvertible, IntegerArithmeticType {}
extension Int: Num {}
protocol Monoid {
static func mempty() -> Self
static func mappend(a: Self, _ b: Self) -> Self
static func mconcat(a: [Self]) -> Self
}
@alskipp
alskipp / flatMappyMonady.hs
Last active August 29, 2015 14:26
Fear not the flatMapper
-- a function that accepts 2 monad args ‘containing’ number types and multiplies the ‘contents’
mult a b =
a >>= \x ->
b >>= \y -> return (x * y)
mult (Just 2) (Just 3)
-- Just 6
mult Nothing Nothing
-- Nothing
@alskipp
alskipp / table_sort.hs
Created June 7, 2015 12:05
Haskell example of spreadsheet-like column sorting
import Data.List
import Data.Ord
data Field = Str String
| Num Double
deriving (Show, Eq, Ord)
data Column = Column { fields::[Field] }
deriving (Show, Eq, Ord)