今日已更新 184 条资讯 | 累计 37433 条内容
关于我们

Shuttle: Small, Type-Safe Composition Primitives for Go

Brooklyn YU 2026年08月25日 23:27 0 次阅读 来源:Dev.to

Sorting a slice by one field is easy. Filtering one collection is easy. Returning (T, bool) is idiomatic. So is writing a nested loop. The friction appears when the same ordering must be shared by a stable sort and an extrema operation, a filter must be reused across several APIs, or a nested traversal grows into four nearly identical loops. At that point, the code is still simple locally, but the semantics are scattered across call sites. Shuttle is an attempt to give those semantics small, typed values. It is not a general-purpose functional programming framework, and it is not a port of Java Stream. Its scope is four focused abstractions: comparators, predicates, optional values, and lazy streams. What Shuttle is Shuttle is one Go module containing four packages: comparator defines Func[T] , a named func(T, T) int for reusable three-way orderings. predicate defines Func[T] , a named func(T) bool with short-circuiting composition. optional defines an eager Optional[T] whose presence bit is independent of the value of T . stream defines a lazy, ordered, sequential Stream[T] over iter.Seq[T] . The types compose through ordinary Go assignability. A predicate.Func[T] can be passed directly to Optional.Filter or Stream.Filter ; a comparator.Func[T] can be passed directly to slices.SortStableFunc , Stream.SortedFunc , or the Stream extrema terminals. The consuming packages do not need to import the descriptor packages to make that work. The module has no third-party runtime dependencies. It deliberately does not include a root shuttle package, an error-carrying stream, parallel operators, I/O sources, or a collectors framework. A realistic nested-data example The repository includes an executable examples/animals program. Its data model contains orders, families, species, subspecies, and animals. The core traversal is a direct adaptation of that example: func animalsFromOrders ( orders [] AnimalOrder ) stream . Stream [ Animal ] { return stream . FromSlice ( orders ) .

本文内容来源于互联网,版权归原作者所有
查看原文