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 / 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
@Swoorup
Swoorup / STRTree.fs
Created October 20, 2020 00:54
Immutable tree backed by NetTopologySuite.Spatial.Index.STRTree
type private InternalElement<'Key, 'Value when 'Key: comparison> =
{ key: 'Key
data: 'Value
polygon: Polygon }
[<Struct>]
type STRtree<'Key, 'Value when 'Key: comparison> =
private { map: Map<'Key, InternalElement<'Key, 'Value>>
strTree: NetTopologySuite.Index.Strtree.STRtree<InternalElement<'Key, 'Value>> }
@Swoorup
Swoorup / bucketlist.md
Last active January 20, 2020 12:28
Bucketlist of libraries/resources I might use for projects.