map :: F[A] => (A => B) => F[B]
flatMap :: F[A] => (A => F[B]) => F[B]
traverse :: G[A] => (A => F[B]) => F[G[B]]
flatTraverse :: G[A] => (A => F[G[B]]) => F[G[B]]
traverse_ :: F[A] => (A => F[B]) => F[Unit]
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.effect._ | |
import cats.effect.implicits._ | |
import cats.implicits._ | |
import cats.effect.concurrent._ | |
import fs2._ | |
object Fs2Timeout { | |
def stopWhenIdle[F[_]: Timer, A]( | |
duration: FiniteDuration | |
)( |
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 akka.http.scaladsl.model.{ContentType, ContentTypes} | |
import akka.stream.Materializer | |
import akka.stream.alpakka.s3.scaladsl.{S3 => AlpakkaS3} | |
import akka.stream.alpakka.s3.{MetaHeaders, MultipartUploadResult} | |
import akka.util.ByteString | |
import cats.effect.{ConcurrentEffect, ContextShift} | |
import fs2.{Pipe, Stream} | |
import org.http4s.Uri | |
import org.joda.time.DateTime |
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 randInt: IO[Int] = ??? | |
def bad = { | |
val x = someIO.unsafeRunSync() // side effects step by step | |
val y = someIO.unsafeRunSync() | |
(x, y) | |
} | |
def ok = { | |
someIO.flatMap { x => |
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
// Referential transparency | |
// An value can be replaced by its definition without changing semantics | |
// Not RT | |
def foo() = { | |
(println("hi"), println("hi")) | |
} | |
// bar() != foo() | |
def bar() = { | |
val x = println(hi) |
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
# skipping details that are irrelevant | |
jobs: | |
test-compile-with-master: | |
environment: | |
<<: *sbt-compile-opts | |
steps: | |
- checkout | |
- run: | |
command: | | |
git config user.email "[email protected]" |
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
#!/usr/bin/env bash | |
set -o errexit | |
## git refresh | |
# | |
# Update all git remotes | |
# Update local main or master branch from the remote | |
# Delete local branches tracking remotely-deleted branches | |
# Delete local branches that have been merged |
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.effect.implicits._ | |
import cats.effect.{ConcurrentEffect, ContextShift, IO, Sync} | |
import cats.implicits._ | |
import com.github.plokhotnyuk.jsoniter_scala.core.{ | |
JsonValueCodec, | |
scanJsonValuesFromStream, | |
writeToArray | |
} | |
import com.github.plokhotnyuk.jsoniter_scala.macros.{ | |
CodecMakerConfig, |
cats-effect Resource
is extremely handy for managing the lifecycle of stateful resources, for example database or queue connections. It gives a main interface of:
trait Resource[F[_], A] {
/** - Acquire resource
* - Run f
* - guarantee that if acquire ran, release will run, even if `use` is cancelled or `f` fails
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 teikametrics | |
import java.nio.charset.{Charset, StandardCharsets} | |
import akka.util.ByteString | |
import fs2.{Chunk, Pipe, RaiseThrowable, Stream} | |
import teikametrics.ByteEncoding._ | |
/** Streamed bytes claiming to be encoded under `BE` | |
* | |
* @param chunkSize A chunk size to use for streaming operations, in order to use a consistent value and minimize array copying |