Skip to content

Instantly share code, notes, and snippets.

@lloydmeta
lloydmeta / AkkaStreamSparkIntegration.scala
Last active January 28, 2021 17:11
Example for how to connect Akka Stream and Spark Streaming by turning creating a Flow element that feeds into an InputDstream
import akka.actor._
import akka.stream.scaladsl.Flow
import org.apache.spark.streaming.dstream.ReceiverInputDStream
import org.apache.spark.streaming.receiver.ActorHelper
import akka.actor.{ ExtensionKey, Extension, ExtendedActorSystem }
import scala.reflect.ClassTag
object AkkaStreamSparkIntegration {
@pathikrit
pathikrit / NQueen.scala
Last active January 19, 2023 21:30
O(n!) solution to the n-Queen puzzle (https://en.wikipedia.org/wiki/Eight_queens_puzzle)
/**
* Solves the n-Queen puzzle in O(n!)
* Let p[r] be the column of the queen on the rth row (must be exactly 1 queen per row)
* There also must be exactly 1 queen per column and hence p must be a permuation of (0 until n)
* There must be n distinct (col + diag) and n distinct (col - diag) for each queen (else bishop attacks)
* @return returns a Iterator of solutions
* Each solution is an array p of length n such that p[i] is the column of the queen on the ith row
*/
def nQueens(n: Int): Iterator[Seq[Int]] =
(0 until n)
@mpilquist
mpilquist / example.md
Last active May 17, 2021 13:17
Properly scheduling effect evaluation in FS2

TL;DR - Use fs2.time.sleep_[Task](delay) ++ Stream.eval(effect) instead of Stream.eval(effect.schedule(delay)).

FS2 never interrupts evaluation of an effect. This can lead to surprising behavior when using the schedule method on Task. Consider this test driver:

def testInterruption[A](effect: Stream[Task, A]): Stream[Task, A] = {
  val logStart = Stream.eval_(Task.delay(println("Started: " + System.currentTimeMillis)))
  val logFinished = Stream.eval_(Task.delay(println("Finished: " + System.currentTimeMillis)))
  val interruptSoonAfterStart =
 Stream.eval(async.signalOf[Task,Boolean](false)).flatMap { cancellationSignal =>
package fs3
import cats.Monad
sealed abstract class Free[F[_], R] {
def flatMap[R2](f: R => Free[F, R2]): Free[F, R2] = Free.Bind(this, f)
}
object Free {
case class Pure[F[_], R](r: R) extends Free[F, R]
case class Eval[F[_], R](fr: F[R]) extends Free[F, R]
@SystemFw
SystemFw / Free conversation.md
Last active October 17, 2023 09:57
Explaining some of the mechanics of interpretation of Free programs

Balaji Sivaraman @balajisivaraman_twitter

Hi all, I need some help understanding a piece of Doobie code from the examples. It is the StreamingCopy one: (https://github.com/tpolecat/doobie/blob/series/0.4.x/yax/example/src/main/scala/example/StreamingCopy.scala). I am using a modified version of the fuseMap2 example from that file. Here’s how I’ve modified it for my requirements:

  def fuseMap[F[_]: Catchable: Monad, A, B](
      source: Process[ConnectionIO, A],
      sink: Vector[A] => ConnectionIO[B],
      delete: ConnectionIO[Unit]
  )(
 sourceXA: Transactor[F],
@SystemFw
SystemFw / Test.scala
Last active June 7, 2019 05:15
Shapeless: Convert between any two compatible case classes, selecting a subset of the fields
object Test {
case class User(name: String, age: Int)
case class UserDTO(name: Option[String], age: Option[Int])
import conversions._
def a = User("John", 24).convertTo[UserDTO](Set("name"))
// res0: Test.UserDTO = UserDTO(Some(John),None)
case class WrongFieldNames(surname: Option[String], age: Option[Int])
@sigma23
sigma23 / covert_json_array_to_jsonlines_jq_or_python.txt
Last active May 15, 2023 19:22
Convert from json array to jsonlines using jq and python
# Run this from the bash command prompt. Make sure that jq is installed https://github.com/stedolan/jq/wiki/Installation
# json_temp.json has the file in the form [{...}, {...}, {...}] and coverts to {...}\n{...}\n
jq -c '.[]' json_temp.json > json_temp.jsonl
# From within python can do this:
pip install jsonlines
import json
import jsonlines
import cats.{ ApplicativeError, MonadError }
import cats.data.{ Kleisli, OptionT }
import cats.effect.Sync
import cats.effect.concurrent.Ref
import cats.syntax.all._
import io.circe.generic.auto._
import io.circe.syntax._
import org.http4s._
import org.http4s.circe.CirceEntityDecoder._
import org.http4s.circe._

Easy Scala Publication

The following describes how you can publish artifacts for any sbt project using the GitHub Package Registry and the sbt-github-packages plugin.

Step 1: Create a GitHub Token

In your GitHub account, go to Settings > Developer settings > Personal access tokens, then click on Generate new token (or click here). Fill in some sort of meaningful name (I chose Dev) and click on the write:packages checkbox:

the new personal access token page with the above steps having been followed

@dnatic09
dnatic09 / TestContainersApiTestHarness.scala
Created May 22, 2020 01:53
Example test harness using TestContainers-Scala
trait TestContainersApiTestHarness extends FlatSpec with Matchers with BeforeAndAfterAll {
private val REDIS_PORT = 6379
private val container = GenericContainer("redis:5.0.8-alpine",
exposedPorts = Seq(REDIS_PORT),
waitStrategy = Wait.forListeningPort()
)
container.start()
private val mappedRedisExternalPort = container.mappedPort(REDIS_PORT)
private val redisClient = new RedisClient("localhost", mappedRedisExternalPort)
private val dao = new RedisTestContainersDao(redisClient)