All Projects → disksing → Iter

disksing / Iter

Licence: bsd-3-clause
Go implementation of C++ STL iterators and algorithms.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Iter

Competitive Programming Resources
This repository consists of data helpful for ACM ICPC programming contest, in general competitive programming.
Stars: ✭ 199 (+50.76%)
Mutual labels:  algorithms, datastructures, stl
Awesome Java Leetcode
👑 LeetCode of algorithms with java solution(updating).
Stars: ✭ 8,297 (+6185.61%)
Mutual labels:  algorithms, datastructures
Competitive Programming
Repository of all my submissions to some competitive programming website (Online Judges), as well as, the implementation of some data structures and algorithms.
Stars: ✭ 53 (-59.85%)
Mutual labels:  algorithms, datastructures
Algorithms
University course material for Algorithms and Data Structures in Java, with a particular emphasis on software testing. Includes exercises, with solutions.
Stars: ✭ 66 (-50%)
Mutual labels:  algorithms, datastructures
Phpalgorithms
A collection of common algorithms implemented in PHP. The collection is based on "Cracking the Coding Interview" by Gayle Laakmann McDowell
Stars: ✭ 865 (+555.3%)
Mutual labels:  algorithms, datastructures
Algorithms
Here is the my solutions for problems in {leetcode, hackerrank, geeksforgeeks}
Stars: ✭ 36 (-72.73%)
Mutual labels:  algorithms, datastructures
Datastructures
🚀 Implementation of core data structures for R
Stars: ✭ 64 (-51.52%)
Mutual labels:  algorithms, datastructures
Cdsa
A library of generic intrusive data structures and algorithms in ANSI C
Stars: ✭ 549 (+315.91%)
Mutual labels:  algorithms, datastructures
Burst
То, чего нет в Бусте
Stars: ✭ 72 (-45.45%)
Mutual labels:  algorithms, stl
C
Collection of various algorithms in mathematics, machine learning, computer science, physics, etc implemented in C for educational purposes.
Stars: ✭ 11,897 (+8912.88%)
Mutual labels:  algorithms, datastructures
Data Structures And Algorithms
Python implementation of common algorithms and data structures interview questions
Stars: ✭ 84 (-36.36%)
Mutual labels:  algorithms, datastructures
30 Seconds Of Cpp
30 Seconds of C++ (STL in C++). Read More about 30C++ here 👉
Stars: ✭ 815 (+517.42%)
Mutual labels:  algorithms, stl
Dsjslib
A library implementing several standard data structures and utilities, in JavaScript. Its written and tested using Node.js which is the target platform.
Stars: ✭ 707 (+435.61%)
Mutual labels:  algorithms, datastructures
Data Structure And Algorithms
A complete and efficient guide for Data Structure and Algorithms.
Stars: ✭ 48 (-63.64%)
Mutual labels:  algorithms, datastructures
Go
Algorithms Implemented in GoLang
Stars: ✭ 7,385 (+5494.7%)
Mutual labels:  algorithms, datastructures
Complete Placement Preparation
This repository consists of all the material required for cracking the coding rounds and technical interviews during placements.
Stars: ✭ 1,114 (+743.94%)
Mutual labels:  algorithms, datastructures
Cracking The Coding Interview
Tests, Questions and Solutions from Cracking the Coding Interview
Stars: ✭ 91 (-31.06%)
Mutual labels:  algorithms, datastructures
Competitive Programming
📌 📚 Solution of competitive programming problems, code templates, Data Structures and Algorithms, hackathons, interviews and much more.
Stars: ✭ 496 (+275.76%)
Mutual labels:  algorithms, datastructures
Algorithms And Data Structures In Java
Algorithms and Data Structures in Java
Stars: ✭ 498 (+277.27%)
Mutual labels:  algorithms, datastructures
Coding Cheat Sheets
Various cheat sheets on CS stuff
Stars: ✭ 1,172 (+787.88%)
Mutual labels:  algorithms, datastructures

iter

Go implementation of C++ STL iterators and algorithms.

Less hand-written loops, more expressive code.

README translations: 简体中文

GoDoc Build Status codecov Go Report Card

Motivation

Although Go doesn't have generics, we deserve to have reuseable general algorithms. iter helps improving Go code in several ways:

  • Some simple loops are unlikely to be wrong or inefficient, but calling algorithm instead will make the code more concise and easier to comprehend. Such as AllOf, FindIf, Accumulate.

  • Some algorithms are not complicated, but it is not easy to write them correctly. Reusing code makes them easier to reason for correctness. Such as Shuffle, Sample, Partition.

  • STL also includes some complicated algorithms that may take hours to make it correct. Implementing it manually is impractical. Such as NthElement, StablePartition, NextPermutation.

  • The implementation in the library contains some imperceptible performance optimizations. For instance, MinmaxElement is done by taking two elements at a time. In this way, the overall number of comparisons is significantly reduced.

