Skip to content

Instantly share code, notes, and snippets.

View haghard's full-sized avatar
🏠
Working from home

Vadim Bondarev haghard

🏠
Working from home
View GitHub Profile
@haghard
haghard / ValidatedValue1.scala
Created February 13, 2025 15:36
ValidatedValue1.scala
import ValidatedValue._
final case class Row(a: Int, b: Double, c: String, aOpt: Option[Int], list: List[Int])
sealed trait ValidatedValue[+A] { self =>
def flatMap[B](f: A => ValidatedValue[B]): ValidatedValue[B] =
self match {
case Valid(v) =>
f(v)
package net.demo.example
import akka.actor.ExtendedActorSystem
import akka.actor.typed.{ActorRef, ActorSystem}
import akka.serialization.SerializerWithStringManifest
import net.demo.example.Reply.{JoinReply, LeaveReply}
import java.io.NotSerializableException
final case class ChatId(value: String) extends AnyVal
@haghard
haghard / flow-example.scala
Last active May 17, 2023 04:19
file-flow
//> using scala "2"
//> using jvm "graalvm-java17:22.3.2"
//> using repository "jitpack"
//> using dep "com.typesafe.akka::akka-stream:2.6.20"
import akka.actor.ActorSystem
import scala.concurrent.Future
import akka.stream.scaladsl.{ Flow, Keep, Sink, Source }
// using jvm "17"
import akka.actor.typed.ActorSystem
import akka.actor.typed.scaladsl.Behaviors
import akka.http.scaladsl.Http
import akka.stream.alpakka.s3.{MultipartUploadResult}
import akka.stream.alpakka.s3.scaladsl.S3
import akka.stream.scaladsl.{Keep, Sink, Source}
import akka.util.ByteString
import com.typesafe.config.ConfigFactory
import java.util.UUID
@haghard
haghard / RingBuffer.scala
Last active September 29, 2023 16:21
RingBuffer
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
import java.util.{Base64, UUID}
import scala.collection.mutable
import scala.reflect.ClassTag
import scala.util.Try
object HashRingBuffer {
type Hash = Array[Byte]
/** {{{
* +---------------------------------------------------------------------------------------------------------------------------------------------+
* | +------------+ +------------+ +-----------------------+ +--------------------+ +--------------+ +------------+ |
* | |OPENED(usr) ○---->|PAID((usr)) ○----->|READY_FOR_DELIVERY(amz)○----->|OUT_OF_DELIVERY(amz)○----->|DELIVERED(amz)○------>|RETURNED(usr) |
* | +------------+ +------------+ +-----------------------+ +--------------------+ +--------------+ +------------+ |
* | | | | +-------------+ |
* | +------------------+---------------------+------->|CANCELED(usr)| |
* | +-------------+
object JvmInternals {
def writeDouble(d: Double): Array[Byte] =
writeLong(java.lang.Double.doubleToRawLongBits(d))
def readDouble(bytes: Array[Byte]) =
java.lang.Double.longBitsToDouble(readLong(bytes))
def writeLong(i: Long): Array[Byte] = {
val array = Array.ofDim[Byte](8)
object JvmInternals {
def arrayOf[T: scala.reflect.ClassTag] = {
val cl = Array.ofDim[T](0).getClass
java.lang.invoke.MethodHandles.byteArrayViewVarHandle(cl, ByteOrder.BIG_ENDIAN)
}
def getByInd[T: scala.reflect.ClassTag](vh: java.lang.invoke.VarHandle, array: Array[Byte], i: Int): T = {
implicitly[scala.reflect.ClassTag[T]].getClass
vh.get(array, i).asInstanceOf[T]
@haghard
haghard / pf_ machinery.scala
Last active April 18, 2020 22:47
The machinery to control when things should be kicked off
//an implementation that gives more control over when the work in a Future starts.
import scala.concurrent.ExecutionContext.Implicits.global
val p = Promise[Unit]()
val f = p.future
val _ = f.map { _ =>
var i = 0
while (i < 10) {
i += 1
package sample.blog.eg
import java.util.concurrent.ThreadLocalRandom
import akka.actor.{ ActorSystem, Scheduler }
import akka.stream.QueueOfferResult.{ Dropped, Enqueued }
import akka.stream.scaladsl.SourceQueueWithComplete
import akka.stream.{ ActorAttributes, ActorMaterializer, Materializer, OverflowStrategy, QueueOfferResult }
import akka.stream.scaladsl.{ FlowWithContext, Keep, Sink, Source }