Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
xuwei-k / functor.scala
Last active December 28, 2015 20:38 — forked from gakuzzzz/functor.scala
trait Functor[A, F[_]] { self =>
def fmap[B](f: A => B): F[B]
}
trait ~>[-F[_], +G[_]]{
def apply[A](fa: F[A]): G[A]
}
object Functor {
@xuwei-k
xuwei-k / -Xprint:jvm
Last active December 26, 2015 16:48 — forked from okapies/gist:7182102
[[syntax trees at end of jvm]] // Param.scala
package <empty> {
final class Param extends Object {
<paramaccessor> private[this] val i: Int = _;
<stable> <accessor> <paramaccessor> def i(): Int = Param.this.i;
override <synthetic> def hashCode(): Int = Param.hashCode$extension(Param.this.i());
override <synthetic> def equals(x$1: Object): Boolean = Param.equals$extension(Param.this.i(), x$1);
def <init>(i: Int): Param = {
Param.this.i = i;
Param.super.<init>();
@xuwei-k
xuwei-k / Prob44_2.scala
Last active December 18, 2015 13:39 — forked from ponkotuy/Prob44_2.d
object Prob44_2 {
def pentagonal(n: Int) = n*(3*n - 1)/2
private[this] val pentagonals = Iterator.from(0).map(pentagonal).takeWhile(_ < (Int.MaxValue >> 2)).toArray
def isPenta(n: Int) = {
val dbl = (1.0 + math.sqrt(1 + 24*n))/6.0
val int = dbl.toInt
pentagonal(int) == n || pentagonal(int + 1) == n
}
scala> class Bippy(xs: List[Int]) extends improving.TypesafeProxy(xs) { def isEmpty = true }
defined class Bippy
scala> val bippy = new Bippy(1 to 10 toList)
bippy: Bippy = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> bippy.slice(3, 7)
[proxy] $line4.$read.$iw.$iw.bippy.slice(3, 7)
res1: List[Int] = List(4, 5, 6, 7)
import scala.language.higherKinds
import scala.language.implicitConversions
trait Forall[M[_]] {
def apply[A]: M[A]
}
trait Maybe[A] extends Forall[({ type F[R] = (A => R, => R) => R })#F]
object Maybe {
The z in scalaz means this: it was early 2008 and I was working for a Java consultancy and so of course, I used the most appropriate tool for the job: scala. But it had *terrible* libraries, so I offered to fix those while also meeting my other objectives. Turns out that the Scala guys were extremely hostile to even half-decent libraries (and still are to this day). I still struggle to wrap my head around this sometimes.
Anyway, so I thought, well fuck it, I will just keep them to myself for now. My (awesome) employer had already agreed that we'd probably open-source such a thing, but I was concerned most about my primary goal. So then it came time to "name" this library. I had named it "scalax" simply so that I did not have the inclination to think of a proper name. Then I found out that such a library was being developed and with my internal name! Whatever, I thought.
So I looked at this scalax library, hoping that I could just abandon my efforts and jump on board with everyone else -- they too had figure
import sbt._
import Keys._
import Build.data
object build extends Build {
lazy val runAll = TaskKey[Unit]("run-all")
lazy val standardSettings = Seq(
runAllIn(Compile)
)
@xuwei-k
xuwei-k / test.scala
Created February 1, 2013 18:14 — forked from paulp/test.scala
package s
object Test {
// Observe that x.companion is statically typed such that foo is callable
def f1() = {
val x = new Foo
println(x) // Foo instance
println(x.companion) // Foo companion
println(x.companion.foo) // I'm foo!
import scala.reflect.runtime.universe._
import scala.reflect._
object App{
def main(args : Array[String]){
val m = toMap(User("Kudo",2,false))
println(m)
//Map(admin_? -> false, gender -> 2, name -> Kudo)
@xuwei-k
xuwei-k / TypeClass.hs
Created November 29, 2012 07:20 — forked from tonymorris/TypeClass.hs
Type-class hierarchy
{-# LANGUAGE NoImplicitPrelude, MultiParamTypeClasses, Rank2Types, TypeOperators #-}
newtype Id a =
Id a
data a :\/ b =
Left a
| Right b
data a :/\ b =