diff --git a/src/core/collections.scala b/src/core/collections.scala index 9ac6aef..d6b27ae 100644 --- a/src/core/collections.scala +++ b/src/core/collections.scala @@ -66,7 +66,7 @@ extension [ElemType](value: Array[ElemType]) newArray.immutable(using Unsafe) extension [KeyType, ValueType](map: Map[KeyType, ValueType]) - def upsert(key: KeyType, op: Maybe[ValueType] => ValueType): Map[KeyType, ValueType] = + def upsert(key: KeyType, op: Optional[ValueType] => ValueType): Map[KeyType, ValueType] = map.updated(key, op(if map.contains(key) then map(key) else Unset)) def collate @@ -107,7 +107,7 @@ object Cursor: inline def of[ElemType](inline seq: CursorSeq[ElemType]): ElemType = seq(cursor.index) - inline def of[ElemType](inline seq: CursorSeq[ElemType], inline offset: Int): Maybe[ElemType] = + inline def of[ElemType](inline seq: CursorSeq[ElemType], inline offset: Int): Optional[ElemType] = if (cursor.index + offset) >= 0 && (cursor.index + offset) < seq.length then seq(cursor.index + offset) else Unset @@ -121,18 +121,18 @@ inline def cursor cursor.of(seq) inline def precursor - [ElemType](using inline seq: Cursor.CursorSeq[ElemType], inline cursor: Cursor.Cursor): Maybe[ElemType] = + [ElemType](using inline seq: Cursor.CursorSeq[ElemType], inline cursor: Cursor.Cursor): Optional[ElemType] = cursor.of(seq, -1) inline def postcursor - [ElemType](using inline seq: Cursor.CursorSeq[ElemType], inline cursor: Cursor.Cursor): Maybe[ElemType] = + [ElemType](using inline seq: Cursor.CursorSeq[ElemType], inline cursor: Cursor.Cursor): Optional[ElemType] = cursor.of(seq, 1) inline def cursorIndex(using inline cursor: Cursor.Cursor): Int = cursor.index inline def cursorOffset [ElemType](n: Int)(using inline seq: Cursor.CursorSeq[ElemType], inline cursor: Cursor.Cursor) - : Maybe[ElemType] = + : Optional[ElemType] = cursor.of(seq, n) extension [ElemType](seq: IndexedSeq[ElemType]) diff --git a/src/core/conversions.scala b/src/core/conversions.scala index 9fe1aee..537722c 100644 --- a/src/core/conversions.scala +++ b/src/core/conversions.scala @@ -55,7 +55,7 @@ object Irrefutable: object Unapply: given maybe[MatchType, ResultType](using ext: Unapply[MatchType, ResultType]) - : Unapply[Maybe[MatchType], ResultType] = + : Unapply[Optional[MatchType], ResultType] = value => if value.absent then None else ext.unapply(value.asInstanceOf[MatchType]) given [ResultType](using ext: Irrefutable[Text, ResultType]): Irrefutable[String, ResultType] = v => diff --git a/src/core/maybe.scala b/src/core/maybe.scala index e88e57c..37c572d 100644 --- a/src/core/maybe.scala +++ b/src/core/maybe.scala @@ -24,11 +24,11 @@ import language.experimental.captureChecking object Unset: override def toString(): String = "[absent]" -type Maybe[ValueType] = Unset.type | ValueType +type Optional[ValueType] = Unset.type | ValueType case class UnsetValueError() extends Error(Message("the value was not set".tt)) -extension [ValueType](maybe: Maybe[ValueType]) +extension [ValueType](maybe: Optional[ValueType]) inline def absent: Boolean = maybe == Unset inline def present: Boolean = maybe != Unset inline def or(inline value: => ValueType): ValueType = if absent then value else maybe.asInstanceOf[ValueType] @@ -41,14 +41,14 @@ extension [ValueType](maybe: Maybe[ValueType]) inline def fm[ValueType2](inline alternative: => ValueType2)(inline fn: ValueType => ValueType2): ValueType2 = if absent then alternative else fn(vouch(using Unsafe)) - inline def let[ValueType2](inline fn: ValueType => ValueType2): Maybe[ValueType2] = + inline def let[ValueType2](inline fn: ValueType => ValueType2): Optional[ValueType2] = if absent then Unset else fn(vouch(using Unsafe)) -extension [ValueType](iterable: Iterable[Maybe[ValueType]]) +extension [ValueType](iterable: Iterable[Optional[ValueType]]) transparent inline def vouched: Iterable[ValueType] = iterable.filter(!_.absent).map(_.vouch(using Unsafe)) -object Maybe: - inline def apply[ValueType](value: ValueType | Null): Maybe[ValueType] = +object Optional: + inline def apply[ValueType](value: ValueType | Null): Optional[ValueType] = if value == null then Unset else value extension [ValueType](option: Option[ValueType]) diff --git a/src/core/rudiments.scala b/src/core/rudiments.scala index 2609f98..ed4b634 100644 --- a/src/core/rudiments.scala +++ b/src/core/rudiments.scala @@ -27,14 +27,14 @@ type Nat = Int & Singleton type Label = String & Singleton extension [ValueType](value: ValueType) - def only[ValueType2](fn: PartialFunction[ValueType, ValueType2]): Maybe[ValueType2] = + def only[ValueType2](fn: PartialFunction[ValueType, ValueType2]): Optional[ValueType2] = Some(value).collect(fn).getOrElse(Unset) def unit: Unit = () def waive: Any => ValueType = _ => value def twin: (ValueType, ValueType) = (value, value) def triple: (ValueType, ValueType, ValueType) = (value, value, value) - def puncture(point: ValueType): Maybe[ValueType] = if value == point then Unset else point + def puncture(point: ValueType): Optional[ValueType] = if value == point then Unset else point inline def is[ValueSubtype <: ValueType]: Boolean = value.isInstanceOf[ValueSubtype] transparent inline def matchable(using Unsafe): ValueType & Matchable = diff --git a/src/core/working.scala b/src/core/working.scala index 83decea..c10fdad 100644 --- a/src/core/working.scala +++ b/src/core/working.scala @@ -21,12 +21,12 @@ import anticipation.* import language.experimental.captureChecking object WorkingDirectory: - def apply(text: Maybe[Text] = Unset): WorkingDirectory = new WorkingDirectory(text) {} + def apply(text: Optional[Text] = Unset): WorkingDirectory = new WorkingDirectory(text) {} given default(using Quickstart): WorkingDirectory = workingDirectories.default @capability -trait WorkingDirectory(val directory: Maybe[Text]): - def path[PathType](using specificPath: SpecificPath[PathType]): Maybe[PathType^{specificPath}] = +trait WorkingDirectory(val directory: Optional[Text]): + def path[PathType](using specificPath: SpecificPath[PathType]): Optional[PathType^{specificPath}] = directory.let(SpecificPath(_)) object HomeDirectory: @@ -37,7 +37,7 @@ case class HomeDirectory(text: Text): def path[PathType: SpecificPath]: PathType = SpecificPath(text) package workingDirectories: - given default: WorkingDirectory = WorkingDirectory(Maybe(System.getProperty("user.dir")).let(_.tt)) + given default: WorkingDirectory = WorkingDirectory(Optional(System.getProperty("user.dir")).let(_.tt)) package homeDirectories: given default: HomeDirectory = HomeDirectory(System.getProperty("user.home").nn.tt) @@ -45,11 +45,11 @@ package homeDirectories: def workingDirectory [PathType] (using directory: WorkingDirectory, specificPath: SpecificPath[PathType]) - : Maybe[PathType^{specificPath}] = + : Optional[PathType^{specificPath}] = directory.path[PathType] def homeDirectory [PathType] (using directory: HomeDirectory, specificPath: SpecificPath[PathType]) - : Maybe[PathType^{specificPath}] = + : Optional[PathType^{specificPath}] = directory.path[PathType] diff --git a/src/test/tests.scala b/src/test/tests.scala index 3f6cca1..f46c1b6 100644 --- a/src/test/tests.scala +++ b/src/test/tests.scala @@ -216,9 +216,9 @@ object Tests extends Suite(t"Rudiments Tests"): List(0, 0, 1, 2, 3, 4, 0, 0, 1, 5, 6, 0, 0, 0, 0).longestTrain(_ == 0) .assert(_ == (11, 4)) - suite(t"Maybe tests"): - val absentInt: Maybe[Int] = Unset - val setInt: Maybe[Int] = 42 + suite(t"Optional tests"): + val absentInt: Optional[Int] = Unset + val setInt: Optional[Int] = 42 test(t"Check whether absent value is absent"): absentInt.absent @@ -253,30 +253,30 @@ object Tests extends Suite(t"Rudiments Tests"): setInt.option .assert(_ == Some(42)) - test(t"Fold over a Maybe"): + test(t"Fold over a Optional"): absentInt.fm(0)(_ + 1) .assert(_ == 0) - test(t"Fold over a set Maybe"): + test(t"Fold over a set Optional"): setInt.fm(0)(_ + 1) .assert(_ == 43) - test(t"Map over an absent Maybe"): + test(t"Map over an absent Optional"): absentInt.let(_ + 1) .assert(_ == Unset) - test(t"Map over a set Maybe"): + test(t"Map over a set Optional"): setInt.let(_ + 1) .assert(_ == 43) - test(t"Construct a new Maybe from a null value"): + test(t"Construct a new Optional from a null value"): val x: String | Null = null - Maybe(x) + Optional(x) .assert(_ == Unset) - test(t"Construct a new Maybe from a possibly-null value"): + test(t"Construct a new Optional from a possibly-null value"): val x: String | Null = "" - Maybe(x) + Optional(x) .assert(_ == "") test(t"Convert an option to a maybe"):