Skip to content

Instantly share code, notes, and snippets.

View deferred2.scala
def consumer(done: Deferred[IO, Unit]) = for {
c <- Consumer.setup
_ <- done.complete(())
msg <- c.read
_ <- IO(println(s"Received $msg"))
} yield ()
def producer(done: Deferred[IO, Unit]) = for {
p <- Producer.setup()
_ <- done.get
View deferred1.scala
abstract class Deferred[F[_], A] {
def get: F[A]
def complete(a: A): F[Unit]
}
View ref6.scala
def modify[B](f: A => (A, B)): F[B] = {
@tailrec
def spin: B = {
val c = ar.get
val (u, b) = f(c)
if (!ar.compareAndSet(c, u)) spin
else b
}
F.delay(spin)
}
View ref5.scala
for {
_ <- someRef.update(_ + 1)
curr <- someRef.get
_ <- IO { println(s"current value is $curr")}
} yield ()
View ref4.scala
def update(f: A => A): F[Unit]
View ref3.scala
def modifyShared(trace: Ref[IO, Shared], msg: String): IO[Unit] = {
for {
sh <- trace.get()
_ <- trace.set(Shared(sh, msg))
} yield ()
}
View ref2.scala
def of[F[_], A](a: A)(implicit F: Sync[F]): F[Ref[F, A]] = F.delay(unsafe(a))
View ref1.scala
abstract class Ref[F[_], A] {
def get: F[A]
def set(a: A): F[Unit]
def modify[B](f: A => (A, B)): F[B]
// ... and more
}
View atomic1.java
/**
* Atomically increments by one the current value.
*
* @return the updated value
*/
public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
View play-fp2-5.scala
private lazy val personService: PersonService[IO] = wire[PersonService[IO]]
private lazy val messagesActionBuilder = new DefaultMessagesActionBuilderImpl(playBodyParsers.defaultBodyParser, messagesApi)
private lazy val messagesControllerComponents = DefaultMessagesControllerComponents(
messagesActionBuilder,
defaultActionBuilder,
playBodyParsers,
messagesApi,
langs,
fileMimeTypes,
ec)
You can’t perform that action at this time.