Skip to content

Instantly share code, notes, and snippets.

@Palleas
Last active April 26, 2018 21:49
Show Gist options
  • Save Palleas/9c9ecbd059cfdf8fb74ebb0d0c9579f8 to your computer and use it in GitHub Desktop.
Save Palleas/9c9ecbd059cfdf8fb74ebb0d0c9579f8 to your computer and use it in GitHub Desktop.
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