Skip to content

Instantly share code, notes, and snippets.

View danstn's full-sized avatar
🚀
Binding. Lifting. Zipping.

Daniel Stankevich danstn

🚀
Binding. Lifting. Zipping.
View GitHub Profile
@danstn
danstn / gist:c9725862f5ec0cb69063
Created March 29, 2016 13:16 — forked from runarorama/gist:a8fab38e473fafa0921d
Compositional application architecture with reasonably priced monads
sealed trait Interact[A]
case class Ask(prompt: String)
extends Interact[String]
case class Tell(msg: String)
extends Interact[Unit]
trait Monad[M[_]] {
def pure[A](a: A): M[A]
@danstn
danstn / free-monad-example.hs
Last active January 4, 2017 01:14
An example of using Free Monads for writing custom AST/DSL and its interpreters.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveFunctor #-}
import Prelude
import Data.String
import Control.Monad.Free
type Program a r = Free (AST a) r
data AST a next =
@danstn
danstn / watch.hs
Last active August 29, 2015 14:27
Very simple Haskell watcher
#!/usr/bin/env runhaskell
{-# LANGUAGE OverloadedStrings #-}
import Turtle
main :: IO ()
main = do
stdout (runWatch $ findSrc)
@danstn
danstn / chjava
Last active August 29, 2015 14:23
chjava bash function
function chjava() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $@`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
@danstn
danstn / opt_type.scala
Last active August 29, 2015 14:20
My Opt type implementation
sealed trait Opt[+A] {
def flatMap[T](f: A => Opt[T]): Opt[T] =
fold[Opt[T]](Nothing)(f)
def map[T](f: A => T): Opt[T] =
fold[Opt[T]](Nothing)(a => Something(f(a)))
def fold[B](g: => B)(f: A => B): B =
this match {
case Something(a) => f(a)
# An equilibrium index of a sequence is an index into the sequence
# such that the sum of elements at lower indices is equal to the
# sum of elements at higher indices.
def eq_indices(arr)
left, right = 0, arr.inject(0, :+)
result = []
arr.each_with_index do |el, i|
right -= el
result << i if right == left