Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
married

Vlad Patryshev vpatryshev

💭
married
View GitHub Profile
@kristopherjohnson
kristopherjohnson / lunar.rs
Last active Aug 15, 2020
Translation of classic Lunar Lander game from FOCAL to Rust
View lunar.rs
//! Translation of
//! <http://www.cs.brandeis.edu/~storer/LunarLander/LunarLander/LunarLanderListing.jpg>
//! by Jim Storer from FOCAL to Rust.
use std::error::Error;
use std::io;
use std::io::prelude::*;
use std::marker::{Send, Sync};
use std::process;
use std::str::FromStr;
View LinVect.scala
object LinVect {
type K = Double
trait NAT[N] { val nat: Int }
def NAT[N: NAT]: NAT[N] = implicitly
def nat[N: NAT]: Int = NAT[N].nat
trait NAT_10
implicit object NAT_10 extends NAT[NAT_10]
@zraffer
zraffer / CAT.scala
Created Nov 6, 2016
sample of abuse of Java/Scala type system for simulate given formal system
View CAT.scala
package cat
object CAT {
// system traits
sealed trait Type[Self <: Type[Self]]
sealed trait Of[Self <: Of[Self, T], T <: Type[T]]
// types
case class Ob()
@zraffer
zraffer / CCC.scala
Created Nov 4, 2016
Scala Types are objects of Cartesian Closed Category
View CCC.scala
// Scala Types are objects of Cartesian Closed Category
// (w/o equalities, probably not a category, sorry)
object CCC {
// category structure
def id[T0]: T0=>T0 = x0=>x0
def mul[T1, T2, T3](f23: T2=>T3, f12: T1=>T2): (T1=>T3) = x1 => f23(f12(x1))
// terminal object; adjunction;
type _1_ = Unit
@zraffer
zraffer / package.scala
Last active Apr 26, 2017
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
@jaytaylor
jaytaylor / EnvHacker.scala
Created Aug 21, 2014
Setting environment variables in Scala JVM
View EnvHacker.scala
trait EnvHacker {
/**
* Portable method for setting env vars on both *nix and Windows.
* @see http://stackoverflow.com/a/7201825/293064
*/
def setEnv(newEnv: Map[String, String]): Unit = {
try {
val processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment")
val theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment")
theEnvironmentField.setAccessible(true)
View ::.scala
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 Nov 28, 2021
A mini STM if you will. I've made a'Transactional' map that mutates in a referentially transparent way.
View TxMapTest.scala
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 Dec 17, 2015
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"
View JValueWithFilter.scala
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 Dec 17, 2015
Pimping Iterable to have zipMany/unzipMany operations
View gist:5537188
// 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