Skip to content

Instantly share code, notes, and snippets.

View timperrett's full-sized avatar
🚧

Timothy Perrett timperrett

🚧
View GitHub Profile
def sitemap = SiteMap(
Menu("Home") / "index" >> LocGroup("public"),
Menu("Search") / "search" >> LocGroup("public"),
Menu("History") / "history" >> LocGroup("public"),
Menu("Auctions") / "auctions" >> LocGroup("public"),
Menu("Auction Detail") / "auction" >> LocGroup("public"),
// admin
Menu("Admin") / "admin" / "index" >> LocGroup("admin"),
Menu("Suppliers") / "admin" / "suppliers" >> LocGroup("admin") submenus(Supplier.menus : _*),
Menu("Auction Admin") / "admin" / "auctions" >> LocGroup("admin") submenus(Auction.menus : _*),
def submit: JsCmd = {
for(amountString <- amountBox ?~ "Amount not entered";
amount <- tryo(BigDecimal(amountString).longValue) ?~! "Amount not a number"
) yield (
tryo(new Bid().auction(auction).customer(Customer.currentUser).amount(amount).save)
) match {
case Full(x) => S.notice("Great, your bid was accepted!")
case Failure(msg, _, _) => S.error(msg)
case _ => S.warning("Unable to place bid at this time.")
}
def bid(xhtml: NodeSeq): NodeSeq = {
var amountBox: Box[String] = Empty
def submit = {
for(as <- amountBox ?~ "Amount not entered";
amount <- tryo(BigDecimal(as).longValue) ?~! "Amount not a number"
) yield {
new Bid().auction(auction).customer(Customer.currentUser).amount(amount).save
}
} match {
case Full(x) => S.notice("Great, your bid was accepted!")
def bid(xhtml: NodeSeq): NodeSeq = {
var amountBox: Box[String] = Empty
def submit = {
for(ass <- amountBox ?~ "Amount not entered";
amo <- tryo(BigDecimal(ass).doubleValue) ?~! "Amount not a number";
auc <- auction;
vld <- tryo(amo).filter(_ >= auc.nextAmount) ?~ "Less that required amount!"
) yield {
new Bid().auction(auction).customer(Customer.currentUser).amount(amo).save
}
class Details extends StatefulSnippet with AuctionHelpers with Loggable {
val dispatch: DispatchIt = {
case "show" => show _
case "bid" => bid _
}
val currentAmountSpan = "current_amount"
val nextAmountSpan = "next_amount"
val amountInputId = "amount"
val xml =
{ if(S.errors.isEmpty) Nil else List(MsgsErrorMeta.get, f(S.errors), S.??("msg.error"), LiftRules.noticesContainerId + "_error") } ++
{ if(S.warnings.isEmpty) Nil else List((MsgsWarningMeta.get, f(S.warnings), S.??("msg.warning"), LiftRules.noticesContainerId + "_warn")) } ++
{ if (S.notices.isEmpty) Nil else List((MsgsNoticeMeta.get, f(S.notices), S.??("msg.notice"), LiftRules.noticesContainerId + "_notice")) } flatMap {
msg => msg._1 match {
case Full(meta) => <div id={msg._4}>{func(msg._2 _, meta.title openOr "",
meta.cssClass.map(new UnprefixedAttribute("class", _, Null)) openOr Null)}</div>
case _ => <div id={msg._4}>{func(msg._2 _, msg._3, Null)}</div>
}
}
[INFO] /Users/timperrett/repositories/lift/lift-framework/framework/lift-persistence/lift-squeryl-record/src/main/scala:-1: info: compiling
[INFO] Compiling 4 source files to /Users/timperrett/repositories/lift/lift-framework/framework/lift-persistence/lift-squeryl-record/target/classes at 1279630839609
[ERROR] /Users/timperrett/repositories/lift/lift-framework/framework/lift-persistence/lift-squeryl-record/src/main/scala/net/liftweb/squerylrecord/RecordTypeMode.scala:35: error: not found: type StringExpression
[INFO] new SelectElementReference[Option[StringType]](FieldReferenceLinker.takeLastAccessedFieldReference.get)(createOutMapperStringTypeOption) with StringExpression[Option[StringType]]
[INFO] ^
[ERROR] /Users/timperrett/repositories/lift/lift-framework/framework/lift-persistence/lift-squeryl-record/src/main/scala/net/liftweb/squerylrecord/RecordTypeMode.
LiftRules.dispatch.append {
case r@Req("some" :: "path" :: Nil,"",GetRequest) => {
val accept = in.headers("accept")
(accept.find(_.toLowerCase.indexOf("text/xml") >= 0).isDefined ||
((in.path.suffix equalsIgnoreCase "xml") && (accept.isEmpty ||
accept.find(_.toLowerCase.indexOf("*/*") >= 0).isDefined)))
? Full(XmlResponse(...)) | Full(JsonReponse(...))
}
}
@timperrett
timperrett / KMeans.scala
Created October 22, 2010 10:45
K-Means Clustering
import scalala.Scalala._;
import scalala.tensor._;
import scalala.tensor.dense_;
object KMeans {
// Returns the centers of the k clusters, and the clusters themselves
def cluster(data: Seq[Vector], k: Int): Seq[Vector] = {
var means = data.take(k);
var converged = false;
@timperrett
timperrett / gist:712032
Created November 23, 2010 16:29
The book store domain
case class Book(publisher: String, title: String)
object Bookshop {
val stock = List(
Book("Bloomsbury", "Harry Potter and the Deathly Hallows"),
Book("Bloomsbury", "Harry Potter and the Goblet of Fire"),
Book("Manning", "Scala in Depth"),
Book("Manning", "Lift in Action")
)
}