Skip to content

Instantly share code, notes, and snippets.

@n4to4
n4to4 / poly2.scala
Created September 25, 2016 22:08
Passing an extra argument into a polymorphic function?
// Passing an extra argument into a polymorphic function?
// http://stackoverflow.com/questions/39628068/passing-an-extra-argument-into-a-polymorphic-function/39637253#39637253
object Main extends App {
import shapeless._, shapeless.poly.~>
val lists = List(1,2) :: List("A", "B") :: List(1.1, 2.2) :: HNil
{
class sss(i: Int) extends (List ~> Set) {
@n4to4
n4to4 / CustomTypeClassDepFn2.scala
Created September 25, 2016 22:32
Pick out the Nth element of a HList of Lists and return that value as a HList of values
// Pick out the Nth element of a HList of Lists and return that value as a HList of values
// http://stackoverflow.com/questions/39584034/pick-out-the-nth-element-of-a-hlist-of-lists-and-return-that-value-as-a-hlist-of/39594978#39594978
object Main extends App {
import shapeless._
val a = List(1,2,3) :: List("a", "b", "c") :: List(true, false, true) :: HNil
trait RowSelect[L <: HList] extends DepFn2[L, Int] {
type Row <: HList
@n4to4
n4to4 / endo.scala
Created September 26, 2016 14:28
endo.scala
object Main extends App {
val f = (_: Int) * 2
val g = (_: Int) + 2
val h = (_: Int) * 3
{
import scalaz.{IList, Endo}
import scalaz.syntax.foldable._
val hh = IList(
@n4to4
n4to4 / doobie.scala
Last active September 27, 2016 22:49
doobie.scala
// https://www.scala-exercises.org/doobie
import doobie.imports._
import scalaz._, Scalaz._
import scalaz.concurrent.Task
import shapeless._
object Main extends App {
val program = 42.point[ConnectionIO]
val xa = DriverManagerTransactor[Task](
@n4to4
n4to4 / TypeclassVsSubtyping.scala
Created September 30, 2016 04:07
typeclass vs subtyping
// https://github.com/adelbertc/faq/blob/master/src/main/tut/typeclasses.compiled.md
object Main extends App {
def sumInts(list: List[Int]): Int = list.foldRight(0)(_ + _)
def concatStrings(list: List[String]): String = list.foldRight("")(_ ++ _)
def unionSets[A](list: List[Set[A]]): Set[A] = list.foldRight(Set.empty[A])(_ union _)
trait Monoid[A] {
@n4to4
n4to4 / stream.scala
Created September 30, 2016 09:48
stream.scala
object Main extends App {
type II = Int \/ Int
def l(idx: Int): II = { println(s"l($idx)"); 0.left }
def r(idx: Int): II = { println(s"r($idx)"); 1.right }
var idx = -1
val xs: Stream[II] = Stream(0, 0, 1, 0, 1) map { x => idx += 1; if (x == 0) l(idx) else r(idx) }
def leftIsLeft()
@n4to4
n4to4 / fold.scala
Created September 30, 2016 10:05
fold
import scalaz._, Scalaz._
object Main extends App {
implicit class verbose[A] (a: A) {
def v[B](log: B): A = {
println(log)
a
}
}
@n4to4
n4to4 / poly.scala
Created October 3, 2016 01:05
map HList with default
// http://stackoverflow.com/questions/39794943/map-hlist-with-default/39795173#39795173
import shapeless._
case class Foo()
trait Wrapper
case class S(s: String) extends Wrapper
case class I(i: Int) extends Wrapper
@n4to4
n4to4 / scalaz.scala
Created October 4, 2016 13:32
scalaz.scala
import shapeless._
import scalaz._, Scalaz._
object Main extends App {
case class Foo(x: String, y: Int, z: Boolean)
case class Bar(x: String, y: Int, z: Boolean)
val fooGen = Generic[Foo]
val barGen = Generic[Bar]
@n4to4
n4to4 / cofree.scala
Created October 5, 2016 02:06
cofree
import scalaz._, scalaz.syntax.show._
import atto._, Atto._
object Main extends App {
case class Prof[A](
name: String,
age: Int,
students: List[A]
)