Skip to content

Instantly share code, notes, and snippets.

View vpatryshev's full-sized avatar
💭
married

Vlad Patryshev vpatryshev

💭
married
View GitHub Profile
@soc
soc / ::.scala
Last active December 18, 2015 05:59
final case class ::[B](private var hd: B, private[scala] var tl: List[B]) extends List[B]
override def head: B = hd
override def tail: List[B] = tl
override def isEmpty: Boolean = false
private def readObject(in: ObjectInputStream): Unit =
val firstObject = in.readObject()
hd = firstObject.asInstanceOf[B]
assert(hd != ListSerializeEnd)
var current: ::[B] = this
@vmarquez
vmarquez / TxMapTest.scala
Last active November 28, 2021 12:33
A mini STM if you will. I've made a'Transactional' map that mutates in a referentially transparent way.
import java.util.concurrent.atomic.AtomicReference
import java.util.concurrent.CountDownLatch
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
import ExecutionContext.Implicits.global
object TxMapTest {
/*
* Example Usage
* We want to show two threads working with the same data source having both of their effects succeed
@softprops
softprops / JValueWithFilter.scala
Last active December 17, 2015 03:19
Does away with this pesky compiler warning in scala 2.10.0 "`withFilter' method does not yet exist on org.json4s.JValue, using `filter' method instead"
import org.json4s.JValue
implicit class LiftJValueWithFilter(self: JValue)
extends JValueWithFilter(self, _ => true)
class JValueWithFilter(self: JValue, p: JValue => Boolean) {
def map(f: JValue => T): List[T] =
self.filter(p).map(f)
def flatMap(f: JValue => List[T]): List[T] =
self.filter(p).flatMap(f)
def foreach(f: JValue => Unit): Unit =
@ryanlecompte
ryanlecompte / gist:5537188
Last active December 17, 2015 02:38
Pimping Iterable to have zipMany/unzipMany operations
// Usage examples:
val a = Vector(1,2,3).zipMany(Vector(1,2,3), Vector(5,6,7), Vector(8,9,10), Vector(11,12,13))
Vector(Vector(1, 1, 5, 8, 11), Vector(2, 2, 6, 9, 12), Vector(3, 3, 7, 10, 13))
a.unzipMany
Vector(Vector(1, 2, 3), Vector(1, 2, 3), Vector(5, 6, 7), Vector(8, 9, 10), Vector(11, 12, 13))
// Implementation
import scala.collection._
import scala.collection.generic.CanBuildFrom
22:06 ~/Projects/Kepler_5923/sandbox (ticket/5923)$ cat Macros.scala
import language.experimental.macros
import scala.reflect.macros.Context
trait Iso[T, U] {
def to(t : T) : U
// def from(u : U) : T
}
object Iso {
@tmyymmt
tmyymmt / HexBytesUtil.scala
Created September 15, 2012 09:37
hex2bytes and bytes2hex fixed
object HexBytesUtil {
def hex2bytes(hex: String): Array[Byte] = {
hex.replaceAll("[^0-9A-Fa-f]", "").sliding(2, 2).toArray.map(Integer.parseInt(_, 16).toByte)
}
def bytes2hex(bytes: Array[Byte], sep: Option[String] = None): String = {
sep match {
case None => bytes.map("%02x".format(_)).mkString
case _ => bytes.map("%02x".format(_)).mkString(sep.get)
@alvinj
alvinj / sbtmkdirs.sh
Last active January 24, 2024 19:39
A shell script to create an SBT project directory structure
#!/bin/bash
#------------------------------------------------------------------------------
# Name: sbtmkdirs
# Version: 1.5
# Purpose: Create an SBT project directory structure with a few simple options.
# Author: Alvin Alexander, http://alvinalexander.com
# License: Creative Commons Attribution-ShareAlike 2.5 Generic
# http://creativecommons.org/licenses/by-sa/2.5/
#------------------------------------------------------------------------------
@retronym
retronym / config.scala
Last active May 9, 2018 05:47
Styles of config propagation: Manual, Implicits, DynamicVariable, Reader
package scalaz.example
object Reader extends App {
/**
* Manual propagation of the environment (in the example, `contextRoot`.)
*/
object Config0 {
def fragment1(contextRoot: String) = <a href={contextRoot + "/foo"}>foo</a>
@luisuribe
luisuribe / gist:826504
Created February 14, 2011 20:41
git gist
// Clone just one branch
mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH
// Aliases and stuff
git config --global user.name "Luis Uribe"