Iterators, Map, Filter, Reduce and list processing in Go (Golang) : implementing Python functional features.

Mapping in Go
go get github.com/serge-hulne/go_iter

Intro: List and Lists processing in Python (quick refresher)

looping
processing_text.png

How can I do that using Go?

Example 1 : Generator

anonymous_function_calling_itself.png
go func() {}
ch1 := make(chan int)
...
for item := range ch1 {
...
}

Example 2 : A fibonacci numbers generator:

Fibonacci numbers generator.
f := Fib(10)Fibonnaci:i =      0i =      1i =      1i =      2i =      3i =      5i =      8i =     13i =     21i =     34i =     55i =     89

Example 3: filtering a list of fibonacci numbers:

filtering fibonacci numbers.
Filter(fibs) // returns a list of even Fibonacci numbers.// it returns:Filter:i =      0i =      2i =      8i =     34
Filter implementation.

Example 4: mapping a list of fibonacci numbers:

Mapping in Go
Map(Filter(sequence))
f := Filter(initial_sequence)
m := Map(f)
// m is now the same as : m := Map(Filter(sequence))

Example 5: Reduce:

map filter reduce : chained ex 1

Example 6: Enumerate:

Enumerate : Use.
Enumerateindex :     0,  value =      0index :     1,  value =      1index :     2,  value =      1index :     3,  value =      2index :     4,  value =      3index :     5,  value =      5index :     6,  value =      8index :     7,  value =     13index :     8,  value =     21index :     9,  value =     34index :    10,  value =     55index :    11,  value =     89

Example 7 : Taking the N first results (Take(n)):

Tale : implementation
Rangeindex =      0, squared =      0index =      1, squared =      1index =      1, squared =      1index =      2, squared =      4index =      3, squared =      9index =      5, squared =     25index =      8, squared =     64
Take(10)

Conclusion:

  • Python.
  • TypeScript.
  • JavaScript.
  • Rust.
  • Dart.
  • Kotlin.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store