Skip to content

Instantly share code, notes, and snippets.

View alaz's full-sized avatar

Alexander Azarov alaz

View GitHub Profile
@alaz
alaz / IfLoggedIn.scala
Created June 16, 2009 14:35
Liftweb's IfLoggedIn implementation
/*
* IfLoggedIn implementation which redirects to the page a user was going to,
* after his/her successful login
*
* based on http://groups.google.com/group/liftweb/browse_thread/thread/bdd5accd4bb623c9/d56e054c4037470f ,
* works with ProtoUser out of the box
*/
// In SiteMap:
lazy val IfLoggedIn = If(User.loggedIn_? _, loginAndComeBack _)
/**
* In reply to http://blog.xebia.com/2009/07/04/real-world-functional-programming-in-scala-comparing-java-clojure-and-scala/
*/
def opt[T](x: Seq[T]): Seq[T] = if (x == null) Nil else x
def indexed[T](coll: Seq[T]): Seq[(Int, T)] = {
val c = opt(coll)
List.range(0,c.length).zip(c.toList)
}
@alaz
alaz / Netbeans 6.7 + Scala config
Created July 9, 2009 11:20
Netbeans 6.7 + Scala config
Netbeans:
http://www.netbeans.org
Scala plugin для NetBeans 6.7 тут:
http://blogtrader.net/dcaoyuan/entry/scala_plugin_version_1_for
У меня еще сделаны такие настройки:
1. в файле NetBeans .../etc/netbeans.conf прописана JDK 1.6:
netbeans_jdkhome=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
/*
* Scala trait to hold Configuration Admin
*/
package com.yours.app
import org.osgi.service.cm.ConfigurationAdmin
trait ConfigAdminHolder {
protected var configAdmin: Option[ConfigurationAdmin] = None
// Sketch of an immutable domain model in Scala
// We used this scheme together with this JPA module:
// http://github.com/jboner/skalman/blob/d1e03a85be3964b9012f9e79dd726b0546342b2b/core/src/main/scala/JPA.scala
// ...and this GenericRepository:
// http://github.com/jboner/skalman/blob/d1e03a85be3964b9012f9e79dd726b0546342b2b/core/src/main/scala/GenericRepository.scala
abstract class Entity[T](val id: Int)
object User {
case class Word(word: String, count: Long) {
override def toString = word + ": " + count
def +(n: Long): Word = Word(word, count + n)
}
private def showWordCloud {
val words = statusTableModel.filteredStatuses.flatMap(_.text.split("\\s"))
val emptyMap = immutable.Map.empty[String, Word].withDefault(w => Word(w, 0))
val counts = words.foldLeft(emptyMap)((map, word) => map(word) += 1)
val countList = counts.values.toList.sort(_.count > _.count)
@alaz
alaz / mongo-scala-driver first example.scala
Created October 13, 2009 18:10
Simple example for mongo-scala-driver "Getting Started" Wiki page
// Model
case class User(val name: String)
// Factory object:
object User extends AbstractShape[User] {
// one scalar field called "name"
object name extends Scalar[String]("name", _.name) with Functional[String]
// fields list
override lazy val * = name :: Nil
def toArray(dbo: DBObject): Seq[Any] = {
def arrayValues(i: Int): Stream[Any] = {
val key = i.toString
if (dbo.containsField(key)) Stream.cons(dbo.get(key), arrayValues(i+1))
else Stream.empty
}
arrayValues(0).toList
//
//
@alaz
alaz / nginx.conf
Created March 6, 2010 15:44 — forked from johnthethird/nginx.conf
Edits and comments to the Nginx config for Riak
# Config for Nginx to act as a front-end for Riak
# The main goal is to proxy all GETs directly to Riak, and disallow anything else (POST, PUT, etc)
# Also, disallow use of the map/reduce query links (i.e. /riak/bucket/key/_,_,_)
# Config is in /etc/nginx/sites-available/default or somewhere like that
# Set up load-balancing to send requests to all nodes in the Riak cluster
# Replace these IPs/ports with the locations of your Riak nodes
upstream riak_hosts {
server 127.0.0.1:8098;
// import for MongoDB Scala Driver
import com.osinka.mongodb._ // <- this replaces Preamble
import com.osinka.mongodb.shape._
import com.mongodb.{Mongo,DBObject}
// Scala complains you are using "case class" here and I understand it. "class" is more
// appropriate as you have no arguments
class User extends MongoObject {
var id: Int = _
var name: String = _