Skip to content

Commit

Permalink
Add appendable to Basics (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianReeves authored Oct 4, 2024
1 parent 2c3ad20 commit 83667db
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
32 changes: 32 additions & 0 deletions morphir/sdk/core/src/morphir/sdk/Appendable.scala
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
}
}
6 changes: 5 additions & 1 deletion morphir/sdk/core/src/morphir/sdk/Basics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ limitations under the License.

package morphir.sdk

object Basics {
trait Basics {
def append[A: Appendable](a: A)(b: A): A = Appendable[A].append(a, b)
}

object Basics extends Basics {

sealed abstract class Order(val value: Int) extends Product with Serializable
object Order {
Expand Down
2 changes: 1 addition & 1 deletion morphir/sdk/core/src/morphir/sdk/Set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import morphir.sdk.Basics.Bool

object Set {
type Set[A] = scala.collection.immutable.Set[A]
private val Set = scala.collection.immutable.Set
private[morphir] val Set = scala.collection.immutable.Set

/** Create an empty set.
*/
Expand Down
44 changes: 44 additions & 0 deletions morphir/sdk/core/test/src/morphir/sdk/AppendableSpec.scala
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))
}
)
)
}

0 comments on commit 83667db

Please sign in to comment.