-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added features used in other projects
- Loading branch information
Showing
5 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ object Uris: | |
.withCause(e) | ||
.log(s"Failed to connect to $uri") | ||
false | ||
|
38 changes: 38 additions & 0 deletions
38
scommons/src/main/scala/org/mbari/scommons/etc/sdk/Eithers.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) Monterey Bay Aquarium Research Institute 2021 | ||
* | ||
* FathomNet code is non-public software. Unauthorized copying of this file, | ||
* via any medium is strictly prohibited. Proprietary and confidential. | ||
* Written by: Brian Schlining <[email protected]> | ||
*/ | ||
|
||
package org.mbari.scommons.etc.sdk | ||
|
||
import java.util.Optional | ||
|
||
object Eithers: | ||
|
||
private val emptyOptionalError = new NoSuchElementException("Optional is empty") | ||
|
||
/** | ||
* Helper to traverse a sequence of items that can fail | ||
* | ||
* @param seq | ||
* The sequence of items | ||
* @param f | ||
* The function to apply to each item in the sequence | ||
* @return | ||
* A sequence of items that have been transformed by the function | ||
*/ | ||
def traverse[A, B](seq: Seq[A])(f: A => Either[Throwable, B]): Either[Throwable, Seq[B]] = | ||
seq.foldLeft(Right(Seq.empty): Either[Throwable, Seq[B]]) { (acc, a) => | ||
for | ||
xs <- acc | ||
x <- f(a) | ||
yield xs :+ x | ||
} | ||
|
||
extension [B](opt: Optional[B]) | ||
def toEither: Either[Throwable, B] = | ||
if opt.isPresent then Right(opt.get) | ||
else Left(emptyOptionalError) |
35 changes: 35 additions & 0 deletions
35
scommons/src/main/scala/org/mbari/scommons/etc/sdk/IO.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) Monterey Bay Aquarium Research Institute 2021 | ||
* | ||
* FathomNet code is non-public software. Unauthorized copying of this file, | ||
* via any medium is strictly prohibited. Proprietary and confidential. | ||
* Written by: Brian Schlining <[email protected]> | ||
*/ | ||
|
||
package org.mbari.scommons.etc.sdk | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.ExecutionContext | ||
|
||
type IO[A, B] = A => Either[Throwable, B] | ||
type AsyncIO[A, B] = A => Future[B] | ||
|
||
object IO: | ||
|
||
extension [A, B](io: IO[A, B]) | ||
|
||
def unit: IO[A, Unit] = a => Right(()) | ||
|
||
def flatMap[C](f: IO[B, C]): IO[A, C] = a => io(a).flatMap(b => f(b)) | ||
|
||
def map[C](f: B => C): IO[A, C] = a => io(a).map(f) | ||
|
||
def foreach(f: B => Unit): IO[A, Unit] = a => | ||
for b <- io(a) | ||
yield f(b) | ||
|
||
def async(using executionContext: ExecutionContext): AsyncIO[A, B] = a => | ||
Future(io(a)).map { | ||
case Right(b) => b | ||
case Left(e) => throw e | ||
} |
13 changes: 13 additions & 0 deletions
13
scommons/src/main/scala/org/mbari/scommons/etc/sdk/Iterables.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.mbari.scommons.etc.sdk | ||
|
||
import scala.concurrent.Future | ||
import org.mbari.scommons.etc.jdk.Futures.* | ||
import scala.concurrent.duration.Duration | ||
import scala.concurrent.ExecutionContext | ||
|
||
object Iterables: | ||
|
||
extension [A, B <: Iterable[A]](xs: B) | ||
def parMap[C](f: A => C)(using executionContext: ExecutionContext): Iterable[C] = | ||
xs.map(x => Future { f(x) }) | ||
.map(_.join(Duration.Inf)) |