1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-11-23 22:14:53 +02:00
Files
fp-go/v2/io/traverse.go
Carsten Leue 3385c705dc Implement v2 using type aliases (#141)
* fix: initial checkin of v2

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: slowly migrate IO

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: migrate MonadTraverseArray and TraverseArray

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: migrate traversal

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: complete migration of IO

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: migrate ioeither

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: refactorY

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: next step in migration

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: adjust IO generation code

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: get rid of more IO methods

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: get rid of more IO

* fix: convert iooption

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: convert reader

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: convert a bit of reader

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: new build script

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: cleanup

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: reformat

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: simplify

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: some cleanup

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: adjust Pair to Haskell semantic

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: documentation and testcases

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: some performance optimizations

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: remove coverage

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: better doc

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

---------

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2025-11-06 09:27:00 +01:00

241 lines
7.4 KiB
Go

// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package io
import (
F "github.com/IBM/fp-go/v2/function"
INTA "github.com/IBM/fp-go/v2/internal/array"
INTR "github.com/IBM/fp-go/v2/internal/record"
)
// MonadTraverseArray applies an IO-returning function to each element of an array
// and collects the results into an IO of an array. Executes in parallel by default.
//
// Example:
//
// fetchUsers := func(id int) io.IO[User] { return fetchUser(id) }
// users := io.MonadTraverseArray([]int{1, 2, 3}, fetchUsers)
// result := users() // []User with all fetched users
func MonadTraverseArray[A, B any](tas []A, f func(A) IO[B]) IO[[]B] {
return INTA.MonadTraverse(
Of[[]B],
Map[[]B, func(B) []B],
Ap[[]B, B],
tas,
f,
)
}
// TraverseArray returns a function that applies an IO-returning function to each element
// of an array and collects the results. This is the curried version of MonadTraverseArray.
// Executes in parallel by default.
//
// Example:
//
// fetchUsers := io.TraverseArray(func(id int) io.IO[User] {
// return fetchUser(id)
// })
// users := fetchUsers([]int{1, 2, 3})
func TraverseArray[A, B any](f func(A) IO[B]) func([]A) IO[[]B] {
return INTA.Traverse[[]A](
Of[[]B],
Map[[]B, func(B) []B],
Ap[[]B, B],
f,
)
}
// TraverseArrayWithIndex is like TraverseArray but the function also receives the index.
// Executes in parallel by default.
//
// Example:
//
// numbered := io.TraverseArrayWithIndex(func(i int, s string) io.IO[string] {
// return io.Of(fmt.Sprintf("%d: %s", i, s))
// })
func TraverseArrayWithIndex[A, B any](f func(int, A) IO[B]) func([]A) IO[[]B] {
return INTA.TraverseWithIndex[[]A](
Of[[]B],
Map[[]B, func(B) []B],
Ap[[]B, B],
f,
)
}
// SequenceArray converts an array of IO computations into an IO of an array of results.
// All computations are executed in parallel by default.
//
// Example:
//
// operations := []io.IO[int]{fetchA(), fetchB(), fetchC()}
// results := io.SequenceArray(operations)
// values := results() // []int with all results
func SequenceArray[A any](tas []IO[A]) IO[[]A] {
return MonadTraverseArray(tas, F.Identity[IO[A]])
}
// MonadTraverseRecord applies an IO-returning function to each value in a map
// and collects the results into an IO of a map. Executes in parallel by default.
//
// Example:
//
// fetchData := func(url string) io.IO[Data] { return fetch(url) }
// urls := map[string]string{"a": "http://a.com", "b": "http://b.com"}
// data := io.MonadTraverseRecord(urls, fetchData)
func MonadTraverseRecord[K comparable, A, B any](tas map[K]A, f func(A) IO[B]) IO[map[K]B] {
return INTR.MonadTraverse(
Of[map[K]B],
Map[map[K]B, func(B) map[K]B],
Ap[map[K]B, B],
tas,
f,
)
}
// TraverseRecord returns a function that applies an IO-returning function to each value
// in a map and collects the results. This is the curried version of MonadTraverseRecord.
// Executes in parallel by default.
func TraverseRecord[K comparable, A, B any](f func(A) IO[B]) func(map[K]A) IO[map[K]B] {
return INTR.Traverse[map[K]A](
Of[map[K]B],
Map[map[K]B, func(B) map[K]B],
Ap[map[K]B, B],
f,
)
}
// TraverseRecordWithIndex is like TraverseRecord but the function also receives the key.
// Executes in parallel by default.
func TraverseRecordWithIndex[K comparable, A, B any](f func(K, A) IO[B]) func(map[K]A) IO[map[K]B] {
return INTR.TraverseWithIndex[map[K]A](
Of[map[K]B],
Map[map[K]B, func(B) map[K]B],
Ap[map[K]B, B],
f,
)
}
// SequenceRecord converts a map of IO computations into an IO of a map of results.
// All computations are executed in parallel by default.
//
// Example:
//
// operations := map[string]io.IO[int]{"a": fetchA(), "b": fetchB()}
// results := io.SequenceRecord(operations)
// values := results() // map[string]int with all results
func SequenceRecord[K comparable, A any](tas map[K]IO[A]) IO[map[K]A] {
return MonadTraverseRecord(tas, F.Identity[IO[A]])
}
// MonadTraverseArraySeq applies an IO-returning function to each element of an array
// and collects the results into an IO of an array. Executes sequentially (one after another).
//
// Example:
//
// fetchUsers := func(id int) io.IO[User] { return fetchUser(id) }
// users := io.MonadTraverseArraySeq([]int{1, 2, 3}, fetchUsers)
func MonadTraverseArraySeq[A, B any](tas []A, f func(A) IO[B]) IO[[]B] {
return INTA.MonadTraverse(
Of[[]B],
Map[[]B, func(B) []B],
ApSeq[[]B, B],
tas,
f,
)
}
// TraverseArraySeq returns a function that applies an IO-returning function to each element
// of an array and collects the results. Executes sequentially (one after another).
// Use this when operations must be performed in order or when parallel execution is not desired.
func TraverseArraySeq[A, B any](f func(A) IO[B]) func([]A) IO[[]B] {
return INTA.Traverse[[]A](
Of[[]B],
Map[[]B, func(B) []B],
ApSeq[[]B, B],
f,
)
}
// TraverseArrayWithIndexSeq is like TraverseArraySeq but the function also receives the index.
// Executes sequentially (one after another).
func TraverseArrayWithIndexSeq[A, B any](f func(int, A) IO[B]) func([]A) IO[[]B] {
return INTA.TraverseWithIndex[[]A](
Of[[]B],
Map[[]B, func(B) []B],
ApSeq[[]B, B],
f,
)
}
// SequenceArraySeq converts an array of IO computations into an IO of an array of results.
// All computations are executed sequentially (one after another).
func SequenceArraySeq[A any](tas []IO[A]) IO[[]A] {
return MonadTraverseArraySeq(tas, F.Identity[IO[A]])
}
// MonadTraverseRecordSeq applies an IO-returning function to each value in a map
// and collects the results into an IO of a map. Executes sequentially.
func MonadTraverseRecordSeq[K comparable, A, B any](tas map[K]A, f func(A) IO[B]) IO[map[K]B] {
return INTR.MonadTraverse(
Of[map[K]B],
Map[map[K]B, func(B) map[K]B],
ApSeq[map[K]B, B],
tas,
f,
)
}
// TraverseRecordSeq returns a function that applies an IO-returning function to each value
// in a map and collects the results. Executes sequentially (one after another).
func TraverseRecordSeq[K comparable, A, B any](f func(A) IO[B]) func(map[K]A) IO[map[K]B] {
return INTR.Traverse[map[K]A](
Of[map[K]B],
Map[map[K]B, func(B) map[K]B],
ApSeq[map[K]B, B],
f,
)
}
// TraverseRecordWithIndeSeq is like TraverseRecordSeq but the function also receives the key.
// Executes sequentially (one after another).
// Note: There's a typo in the function name (Inde instead of Index) for backward compatibility.
func TraverseRecordWithIndeSeq[K comparable, A, B any](f func(K, A) IO[B]) func(map[K]A) IO[map[K]B] {
return INTR.TraverseWithIndex[map[K]A](
Of[map[K]B],
Map[map[K]B, func(B) map[K]B],
ApSeq[map[K]B, B],
f,
)
}
// SequenceRecordSeq converts a map of IO computations into an IO of a map of results.
// All computations are executed sequentially (one after another).
func SequenceRecordSeq[K comparable, A any](tas map[K]IO[A]) IO[map[K]A] {
return MonadTraverseRecordSeq(tas, F.Identity[IO[A]])
}