lisette

package module
v0.2.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2026 License: MIT Imports: 7 Imported by: 0

README

lisette/prelude

Types for Lisette, a language inspired by Rust that compiles to Go.

Type Description
Option[T] A value that is either present (Some) or absent (None). Replaces nilable pointers and comma-ok patterns.
Result[T, E] A value that is either a success (Ok) or a failure (Err). Replaces (T, error) return patterns.
Partial[T, E] A result that may carry both a value and an error (Ok, Err, or Both). For non-exclusive (T, error) returns.

Usage from Go

import lisette "github.com/ivov/lisette/prelude"

// Option
opt := lisette.MakeOptionSome(42)
opt.Is_some() // true
opt.Unwrap_or(0) // 42
none := lisette.MakeOptionNone[int]()
none.Unwrap_or(0) // 0

// Result
res := lisette.MakeResultOk[int, string](42)
res.Is_ok() // true
res.Unwrap_or(0) // 42

Learn more

By visiting the repository: github.com/ivov/lisette

Documentation

Overview

Package lisette provides core types for Lisette, a language inspired by Rust that compiles to Go.

For more information, visit https://lisette.run

Example (MapGet)
package main

import (
	"fmt"

	lisette "github.com/ivov/lisette/prelude"
)

func main() {
	m := map[string]int{"alice": 100, "bob": 200}
	fmt.Println(lisette.MapGet(m, "alice"))
	fmt.Println(lisette.MapGet(m, "charlie"))
}
Output:
Some(100)
None

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChannelClose

func ChannelClose[T any](ch chan T)

func ChannelSend

func ChannelSend[T any](ch chan T, value T) (sent bool)

func EnumeratedSliceFold

func EnumeratedSliceFold[T any, U any](s []T, init U, f func(U, Tuple2[int, T]) U) U

func EnumeratedSliceMap

func EnumeratedSliceMap[T any, U any](s []T, f func(Tuple2[int, T]) U) []U

func IsNilInterface added in v0.1.3

func IsNilInterface(x any) bool

IsNilInterface reports whether an interface value is nil or contains a nil pointer (typed nil). Returns true for both cases.

func MapFrom

func MapFrom[K comparable, V any](pairs []Tuple2[K, V]) map[K]V

func OptionMapOr

func OptionMapOr[T any, U any](opt Option[T], def U, f func(T) U) U

func OptionMapOrElse

func OptionMapOrElse[T any, U any](opt Option[T], def func() U, f func(T) U) U

func ResultMapOr

func ResultMapOr[T any, U any, E any](res Result[T, E], def U, f func(T) U) U

func RuneAt added in v0.2.0

func RuneAt(s string, i int) rune

RuneAt returns the rune at rune-index i in s. Panics on out-of-range i.

func SenderClose

func SenderClose[T any](ch chan<- T)

func SenderSend

func SenderSend[T any](ch chan<- T, value T) (sent bool)

func SliceAll

func SliceAll[T any](s []T, f func(T) bool) bool

func SliceFilter

func SliceFilter[T any](s []T, f func(T) bool) []T

func SliceFold

func SliceFold[T any, U any](s []T, init U, f func(U, T) U) U

func SliceMap

func SliceMap[T any, U any](s []T, f func(T) U) []U

func SliceToAny added in v0.1.14

func SliceToAny[T any](s []T) []any

func Substring added in v0.2.0

func Substring(s string, start, end int) string

Substring returns s[start:end] in rune indices. Panics on bad bounds.

func SubstringFrom added in v0.2.0

func SubstringFrom(s string, start int) string

SubstringFrom returns s[start:] in rune indices. Panics on bad bounds.

func SubstringTo added in v0.2.0

func SubstringTo(s string, end int) string

SubstringTo returns s[:end] in rune indices. Panics on bad bounds.

Types

type Option

type Option[T any] struct {
	Tag     OptionTag
	SomeVal T
}
Example
package main

import (
	"fmt"

	lisette "github.com/ivov/lisette/prelude"
)

func main() {
	some := lisette.MakeOptionSome(42)
	none := lisette.MakeOptionNone[int]()

	fmt.Println(some.IsSome())
	fmt.Println(none.IsNone())
	fmt.Println(some.UnwrapOr(0))
	fmt.Println(none.UnwrapOr(0))
}
Output:
true
true
42
0

func AssertType

func AssertType[T any](value any) Option[T]

func ChannelReceive

func ChannelReceive[T any](ch chan T) Option[T]

func EnumeratedSliceFind

func EnumeratedSliceFind[T any](s []T, f func(Tuple2[int, T]) bool) Option[Tuple2[int, T]]

func MakeOptionNone

func MakeOptionNone[T any]() Option[T]

func MakeOptionSome

func MakeOptionSome[T any](arg T) Option[T]

func MapGet

func MapGet[K comparable, V any](m map[K]V, key K) Option[V]

func OptionAndThen

