From 94bcfde0d3cd161590440bc99e656918dcbe0386 Mon Sep 17 00:00:00 2001 From: "Dr. Carsten Leue" Date: Fri, 28 Jul 2023 22:50:52 +0200 Subject: [PATCH] fix: add missing array traversal to either Signed-off-by: Dr. Carsten Leue --- array/array.go | 8 +++++ array/generic/array.go | 18 +++++++++++ context/readerioeither/gen.go | 2 +- context/readerioeither/generic/gen.go | 2 +- either/array.go | 46 +++++++++++++++++++++++++++ either/gen.go | 2 +- function/binds.go | 2 +- function/gen.go | 2 +- identity/gen.go | 2 +- internal/apply/gen.go | 2 +- io/gen.go | 2 +- io/generic/gen.go | 2 +- ioeither/gen.go | 2 +- ioeither/generic/gen.go | 2 +- iooption/gen.go | 2 +- iooption/generic/gen.go | 2 +- option/gen.go | 2 +- reader/gen.go | 2 +- reader/generic/gen.go | 2 +- readerioeither/gen.go | 2 +- readerioeither/generic/gen.go | 2 +- tuple/gen.go | 2 +- 22 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 either/array.go diff --git a/array/array.go b/array/array.go index e019298..3ac7f07 100644 --- a/array/array.go +++ b/array/array.go @@ -280,3 +280,11 @@ func IsNonNil[A any](as []A) bool { func ConstNil[A any]() []A { return array.ConstNil[[]A]() } + +func SliceRight[A any](start int) func([]A) []A { + return G.SliceRight[[]A](start) +} + +func Copy[A any](b []A) []A { + return G.Copy(b) +} diff --git a/array/generic/array.go b/array/generic/array.go index bd57abe..b704169 100644 --- a/array/generic/array.go +++ b/array/generic/array.go @@ -180,3 +180,21 @@ func MatchLeft[AS ~[]A, A, B any](onEmpty func() B, onNonEmpty func(A, AS) B) fu return onNonEmpty(as[0], as[1:]) } } + +func Slice[AS ~[]A, A any](start int, end int) func(AS) AS { + return func(a AS) AS { + return a[start:end] + } +} + +func SliceRight[AS ~[]A, A any](start int) func(AS) AS { + return func(a AS) AS { + return a[start:] + } +} + +func Copy[AS ~[]A, A any](b AS) AS { + buf := make(AS, len(b)) + copy(buf, b) + return buf +} diff --git a/context/readerioeither/gen.go b/context/readerioeither/gen.go index 9fce20d..0c66126 100644 --- a/context/readerioeither/gen.go +++ b/context/readerioeither/gen.go @@ -17,7 +17,7 @@ package readerioeither // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:42:22.8252325 +0200 CEST m=+0.010212601 +// 2023-07-28 22:48:20.012425 +0200 CEST m=+0.019517901 import ( "context" diff --git a/context/readerioeither/generic/gen.go b/context/readerioeither/generic/gen.go index 607b905..20edaec 100644 --- a/context/readerioeither/generic/gen.go +++ b/context/readerioeither/generic/gen.go @@ -17,7 +17,7 @@ package generic // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:42:22.8252325 +0200 CEST m=+0.010212601 +// 2023-07-28 22:48:20.012425 +0200 CEST m=+0.019517901 import ( "context" diff --git a/either/array.go b/either/array.go new file mode 100644 index 0000000..f6104e6 --- /dev/null +++ b/either/array.go @@ -0,0 +1,46 @@ +// Copyright (c) 2023 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 either + +import ( + F "github.com/IBM/fp-go/function" + RA "github.com/IBM/fp-go/internal/array" +) + +// TraverseArray transforms an array +func TraverseArrayG[GA ~[]A, GB ~[]B, E, A, B any](f func(A) Either[E, B]) func(GA) Either[E, GB] { + return RA.Traverse[GA]( + Of[E, GB], + Map[E, GB, func(B) GB], + Ap[GB, E, B], + + f, + ) +} + +// TraverseArray transforms an array +func TraverseArray[E, A, B any](f func(A) Either[E, B]) func([]A) Either[E, []B] { + return TraverseArrayG[[]A, []B](f) +} + +func SequenceArrayG[GA ~[]A, GOA ~[]Either[E, A], E, A any](ma GOA) Either[E, GA] { + return TraverseArrayG[GOA, GA](F.Identity[Either[E, A]])(ma) +} + +// SequenceArray converts a homogeneous sequence of either into an either of sequence +func SequenceArray[E, A any](ma []Either[E, A]) Either[E, []A] { + return SequenceArrayG[[]A](ma) +} diff --git a/either/gen.go b/either/gen.go index fd15d99..204bbfe 100644 --- a/either/gen.go +++ b/either/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:42:24.2984201 +0200 CEST m=+0.008649601 +// 2023-07-28 22:48:23.165015 +0200 CEST m=+0.038998801 package either diff --git a/function/binds.go b/function/binds.go index dfbf1bb..0819573 100644 --- a/function/binds.go +++ b/function/binds.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:42:51.4021911 +0200 CEST m=+0.009058101 +// 2023-07-28 22:48:40.161663 +0200 CEST m=+0.009458101 package function // Combinations for a total of 1 arguments diff --git a/function/gen.go b/function/gen.go index 6295638..4c55aa3 100644 --- a/function/gen.go +++ b/function/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:42:43.1490717 +0200 CEST m=+0.010222401 +// 2023-07-28 22:48:27.8029852 +0200 CEST m=+0.051432001 package function // Pipe0 takes an initial value t0 and successively applies 0 functions where the input of a function is the return value of the previous function diff --git a/identity/gen.go b/identity/gen.go index 4c56b2b..e64fcd2 100644 --- a/identity/gen.go +++ b/identity/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:42:58.8116745 +0200 CEST m=+0.008252901 +// 2023-07-28 22:48:46.7920356 +0200 CEST m=+0.045177201 package identity diff --git a/internal/apply/gen.go b/internal/apply/gen.go index 89cbd74..7c0e9c6 100644 --- a/internal/apply/gen.go +++ b/internal/apply/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:00.6284756 +0200 CEST m=+0.013494201 +// 2023-07-28 22:48:49.4210571 +0200 CEST m=+0.040316501 package apply diff --git a/io/gen.go b/io/gen.go index 27e836f..fd82dc6 100644 --- a/io/gen.go +++ b/io/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:07.1016885 +0200 CEST m=+0.039744901 +// 2023-07-28 22:48:57.7378739 +0200 CEST m=+0.063371401 package io diff --git a/io/generic/gen.go b/io/generic/gen.go index 66546f7..30f7d3d 100644 --- a/io/generic/gen.go +++ b/io/generic/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:07.1026882 +0200 CEST m=+0.040744601 +// 2023-07-28 22:48:57.7499229 +0200 CEST m=+0.075420401 package generic diff --git a/ioeither/gen.go b/ioeither/gen.go index 13926ae..21b061d 100644 --- a/ioeither/gen.go +++ b/ioeither/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:09.9125541 +0200 CEST m=+0.083065801 +// 2023-07-28 22:49:00.7618943 +0200 CEST m=+0.065441701 package ioeither diff --git a/ioeither/generic/gen.go b/ioeither/generic/gen.go index 59e0ffb..f2ed740 100644 --- a/ioeither/generic/gen.go +++ b/ioeither/generic/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:09.9180997 +0200 CEST m=+0.088611401 +// 2023-07-28 22:49:00.7765028 +0200 CEST m=+0.080050201 package generic diff --git a/iooption/gen.go b/iooption/gen.go index 523bf64..b581d92 100644 --- a/iooption/gen.go +++ b/iooption/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:14.520059 +0200 CEST m=+0.012057201 +// 2023-07-28 22:49:03.8471425 +0200 CEST m=+0.087124201 package iooption diff --git a/iooption/generic/gen.go b/iooption/generic/gen.go index 4563a65..d8f3715 100644 --- a/iooption/generic/gen.go +++ b/iooption/generic/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:14.5210603 +0200 CEST m=+0.013058501 +// 2023-07-28 22:49:03.8541587 +0200 CEST m=+0.094140401 package generic diff --git a/option/gen.go b/option/gen.go index 43d9737..96ec413 100644 --- a/option/gen.go +++ b/option/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:17.2891664 +0200 CEST m=+0.022524801 +// 2023-07-28 22:49:06.6236747 +0200 CEST m=+0.039283301 package option diff --git a/reader/gen.go b/reader/gen.go index 4a3b346..2ec17dc 100644 --- a/reader/gen.go +++ b/reader/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:24.9416992 +0200 CEST m=+0.030259001 +// 2023-07-28 22:49:14.4173227 +0200 CEST m=+0.013163201 package reader diff --git a/reader/generic/gen.go b/reader/generic/gen.go index 1324f7e..b7c5454 100644 --- a/reader/generic/gen.go +++ b/reader/generic/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:24.9574807 +0200 CEST m=+0.046040501 +// 2023-07-28 22:49:14.4173227 +0200 CEST m=+0.013163201 package generic diff --git a/readerioeither/gen.go b/readerioeither/gen.go index ae7b17d..0676ab5 100644 --- a/readerioeither/gen.go +++ b/readerioeither/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:27.7019865 +0200 CEST m=+0.066240401 +// 2023-07-28 22:49:16.5272334 +0200 CEST m=+0.043108701 package readerioeither diff --git a/readerioeither/generic/gen.go b/readerioeither/generic/gen.go index b077186..2cf756f 100644 --- a/readerioeither/generic/gen.go +++ b/readerioeither/generic/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:27.7039855 +0200 CEST m=+0.068239401 +// 2023-07-28 22:49:16.5292332 +0200 CEST m=+0.045108501 package generic diff --git a/tuple/gen.go b/tuple/gen.go index 5519bc3..1ccec37 100644 --- a/tuple/gen.go +++ b/tuple/gen.go @@ -15,7 +15,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2023-07-28 15:43:31.1018593 +0200 CEST m=+0.128090501 +// 2023-07-28 22:49:18.7141124 +0200 CEST m=+0.109464201 package tuple