-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c3ad20
commit 83667db
Showing
4 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package morphir.sdk | ||
|
||
/// Type class for appending two values of the same type. | ||
trait Appendable[A] { | ||
def append(a: A, b: A): A | ||
} | ||
|
||
object Appendable extends LowerPriorityAppendable { | ||
def apply[A](implicit appendable: Appendable[A]): Appendable[A] = appendable | ||
|
||
implicit val stringAppendable: Appendable[String] = new Appendable[String] { | ||
override def append(a: String, b: String): String = a + b | ||
} | ||
|
||
implicit def listAppendable[A]: Appendable[List[A]] = new Appendable[List[A]] { | ||
override def append(a: List[A], b: List[A]): List[A] = a ++ b | ||
} | ||
|
||
implicit def vectorAppendable[A]: Appendable[Vector[A]] = new Appendable[Vector[A]] { | ||
override def append(a: Vector[A], b: Vector[A]): Vector[A] = a ++ b | ||
} | ||
|
||
implicit def setAppendable[A]: Appendable[Set[A]] = new Appendable[Set[A]] { | ||
override def append(a: Set[A], b: Set[A]): Set[A] = a ++ b | ||
} | ||
} | ||
|
||
trait LowerPriorityAppendable { | ||
implicit def iterableAppendable[A]: Appendable[Iterable[A]] = new Appendable[Iterable[A]] { | ||
override def append(a: Iterable[A], b: Iterable[A]): Iterable[A] = a ++ b | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
morphir/sdk/core/test/src/morphir/sdk/AppendableSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package morphir.sdk | ||
|
||
import morphir.testing.MorphirBaseSpec | ||
|
||
import zio.test.Assertion._ | ||
import zio.test._ | ||
import morphir.testing.MorphirBaseSpec | ||
|
||
object AppendableSpec extends MorphirBaseSpec { | ||
def spec = suite("AppendableSpec")( | ||
suite("Appendable.append spec")( | ||
test("Appending two strings") { | ||
val a = "Hello, " | ||
val b = "World!" | ||
val expected = "Hello, World!" | ||
assert(morphir.sdk.Basics.append(a)(b))(equalTo(expected)) | ||
}, | ||
test("Appending two lists") { | ||
val a = List(1, 2, 3) | ||
val b = List(4, 5, 6) | ||
val expected = List(1, 2, 3, 4, 5, 6) | ||
assert(morphir.sdk.Basics.append(a)(b))(equalTo(expected)) | ||
}, | ||
test("Appending two sets") { | ||
val a = morphir.sdk.Set.Set(1, 2, 3) | ||
val b = morphir.sdk.Set.Set(4, 5, 6) | ||
val expected = morphir.sdk.Set.Set(1, 2, 3, 4, 5, 6) | ||
assert(morphir.sdk.Basics.append(a)(b))(equalTo(expected)) | ||
}, | ||
test("Appending two iterables") { | ||
val a = Iterable(1, 2, 3) | ||
val b = Iterable(4, 5, 6) | ||
val expected = Iterable(1, 2, 3, 4, 5, 6) | ||
assert(morphir.sdk.Basics.append(a)(b))(equalTo(expected)) | ||
}, | ||
test("Appending two vectors") { | ||
val a = Vector(1, 2, 3) | ||
val b = Vector(4, 5, 6) | ||
val expected = Vector(1, 2, 3, 4, 5, 6) | ||
assert(morphir.sdk.Basics.append(a)(b))(equalTo(expected)) | ||
} | ||
) | ||
) | ||
} |