func OptionAndThen[T any, U any](opt Option[T], f func(T) Option[U]) Option[U]

func OptionFlatten

func OptionFlatten[U any](opt Option[Option[U]]) Option[U]

func OptionFromCommaOk added in v0.1.20

func OptionFromCommaOk[T any](val T, ok bool) Option[T]

OptionFromCommaOk wraps a Go comma-ok pair `(value, ok)` into an `Option[T]`.

func OptionFromNilable added in v0.1.20

func OptionFromNilable[T any](val T, isNil bool) Option[T]

OptionFromNilable wraps a Go nilable `T` (pointer, function, interface, map, slice, channel) into an `Option[T]`.

func OptionFromPointer added in v0.1.25

func OptionFromPointer[T any](ptr *T) Option[T]

OptionFromPointer wraps a Go `*T` into an `Option[T]`, dereferencing the pointer for the Some branch. Used when reading Go-imported struct fields declared `*T` (T value-typed) — the Lisette typedef is `Option<T>`.

func OptionMap

func OptionMap[T any, U any](opt Option[T], f func(T) U) Option[U]
Example
package main

import (
	"fmt"

	lisette "github.com/ivov/lisette/prelude"
)

func main() {
	opt := lisette.MakeOptionSome(21)
	doubled := lisette.OptionMap(opt, func(v int) int { return v * 2 })
	fmt.Println(doubled)
}
Output:
Some(42)

func OptionZip

func OptionZip[T any, U any](opt Option[T], other Option[U]) Option[Tuple2[T, U]]

func ReceiverReceive

func ReceiverReceive[T any](ch <-chan T) Option[T]

func SliceFind

func SliceFind[T any](s []T, predicate func(T) bool) Option[T]

func SliceGet

func SliceGet[T any](s []T, index int) Option[T]

func (Option[T]) Filter

func (opt Option[T]) Filter(pred func(T) bool) Option[T]

func (Option[T]) IsNone

func (opt Option[T]) IsNone() bool

func (Option[T]) IsSome

func (opt Option[T]) IsSome() bool

func (Option[T]) IsZero

func (opt Option[T]) IsZero() bool

func (Option[T]) MarshalJSON

func (opt Option[T]) MarshalJSON() ([]byte, error)

func (Option[T]) OrElse

func (opt Option[T]) OrElse(f func() Option[T]) Option[T]

func (*Option[T]) Scan added in v0.1.22

func (opt *Option[T]) Scan(src any) error

func (Option[T]) String

func (opt Option[T]) String() string

func (*Option[T]) Take

func (opt *Option[T]) Take() Option[T]

func (*Option[T]) UnmarshalJSON

func (opt *Option[T]) UnmarshalJSON(data []byte) error

func (Option[T]) UnwrapOr

func (opt Option[T]) UnwrapOr(def T) T

func (Option[T]) UnwrapOrElse

func (opt Option[T]) UnwrapOrElse(f func() T) T

func (Option[T]) Value added in v0.1.22

func (opt Option[T]) Value() (driver.Value, error)

type OptionTag

type OptionTag int
const (
	OptionSome OptionTag = iota
	OptionNone
)

type PanicValue

type PanicValue struct {
	Value any
	Stack []byte
}

func (PanicValue) AsError

func (p PanicValue) AsError() Option[error]

func (PanicValue) Message

func (p PanicValue) Message() string

func (PanicValue) StackTrace

func (p PanicValue) StackTrace() string

func (PanicValue) String

func (p PanicValue) String() string

type Partial added in v0.1.3

type Partial[T any, E any] struct {
	Tag    PartialTag
	OkVal  T
	ErrVal E
}

func MakePartialBoth added in v0.1.3

func MakePartialBoth[T any, E any](val T, err E) Partial[T, E]

func MakePartialErr added in v0.1.3

func MakePartialErr[T any, E any](arg E) Partial[T, E]

func MakePartialOk added in v0.1.3

func MakePartialOk[T any, E any](arg T) Partial[T, E]

func PartialMap added in v0.1.3

func PartialMap[T any, U any, E any](p Partial[T, E], f func(T) U) Partial[U, E]

func PartialMapErr added in v0.1.3

func PartialMapErr[T any, E any, F any](p Partial[T, E], f func(E) F) Partial[T, F]

func (Partial[T, E]) Err added in v0.1.3

func (p Partial[T, E]) Err() Option[E]

func (Partial[T, E]) IsBoth added in v0.1.3

func (p Partial[T, E]) IsBoth() bool

func (Partial[T, E]) IsErr added in v0.1.3

func (p Partial[T, E]) IsErr() bool

func (Partial[T, E]) IsOk added in v0.1.3

func (p Partial[T, E]) IsOk() bool

func (Partial[T, E]) Ok added in v0.1.3

func (p Partial[T, E]) Ok() Option[T]

func (Partial[T, E]) String added in v0.1.3

func (p Partial[T, E]) String() string

