Skip to content

Instantly share code, notes, and snippets.

@n4to4
n4to4 / ScalaExercisesShapeless.scala
Created August 22, 2016 14:56
ScalaExercisesShapeless
import shapeless._
import scala.language.implicitConversions
// object Main extends App {
// }
object HeterogenousLists extends App {
import poly._
object choose extends (Set ~> Option) {
@n4to4
n4to4 / Decode.scala
Created August 29, 2016 06:44
Decoding JSON into Shapeless's sized collections in Scala with circe:
// http://stackoverflow.com/questions/39183041/circe-type-level-json-a-function/39183581#39183581
// https://twitter.com/travisbrown/status/769579633771773954
object Main extends App {
import cats.data.Xor
import io.circe.{Decoder, DecodingFailure}
import shapeless.{Nat, Sized}
import shapeless.ops.nat.ToInt
import shapeless.syntax.sized._
@n4to4
n4to4 / gist:0a1731b946eeb7085c1cb36ae98df7e2
Created August 30, 2016 02:01 — 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]
@n4to4
n4to4 / TATB.scala
Created September 5, 2016 04:07
constraint-on-hlist-check-for-single-occurrence-of-a-type
/**
* http://stackoverflow.com/questions/39226147/constraint-on-hlist-check-for-single-occurrence-of-a-type/39226608#39226608
*/
import shapeless._, ops.hlist.{ToList}
trait T
case class TA() extends T
case class TB() extends T
@n4to4
n4to4 / Day7.scala
Created September 7, 2016 21:43
State examples from herding-cats
object St0 {
type Stack = List[Int]
def pop(s0: Stack): (Stack, Int) =
s0 match {
case x :: xs => (xs, x)
case Nil => sys.error("stack is empty")
}
def push(s0: Stack, a: Int): (Stack, Unit) =
@n4to4
n4to4 / firstSecond.scala
Last active September 12, 2016 22:56
Scala equivalent of Haskell first and second
import scalaz._, Scalaz._
object Main extends App {
// Scala equivalent of Haskell first and second
// http://stackoverflow.com/questions/39457088/scala-equivalent-of-haskell-first-and-second
type T2 = Tuple2[Int, Int]
val f = (n: Int) => n + 1
val g = (n: Int) => n * 100
import shapeless._
import shapeless.test._
trait Weight[A] { def apply(a: A): Int }
object Weight {
def apply[A](f: A => Int) = new Weight[A] { def apply(a: A) = f(a) }
}
case class Foo(i: Int, s: String)
@n4to4
n4to4 / iteratee.scala
Created September 20, 2016 21:49
iteratee sample
object Main extends App {
import io.iteratee.modules.id._
val singleNumE = enumOne(42)
val singleNumI = takeI[Int](1)
val singleNumResult = singleNumE.into(singleNumI)
println(singleNumResult)
val incrementNumEE = map[Int, Int](_ + 1)
val incrementedNumResult = singleNumE.through(incrementNumEE).into(singleNumI)
@n4to4
n4to4 / circe.scala
Created September 20, 2016 21:50
circe example
object CirceMain extends App {
import io.circe.Encoder
import io.circe.syntax._
case class Person(firstName: String, lastName: String, age: Int)
val person = Person("Joe", "Black", 42)
{
implicit val personEnc: Encoder[Person] = Encoder.forProduct3(
"firstName", "lastName", "age") {
@n4to4
n4to4 / poly.scala
Created September 25, 2016 21:44
Using a polymorphic function to extract an object from Options
// Using a polymorphic function to extract an object from Options
// http://stackoverflow.com/questions/39628022/using-a-polymorphic-function-to-extract-an-object-from-options/39636969#39636969
object Main extends App {
import scalaz._, syntax.std.option._
import shapeless._, shapeless.poly.~>
val options = 1.some :: "A".some :: 3.5.some :: HNil
object uuu extends (Option ~> Id) {