mirror of
https://github.com/IBM/fp-go.git
synced 2025-11-23 22:14:53 +02:00
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>
This commit is contained in:
77
v2/array/zip.go
Normal file
77
v2/array/zip.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// 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 array
|
||||
|
||||
import (
|
||||
G "github.com/IBM/fp-go/v2/array/generic"
|
||||
T "github.com/IBM/fp-go/v2/tuple"
|
||||
)
|
||||
|
||||
// ZipWith applies a function to pairs of elements at the same index in two arrays,
|
||||
// collecting the results in a new array. If one input array is shorter, excess elements
|
||||
// of the longer array are discarded.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// names := []string{"Alice", "Bob", "Charlie"}
|
||||
// ages := []int{30, 25, 35}
|
||||
//
|
||||
// result := array.ZipWith(names, ages, func(name string, age int) string {
|
||||
// return fmt.Sprintf("%s is %d years old", name, age)
|
||||
// })
|
||||
// // Result: ["Alice is 30 years old", "Bob is 25 years old", "Charlie is 35 years old"]
|
||||
func ZipWith[FCT ~func(A, B) C, A, B, C any](fa []A, fb []B, f FCT) []C {
|
||||
return G.ZipWith[[]A, []B, []C, FCT](fa, fb, f)
|
||||
}
|
||||
|
||||
// Zip takes two arrays and returns an array of corresponding pairs (tuples).
|
||||
// If one input array is shorter, excess elements of the longer array are discarded.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// names := []string{"Alice", "Bob", "Charlie"}
|
||||
// ages := []int{30, 25, 35}
|
||||
//
|
||||
// pairs := array.Zip(ages)(names)
|
||||
// // Result: [(Alice, 30), (Bob, 25), (Charlie, 35)]
|
||||
//
|
||||
// // With different lengths
|
||||
// pairs2 := array.Zip([]int{1, 2})([]string{"a", "b", "c"})
|
||||
// // Result: [(a, 1), (b, 2)]
|
||||
func Zip[A, B any](fb []B) func([]A) []T.Tuple2[A, B] {
|
||||
return G.Zip[[]A, []B, []T.Tuple2[A, B]](fb)
|
||||
}
|
||||
|
||||
// Unzip is the reverse of Zip. It takes an array of pairs (tuples) and returns
|
||||
// two corresponding arrays, one containing all first elements and one containing all second elements.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import "github.com/IBM/fp-go/v2/tuple"
|
||||
//
|
||||
// pairs := []tuple.Tuple2[string, int]{
|
||||
// tuple.MakeTuple2("Alice", 30),
|
||||
// tuple.MakeTuple2("Bob", 25),
|
||||
// tuple.MakeTuple2("Charlie", 35),
|
||||
// }
|
||||
//
|
||||
// result := array.Unzip(pairs)
|
||||
// // Result: (["Alice", "Bob", "Charlie"], [30, 25, 35])
|
||||
// names := result.Head // ["Alice", "Bob", "Charlie"]
|
||||
// ages := result.Tail // [30, 25, 35]
|
||||
func Unzip[A, B any](cs []T.Tuple2[A, B]) T.Tuple2[[]A, []B] {
|
||||
return G.Unzip[[]A, []B, []T.Tuple2[A, B]](cs)
|
||||
}
|
||||
Reference in New Issue
Block a user