Last active
April 26, 2018 21:49
-
-
Save Palleas/9c9ecbd059cfdf8fb74ebb0d0c9579f8 to your computer and use it in GitHub Desktop.
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 Cocoa | |
precedencegroup ForwardApplication { | |
associativity: left | |
} | |
precedencegroup ForwardComposition { | |
associativity: left | |
higherThan: ForwardApplication | |
} | |
infix operator |>: ForwardApplication | |
func |> <A, B>(a: A, f: (A) throws -> B) throws -> B { | |
return try f(a) | |
} | |
infix operator >>>: ForwardComposition | |
func >>> <A, B, C>(f: @escaping (A) throws -> B, g: @escaping (B) throws -> C) -> ((A) throws -> C) { | |
return { a in | |
try g(f(a)) | |
} | |
} | |
typealias EmojiMapping = [String: URL] | |
func loadJSON<T: Decodable>(file: URL) throws -> T { | |
let decoder = JSONDecoder() | |
let content = try Data(contentsOf: file) | |
return try decoder.decode(T.self, from: content) | |
} | |
func makeReplacer(maps: EmojiMapping) -> (String) -> NSAttributedString? { | |
return { name in | |
guard let url = maps[name] else { | |
return nil | |
} | |
let icon = NSTextAttachment() | |
icon.image = NSImage(contentsOf: url) | |
return NSAttributedString(attachment: icon) | |
} | |
} | |
let replace = try URL(fileURLWithPath: "emojis.json") | |
|> loadJSON | |
>>> makeReplacer | |
let result = replace("poop") | |
print("Result = \(result)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment