This file contains hidden or 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
struct Foo<A> { | |
// here `A` is a whole new type parameter, different from the above `A` | |
func bar<A>() -> Foo<A> { | |
return Foo<A>() | |
} | |
} | |
let x = Foo<Int>() |
This file contains hidden or 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
// There's a natural way to lift a predicate `(A) -> Bool` to a function `(A) -> A? | |
func optionalBool<A>(_ p: @escaping (A) -> Bool) -> (A) -> A? { | |
return { p($0) ? .some($0) : nil } | |
} | |
// There's a natural way to lift a predicate `(A) -> Bool` to a function `(A) -> Either<A, A> | |
func eitherBool<A>(_ p: @escaping (A) -> Bool) -> (A) -> Either<A, A> { | |
// notice the similarities of this with `optionalBool`: | |
return { p($0) ? .right($0) : .left($0) } | |
} |
This file contains hidden or 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 Foundation | |
func zip<A, B, R>( | |
_ f: @escaping ((A) throws -> R) throws -> R, | |
_ g: @escaping ((B) throws -> R) throws -> R, | |
with: (A, B) -> R | |
) throws -> R { | |
return try f { a in | |
try g { b in |
This file contains hidden or 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 Foundation | |
import PlaygroundSupport | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
URLSession.shared.dataTask(with: URL(string: "/")!) { data, response, error in | |
print(data) // nil | |
print(response) // nil | |
print(error) // some error | |
} | |
.resume() |
This file contains hidden or 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
// https://twitter.com/UINT_MIN/status/968524617714249729 | |
struct Prism<A, B> { | |
let preview: (A) -> B? | |
let review: (B) -> A | |
} | |
extension Prism where A == String, B == Int { | |
static var toInt: Prism { | |
return Prism(preview: Int.init, review: String.init) |
This file contains hidden or 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
// ---------------------------------------------------- | |
// These 3 helpers is all you need to unlock functional | |
// setters for every field of every immutable struct. | |
// ---------------------------------------------------- | |
// A generic way to a lift writable key path and a value to a setter function. | |
func set<A, B>(_ kp: WritableKeyPath<A, B>, _ b: B) -> (A) -> A { | |
return { a in | |
var a = a | |
a[keyPath: kp] = b |
This file contains hidden or 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
enum StringOrInt { | |
case string(String) | |
case int(Int) | |
} | |
func isInt(_ stringOrInt: StringOrInt) -> Bool { | |
switch stringOrInt { | |
case .string: return false | |
case .int: return true | |
} |
This file contains hidden or 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
{-# LANGUAGE EmptyCase #-} | |
data Never | |
absurd :: Never -> a | |
absurd a = case a of {} | |
main = do | |
putStrLn "It compiles!" |
This file contains hidden or 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
{-# LANGUAGE EmptyCase #-} | |
data Never | |
absurd :: Never -> a | |
absurd a = case a of {} | |
main = do | |
putStrLn "It compiles!" |
This file contains hidden or 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
class Never private constructor() | |
fun <A> absurd(never: Never): A { | |
when(never) {} | |
} | |
println("It compiles!") |