Skip to content

Commit

Permalink
comments in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
KSpaceer committed Jan 1, 2024
1 parent 85ab3f9 commit 71d0649
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
8 changes: 6 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func ExampleNew() {
arr := [5]int{5, 10, 15, 20, 25}
var idx int

// function to manually iterator over slice.
// It is more comfortable to use itertools.NewSliceIterator in this case
f := func() (int, bool) {
if idx >= len(arr) {
return 0, false
Expand Down Expand Up @@ -170,6 +172,7 @@ func ExampleIterator_Collect() {
s := []string{"First", "Second", "Third"}
idx := len(s) - 1

// function to iterate over slice in reverse order
f := func() (string, bool) {
if idx < 0 {
return "", false
Expand Down Expand Up @@ -435,8 +438,8 @@ func ExampleEnumerate() {
iter := itertools.Enumerate(itertools.NewSliceIterator(people))

for iter.Next() {
elem := iter.Elem()
fmt.Printf("Index: %d ||| Name: %s ||| Age: %d\n", elem.Second, elem.First.Name, elem.First.Age)
person, i := iter.Elem().Unpack()
fmt.Printf("Index: %d ||| Name: %s ||| Age: %d\n", i, person.Name, person.Age)
}
// Output:
// Index: 0 ||| Name: Bob ||| Age: 31
Expand Down Expand Up @@ -566,6 +569,7 @@ func ExampleNewMapIterator() {
return slices.Contains(summerMonths, p.Second)
})

// iterating over map keys (months numbers) rather than entire key-value pairs
numbersIter := itertools.Map(iter, func(p itertools.Pair[int, string]) int {
return p.First
})
Expand Down
10 changes: 10 additions & 0 deletions mapfilter_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import (
"time"
)

// imitating account name or something
type Account string

// stub representation of financial transaction
type Transaction struct {
From Account
To Account
Amount int
Timestamp time.Time
}

// stub representation of transaction message from message queue
type Message struct {
FromTo string
Amount int
Expand All @@ -31,6 +34,7 @@ func messageToTransaction(msg Message) Transaction {
return tx
}

// mock fraud checker for financial transactions
type FraudChecker struct {
definitelyFraudulent Account
}
Expand All @@ -39,6 +43,7 @@ func (fc *FraudChecker) IsFraudulentTransaction(tx Transaction) bool {
return tx.To == fc.definitelyFraudulent || tx.From == fc.definitelyFraudulent
}

// mock fraud transaction alerter
type Alerter struct{}

func (Alerter) Alert(tx Transaction) {
Expand All @@ -50,8 +55,10 @@ func (Alerter) Alert(tx Transaction) {
fmt.Println(sb.String())
}

// mock consumer of message queue/broker
type MessageConsumer []Message

// imitating process of message consumption
func (mc *MessageConsumer) StartConsume() <-chan Message {
messages := make([]Message, len(*mc))
copy(messages, *mc)
Expand Down Expand Up @@ -110,8 +117,11 @@ func Example_mapFilter() {
alerter := Alerter{}

iter := itertools.Map(
// iterating over messages
itertools.NewChanIterator(ch),
// mapping them to transactions
messageToTransaction,
// keeping only fraudulent ones
).Filter(fraudChecker.IsFraudulentTransaction)

iter.Range(func(tx Transaction) bool {
Expand Down
3 changes: 3 additions & 0 deletions mapreduce_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ func Example_mapReduce() {

stddev := itertools.
Map(
// iterating over data
itertools.NewSliceIterator(data),
// transforming data from int to float64
func(n int) float64 { return float64(n) },
).
// calculating standard deviation of data
Reduce(0, func(acc float64, elem float64) float64 {
return acc + (elem-avg)*(elem-avg)
})
Expand Down
3 changes: 3 additions & 0 deletions set_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ func Example_set() {
const batchSize = 2500

iter := itertools.
// batching incoming values into batches with size batchSize (2500)
Batched(itertools.NewMapKeysIterator(values).
// keeping only numbers divisible by 4
Filter(func(n int) bool { return n%4 == 0 }),
batchSize)

Expand All @@ -40,6 +42,7 @@ func Example_set() {
}

func process(values []int) {
// long processing imitation
time.Sleep(time.Duration(len(values)) * time.Microsecond)
fmt.Printf("processed %d items\n", len(values))
}

0 comments on commit 71d0649

Please sign in to comment.