Skip to content

Instantly share code, notes, and snippets.

@kings13y
kings13y / AuthznIntent.scala
Created February 1, 2011 20:52
A sample snippet of code to show the intent for request authorization and data filtering
object AuthznIntent {
// AuthorizationRequest is a template for a Rule in the system (basically, a rule with no actions on the Rule result)
// By default the example is referencing variables as per entities that exist in the JAAS model, namely having the 'Principal' variable act as the identity of the caller and a number of subjects, aka user credentials, which would be associated with the Princicpal.
// The requestor id is also acting as the users token for service innvocation (aka a session scoped token), and the account Id represents the entity on which we
// are perfroming the action
case class AuthorizationRequest(requestorId: String, operationName: String, principal: String, subjects: Set[String], accountId: String)
// The expected response from the operation. Rules could be enacted before calling the action (preventative rules) or after (filtering rules)
case class PayloadResponse(var attribA : String = "a", var attribB : String = "b", var attribC : String = "c")
@kings13y
kings13y / VariablePatternMatch.scala
Created March 22, 2011 22:30
Variable pattern matching example
val x = "someVariable"
x match {
case xVarBound => "matched your variable.. it's value is: " + xVarBound
}
@kings13y
kings13y / ConstantPatternMatch.scala
Created March 22, 2011 22:19
Simple example of Constant pattern matching
val FOO = 1
val BAR = 2
val x = 2
x match {
case FOO => "matched a FOO !"
case BAR => "matched a BAR !"
case _ => "matched NOTHING !"
}
@kings13y
kings13y / ConstructorPatternMatch.scala
Created March 22, 2011 22:41
Simple Constructor Pattern Match example
case class Person(firstname: String, surname: String)
val p = Person("K", "D");
p match {
case Person("A", _) => "Person A"
case Person("K", _) => "Person K"
case _ => "missing person"
}
@kings13y
kings13y / ArrayPatternMatch.scala
Created March 22, 2011 23:09
Array Pattern matching in Scala
val oneToFour = List(1, 2, 3, 4)
def sumElements(listOfInts: List[Int]) : Int = listOfInts match {
case List() => 0 // If it is an empty list, just return zero
case head :: rest => head + sumElements(rest) // Otherwise, recurse and sum elements
}
sumElements(oneToFour)
@kings13y
kings13y / TuplePatternMatch.scala
Created March 22, 2011 23:39
Simple Tuple pattern match
val doubleA1 = ("A", 1)
val doubleA2 = ("A", 2)
val doubleB1 = ("B", 3)
def tupleMatcher(tuple: Tuple2[String, Int]) = tuple match {
case (_,1) => "matched A1 !"
case (_,2) => "matched A2 !"
case (a,b) if(a == "B") => "matched and using a guard for B !"
}
@kings13y
kings13y / ArrayPatterMatchWithWildcard.scala
Created March 22, 2011 23:49
Array pattern match using the sequence wildcard
val oneToFour = List(1, 2, 3, 4)
val odds = List(1, 3, 5, 7)
def findWhereSecondElemIsAThree(listOfInts: List[Int]) = listOfInts match {
case List(_, 3, _*) => "Found a list where second element == 3"
case List(_*) => "List with 0 or more elements"
}
findWhereSecondElemIsAThree(oneToFour)
findWhereSecondElemIsAThree(odds)
@kings13y
kings13y / TypePatternMatchWithExceptions.scala
Created March 23, 2011 00:03
Type pattern match example using Exceptions as the Types
import java.io.{FileReader,FileNotFoundException,IOException}
try {
val file = new FileReader("noSuchFile")
} catch {
case e: FileNotFoundException => println("File not found !")
// Handle missing file
case e: IOException => println("IO Exception !")
// Handle other I/O error
case e => e.printStackTrace()
@kings13y
kings13y / ForExpressionAndPatternMatching.scala
Created March 23, 2011 00:13
Simple example of using extraction in a for expression
val doubleA1 = ("A", 1)
val doubleA2 = ("A", 2)
for((a,b) <- List(doubleA1, doubleA2)) {
println(a + " : " + b)
}
@kings13y
kings13y / XmlPatternMatchin.scala
Created March 23, 2011 11:34
Simple example of XML pattern matching
import scala.xml._
val someXML = <a><b name="nameB1">valueB1</b><b name="nameB2">valueB2</b></a>
someXML match {
case <a>{b @ _* }</a> => for(theB <- b) println("MANUAL PATTERN MATCHING: " + theB \ "@name")
}
for(theB <- (someXML \\ "b")) println("BUILT IN XML SUPPORT: " + theB \ "@name")