Skip to content

Commit

Permalink
add SOption.map and filter;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Jan 17, 2019
1 parent 95df7b6 commit 87ad4d0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/scala/sigmastate/types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,21 @@ object SOption extends STypeCompanion {
val GetOrElseMethod = SMethod(this, GetOrElse, SFunc(IndexedSeq(SOption(tT), tT), tT, Seq(STypeParam(tT))), 4)
val FoldMethod = SMethod(this, Fold, SFunc(IndexedSeq(SOption(tT), tR, SFunc(tT, tR)), tR, Seq(STypeParam(tT), STypeParam(tR))), 5)
val ToCollMethod = SMethod(this, "toColl", SCollection(tT), 6, MethodCallIrBuilder)
val MapMethod = SMethod(this, "map",
SFunc(IndexedSeq(SOption(tT), SFunc(tT, tR)), SOption(tR), Seq(STypeParam(tT), STypeParam(tR))),
7, MethodCallIrBuilder)
val FilterMethod = SMethod(this, "filter",
SFunc(IndexedSeq(SOption(tT), SFunc(tT, SBoolean)), SOption(tT), Seq(STypeParam(tT))),
8, MethodCallIrBuilder)
val methods: Seq[SMethod] = Seq(
IsEmptyMethod,
IsDefinedMethod,
GetMethod,
GetOrElseMethod,
FoldMethod,
ToCollMethod
ToCollMethod,
MapMethod,
FilterMethod,
)
def apply[T <: SType](implicit elemType: T, ov: Overload1): SOption[T] = SOption(elemType)
def unapply[T <: SType](tOpt: SOption[T]): Option[T] = Some(tOpt.elemType)
Expand Down
24 changes: 24 additions & 0 deletions src/test/scala/sigmastate/lang/SigmaCompilerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,28 @@ class SigmaCompilerTest extends SigmaTestingCommons with LangTests with ValueGen
mkMethodCall(GroupElementConstant(g1), SGroupElement.ExpMethod, IndexedSeq(BigIntConstant(1)))
)
}

property("SOption.map") {
testMissingCosting("getVar[Int](1).map({(i: Int) => i + 1})",
mkMethodCall(GetVarInt(1),
SOption.MapMethod.withConcreteTypes(Map(SOption.tT -> SInt, SOption.tR -> SInt)),
IndexedSeq(Terms.Lambda(
Vector(("i", SInt)),
SInt,
Some(Plus(Ident("i", SInt).asIntValue, IntConstant(1)))))
)
)
}

property("SOption.filter") {
testMissingCosting("getVar[Int](1).filter({(i: Int) => i > 0})",
mkMethodCall(GetVarInt(1),
SOption.FilterMethod.withConcreteTypes(Map(SOption.tT -> SInt)),
IndexedSeq(Terms.Lambda(
Vector(("i", SInt)),
SBoolean,
Some(GT(Ident("i", SInt).asIntValue, IntConstant(0)))))
)
)
}
}

0 comments on commit 87ad4d0

Please sign in to comment.