Skip to content

Instantly share code, notes, and snippets.

View Daenyth's full-sized avatar

Alanna Stone Daenyth

View GitHub Profile
@Daenyth
Daenyth / Fs2Timeout.scala
Last active June 4, 2019 19:56
Resettable fs2 stream timeout
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
)(
@Daenyth
Daenyth / AlpakkaS3Put.scala
Created May 23, 2019 17:31
AlpakkaS3Put for fs2
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
@Daenyth
Daenyth / flatMaps.sc
Created May 6, 2019 17:54
flatMap examples
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 =>
@Daenyth
Daenyth / rt.sc
Created May 6, 2019 17:36
Referential transparency example
// 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)
@Daenyth
Daenyth / 1-MapTraverse.md
Last active June 25, 2024 13:05
Scala (cats) map/traverse parallels

Parallels between map and similar functions

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]
@Daenyth
Daenyth / circle.yml
Created April 19, 2019 19:13
circleci run step to fail compile if master merge is bad
# skipping details that are irrelevant
jobs:
test-compile-with-master:
environment:
<<: *sbt-compile-opts
steps:
- checkout
- run:
command: |
git config user.email "[email protected]"
@Daenyth
Daenyth / git-refresh.sh
Last active November 8, 2023 15:43
Git refresh
#!/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
@Daenyth
Daenyth / JsoniterSpec.scala
Created April 16, 2019 19:56
Jsoniter fs2 pipe with streaming
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,
@Daenyth
Daenyth / CachedResource-Blog.md
Last active March 26, 2024 17:19
CachedResource for cats-effect

Concurrent resource caching for cats

Motivation

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
@Daenyth
Daenyth / ByteStream.scala
Created April 8, 2019 19:59
ByteStream for fs2 - type-tagged byte encoding for Stream[F, Byte]
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