Skip to content

Instantly share code, notes, and snippets.

// http://stackoverflow.com/a/2257844/4933928
import collection.mutable.Buffer
import java.{lang => jl, util => ju}
trait ==>>[A, B] extends (A => B) {
def apply(a: A): B
}
object ==>> {
@mathhun
mathhun / reflectiontypeclass.scala
Created May 4, 2016 04:15
reflection vs typeclass
object Main extends App {
import scala.reflect._
import scala.reflect.runtime.universe._
def foo[A: TypeTag](x: A) = typeOf[A] match {
case t if t <:< typeOf[AnyVal] => println(s"anyval: $x")
//case t if t <:< typeOf[AnyRef] => println(s"anyref: $x")
case _ => println(s"else: $x")
}
foo(1)
shellPrompt := { state =>
def textColor(color: Int) = { s"\033[38;5;${color}m" }
def backgroundColor(color:Int) = { s"\033[48;5;${color}m" }
def reset = { s"\033[0m" }
def formatText(str: String)(txtColor: Int, backColor: Int) = {
s"${textColor(txtColor)}${backgroundColor(backColor)}${str}${reset}"
}
val red = 1
val green = 2
@mathhun
mathhun / build.sbt
Created February 23, 2016 22:47
ssh
scalaVersion := "2.11.7"
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.2.0"
libraryDependencies += "com.decodified" %% "scala-ssh" % "0.7.0"
@mathhun
mathhun / validation.scala
Last active February 19, 2016 22:22
learning scalaz
//object Z {
object Z extends App {
import scalaz._
import Scalaz._
import scalaz.Validation.FlatMap._
def loadCsv(): List[String] = {
List("1,me,3,4", "2,he,0,100", "No3,aaaaaaaaaaaaaaaa,1,10")
}
@mathhun
mathhun / learn.hs
Last active October 20, 2015 16:39
example from LearnYouAHaskell
module Main where
import Control.Monad.Identity
import Control.Monad.State
import Control.Monad.Writer
type Stack = [Int]
type WriterStack a = WriterT [String] (StateT Stack Identity) a
run :: WriterStack a -> Stack -> ((a, [String]), Stack)
@mathhun
mathhun / fib.rb
Last active August 29, 2015 14:26
fibonacci numbers
#!/usr/bin/env ruby
require 'benchmark'
module FibSlow
def self.fib(n)
if n <= 1
return n
end
package main
import "fmt"
// 2x2 matrix multiplication
func mul(a, b [4]int) [4]int {
var ret [4]int
ret[0] = (a[0]*b[0] + a[1]*b[2]) % 1000
ret[1] = (a[0]*b[1] + a[1]*b[3]) % 1000
ret[2] = (a[2]*b[0] + a[3]*b[2]) % 1000
#!/usr/bin/env ruby
N = 10 ** 8
def exp(a, n)
y = if n <= 2
a ** n
elsif n.even?
x = exp(a, n/2)
x * x
numbers = []
while n = gets
numbers.push n.to_i
end
ntimes = 0
sorted = numbers.sort
indexes = []
numbers.each_with_index do |n, idx|