Skip to content

Instantly share code, notes, and snippets.

@davidkellis
Created October 25, 2017 15:17
Show Gist options
  • Save davidkellis/5fca15a1989860f8c0a3578699cba3bf to your computer and use it in GitHub Desktop.
Save davidkellis/5fca15a1989860f8c0a3578699cba3bf to your computer and use it in GitHub Desktop.
brainstorming truthy
package truthy
interface Truthy for T {
fn truthy?(T) -> Boolean
}
def ||[A: Truthy, B: Truthy](a: A, b: B): A | B => truthy?(a) ? a : b
def &&[A: Truthy, B: Truthy](a: A, b: B): A | B => truthy?(a) ? a : b
def !(a: Truthy): Boolean => !a.truthy?
impl Truthy for Boolean {
fn truthy?(v: Boolean) => v
}
impl Truthy for Nil {
fn truthy?(v: Nil) => false
}
// U is a union type that is a superset of the single-member union type Nil
impl [U supersetOf Nil] Truthy for U {
fn truthy?(v: U) => v match {
case Nil => false
case _ => true
}
}
impl Truthy for T {
fn truthy?(v: Nil) => true
}
// then in another package, you do this:
package main
import truthy.*
fn main() {
b = true
s: String? = "foo"
i: Int? = 5
if s && i && b {
puts("boolean=$b string=$s int=$i")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment