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
#! /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 - \ |
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
// 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() | |
} |
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
/* | |
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!") } |
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
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]) | |
*/ | |
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 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" |
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
extension SequenceType { | |
func find(predicate: Generator.Element -> Bool) -> Generator.Element? { | |
for x in self { | |
if predicate(x) { return x } | |
} | |
return .None | |
} | |
} | |
infix operator <|> {} |
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
/* | |
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> { |
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
// 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 | |
} |
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
-- 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 |
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 Data.List | |
import Data.Ord | |
data Field = Str String | |
| Num Double | |
deriving (Show, Eq, Ord) | |
data Column = Column { fields::[Field] } | |
deriving (Show, Eq, Ord) |
NewerOlder