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
// Here is how I would curry a product function. I really like the word curry, btw. | |
def product(f: Int => Int)(a: Int, b: Int): Int = | |
if (a > b) 1 else f(a) * product(f)(a + 1, b) | |
// Factorial can now be written as follows | |
// Because factorial is by definition the product of all numbers 1 to n | |
def factorial(n: Int) = product(identity)(1, n) | |
// True! | |
factorial(5) == 120 |
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
/** | |
* 2. Purely Functional Sets. | |
*/ | |
trait FunSets { | |
/** | |
* We represent a set by its characteristic function, i.e. | |
* its `contains` predicate. | |
*/ | |
override type FunSet = Int => Boolean |
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
trait List[T] { | |
def isEmpty:Boolean | |
def head: T | |
def tail: List[T] | |
} | |
class Cons[T] (val head: T, val tail: List[T]) extends List[T] { | |
def isEmpty = false; | |
} |
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
def mergeSort(xs: List[Int]): List[Int] = { | |
val n = xs.length / 2 | |
if (n == 0) xs | |
else { | |
val (firstHalf, secondHalf) = xs splitAt n | |
merge(mergeSort(firstHalf), mergeSort(secondHalf)) | |
} | |
} | |
def merge(xs: List[Int], ys: List[Int]): List[Int] = |
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
def insertionSort(xs : List[Int]) : List[Int] = { | |
xs match{ | |
case Nil => Nil | |
case x :: xs1 => insert(x, isort(xs1)) | |
} | |
} | |
def insert(x : Int, xs : List[Int]) : List[Int] = { | |
xs match { | |
case Nil => List(x) |
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
// Excercise 1 | |
def pascal(c: Int, r: Int): Int = | |
if (c == 0 || r == c) 1 else pascal(c - 1, r - 1) + pascal(c, r - 1) | |
// Excercise 2 | |
def balance(chars: List[Char]): Boolean = { | |
def loop(i: Int, amountOpen: Int): Boolean = | |
if (i >= chars.size) amountOpen == 0 | |
else if (amountOpen < 0) false |
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
[Test] | |
public void PublicMethodTests() | |
{ | |
var publicMethods = | |
MethodMembers().That().ArePublic() | |
.And().AreNotConstructors() | |
.And().AreNotDeclaredIn(Interfaces()) | |
.As("implemented public methods"); | |
var rule = publicMethods.Should() |
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
it("business logic should not depend on the frontend", async ()=> { | |
const rule = filesOfProject() | |
.inFolder("business") | |
.shouldNot() | |
.dependOnFiles() | |
.inFolder("frontend") | |
await expect(rule).toPassAsync() | |
}) |
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
[Test] | |
public void ServicesShouldBeAuthorized() | |
{ | |
var services = Classes().That() | |
.DependOnAny(typeof(ServerCallContext)) | |
.And().AreNotAbstract() | |
.As("Services"); | |
var rule = services | |
.Should() |
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
private final JavaClasses classes = new ClassFileImporter().importPackages("com.tngtech.archunit.example.layers"); | |
@Test | |
public void layer_dependencies_are_respected() { | |
layeredArchitecture() | |
.layer("Controllers").definedBy("..controller..") | |
.layer("Services").definedBy("..layers.service..") | |
.layer("Persistence").definedBy("..layers.persistence..") |