forked from tpolecat/doobie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
align.scala
31 lines (25 loc) · 937 Bytes
/
align.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Copyright (c) 2013-2020 Rob Norris and Contributors
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package doobie.syntax
import cats.data.Ior
import scala.annotation.tailrec
import scala.language.implicitConversions
final class AlignSyntax[A](as: List[A]) {
def align[B](bs: List[B]): List[Ior[A, B]] = {
@tailrec
def go(as: List[A], bs: List[B], acc: List[Ior[A, B]]): List[Ior[A, B]] =
(as, bs) match {
case (a :: as, b :: bs) => go(as, bs, Ior.Both(a, b) :: acc)
case (a :: as, Nil) => go(as, Nil, Ior.Left(a) :: acc)
case (Nil, b :: bs) => go(Nil, bs, Ior.Right(b) :: acc)
case (Nil, Nil) => acc.reverse
}
go(as, bs, Nil)
}
}
trait ToAlignSyntax {
implicit def toDoobieAlignSyntax[A](as: List[A]): AlignSyntax[A] =
new AlignSyntax(as)
}
object align extends ToAlignSyntax