func (Partial[T, E]) UnwrapOr added in v0.1.3

func (p Partial[T, E]) UnwrapOr(def T) T

func (Partial[T, E]) UnwrapOrElse added in v0.1.3

func (p Partial[T, E]) UnwrapOrElse(f func(E) T) T

type PartialTag added in v0.1.3

type PartialTag int
const (
	PartialOk PartialTag = iota
	PartialErr
	PartialBoth
)

type Range

type Range[T any] struct {
	Start T
	End   T
}

type RangeFrom

type RangeFrom[T any] struct {
	Start T
}

type RangeInclusive

type RangeInclusive[T any] struct {
	Start T
	End   T
}

type RangeTo

type RangeTo[T any] struct {
	End T
}

type RangeToInclusive

type RangeToInclusive[T any] struct {
	End T
}

type Result

type Result[T any, E any] struct {
	Tag    ResultTag
	OkVal  T
	ErrVal E
}
Example
package main

import (
	"fmt"

	lisette "github.com/ivov/lisette/prelude"
)

func main() {
	ok := lisette.MakeResultOk[int, string](42)
	err := lisette.MakeResultErr[int, string]("something went wrong")

	fmt.Println(ok.IsOk())
	fmt.Println(err.IsErr())
	fmt.Println(ok.UnwrapOr(0))
	fmt.Println(err.UnwrapOr(0))
}
Output:
true
true
42
0

func MakeResultErr

func MakeResultErr[T any, E any](arg E) Result[T, E]

func MakeResultOk

func MakeResultOk[T any, E any](arg T) Result[T, E]

func OptionOkOr

func OptionOkOr[T any, E any](opt Option[T], err E) Result[T, E]

func OptionOkOrElse

func OptionOkOrElse[T any, E any](opt Option[T], f func() E) Result[T, E]

func RecoverBlock

func RecoverBlock[T any](f func() T) Result[T, PanicValue]

func ResultAndThen

func ResultAndThen[T any, U any, E any](res Result[T, E], f func(T) Result[U, E]) Result[U, E]

func ResultMap

func ResultMap[T any, U any, E any](res Result[T, E], f func(T) U) Result[U, E]

func ResultMapErr

func ResultMapErr[T any, E any, F any](res Result[T, E], f func(E) F) Result[T, F]

func ResultOrElse

func ResultOrElse[T any, E any, F any](res Result[T, E], f func(E) Result[T, F]) Result[T, F]

func (Result[T, E]) Err

func (res Result[T, E]) Err() Option[E]

func (Result[T, E]) IsErr

func (res Result[T, E]) IsErr() bool

func (Result[T, E]) IsOk

func (res Result[T, E]) IsOk() bool

func (Result[T, E]) Ok

func (res Result[T, E]) Ok() Option[T]

func (Result[T, E]) String

func (res Result[T, E]) String() string

func (Result[T, E]) UnwrapOr

func (res Result[T, E]) UnwrapOr(def T) T

func (Result[T, E]) UnwrapOrElse

func (res Result[T, E]) UnwrapOrElse(f func(E) T) T

type ResultTag

type ResultTag int
const (
	ResultOk ResultTag = iota
	ResultErr
)

type Tuple2

type Tuple2[T any, U any] struct {
	First  T
	Second U
}

func ChannelSplit

func ChannelSplit[T any](ch chan T) Tuple2[chan<- T, <-chan T]

func EnumeratedSliceFilter

func EnumeratedSliceFilter[T any](s []T, f func(Tuple2[int, T]) bool) []Tuple2[int, T]

func MakeTuple2

func MakeTuple2[T any, U any](first T, second U) Tuple2[T, U]

func (Tuple2[T, U]) String

func (t Tuple2[T, U]) String() string

type Tuple3

type Tuple3[T any, U any, V any] struct {
	First  T
	Second U
	Third  V
}

func MakeTuple3

func MakeTuple3[T any, U any, V any](first T, second U, third V) Tuple3[T, U, V]

func (Tuple3[T, U, V]) String

func (t Tuple3[T, U, V]) String() string

type Tuple4

type Tuple4[T any, U any, V any, W any] struct {
	First  T
	Second U
	Third  V
	Fourth W
}

func MakeTuple4

func MakeTuple4[T any, U any, V any, W any](first T, second U, third V, fourth W) Tuple4[T, U, V, W]

func (Tuple4[T, U, V, W]) String

func (t Tuple4[T, U, V, W]) String() string

type Tuple5

type Tuple5[T any, U any, V any, W any, X any] struct {
	First  T
	Second U
	Third  V
	Fourth W
	Fifth  X
}

func MakeTuple5

func MakeTuple5[T any, U any, V any, W any, X any](first T, second U, third V, fourth W, fifth X) Tuple5[T, U, V, W, X]

func (Tuple5[T, U, V, W, X]) String

func (t Tuple5[T, U, V, W, X]) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL