Data Processing

How different languages handle the processing of data through projection and aggregation.

Go Language Map/Reduce


Map

Data projection from a list or array-like data structure.

In Go, mapping a function over a slice requires iteration using for loops or the range keyword.

Example: Mapping a function to square numbers

package main import ( "fmt" ) func square(n int) int { return n * n } func mapSlice(nums []int, fn func(int) int) []int { result := make([]int, len(nums)) for i, num := range nums { result[i] = fn(num) } return result } func main() { numbers := []int{1, 2, 3, 4, 5} squares := mapSlice(numbers, square) fmt.Println(squares) // [1, 4, 9, 16, 25] }

Example: Filtering Even Numbers

package main import "fmt" func filterSlice(nums []int, predicate func(int) bool) []int { result := []int{} for _, num := range nums { if predicate(num) { result = append(result, num) } } return result } func main() { numbers := []int{1, 2, 3, 4, 5} evens := filterSlice(numbers, func(n int) bool { return n%2 == 0 }) fmt.Println(evens) // [2, 4] }

Error Loading

Missing


Reduce

Reduction of a list or array-like data structure into a new aggregate object.

Go does not have a built-in reduce function like Python, but you can implement one using iteration.

Example: Reducing a Slice to Sum All Elements

package main import "fmt" func reduce(nums []int, fn func(int, int) int, initial int) int { result := initial for _, num := range nums { result = fn(result, num) } return result } func main() { numbers := []int{1, 2, 3, 4, 5} sumNumbers := reduce(numbers, func(acc, n int) int { return acc + n }, 0) fmt.Println(sumNumbers) // 15 }

Better Alternative using a Simple Loop

package main import "fmt" func main() { numbers := []int{1, 2, 3, 4, 5} sum := 0 for _, num := range numbers { sum += num } fmt.Println(sum) // 15 }

Missing


Deferred Execution

Execution of operations is delayed until necessary.

Go provides deferred execution using the defer keyword. This is typically used for resource cleanup, ensuring that certain actions (such as closing files or unlocking mutexes) occur before the function exits.

Example: Deferring Execution

package main import "fmt" func main() { fmt.Println("Start") defer fmt.Println("This will be printed last") fmt.Println("Middle") }

Output:

Start Middle This will be printed last

Missing


Deferred Execution with AST

Execution is deferred and represented as an Abstract Syntax Tree (AST).

Go does not have an equivalent to Python’s AST manipulation for execution deferral, but it does provide reflection and closures that allow dynamic execution.

Example: Using Closures for Deferred Execution

package main import ( "fmt" ) func deferredExecution(fn func()) { defer fn() } func main() { deferredExecution(func() { fmt.Println("Deferred function executed") }) fmt.Println("Main function finished") }

Output:

Main function finished Deferred function executed

Missing