Skip to content

Instantly share code, notes, and snippets.

<!DOCTYPE html>
<html>
<head>
<title>Front-end Setup</title>
</head>
<body>
<h2>Frontend Setup</h2>
</body>
</html>
@aakashns
aakashns / index.html
Last active December 27, 2015 22:19
added bootstrap css and js
<!DOCTYPE html>
<html>
<head>
<title>Front-end Setup</title>
<link rel="stylesheet" href="../../static/lib/css/bootstrap.min.css">
</head>
<body>
<div class="container">
@aakashns
aakashns / PairGroup.scala
Last active August 29, 2015 14:17
Group type class with context bounds
implicit def pairGroup[T1: Group, T2: Group]: Group[(T1, T2)] = {
val t1Group = implicitly[Group[T1]]
val t2Group = implicitly[Group[T2]]
new Group[(T1, T2)] {
val zero = (t1Group.zero, t2Group.zero)
def plus(x: (T1, T2), y: (T1, T2)): (T1, T2) = (x, y) match {
case ((x1, x2), (y1, y2)) =>
(t1Group.plus(x1, y1), t2Group.plus(x2, y2))
@aakashns
aakashns / 0_reuse_code.js
Last active August 29, 2015 14:17
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@aakashns
aakashns / GroupAnyVal.scala
Last active August 29, 2015 14:17
Group type class with helper class and implicit class for syntax
object GroupSyntax {
implicit class GroupOps[T](val x: T) extends AnyVal {
def |+|(y: T)(implicit ev: Group[T]): T = ev.plus(x, y)
def inverse(implicit ev: Group[T]): T = ev.inverse(x)
def |-|(y: T)(implicit ev: Group[T]): T = ev.minus(x, y)
}
def zero[T: Group]: T = Group[T].zero
}
@aakashns
aakashns / DoubleGroup.scala
Last active August 29, 2015 14:17
Group type class with no improvements
implicit object DoubleGroup extends Group[Double] {
val zero: Double = 0.0
def plus(x: Double, y: Double): Double = x + y
def inverse(x: Double): Double = -x
}
@aakashns
aakashns / Group.scala
Last active August 29, 2015 14:17
Group type class with apply method.
object Group {
def apply[T: Group]: Group[T] = implicitly[Group[T]]
// Instances of Group[T]...
}
@aakashns
aakashns / Group.scala
Last active August 29, 2015 14:17
Group type class with companion
object Group extends TypeClassCompanion[Group] {
// Instances ...
}
@aakashns
aakashns / Group.scala
Last active August 29, 2015 14:17
Group type class with implicit annotation
import annotation.implicitNotFound
@implicitNotFound("No member of type class Group found for type ${T}")
trait Group[T] {
// code ..
}
@aakashns
aakashns / sum.scala
Last active August 29, 2015 14:17
sum, sumNonEmpty and sumDifference
// xs.foldLeft(z)(op) reduces to
// (..(((z op x(0)) op x(1)) op x(2)) ... op x(n))
def sum(elems : Seq[Int]): Int =
elems.foldLeft(0) { _ + _ }