All Projects → carlmjohnson → truthy

carlmjohnson / truthy

Licence: MIT license
Package truthy provides truthy condition testing with Go generics

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to truthy

Go Generics Example
Example code for Go generics
Stars: ✭ 121 (+290.32%)
Mutual labels:  generics
Arrow Meta
Functional companion to Kotlin's Compiler
Stars: ✭ 246 (+693.55%)
Mutual labels:  generics
async
Synchronization and asynchronous computation package for Go
Stars: ✭ 104 (+235.48%)
Mutual labels:  generics
Gomacro
Interactive Go interpreter and debugger with REPL, Eval, generics and Lisp-like macros
Stars: ✭ 1,784 (+5654.84%)
Mutual labels:  generics
Syncmap
A typed implementation of the Go sync.Map using code generation
Stars: ✭ 200 (+545.16%)
Mutual labels:  generics
go-generics
Go的泛型文档、代码、讨论
Stars: ✭ 43 (+38.71%)
Mutual labels:  generics
Promis
The easiest Future and Promises framework in Swift. No magic. No boilerplate.
Stars: ✭ 110 (+254.84%)
Mutual labels:  generics
parco
🏇🏻 generalist, fast and tiny binary parser and compiler generator, powered by Go 1.18+ Generics
Stars: ✭ 57 (+83.87%)
Mutual labels:  generics
Staticvec
Implements a fixed-capacity stack-allocated Vec alternative backed by an array, using const generics.
Stars: ✭ 236 (+661.29%)
Mutual labels:  generics
rust-amplify
Amplifying Rust language capabilities: multiple generic trait implementations, type wrappers, bit-precise numerics, derive macros
Stars: ✭ 38 (+22.58%)
Mutual labels:  generics
Go generics
Templates, generics engine for Go
Stars: ✭ 141 (+354.84%)
Mutual labels:  generics
Higgledy
Higher-kinded data via generics
Stars: ✭ 153 (+393.55%)
Mutual labels:  generics
lancet
A comprehensive, efficient, and reusable util function library of go.
Stars: ✭ 2,228 (+7087.1%)
Mutual labels:  generics
Envy
😠 Environmentally friendly environment variables
Stars: ✭ 132 (+325.81%)
Mutual labels:  generics
Java-Interview-Programs
Core Java Projects with complete source code
Stars: ✭ 48 (+54.84%)
Mutual labels:  generics
Ply
Painless polymorphism
Stars: ✭ 117 (+277.42%)
Mutual labels:  generics
Gengen
A Go source transformation tool for generics
Stars: ✭ 253 (+716.13%)
Mutual labels:  generics
generics
Deprecated! See https://github.com/golang-design/go2generics.
Stars: ✭ 26 (-16.13%)
Mutual labels:  generics
Java-Programs
Java Practiced Problems including concepts of OOPS, Interface, String , Collection.
Stars: ✭ 51 (+64.52%)
Mutual labels:  generics
go-ringbuf
Lock-free MPMC Ring Buffer (Generic) for SMP, in golang. Some posts in chinese:
Stars: ✭ 43 (+38.71%)
Mutual labels:  generics

Truthy Go Reference

Truthiness

Truthy is a package which uses generics (Go 1.18+) to create useful boolean tests and helper functions.

Examples

// truthy.Value returns the truthiness of any argument.
// If the value's type has a Bool() bool method, the method is called and returned.
// If the type has an IsZero() bool method, the opposite value is returned.
// Slices and maps are truthy if they have a length greater than zero.
// All other types are truthy if they are not their zero value.

truthy.Value(0) // false
truthy.Value(1) // true

truthy.Value("") // false
truthy.Value(" ") // true

truthy.Value([]byte(``)) // false
truthy.Value([]byte(` `)) // true

truthy.Value([]int{}) // false
truthy.Value([]int{1, 2, 3}) // true

var err error
truthy.Value(err) // false
truthy.Value(errors.New("hi")) // true
if truthy.Value(err) {
	panic(err)
}


var p *int
truthy.Value(p) // false

p = new(int)
// truthy does not check value underlying pointer!
truthy.Value(p) // true

// Ever wish Go had ? : ternary operators?
// Now it has a ternary function.
x := truthy.Cond("", 1, 10) // x == 10

// truthy.Cond cannot lazily evaluate its arguments,
// but you can use a closure to fake it.
s := truthy.Cond([]string{""},
	func() string {
		// do some calculation
		return "foo"
	},
	func() string {
		// do some calculation
		return "bar"
	})()
// s == "foo"


// How about an equivalent of the nullish coalescing operator ??
// as seen in C#, JavaScript, PHP, etc.:
var s string
truthy.First(s, "default") // "default"
s = "something"
truthy.First(s, "default") // "something"
truthy.First(0, 0*1, 1-1, 0x10-10) // 6

// Easily set defaults
n := getUserInput()
truthy.SetDefault(&n, 42)

// Collection testing and filtering
truthy.Any(0, 1, 2) // true
truthy.All(0, 1, 2) // false

ss := []string{"", "a", "b", ""}
truthy.Filter(&ss) // ss == []string{"a", "b"}

// Logical operators
if truthy.Or("1", 0) {
	fmt.Println("yay") // prints yay
}

if truthy.And(300, "") {
	fmt.Println("boo") // not executed
}

Installation

As of October 2021, Go 1.18 is not released, but you can install Go tip with

$ go install golang.org/dl/gotip@latest
$ gotip download
$ gotip init me/myproject
$ gotip get github.com/carlmjohnson/truthy

FAQs

Oh god

This is the correct reaction.

Isn't this just using reflection? Does it even really require generics?

I tried to write a non-generic version of this package first, but you can’t reflect on an interface type. When you do reflect.Value(x), you lose the fact that x was, e.g. an error, because reflect.Value() only takes interface{} and the conversion loses the interface type. You’d end up saying whether the underlying concrete type was empty or not, which is typically not what you want. To work around that, you could require that everything is passed as a pointer, e.g. reflect.Value(&err), but truthy.Value(&err) sucks as an API. If you look at how truthy.Value() works, it accepts a value of type T, and then passes *T to reflect.Value() and calls value.Elem() to finally get the correct reflection type. So, on a technical level, you couldn’t quite make this API work without generics, although it could be close. However, truthy.Filter(), truthy.SetDefault(), truthy.Any(), and truthy.All() could be implemented with pure reflection, although the implementation would be a lot uglier.

Then there’s truthy.First(). To be honest, truthy.First() is the only part of the package that I consider actually useful, and even that, I mostly expect it to be used for picking a string or default. Anyhow, it requires generics to avoid the cast back from interface type to the concrete type.

Should I use this package?

Probably not. It's a little bit of a joke package, but the truthy.First() and truthy.SetDefault() functionality seem useful, especially for strings. Time will tell what best practices around the use of generics in Go turn out to be.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].