Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transforming and consuming them.

iter

Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transforming and consuming them.

Usage

package main

import (
  "fmt"
  "strings"

  "mtoohey.com/iter"
)

func main() {
  initial := iter.Elems([]string{"hello", "beautiful", "world"})
  result := initial.Filter(func(s string) bool {
    return len(s) < 6
  }).MapEndo(strings.ToUpper).Collect()
  fmt.Println(result) // produces: [HELLO WORLD]
}

See examples/ for more demonstrations.

Notes

  • It should go without saying that since 1.18 is still beta, so is this library.

Similar Projects

Owner
Matthew Toohey
🏫 2nd Year UofT CS, 🥇 2020 Schulich Leader, 💻 Open Source Contributor, 🐧 Arch BTW
Matthew Toohey
Comments
  • Using custom domain for module path is suspicious

    Using custom domain for module path is suspicious

    I'd love to use this, but as-is I have to vendor it or something otherwise I run the risk of a single developer letting his domain lapse and ruining my application dependencies. Consider changing this.

  • Concurrent Versions of Reduction Operations

    Concurrent Versions of Reduction Operations

    It may be possible to create concurrent versions of many other reduction functions, like we've already done for ForEachCon:

    • [x] Consume d5ce279edc8d8ff5f7c80981aa97c858cd7cd26a
    • [x] Collect and CollectInto cdc8cb11390d8400d16d5a9dae671fe862dc86a8
    • [x] All 0310170a2733583b020ff66c6ca42ce60c176f9c
    • [x] Any 0310170a2733583b020ff66c6ca42ce60c176f9c
    • [x] Count 32ad517d17092210794bb4cb46b7f111bdf0c6a1
    • [ ] Find and variants (will require consuming the whole iterator)
    • [ ] Fold and variants (FoldAC - associative and commutative, since we can parallelize the reduction when we have associativity and commutativity)
    • [ ] Last
    • [ ] TryFold and variants
    • [ ] TryForEach
    • [ ] Reduce (AC variant too, as with Fold)
    • [ ] Position

    Some of the above may not be possible, I haven't thought things through completely. Implementing some of these may also reveal thread safety issues with the implementations of the non-reductive functions, since they haven't been tested concurrently.

  • Create variadic producer to be used in the place of Empty, Once, etc.

    Create variadic producer to be used in the place of Empty, Once, etc.

    This kind of duplicates what Elems does, since this could be used in its place with ... syntax. Also, Elems can already do what this would but it takes more boilerplate to declare the slice literal. Maybe I should just leave things as is, or maybe I should replace Elems, or maybe I should leave both, I'm not sure... replacing it would be a breaking change though...

  • CI and Testing Improvements

    CI and Testing Improvements

    • [ ] Mandate performance or notify about regressions.
    • [x] Mandate test and benchmark code coverage. 5decdec1a5ebde32ea2753ff5f70bf0a0777fadb
    • [x] Mandate 100% godoc coverage. #13
    • [x] Switch to testify instead of using custom asserts. 0bb543a447f53b57c4947dd46f9c9bdef92dcc2a
    • [x] Add/convert existing tests to fuzz tests d6174ab60fd2928454d44f9e0e512aa9b052f702
    • [x] Run CI in a nix devshell
  • Documentation

    Documentation

    • [x] chain.go
    • [x] chan.go
    • [ ] ~~constraints.go~~ (not public)
    • [x] cycle.go
    • [x] filter.go
    • [x] filter_map.go
    • [x] flat_map.go
    • [x] inspect.go
    • [x] iter.go
    • [x] map.go
    • [x] map_while.go
    • [x] ordered_iter.go
    • [x] range.go
    • [x] slice.go
    • [x] step_by.go
    • [x] string.go
    • [x] take.go
    • [x] take_while.go
    • [x] zip.go
  • refactor!: switch to function source

    refactor!: switch to function source

    This pull request refactors the whole library so that Iter[T] is defined as:

    type Iter[T any] func() (T, bool)
    

    This results not only in significantly better performance (a whole 32% better on average!), but also in much simpler code. See this file for exact performance changes, there are a few outliers where it seems there have been regressions, but I'll do what I can to improve those after this is merged.

    TODO After Merging

    • [x] Fix performance regressions
    • [x] Fix codecoverage again and remove commented test code
    • [x] Resolve TODO comments
    • [x] Add acknowledgements of those who suggested these changes to the README 1520efa36323bc0566c7cbda9532fb96605b3d7b

    Closes #9.

  • Add Windows Method

    Add Windows Method

    As seen in: https://doc.rust-lang.org/std/slice/struct.Windows.html, but this should probably be done on the Iter type directly, instead of on the slice to produce an iterator.

  • test: benchmark all the things

    test: benchmark all the things

    This PR adds benchmarks for pretty much everything. There are mupltiple bugs that this PR brings to light, if you examine the benchmark results, but it does not attempt to resolve them, that will be left to future PRs.

  • Remove Range

    Remove Range

    Range is redundant, because it accomplishes the same thing as InfRange combined with Take or TakeWhile depending on the desired result.

    Having three different ranges, with parameters:

    • (start T, step T)
    • (start T)
    • ()

    ...would be more helpful.

  • Improve Test Code Coverage

    Improve Test Code Coverage

    Current coverage can be checked with:

    go1.18beta1 test -coverprofile=/tmp/coverage.out && go1.18beta1 tool cover -html=/tmp/coverage.out
    

    Once the coverage is improved, coverage reports should be added to CI to ensure it stays that way.

