1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-12-21 23:47:34 +02:00
Files
fp-go/v2/tuple/examples_test.go
Dr. Carsten Leue 4ebfcadabe fix: add better tests
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2025-12-16 14:03:01 +01:00

74 lines
1.6 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 tuple_test
import (
"fmt"
"github.com/IBM/fp-go/v2/tuple"
)
func ExampleOf() {
t := tuple.Of(42)
fmt.Println(t)
// Output:
// Tuple1[int](42)
}
func ExampleFirst() {
t := tuple.MakeTuple2("hello", 42)
s := tuple.First(t)
fmt.Println(s)
// Output:
// hello
}
func ExampleSecond() {
t := tuple.MakeTuple2("hello", 42)
n := tuple.Second(t)
fmt.Println(n)
// Output:
// 42
}
func ExampleSwap() {
t := tuple.MakeTuple2("hello", 42)
swapped := tuple.Swap(t)
fmt.Println(swapped)
// Output:
// Tuple2[int, string](42, hello)
}
func ExampleOf2() {
pairWith42 := tuple.Of2[string](42)
t := pairWith42("hello")
fmt.Println(t)
// Output:
// Tuple2[string, int](hello, 42)
}
func ExampleBiMap() {
t := tuple.MakeTuple2(5, "hello")
mapper := tuple.BiMap(
func(s string) int { return len(s) },
func(n int) string { return fmt.Sprintf("%d", n*2) },
)
result := mapper(t)
fmt.Println(result)
// Output:
// Tuple2[string, int](10, 5)
}