Skip to content

Instantly share code, notes, and snippets.

package ru.lester.shapeless
import shapeless._
import poly._, record._, syntax._, syntax.singleton._, labelled.{FieldType, field}
object MapOverBookGeneric {
case class Book(author: String, title: String, id: Int, price: Double)
val bookGen = LabelledGeneric[Book]
package ru.lester.shapeless
import shapeless._
object ShowGeneric {
trait Show[T] {
def show(t: T): String
}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE RankNTypes #-}
module Main where
import Control.Applicative
import Control.Monad
import Control.Monad.State
@vivanov
vivanov / MonadListenOps.scala
Created October 4, 2014 18:26
An example of Monad Listen usage
package ru.lester.scalaz7.monadlisten
import scalaz._, Scalaz._
object MonadListenOps {
implicit val monadListen = EitherT.monadListen[Writer, List[String], String]
def validateNegative(x: Int) = if(x < 0) monadListen.left[Int]("Number is negative") else monadListen.right[Int](x)
def validateEmpty(str: String) = Option(str).fold(monadListen.left[String]("String is null")){s => if(s.isEmpty || s.forall(_.isWhitespace)) monadListen.left[String]("String is empty") else monadListen.right[String](s)}
@vivanov
vivanov / TeletypeOps.scala
Last active August 29, 2015 14:07
An example of simple teletype program with Scalaz Free monad
package ru.lester.scalaz7.free
import scalaz._
import scalaz.effect._
import Scalaz._
import IO._
sealed trait TeletypeF[+S]
case class PrintLn[+S](a: String, s: S) extends TeletypeF[S]
@vivanov
vivanov / poly_func.scala
Created December 14, 2012 20:52
Polymorphic function values example
trait ~>[F[_], G[_]] {
def apply[T](f: F[T]): G[T]
}
object last extends (List ~> Option) {
def apply[T](l: List[T]): Option[T] = l match {
case x::Nil=> Some(x)
case _::xs => last(xs)
case _ => None
}
@vivanov
vivanov / my_quicksort.scala
Created July 9, 2012 10:42
My implementation of QuickSort algorithm
object QuickSort extends App {
sealed trait PivotStrategy
case class First() extends PivotStrategy
case class Last() extends PivotStrategy
case class Medium() extends PivotStrategy
//val array = Array(3, 8, 2, 5, 1, 4 ,7, 6)
var total = 0
@vivanov
vivanov / iter_naming_enum.scala
Created January 2, 2012 22:21
Example of Scalaz Iteratee dealing with legacy NamingEnumeration interface
// returns map of pairs (attribute name -> list of attribute values)
def getAttributesMap(dn: String) : Option[Map[String, List[String]]] = {
val attributes: Option[Attributes] = // get javax.naming.directory.Attributes from LDAP
attributes.flatMap(attrs => {
val allAttributes = Option(attrs.getAll());
allAttributes.map(allAttrs => {
val res = enumNamingEnumeration(allAttrs, accum, get) map (_.run)
res.unsafePerformIO ~> Map.empty[String, List[String]]
})
})