Skip to content

Commit

Permalink
add try yes/no
Browse files Browse the repository at this point in the history
  • Loading branch information
molarmanful committed Nov 21, 2023
1 parent 0d524f8 commit fb12ad2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion sclin/src/ANY.scala
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ enum ANY:
case _ => toNUM.x.toDouble.toDBL

def toERR: ERR = this match
case x: ERR => x
case Ery(x) => ERR(x)
case x => LinEx("_", x.toString).pipe(ERR(_))

def toTRY: TRY = this match
Expand Down Expand Up @@ -1117,6 +1117,13 @@ object ANY:
case CMD(x) => Some(x)
case _ => None

object Ery:

def unapply(a: ANY): Option[Throwable] = a match
case ERR(x) => Some(x)
case TRY(Failure(x)) => Some(x)
case _ => None

extension [T](xs: Iterable[T])

def packWith(f: (T, T) => Boolean): Iterable[Iterable[T]] =
Expand Down
10 changes: 10 additions & 0 deletions sclin/src/Cmd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ extension (env: ENV)
*/
case ">!?" => env.envTRY
/*
@s a -> TRY
Converts `a` to a `TRY` that succeeds.
*/
case ">!+" => env.envYES
/*
@s a -> TRY
Converts `a` to a `TRY` that fails.
*/
case ">!-" => env.envNO
/*
@s a -> TF
Converts `a` to `TF`.
*/
Expand Down
4 changes: 4 additions & 0 deletions sclin/src/Lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import scala.collection.immutable.VectorMap
import scala.concurrent.*
import scala.concurrent.duration.*
import scala.util.chaining.*
import scala.util.Failure
import scala.util.Success
import scala.util.Try
import spire.implicits.*
import spire.math.*
Expand Down Expand Up @@ -160,6 +162,8 @@ extension (env: ENV)
def envFN: ENV = env.mod1(_.toFN(env))
def envTASK: ENV = env.mod1(_.toTASK)
def envTRY: ENV = env.mod1(_.toTRY)
def envYES: ENV = env.mod1(Success(_).toTRY)
def envNO: ENV = env.mod1(_.toERR.x.pipe(Failure(_)).toTRY)
def envERR: ENV = env.mod2((x, y) => ERR(LinERR(env, y.toString, x.toString)))
def envTF: ENV = env.mod1(_.toTF)
def otoTF: ENV = env.mod1(_.toOBS.x.nonEmptyL.map(_.boolTF).toTASK)
Expand Down

0 comments on commit fb12ad2

Please sign in to comment.