Skip to content

Instantly share code, notes, and snippets.

@hexx
hexx / MonoidalLaw.scala
Created December 11, 2012 16:07
MonoidalLaw
trait MonoidalLaw extends FunctorLaw {
def iso1[A] = new (A <=> (Unit, A)) {
def to = a => ((), a)
def from = { case (_, a) => a }
}
def iso2[A] = new (A <=> (A, Unit)) {
def to = a => (a, ())
def from = { case (a, _) => a }
}
@hexx
hexx / board.coffee
Created October 3, 2012 19:50
board.coffee
$ ->
class Message extends Backbone.Model
class MessageList extends Backbone.Collection
url: "/messages"
Messages = new MessageList
class MessageView extends Backbone.View
tagName: "tr"
@hexx
hexx / board.scala
Created October 3, 2012 19:49
board.scala
package com.github.hexx.messageboard
import java.util.Date
import unfiltered.request._
import unfiltered.response._
import com.github.hexx.gaeds.{ Datastore, Mapper, Property }
import com.github.hexx.gaeds.Property._
import net.liftweb.json._
class Message(
@hexx
hexx / gaeds-0.2.0-sample2.scala
Created October 2, 2012 18:39
gaeds 0.2.0 description sample 2
// Model to JSON
(new Person("John", 13)).toJson // -> {"name":"John","age":13}
// JSON to Model
Person.fromJson("""{"name":"John","age":13}""")
@hexx
hexx / gaeds-0.2.0-sample1.scala
Created October 2, 2012 18:34
gaeds 0.2.0 description sample 1
// Low-Level API
val q = new Query("Person")
val f1 = new FilterPredicate("age", GREATER_THAN_OR_EQUAL, 10)
val f2 = new FilterPredicate("age", LESS_THAN_OR_EQUAL, 20)
q.setFilter(CompositeFilterOperator.and(f1, f2))
DatastoreServiceFactory.getDatastoreService.prepare(q).asIterator
// Gaeds
Person.query.filter(p => (p.age #>= 10) and (p.age #<= 20)).asIterator
@hexx
hexx / start-scalaz2-exercise4.scala
Created September 17, 2012 19:02
Start Scalaz2 Exercise4
def get(s: String): Kleisli[Option, Map[String, String], String] = Kleisli(_ get s)
@hexx
hexx / start-scalaz2-exercise3.scala
Created September 17, 2012 18:38
Start Scalaz2 Exercise3
lazy val h: String => Option[String] = _ |> f >>= g
@hexx
hexx / start-scalaz2-exercise2.scala
Created September 17, 2012 18:16
Start Scalaz2 Exercise2
def get[K, V](k: K)(m: Map[K, V]): String \?/ V =
try {
m(k).success
} catch {
case e: Throwable => e.getMessage.failure
}
def user(m: Map[String, String]): NonEmptyList[String] \?/ User =
(get("id")(m).toValidationNEL |@| get("age")(m).flatMap(parseInt).toValidationNEL)((id, age) => User(id, age.toInt))
@hexx
hexx / start-scalaz2-exercise1.scala
Created September 17, 2012 17:55
Start Scalaz2 Exercise1
def BuyAndCheck(book: Book): StateT[Option, Person, Unit] =
for {
_ <- buy(book).lift[Option]
_ <- check
} yield ()
@hexx
hexx / Test.scala
Created September 12, 2012 06:47
rpscala86-hygiene-2.scala
import Macros._
object Test extends App {
val a = 28
println("non-hygienic: " + nonHygienicA())
println("hygienic: " + hygienicA())
}