Skip to content

Instantly share code, notes, and snippets.

View Vilkina's full-sized avatar
❤️
I may be slow to respond.

Aleksandra A Vilkina

❤️
I may be slow to respond.
View GitHub Profile
@Vilkina
Vilkina / immutable-red-black-tree.js
Created April 24, 2023 08:14
Red-Black Tree in JavaScript with ES6 syntax
class Node {
constructor(key, value, color, left = null, right = null) {
Object.assign(this, { key, value, color, left, right });
}
async insert(key, value) {
const cmp = key < this.key ? "left" : "right";
const child = this[cmp];
const newChild = child === null ? new Node(key, value, true) : await child.insert(key, value);
return newChild.color && this.color ? this.fixup(newChild, cmp) : new Node(this.key, this.value, this.color, cmp === "left" ? newChild : this.left, cmp === "right" ? newChild : this.right);
object Test {
import zio._
import zio.duration._
import zio.kafka.consumer._
val settings: ConsumerSettings =
ConsumerSettings(List("localhost:9092"))
.withGroupId("group")
.withClientId("client")
object Test {
import zio._
import zio.duration._
import zio.kafka.consumer._
val settings: ConsumerSettings =
ConsumerSettings(List("localhost:9092"))
.withGroupId("group")
.withClientId("client")
import zio._
import zio.console._
import zio.kafka.consumer._
import zio.kafka.serde._
val subscription: Subscription = Subscription.topics("topic")
val readKafka: RIO[Console with Blocking with Clock, Unit] =
Consumer.consumeWith(settings, subscription, Serde.string, Serde.string) {
case (key, value) =>
putStrLn(s"Received message ${key}: ${value}")
object Test {
import zio._
import zio.duration._
import zio.kafka.consumer._
val settings: ConsumerSettings =
ConsumerSettings(List("localhost:9092"))
.withGroupId("group")
.withClientId("client")
name := "zio-kafka-test"
version := "0.1"
scalaVersion := "2.13.3"
libraryDependencies += "dev.zio" %% "zio-kafka" % "0.12.0"
libraryDependencies += "dev.zio" %% "zio-streams" % "1.0.0"
sealed trait TreeSeq[+A] {
lazy val size: Int = this match {
case Leaf(_) => 1
case Branch(l, r) => (l.size + r.size)
}
def +[A1 >: A](a1: A1): TreeSeq[A1] = TreeSeq(a1)
@Vilkina
Vilkina / MyStream.scala
Created July 7, 2020 07:59
Example of implementing stream
package exercises
import scala.annotation.tailrec
abstract class MyStream[+A] {
def isEmpty: Boolean
def head: A
def tail: MyStream[A]
package repeat
abstract class MyList[+A] {
def head: A
def tail: MyList[A]
def isEmpty: Boolean
def add[B >: A](element: B): MyList[B]
def printElements: String
override def toString: String = "[" + printElements + "]"
[info] Benchmark (size) Mode Cnt Score Error Units
[info] ArrayBenchmarks.find 1000 avgt 15 40.282 ± 0.109 ns/op
[info] ArrayBenchmarks.findOptimized 1000 avgt 15 7.645 ± 0.002 ns/op
[info] ArrayBenchmarks.flatMap 1000 avgt 15 31442.367 ± 11.125 ns/op
[info] ArrayBenchmarks.flatMapOptimized 1000 avgt 15 14086.659 ± 35.318 ns/op
[info] ArrayBenchmarks.fold 1000 avgt 15 13050.998 ± 6.377 ns/op
[info] ArrayBenchmarks.map 1000 avgt 15 10070.717 ± 15.476 ns/op
[info] ArrayBenchmarks.mapOptimized 1000 avgt 15 461.821 ± 2.040 ns/op
[info] ChunkArrayBenchmarks.find 1000 avgt 15 23.068 ± 0.012 ns/op
[info] ChunkArrayBenchmarks.flatMap 1000 avgt 15 72463.542 ± 111.816 ns/op