Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more examples to the source code
- Loading branch information
Miguel Pastor
committed
Mar 20, 2012
1 parent
314e05c
commit 0f96e7b
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...ain/scala/com/blogspot/miguelinlas3/scala/intro/highorderunction/search/SearchFiles.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...blogspot/miguelinlas3/scala/intro/highorderunction/transaction/TransactionalService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} | ||
|
||
} | ||
} |