-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathdata_pipeline.go
More file actions
124 lines (107 loc) · 3.31 KB
/
Copy pathdata_pipeline.go
File metadata and controls
124 lines (107 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Real-world example: Data processing pipeline with lambdas
// Concise lambda syntax makes functional transformations readable
//
// === Design Decision: Lambda Syntax ===
//
// Dingo supports two lambda styles that compile to Go function literals:
//
// Rust-style: |x| x + 1 → func(x any) any { return x + 1 }
// TypeScript-style: (x) => x + 1 → func(x any) any { return x + 1 }
//
// Types are inferred from context when passed to generic functions.
package main
import (
"fmt"
"strings"
)
type User struct {
ID int
Name string
Email string
Age int
Active bool
Premium bool
}
// ProcessUsers demonstrates a data pipeline with lambdas
func ProcessUsers(users []User) {
// Filter active premium users over 18
// TypeScript-style: (params) => expr
eligible := Filter(users, func(u User) bool { return u.Active && u.Premium && u.Age >= 18 })
// Transform to display format
// Rust-style: |params| expr
names := Map(eligible, func(u User) string { return fmt.Sprintf("%s <%s>", u.Name, u.Email) })
// Multi-line lambda for complex logic
// TypeScript-style with block
summary := Reduce(eligible, "", func(acc string, u User) string {
if acc == "" {
return u.Name
}
return acc + ", " + u.Name
})
fmt.Println("Eligible users:")
for _, name := range names {
fmt.Printf(" - %s\n", name)
}
fmt.Printf("Summary: %s\n", summary)
}
// Higher-order functions that accept lambdas
func Filter[T any](items []T, predicate func(T) bool) []T {
var result []T
for _, item := range items {
if predicate(item) {
result = append(result, item)
}
}
return result
}
func Map[T, R any](items []T, transform func(T) R) []R {
result := make([]R, len(items))
for i, item := range items {
result[i] = transform(item)
}
return result
}
func Reduce[T, R any](items []T, initial R, reducer func(R, T) R) R {
result := initial
for _, item := range items {
result = reducer(result, item)
}
return result
}
// Sorting with custom comparator
func SortUsers(users []User, compare func(User, User) bool) []User {
// Copy to avoid mutating original
sorted := make([]User, len(users))
copy(sorted, users)
// Simple bubble sort for demo
for i := 0; i < len(sorted)-1; i++ {
for j := 0; j < len(sorted)-i-1; j++ {
if compare(sorted[j+1], sorted[j]) {
sorted[j], sorted[j+1] = sorted[j+1], sorted[j]
}
}
}
return sorted
}
func main() {
users := []User{
{ID: 1, Name: "Alice", Email: "alice@example.com", Age: 30, Active: true, Premium: true},
{ID: 2, Name: "Bob", Email: "bob@example.com", Age: 17, Active: true, Premium: true},
{ID: 3, Name: "Charlie", Email: "charlie@example.com", Age: 25, Active: false, Premium: true},
{ID: 4, Name: "Diana", Email: "diana@example.com", Age: 28, Active: true, Premium: false},
{ID: 5, Name: "Eve", Email: "eve@example.com", Age: 35, Active: true, Premium: true},
}
ProcessUsers(users)
// Sort by age (ascending) using lambda
byAge := SortUsers(users, func(a User, b User) bool { return a.Age < b.Age })
fmt.Println("\nUsers by age:")
for _, u := range byAge {
fmt.Printf(" %s (%d)\n", u.Name, u.Age)
}
// Sort by name using lambda
byName := SortUsers(users, func(a User, b User) bool { return strings.ToLower(a.Name) < strings.ToLower(b.Name) })
fmt.Println("\nUsers by name:")
for _, u := range byName {
fmt.Printf(" %s\n", u.Name)
}
}