Skip to content

Commit

Permalink
Added more examples to the source code
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Pastor committed Mar 20, 2012
1 parent 314e05c commit 0f96e7b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
@@ -0,0 +1,25 @@
package com.blogspot.miguelinlas3.scala.intro.highorderunction.search

object Search {

private def filesInCurrentFolder = (new java.io.File(".")).listFiles

private def filesMatching(matcher: String => Boolean) =
for (file <- filesInCurrentFolder; if matcher(file.getName)) yield file

def filesEndingWith(query: String) = filesMatching(_.endsWith(query))

def filesContaining(query: String) = filesMatching(_.contains(query))

def filesMatchRegex(query: String) = filesMatching(_.matches(query))


def main (args : Array[String]) : Unit = {

val foundFiles = filesEndingWith("sbt")

for (file <- foundFiles) {
println (file)
}
}
}
@@ -0,0 +1,43 @@
package com.blogspot.miguelinlas3.scala.intro.highorderunction.transaction

import com.blogspot.miguelinlas3.scala.intro.highorderunction.transaction.manager.{ MockTransactionManager, TransactionManagement }

class TransactionalService(txManager: MockTransactionManager) extends TransactionManagement(txManager) {

def sum(x: Int, y: Int): Int = {
transactional[Int]() {
x + y
}
}

def buggy(x: Int, y: Int): Int = {
transactional[Int]() {
val result = x + y
if (result == 2)
throw new Exception()
result
}
}
}

object TransactionalService {

def main(args: Array[String]): Unit = {
def transactionalService = new TransactionalService(new MockTransactionManager())

for (index <- 1 to 10) {
val result = transactionalService.sum(index, index)
println("Result:" + result)
}

for (index <- 1 to 10) {
try {
val result = transactionalService.buggy(index, index)
println("Result:" + result)
} catch {
case e => println("An error has ocurred and the transaction has been rolled back")
}
}

}
}

0 comments on commit 0f96e7b

Please sign in to comment.