Go iter tools (for iterating , mapping, filtering, reducing streams -represented as channels-)

Go iter tools (for iterating , mapping, filtering, reducing streams -represented as channels-)

Jan 1, 2023
Iter tools for Go(Golang)

Iter tools for Go(Golang) The stable version has been moved to : https://github.com/serge-hulne/go_iter Defines: Filter Map Reduce Range Take on colle

Sep 26, 2022
Go implementation of C++ STL iterators and algorithms.

iter Go implementation of C++ STL iterators and algorithms. Less hand-written loops, more expressive code. README translations: 简体中文 Motivation Althou

Dec 19, 2022
Juniper is an extension to the Go standard library using generics, including containers, iterators, and streams.

Juniper Juniper is a library of extensions to the Go standard library using generics, including containers, iterators, and streams. container/tree con

Dec 25, 2022
Custom generic HTTP handler providing automatic JSON decoding/encoding of HTTP request/response to your concrete types

gap Custom generic HTTP handler providing automatic JSON decoding/encoding of HTTP request/response to your concrete types. gap.Wrap allows to use the

Aug 28, 2022
Generic types that are missing from Go, including sets, trees, sorted lists, etc.

go-typ Generic types that are missing from Go, including sets, trees, sorted lists, etc. All code is implemented with 0 dependencies and in pure Go co

Dec 4, 2022
Package set is a small wrapper around the official reflect package that facilitates loose type conversion and assignment into native Go types.

Package set is a small wrapper around the official reflect package that facilitates loose type conversion and assignment into native Go types. Read th

Dec 27, 2022
succinct provides several static succinct data types

succinct provides several static succinct data types Succinct Set Synopsis Performance Implementation License Succinct Set

Jan 5, 2023
Nullable Go types that can be marshalled/unmarshalled to/from JSON.

Nullable Go types Description This package provides nullable Go types for bool, float64, int64, int32, string and time.Time replacing sql.NullString,

Dec 12, 2022
Go strcut based enum, support all types.

go-ultra-enum go-ultra-enum is an enum generator for Go. It is inspired by the powerful enum types found in Java. go-ultra-enum has the following capa

Dec 21, 2021
A generic Go library for implementations of tries (radix trees), state commitments and proofs of inclusion

trie.go Go library for implementations of tries (radix trees), state commitments and proof of inclusion for large data sets. It implements a generic 2

Aug 3, 2022
The first generic linked list in go :dancer:

linkedlist.go The first generic linked list in go ?? gotip first of all you need to install the master version of golang. go install golang.org/dl/got

Dec 7, 2022
Go datastructures for a generic world

Generic data structures for Go With the upcoming release of 1.18, it's now possible to build a library of generic data structures in Go. The purpose o

Nov 9, 2021
A slice backed binary heap with support for generic type parameters.

go-binaryheap A slice backed binary heap where the order can be customized by a comparison function. The main branch now requires go 1.18 because the

Jun 13, 2022
Go library for encoding native Go structures into generic map values.

wstructs origin: github.com/things-go/structs Go library for encoding native Go structures into generic map values. Installation Use go get. go ge

Jan 10, 2022
This repo is where I'll be attempting to capture some generic algorithms written in Go

Go Generic Algorithms Welcome friends! ?? This repo is where I'll be attempting

Apr 13, 2022
Library of generic data structures for Go.

gods Library of generic data structures for Go. priority queue sorted list priority queue unsorted list priority queue heap priority queue adaptable h

Dec 26, 2022
Package ring provides a high performance and thread safe Go implementation of a bloom filter.

ring - high performance bloom filter Package ring provides a high performance and thread safe Go implementation of a bloom filter. Usage Please see th

Nov 20, 2022