There are alternative libraries have similar goals, such as gostl, gods and go-stp. What makes iter unique is:

  • Non-intrusive. Instead of introducing new containers, iter tends to reuse existed containers in Go (slice, string, list.List, etc.) and use iterators to adapt them to algorithms.

  • Full algorithms (>100). It includes almost all algorithms come before C++17. Check the Full List.

Examples

The examples are run with some function alias to make it simple. See example_test.go for the detail.

Print a list.List
l := list.New()
for i := 1; i <= 5; i++ {
  l.PushBack(i)
}
for e := l.Front(); e != nil; e = e.Next() {
  fmt.Print(e.Value)
  if e.Next() != nil {
    fmt.Print("->")
  }
}
// Output:
// 1->2->3->4->5
l := list.New()
GenerateN(ListBackInserter(l), 5, IotaGenerator(1))
Copy(lBegin(l), lEnd(l), IOWriter(os.Stdout, "->"))
// Output:
// 1->2->3->4->5
Reverse a string
s := "!dlrow olleH"
var sb strings.Builder
for i := len(s) - 1; i >= 0; i-- {
  sb.WriteByte(s[i])
}
fmt.Println(sb.String())

b := []byte(s)
for i := len(s)/2 - 1; i >= 0; i-- {
  j := len(s) - 1 - i
  b[i], b[j] = b[j], b[i]
}
fmt.Println(string(b))
// Output:
// Hello world!
// Hello world!
s := "!dlrow olleH"
fmt.Println(MakeString(StringRBegin(s), StringREnd(s)))

b := []byte(s)
Reverse(begin(b), end(b))
fmt.Println(string(b))
// Output:
// Hello world!
// Hello world!
In-place deduplicate (from SliceTricks, with minor change)
in := []int{3, 2, 1, 4, 3, 2, 1, 4, 1}
sort.Ints(in)
j := 0
for i := 1; i < len(in); i++ {
  if in[j] == in[i] {
    continue
  }
  j++
  in[j] = in[i]
}
in = in[:j+1]
fmt.Println(in)
// Output:
// [1 2 3 4]
in := []int{3, 2, 1, 4, 3, 2, 1, 4, 1}
Sort(begin(in), end(in))
Erase(&in, Unique(begin(in), end(in)))
fmt.Println(in)
// Output:
// [1 2 3 4]
Sum all integers received from a channel
ch := make(chan int)
go func() {
  for _, x := range rand.Perm(100) {
    ch <- x + 1
  }
  close(ch)
}()
var sum int
for x := range ch {
  sum += x
}
fmt.Println(sum)
// Output:
// 5050
ch := make(chan int)
go func() {
  CopyN(IotaReader(1), 100, ChanWriter(ch))
  close(ch)
}()
fmt.Println(Accumulate(ChanReader(ch), ChanEOF, 0))
// Output:
// 5050
Remove consecutive spaces in a string
str := "  a  quick   brown  fox  "
var sb strings.Builder
var prevIsSpace bool
for i := 0; i < len(str); i++ {
  if str[i] != ' ' || !prevIsSpace {
    sb.WriteByte(str[i])
  }
  prevIsSpace = str[i] == ' '
}
fmt.Println(sb.String())
// Output:
// a quick brown fox
str := "  a  quick   brown  fox  "
var sb StringBuilderInserter
UniqueCopyIf(sBegin(str), sEnd(str), &sb,
  func(x, y Any) bool { return x.(byte) == ' ' && y.(byte) == ' ' })
fmt.Println(sb.String())
// Output:
// a quick brown fox
Collect N maximum elements from a channel
// Need to manually mantain a min-heap.
top := make([]int, 5)
PartialSortCopyBy(ChanReader(ch), ChanEOF, begin(top), end(top),
  func(x, y Any) bool { return x.(int) > y.(int) })
Copy(begin(top), end(top), IOWriter(os.Stdout, ", "))
Print all permutations of ["a", "b", "c"]
// Usually requires some sort of recursion
s := []string{"a", "b", "c"}
for ok := true; ok; ok = NextPermutation(begin(s), end(s)) {
  fmt.Println(s)
}
// Output:
// [a b c]
// [a c b]
// [b a c]
// [b c a]
// [c a b]
// [c b a]

Thanks

License

BSD 3-Clause

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].