Skip to content

Instantly share code, notes, and snippets.

View Swoorup's full-sized avatar
🎲
Focusing

Swoorup Joshi Swoorup

🎲
Focusing
View GitHub Profile
@Swoorup
Swoorup / Demo.scala
Created June 28, 2022 07:51
Typed Akka like actors using fs2 and cats
import cats.effect.syntax.all.*
import cats.syntax.all.*
import cats.effect.*
import fs2.Stream
import cats.effect.std.Queue
import scala.concurrent.duration.*
case class FSM[F[_], S, I, O](run: (S, I) => F[(S, O)])
case class Ping[F[_]](replyTo: Deferred[F, String])
@Swoorup
Swoorup / Stream.ts
Created May 23, 2022 17:09
Fetch ndjson stream idea...
import * as S from '@typed/fp/Stream';
import * as E from 'fp-ts/Either';
import { pipe } from 'fp-ts/function';
import * as TE from 'fp-ts/TaskEither';
export function streamFromTaskEither<A>(taskEither: TE.TaskEither<Error, A>): S.Stream<A> {
return pipe(
taskEither,
S.fromTask,
S.chain(E.match(S.throwError, S.of)),
@Swoorup
Swoorup / ArrayLog.scala
Created January 31, 2022 16:28
Append only data structure backed by array.
final case class ArrayLog[A](
private val arr: Array[A],
private val curWriteIdx: Int,
maxLength: Int
)(using ct: ClassTag[A]) { x =>
private inline def offset: Int =
if curWriteIdx < maxLength then 0 else curWriteIdx - maxLength
inline def size: Int =
@Swoorup
Swoorup / RangeCodec.scala
Created November 19, 2021 10:00
Range Codec for skunk
package infrastructure.repository.codec
import skunk.Codec
import PgRangeSupportUtils.*
enum EdgeType:
case `[_,_)`
case `(_,_]`
case `(_,_)`
case `[_,_]`
@Swoorup
Swoorup / CustomerMessageBrokerMapK.scala
Last active November 2, 2021 04:45
Type safe Message Broker/routing using MapK
import cats.Id
import cats.effect.{ IO, Resource }
import cats.effect.unsafe.IORuntime
import cats.implicits.*
import com.codedx.util.MapK
import fs2.Stream
import fs2.concurrent.Topic
import scala.collection.mutable.ListBuffer
import scala.concurrent.duration.*
@Swoorup
Swoorup / CustomerMessageBroker.scala
Last active November 1, 2021 15:47
Type safe message router/broker
import cats.implicits.*
import cats.Id
import scala.collection.mutable.ListBuffer
import fs2.Stream
// The message broking interface
trait MessageBroker[F[_], Router[_]]:
def push[MsgKey: ValueOf](value: Router[MsgKey]): F[Unit]
def subscribe[MsgKey: ValueOf]: Stream[F, Router[MsgKey]]
@Swoorup
Swoorup / FMarshaller.scala
Created April 29, 2021 11:19
Scala 3 - Cats effect - Akka Http Marshalling
import akka.http.scaladsl.marshalling.{GenericMarshallers, ToResponseMarshaller}
import cats.Id
import cats.effect.IO
import cats.effect.std.Dispatcher
import cats.syntax.apply.*
import scala.concurrent.Future
object FMarshaller:
given [A](using m: ToResponseMarshaller[A]): ToResponseMarshaller[Future[A]] =
GenericMarshallers.futureMarshaller
@Swoorup
Swoorup / Subscription.scala
Created February 3, 2021 06:00
Scala 3 Match types Channel subscription
case class TradeEvent()
case class OrderEvent()
type Channel = "trades" | "orders"
// def sub2[Channels <: Tuple](channels: Channels)(using Channel <:< Tuple.Union[Channels]) = channels.toList
type SelChannel[C <: Tuple] = C match {
case "trades" *: xs => TradeEvent | SelChannel[xs]
case "orders" *: xs => OrderEvent | SelChannel[xs]
@Swoorup
Swoorup / union.fsx
Last active February 2, 2021 03:18
Testing Anonymous typed-tagged unions.
open System;;
let random = new Random();
let _rollTheDice (): (int|string) =
let number = random.Next(1, 6)
if (number >= 2) then number else "Winner"
let cls () = System.Console.Clear();;
type A = (int|string|int64)
type CompareResult<'a, 'b> =
| FoundPair of 'a * 'b
| UnmatchedLHS of 'a
| UnmatchedRHS of 'b
[<RequireQualifiedAccess>]
module ListCompare =
let compare xsKeySelector ysKeySelector xs ys =
let xs =
xs