Skip to content

Instantly share code, notes, and snippets.

@hochgi
hochgi / Puzzles.scala
Created June 18, 2023 17:46
code puzzles
object Puzzles {
/**
* 8 queens
* https://en.wikipedia.org/wiki/Eight_queens_puzzle
* generalized to any N
*/
object Queens {
/**
@hochgi
hochgi / README.md
Last active May 3, 2022 14:25
scala-async-parallel-concurrent
@hochgi
hochgi / UtilGraveyard.scala
Created February 24, 2022 15:09
just a place to keep deleted stuff to ease my sentiment (better than leaving commented out in the code)
implicit val decodeResultMonad: Monad[DR] = new Monad[DR] {
override def flatMap[A, B](fa: DR[A])(f: A => DR[B]): DR[B] = fa.flatMap(f)
override def tailRecM[A, B](a: A)(f: A => DR[Either[A, B]]): DR[B] = f(a) match {
case DR.Value(Left(aa)) => tailRecM(aa)(f)
case DR.Value(Right(b)) => DR.Value(b)
case failedDecodeResult =>
// scalastyle:off null
failedDecodeResult.map(_.getOrElse(null.asInstanceOf[B])) // stays DR.failure - map is not applied
// scalastyle:on null
}
@hochgi
hochgi / XmlToCsvConverter.scala
Created October 29, 2021 06:29
convert a tree structure (e.g. derived from XML) to a csv where same labeled nodes make full outer join of inner subtrees (empty cells as `null`)
package com.hochgi
package object util {
sealed trait Value
case class Attribute(k: String, v: String) extends Value
case class Content(s: String) extends Value
case class Tree(label: String, values: List[Value], children: List[Tree])
@hochgi
hochgi / StreamEventInspector.scala
Created July 7, 2021 12:18
akka-streams custom stream inspector
import akka.stream.{Attributes, FlowShape, Inlet, Outlet}
import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler}
import com.typesafe.scalalogging.Logger
object StreamEventInspector {
def default[T](logger: Logger, context: String, printElem: T => String): StreamEventInspector[T] = {
val ctx = "[" + context + "] "
new StreamEventInspector[T](
() => logger.info(ctx + "upstream completed"),
ex => logger.error(ctx + "upstream failure", ex),
@hochgi
hochgi / Tree.py
Last active July 27, 2021 13:37
Tree parsing exercise python
from typing import Optional
class Tree:
def __init__(self, value: int, children: list):
self.value = value
self.children = children # list of Tree
def format_tree(t: Tree) -> str:
pass
@hochgi
hochgi / Tree.java
Last active June 16, 2021 07:01
Tree parsing exercise java
package scratch.example;
import java.util.List;
public class Tree {
int value;
List<Tree> children;
public Tree(int value, List<Tree> children) {
@hochgi
hochgi / 0.17.19.png
Last active June 13, 2021 17:11
tapir 0.17.19 vs 0.18.0-M15
0.17.19.png
@hochgi
hochgi / LogbackEventLogger.scala
Created October 20, 2020 08:17
implementation of internal event logger lib backed by logback's rolling file appender
package com.sparkbeyond.engine.util.logging.eventslogger
import java.nio.charset.StandardCharsets
import akka.Done
import ch.qos.logback.core.rolling.{
FixedWindowRollingPolicy,
RollingFileAppender,
SizeBasedTriggeringPolicy
}
@hochgi
hochgi / data-science-bowl-2019.ipynb
Created December 5, 2019 14:20
data-science-bowl-2019 jupyter notebook sandbox
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.