Skip to content

Commit

Permalink
Rename Maybe to Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Dec 8, 2023
1 parent a7cfe8a commit 362b244
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/core/collections.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion src/core/conversions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
12 changes: 6 additions & 6 deletions src/core/maybe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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])
Expand Down
4 changes: 2 additions & 2 deletions src/core/rudiments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
12 changes: 6 additions & 6 deletions src/core/working.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -37,19 +37,19 @@ 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)

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]
22 changes: 11 additions & 11 deletions src/test/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"):
Expand Down

0 comments on commit 362b244

Please sign in to comment.