Skip to content

Instantly share code, notes, and snippets.

Kingsley Davies kings13y

Block or report user

Report or block kings13y

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@kings13y
kings13y / AuthznIntent.scala
Created Feb 1, 2011
A sample snippet of code to show the intent for request authorization and data filtering
View AuthznIntent.scala
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 Mar 22, 2011
Variable pattern matching example
View VariablePatternMatch.scala
val x = "someVariable"
x match {
case xVarBound => "matched your variable.. it's value is: " + xVarBound
}
@kings13y
kings13y / ConstantPatternMatch.scala
Created Mar 22, 2011
Simple example of Constant pattern matching
View ConstantPatternMatch.scala
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 Mar 22, 2011
Simple Constructor Pattern Match example
View ConstructorPatternMatch.scala
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 Mar 22, 2011
Array Pattern matching in Scala
View ArrayPatternMatch.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 Mar 22, 2011
Simple Tuple pattern match
View TuplePatternMatch.scala
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 Mar 22, 2011
Array pattern match using the sequence wildcard
View ArrayPatterMatchWithWildcard.scala
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 Mar 23, 2011
Type pattern match example using Exceptions as the Types
View TypePatternMatchWithExceptions.scala
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 Mar 23, 2011
Simple example of using extraction in a for expression
View ForExpressionAndPatternMatching.scala
val doubleA1 = ("A", 1)
val doubleA2 = ("A", 2)
for((a,b) <- List(doubleA1, doubleA2)) {
println(a + " : " + b)
}
@kings13y
kings13y / XmlPatternMatchin.scala
Created Mar 23, 2011
Simple example of XML pattern matching
View XmlPatternMatchin.scala
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")
You can’t perform that action at this time.