Skip to content

Instantly share code, notes, and snippets.

@Daenyth
Daenyth / MonadAndFs2Ops.md
Last active August 22, 2023 15:58
Cheat sheet for common cats monad and fs2 operation shapes
Operation Input Result Notes
map F[A] , A => B F[B] Functor
apply F[A] , F[A => B] F[B] Applicative
(fa, fb, ...).mapN (F[A], F[B], ...) , (A, B, ...) => C F[C] Applicative
(fa, fb, ...).tupled (F[A], F[B], ...) F[(A, B, ...)] Applicative
flatMap F[A] , A => F[B] F[B] Monad
traverse F[A] , A => G[B] G[F[A]] Traversable; fa.traverse(f) == fa.map(f).sequence; "foreach with effects"
sequence F[G[A]] G[F[A]] Same as fga.traverse(identity)
attempt F[A] F[Either[E, A]] Given ApplicativeError[F, E]
@Daenyth
Daenyth / DynamicSlickSort.scala
Created March 27, 2018 10:22
Slick support for dynamic sort with pagination
import slick.lifted.Rep
import slick.ast.Ordering.Direction
import slick.ast.Ordering
import slick.lifted.Query
import slick.lifted.ColumnOrdered
import slick.lifted.Ordered
import scala.util.control.NoStackTrace
case class PageRequest(offset: Int,
import cats.{Contravariant, Functor, Semigroupal}
import slick.jdbc.{
GetResult,
PositionedParameters,
PositionedResult,
SetParameter
}
trait RawSqlInstances {
@Daenyth
Daenyth / DoobieSlickCompat.scala
Last active June 16, 2023 14:02
Slick Database => doobie Transactor
import cats.effect.Async
import doobie.util.transactor.Transactor
import scala.concurrent.ExecutionContext
trait DoobieSlickCompat {
/** Create a Doobie Transactor backed by the same Jdbc DataSource as the live slick Database */
def transactorFromSlick[F[_]: Async](
slickDb: slick.jdbc.JdbcBackend#Database,
@Daenyth
Daenyth / toki-pona-words.txt
Last active May 13, 2023 22:03
lines in /usr/share/dict/words that match toki pona phonotactics
a
ajaja
ajowan
aka
akala
akali
akan
akasa
ake
akeki
@Daenyth
Daenyth / S3Api.scala
Last active May 12, 2023 12:51
fs2 S3 Multipart Upload and download [OBSOLETE, use fs2-aws instead]
// Copyright 2019-2021 github.com/daenyth
// Under the MIT license
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNE
@Daenyth
Daenyth / monad-li-seme.md
Last active May 11, 2023 15:12
What's a monad

"Monad" is a word that describes a set of behaviors

In scala, we use the Monad[Foo] typeclass from cats to define instances of this behavior.

The essence of its behavior is the ability to describe a series of computations, where one computation depends on the result of the computation that came before it.

For example, Monad[Option] shows that the Option[A] data type can be used to describe computations of A which may result in no value.

@Daenyth
Daenyth / KeyedEnqueue.scala
Created October 9, 2019 14:22
fs2 groupBy / KeyedEnqueue
import cats.Monad
import cats.effect.concurrent.{Ref, Semaphore}
import cats.effect.{Concurrent, Resource}
import cats.implicits._
import fs2.{Pipe, Stream}
import fs2.concurrent.{NoneTerminatedQueue, Queue}
/** Represents the ability to enqueue keyed items into a stream of queues that emits homogenous keyed streams.
*
@Daenyth
Daenyth / ImageExtractor.java
Last active March 31, 2023 08:27
Java class to extract an image from an html page using a method similar to Google+'s
import java.io.IOException;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
/**
* Given a url to a web page, extract a suitable image from that page. This will
* attempt to follow a method similar to Google+, as described <a href=
@Daenyth
Daenyth / StreamRetry.scala
Last active December 11, 2022 21:31
fs2 Stream 1.x / Simple example of retry with logging
import cats.effect.Sync
import io.chrisdavenport.log4cats.Logger
import cats._
import cats.implicits._
import scala.concurrent.duration._
class RetryExample[F[_]](implicit F: Sync[F], log: Logger[F]) {
case class ApiError(msg: String) extends Exception(msg)
private def getTempFromInternet: EitherT[F, ApiError, Float] = ???