Thanks to visit codestin.com
Credit goes to github.com

Skip to content

suryanshu-09/hof

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Higher Order Functions in Go 1.23+ using Iterators and Generics

A checklist of JavaScript-style higher-order functions implemented in Go.

(Added Square[E Number]([]E) iter.Seq[E] and Cube[E Number]([]E) iter.Seq[E])


Core Array Methods

  • Map[E, T]([]E, func(E) T) iter.Seq[T] — Transform each element
  • Filter[E]([]E, func(E) bool) iter.Seq[E] — Keep elements that satisfy a condition
  • Reduce[T, U]([]T, func(U, T) U, init U) U — Accumulate values into one
  • ForEach[E]([]E, func(E)) — Apply side-effects (printing, logging, etc.)
  • Find[E]([]E, func(E) bool) (E, bool) — Return first element satisfying condition
  • Some[E]([]E, func(E) bool) bool — Return true if any element matches
  • Every[E]([]E, func(E) bool) bool — Return true if all elements match

Numeric Helpers

  • Square[E Number]([]E) iter.Seq[E] — Find the square of each element
  • Cube[E Number]([]E) iter.Seq[E] — Find the cube of each element
  • Sum[E Number]([]E) E — Add all numbers
  • Average[E Number]([]E) float64 — Compute mean
  • Max[E Number]([]E) E — Find maximum value
  • Min[E Number]([]T) E — Find minimum value

Collection Utilities

  • GroupBy[T, K comparable]([]T, func(T) K) map[K][]T — Cluster elements by key
  • Partition[T]([]T, func(T) bool) ([]T, []T) — Split into matching/non-matching
  • Unique[T comparable]([]T) []T — Remove duplicates
  • Zip[A, B]([]A, []B) [][2]any — Combine two slices
  • Unzip[A, B]([][2]any) ([]A, []B) — Split pairs
  • FlatMap[T, U]([]T, func(T) []U) []U — Map + flatten in one step
  • Chunk[T]([]T, size int) [][]T — Split slice into groups

Functional Composition

  • Compose[A, B, C](f func(B) C, g func(A) B) func(A) C — Compose functions (right-to-left)
  • Pipe[A, B, C](f func(A) B, g func(B) C) func(A) C — Compose functions (left-to-right)
  • Curry patterns using closures — Turn multi-arg func into chain of funcs

About

Higher Order Functions in Golang using Iterators and Generics

Resources

License

Stars

Watchers

Forks

Packages

No packages published