Skip to content

mkch/iter2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Apr 22, 2025
0a8d322 · Apr 22, 2025

History

18 Commits
Aug 27, 2024
Aug 27, 2024
Aug 26, 2024
Aug 26, 2024
Aug 26, 2024
Aug 27, 2024
Feb 7, 2025
Aug 26, 2024
Aug 26, 2024
Apr 22, 2025
Feb 7, 2025
Apr 22, 2025
Aug 27, 2024
Aug 27, 2024

Repository files navigation

iter2

Go iter utilities.

  1. Operations of iterator:

    Zipping two Seqs to one Seq of pairs:

    func ExampleZip() {
        ks := []int{1, 2, 3}
        vs := []string{"one", "two", "three"}
    
        type pair struct {
            N   int
            Str string
        }
    
        s := slices.Collect(iter2.Zip(slices.Values(ks), slices.Values(vs), func(i int, s string) pair { return pair{i, s} }))
        fmt.Println(s)
        // Output: [{1 one} {2 two} {3 three}]
    }

    Zipping tow Seqs to one Seq2:

    func ExampleZip2() {
        ks := []int{1, 2, 3}
        vs := []string{"one", "two", "three"}
        zipped := iter2.Zip2(slices.Values(ks), slices.Values(vs))
        m := maps.Collect(zipped)
        for k, v := range m {
            fmt.Println(k, v)
        }
        // Unordered output:
        // 1 one
        // 2 two
        // 3 three
    }

    Concating 2 Seqs:

    func ExampleConcat() {
        seq1 := slices.Values([]int{1, 2, 3})
        seq2 := slices.Values([]int{4, 5})
        seq := iter2.Concat(seq1, seq2)
        fmt.Println(slices.Collect(seq))
        // Output:
        // [1 2 3 4 5]
    }
  2. Map DB rows:

    import (
        "database/sql"
        _ "modernc.org/sqlite"
        // and more
    )
    
    func ExampleAllRows_tableStruct() {
        db, err := sql.Open("sqlite", ":memory:")
        if err != nil {
            panic(err)
        }
        q := `
    create temp table users (id integer, name text); -- Create temp table for queries.
    insert into users values (1, "User1"); -- Populate temp table.
    insert into users values (2, "User2");
    insert into users values (3, "User3");
    
    -- First result set.
    select * from users;
    `
        type User struct {
            ID   int
            Name string
        }
    
        users := slices.Collect(
            iter2.Map(
                iter2.MustAllRows(db.Query(q)), func(row iter2.Row) (user User) {
                    row.Scan(&user.ID, &user.Name)
                    return
                }))
        fmt.Println(users)
        // Should output:
        // [{1 User1} {2 User2} {3 User3}]
    }

More examples here.

About

Go iter utilities.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages