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
import zio._ | |
import zio.stream._ | |
import zio.console._ | |
val streamIn = Stream.fromIterable(Set("first line\nsecond line\nthird ", "line")) | |
// or ZSink.splitDelimiter("\n") | |
val streamOut = streamIn.transduce(ZSink.splitLines).flatMap(chunk => Stream.fromChunk(chunk)) | |
val runtime = new DefaultRuntime {} | |
runtime.unsafeRun(streamOut.map(_.replaceAll("\n","")).foreach(i => putStrLn(i))) |
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
def streamWithQueue = Action { | |
implicit val myExecutionContext: ExecutionContext = actorSystem.dispatchers.lookup("queue-context") | |
val (queue: SourceQueueWithComplete[String], source: Source[String, NotUsed]) = { | |
Source.queue[String](100, OverflowStrategy.backpressure).preMaterialize | |
} | |
// simulate a background process feeding the queue | |
Future.traverse((1 to 1000).toList) { i => |
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
package hello.world | |
import slinky.web.html._ | |
import slinky.core.facade.Hooks._ | |
import slinky.core.FunctionalComponent | |
import scala.scalajs.js | |
import scala.scalajs.js.annotation.JSImport | |
@JSImport("resources/App.css", JSImport.Default) | |
@js.native |
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
import cats.implicits._ | |
import cats.data._ | |
import NonEmptyChain._ | |
case class Name(value: String) | |
case class Age(value: Int) | |
case class Person(name: Name, age: Age) | |
trait Error | |
case object ParsingError extends Error |
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
package fpmax | |
import scala.util.Try | |
import scala.io.StdIn.readLine | |
object App0 { | |
def main: Unit = { | |
println("What is your name?") | |
val name = readLine() |
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
let dropListElement = (n, list) => { | |
let rec aux = (i, list) => | |
switch list { | |
| [] => [] | |
| [x, ...xs] => i == n ? xs : [x, ...aux(i + 1, xs)] | |
}; | |
aux(0, list); | |
}; |
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
import arrow.core.Either | |
import arrow.core.Either.Companion.left | |
import arrow.core.Either.Companion.right | |
import arrow.core.Option | |
import arrow.core.Some | |
import arrow.core.flatMap | |
import arrow.instances.either.monad.binding | |
import kotlinx.coroutines.async | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.runBlocking |
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
import scala.util._ | |
implicit class EitherLeftOps[L](eithers: Seq[Either[L,_]]){ | |
def collectLefts: Seq[L] = eithers.collect { | |
case Left(l) => l | |
} | |
} | |
implicit class EitherRightOps[R](eithers: Seq[Either[_,R]]){ | |
def collectRights: Seq[R] = eithers.collect { |
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
import cats.implicits._ | |
val f = (x: Int) => x+1 | |
val g = (x: Int) => x+2 | |
val h = (x: Int) => x+3 | |
// Cats has an Applicative instance for Function1 so you can traverse/sequence it | |
val l = List(f, g, h).sequence //l : Function1[Int, List[Int]] | |
l(1) //List(2, 3, 4) |
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
implicit class AnyEx[T](val v: T) extends AnyVal { | |
def |>[U](f: T ⇒ U): U = f(v) | |
} | |
//example | |
def f(l: List[Int]) = l.filter(x => x >1) | |
def g(l: List[Int]) = l.filter(x => x <3) | |
List(1,2,3) |> f |> g //List(2) |