Skip to content

Commit

Permalink
Alias PartialFunction and provide a fixpoint method implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Dec 8, 2024
1 parent 1fa6856 commit 3435a6b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/core/rudiments-core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ import vacuous.*
type Nat = Int & Singleton
type Label = String & Singleton

infix type ~> [-DomainType, +RangeType] = PartialFunction[DomainType, RangeType]

def fixpoint[ValueType](initial: ValueType)
(fn: (recur: (ValueType => ValueType)) ?=> (ValueType => ValueType)): ValueType =

def recurrence(fn: (recur: ValueType => ValueType) ?=> ValueType => ValueType)
: ValueType => ValueType =
fn(using recurrence(fn(_)))

recurrence(fn)(initial)

extension [ValueType](value: ValueType)
def unit: Unit = ()
def waive: Any => ValueType = _ => value
Expand All @@ -42,7 +53,8 @@ extension [ValueType](value: ValueType)
inline def iff(inline predicate: Boolean)(inline lambda: ValueType => ValueType): ValueType =
if predicate then lambda(value) else value

inline def iff(inline predicate: ValueType => Boolean)(inline lambda: ValueType => ValueType): ValueType =
inline def iff(inline predicate: ValueType => Boolean)(inline lambda: ValueType => ValueType)
: ValueType =
if predicate(value) then lambda(value) else value

inline def is[ValueSubtype <: ValueType]: Boolean = value.isInstanceOf[ValueSubtype]
Expand Down

0 comments on commit 3435a6b

Please sign in to comment.