-
Notifications
You must be signed in to change notification settings - Fork 349
/
align.scala
29 lines (24 loc) · 897 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
// Copyright (c) 2013-2017 Rob Norris
// 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
final class AlignSyntax[A](as: List[A]) {
def align[B](bs: List[B]): List[A Ior B] = {
@tailrec
def go(as: List[A], bs: List[B], acc: List[A Ior B]): List[A Ior 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