Skip to content

Instantly share code, notes, and snippets.

View Mzk-Levi's full-sized avatar

Melchizedek Mzk-Levi

View GitHub Profile
@runarorama
runarorama / gist:6956002
Last active December 25, 2015 09:39
Three theories of value

Subjectivist theory: The subjectivist theory holds that the good resides in consciousness, independent of reality. If the wish or feeling of value exists in the mind, then that constitutes value. Specific versions differ on whose mind is involved. It could be God's, society's (i.e. other people's), or one's own. Both "it is a value because God says so" and "it is a value because my feelings say so" are instances of this theory. The subjectivist theory holds that the value bears no relation to the facts of reality, that it is the product of consciousness, created by feelings, desires, intuitions, or whims. The irreducible primary in this theory is: desire.

Intrincisist theory: The intrinsicist theory holds that the good resides in reality, independent of consciousness. That is "the good" is good in, by, and of itself, regardless of its relationship with anyone or anything else. "Gold is inherently valuable", and "always indent five spaces", and "we must always serve our country", are instances of this theor

@tonymorris
tonymorris / type-class.scala
Last active December 23, 2015 04:18
Scala type-class hierarchy
import annotation._
case class Pronunciation(pronounce: String) extends StaticAnnotation
case class Law(name: String, denotation: String) extends StaticAnnotation
trait ~>[F[_], G[_]] {
def apply[A]: F[A] => G[A]
}
case class Id[A](x: A)
@johnynek
johnynek / twophase.scala
Last active October 2, 2020 22:36
A two-phase commit Transaction Monad. See: http://en.wikipedia.org/wiki/Two-phase_commit_protocol
// Run this with scala <filename>
/**
* A Two-phase commit Monad
*/
trait Transaction[+T] {
def map[U](fn: T => U): Transaction[U] = flatMap { t => Constant(fn(t)) }
def flatMap[U](fn: T => Transaction[U]): Transaction[U] =
FlatMapped(this, fn)
@t0d3a
t0d3a / demo.scala
Last active December 21, 2015 17:19 — forked from travisbrown/demo.scala
def showTreeL(t: TL, depth: Int = 0): Unit = t match {
case LL(i) => printf("%s- %d\n", " " * depth * 2, i)
case FL(l) =>
printf("%s-\n", " " * depth * 2)
l.map((x) => showTreeL(x,depth + 1))
}
var zL = 0
def fL(i: Int): TL = if (i > 0) FL(List(fL(i - 1), fL(i - 1))) else { zL = zL + 1; LL(zL) }
@pthariensflame
pthariensflame / IndexedPrivilege.md
Last active November 7, 2019 10:58
An introduction to the indexed privilege monad in Haskell, Scala and C#.

The Indexed Privilege Monad in Haskell, Scala, and C#

We've already looked at two different indexed monads in our tour so far, so let's go for a third whose regular counterpart isn't as well known: the privilege monad.

Motivation

The regular privilege monad allows you to express constraints on which operations a given component is allowed to perform. This lets the developers of seperate interacting components be statically assured that other components can't access their private state, and it gives you a compile-time guarantee that any code that doesn't have appropriate permissions cannot do things that would require those permissions. Unfortunately, you cannot easily, and sometimes cannot at all, build code in the privilege monad that gains or loses permissions as the code runs; in other words, you cannot (in general) raise or lower your own privilege level, not even when it really should be a

@stew
stew / UnapplyMT.scala
Last active December 19, 2015 20:58
I wrote an Unapply for MonadTrans instances which would catch both things like OptionT[M[_],A] and EitherT[M[_], A, B], then couldn't remember what I was trying to solve by doing it. so I'll just stick it here for now
trait UnapplyMT[TC[_[_[_], _]], N[_], MTNA] {
/** the type constructor */
type MT[_[_], _]
/** The type that MT is applied to */
type A
/** The instance of the type class */
def TC: TC[MT]
@pthariensflame
pthariensflame / IndexedCont.md
Last active April 3, 2022 00:30
An introduction to the indexed continuation monad in Haskell, Scala, and C#.

The Indexed Continuation Monad in Haskell, Scala, and C#

The indexed state monad is not the only indexed monad out there; it's not even the only useful one. In this tutorial, we will explore another indexed monad, this time one that encapsulates the full power of delimited continuations: the indexed continuation monad.

Motivation

The relationship between the indexed and regular state monads holds true as well for the indexed and regular continuation monads, but while the indexed state monad allows us to keep a state while changing its type in a type-safe way, the indexed continuation monad allows us to manipulate delimited continuations while the return type of the continuation block changes arbitrarily. This, unlike the regular continuation monad, allows us the full power of delimited continuations in a dynamic language like Scheme while still remaining completely statically typed.

@YoEight
YoEight / conversion.scala
Last active December 18, 2015 01:48
Scala-machines Machine to Play Enumerator
import com.clarifi.machines._
import play.api.libs.iteratee.{ Enumerator, Input, Iteratee }
import play.api.libs.concurrent.Execution.Implicits._
import scala.concurrent.Future
def simple[K, E](machine: Machine[K, E]): Enumerator[E] = new Enumerator[E] {
def apply[A](start: Iteratee[E, A]): Future[Iteratee[E, A]] =
val result = machine.foldLeft(Future.successful(start)) { (future, value) ⇒
@zvozin
zvozin / BufferActor.scala
Last active December 17, 2015 09:19
Time-and-space bounded Scalaz-based actor. Buffers elements of type A until either buffer size has been reached, or the time bound has elapsed.
import scalaz.concurrent.Actor
import collection.mutable.ArrayBuffer
import java.util.Timer
/**
* Usage:
*
* <code>
* val buffer = BufferActor[String](onFlush = _ foreach (println(_), onError = println(_))
* buffer ! "Now we're talking!"
@halcat0x15a
halcat0x15a / gist:5376994
Created April 13, 2013 04:57
Maybe Monad with Church Encoding in Scala
import scala.language.higherKinds
import scala.language.implicitConversions
import scala.language.reflectiveCalls
trait Forall[M[_]] {
def apply[A]: M[A]
}
object Maybe {