Skip to content

Instantly share code, notes, and snippets.


Vlad Patryshev vpatryshev

View GitHub Profile
vpatryshev / ImplicitConverter.scala
Created Jan 31, 2022
How to build an implicit generic converter
View ImplicitConverter.scala
class Test extends AnyFlatSpec with Matchers {
"transformer" should "work" in {
// source:
sealed trait NotNothing[-T]
object NotNothing {
@implicitAmbiguous("inst() method needs a generic parameter type, which is missing")
implicit val nothingIsNothing = new NotNothing[Nothing]{}
implicit def notNothing[T] = new NotNothing[T] {}
vpatryshev / CovarianceAndContravariance.scala
Last active Aug 24, 2021
## Covariance and Contravariance Illustrated
View CovarianceAndContravariance.scala
import Experiments_1.Q2
object Experiments_1:
// class Q1[+T]:
// def enqueue(x: T): Unit = println(s"Q1 enqueued $x")
// val x1: Q1[Any] = new Q1[Int]
class Q2[-T]:
def enqueue(x: T): Unit = println(s"Q2 enqueued $x")
vpatryshev / EnvHacker.scala
Last active Aug 4, 2021 — forked from jaytaylor/EnvHacker.scala
Setting environment variables in Scala JVM
View EnvHacker.scala
import java.util.{Collections, Map => JavaMap}
import scala.collection.JavaConverters._
trait EnvHacker {
* Portable method for setting env vars on both *nix and Windows.
* @see
def setEnv(newEnv: Map[String, String]): Unit = {
try {
View gist:dfdee1b6b0813c8bed5b0917df732e8c
// Before
def buildItem(eob:EOB)(props: Props): Result[EOB_item] = {
implicit val empty: String = "" // weird
for (sDate <- props valueOf DateOfServicePerItem;
date <- DateFormat("MM/dd/yyyy").parseCurrent(sDate);
description <- props valueOf ProcedureDescription;
sBilled <- props valueOf BilledPerItem;
billed <- dollars(sBilled);
vpatryshev / ZFC in Java
Created Nov 29, 2013
Wrote this code in July 2008, implementing ZFC set theory in Java. Kind of funny; it worked.
View ZFC in Java
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Arrays;
import java.util.Map;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.SortedSet;
import java.util.TreeSet;
vpatryshev / package.scala
Created Dec 16, 2015 — forked from zraffer/package.scala
a few operations with functors
View package.scala
package object types {
import scala.language.reflectiveCalls
import scala.language.higherKinds
// quantifiers aka (co)ends
type Forall[+F[_]] = { def apply[X]: F[X] }
type Exists[+F[_]] = F[_]
// basic categorical notions
vpatryshev / binary tree inorder
Created Apr 27, 2016
implementation of inorder scan of a binary tree
View binary tree inorder
case class BT[T](left: Option[BT[T]], value: T, right: Option[BT[T]]);
def inorder[T](t:BT[T], out: T=>Unit): Unit = {
val stack: mutable.Stack[(Boolean, BT[T])] = new mutable.Stack[(Boolean, BT[T])]()
def p(n: BT[T]) = stack.push((false, n))
while (!stack.isEmpty) {
(stack.pop() match {
case (true, top) =>
View gist:7076235
def downloadPDF(url: String): Result[(File, String)] = {
loadPage(url) andThen
waitForSelector("div.textLayer") andThen
runJS("return extractPdfContent()") andThen {
Thread.sleep(2000) // give browser a chance
val extracted = runJS("return intBuf2hex(extractedPdf)") map (_.toString)
val pdf = extracted flatMap
(_.decodeHex #> File.createTempFile("download", ".pdf"))
val html = runJS("return _$('div.textLayer').innerHTML") map (_.toString)
View gist:6531769
def spendNotMoreThan[T](time: Duration) = new {
def on(op: => Result[T]): Result[T] = {
var res:Result[T] = Empty
var done = new AtomicBoolean(false)
val nanos = time.toNanos
val upto = System.nanoTime + nanos
val worker = new Thread {
override def run {
res = op
vpatryshev / HtmlPropsExtraction.scala
Last active Dec 21, 2015
sample code for html data extraction
View HtmlPropsExtraction.scala
// we already have a bunch of patient properties on one tab (in props1); now get another bunch from another tab
// we click it and wait for the element to show up; then:
val props2 = extractProperties("div#ctl00_ContentPlaceHolder1_Details_Container2") map (_.trimPrefixes)
// trimming prefixes means we do not care about all the additional keys, but just a variety of values
// now let's blend the two sets together; <*> will produce either Good((p1,p2)) or Bad(errors1 ++ errors2)
val propsOpt: Result[(Props, Props)] = props1 <*> props2
// then we want to merge together two sets of patient properties; that's what we do in map
val patientData = propsOpt map ((xy:(Props, Props)) => (xy._1 ++ xy._2))
debug(s"Got patient data\n $patientData")