forked from system-f/fp-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:tonymorris/course
- Loading branch information
Showing
5 changed files
with
94 additions
and
67 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,13 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<component name="FacetManager"> | ||
<facet type="scala" name="Scala"> | ||
<configuration> | ||
<option name="compilerLibraryLevel" value="Global" /> | ||
<option name="compilerLibraryName" value="scala-2.9.2" /> | ||
<option name="languageLevel" value="Scala 2.9" /> | ||
</configuration> | ||
</facet> | ||
</component> | ||
<component name="NewModuleRootManager" inherit-compiler-output="false"> | ||
<output url="file://$MODULE_DIR$/target/scala-2.9.2/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/scala-2.9.2/test-classes" /> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/test/src" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/.idea" /> | ||
<excludeFolder url="file://$MODULE_DIR$/dist" /> | ||
<excludeFolder url="file://$MODULE_DIR$/project" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="scala-2.9.2" level="application" /> | ||
<orderEntry type="library" name="specs2" level="application" /> | ||
<orderEntry type="module-library"> | ||
<library> | ||
<CLASSES> | ||
<root url="jar://$USER_HOME$/.ivy2/cache/org.scalaz/scalaz-core_2.9.2/jars/scalaz-core_2.9.2-7.0.0-M3.jar!/" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES /> | ||
</library> | ||
</orderEntry> | ||
</component> | ||
</module> | ||
|
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 |
---|---|---|
@@ -1,37 +1,37 @@ | ||
package L01 | ||
|
||
import org.scalatest.FeatureSpec | ||
import org.scalatest.prop.Checkers | ||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Arbitrary.arbitrary | ||
import org.scalacheck.Gen.oneOf | ||
|
||
import scalaz._ | ||
import org.scalacheck.Gen | ||
import org.specs2.mutable.Specification | ||
import org.specs2.ScalaCheck | ||
import scalaz.{Validation => _, _} | ||
import Scalaz._ | ||
|
||
class ValidationTests extends FeatureSpec with Checkers { | ||
class ValidationTests extends Specification with ScalaCheck { | ||
|
||
implicit def arbitraryValidationInt(implicit a: Arbitrary[Int]): Arbitrary[Validation[Int]] = | ||
Arbitrary(oneOf(arbitrary[Int] map (x => Value(x)), | ||
arbitrary[Int] map (x => Error(x.toString)))) | ||
|
||
feature("Validation") { | ||
scenario("isError is not equal to isValue") (check(prop_isError_isValue)) | ||
scenario("valueOr produces or isValue") (check(prop_valueOr)) | ||
scenario("errorOr produces or isError") (check(prop_errorOr)) | ||
scenario("map maps") (check(prop_map)) | ||
"Validation" >> { | ||
"isError is not equal to isValue" >> prop(isError_isValue) | ||
"valueOr produces or isValue" >> prop(valueOr) | ||
"errorOr produces or isError" >> prop(errorOr) | ||
"map maps" >> prop(map) | ||
} | ||
|
||
val prop_isError_isValue: Validation[Int] => Boolean = | ||
implicit def arbitraryValidationInt(implicit a: Arbitrary[Int]): Arbitrary[Validation[Int]] = | ||
Arbitrary(Gen.oneOf(arbitrary[Int] map (x => Value(x)), | ||
arbitrary[Int] map (x => Error(x.toString)))) | ||
|
||
val isError_isValue: Validation[Int] => Boolean = | ||
x => x.isError /== x.isValue | ||
|
||
val prop_valueOr: (Validation[Int], Int) => Boolean = | ||
val valueOr: (Validation[Int], Int) => Boolean = | ||
(x, n) => x.isValue || x.valueOr(n) == n | ||
|
||
val prop_errorOr: (Validation[Int], String) => Boolean = | ||
val errorOr: (Validation[Int], String) => Boolean = | ||
(x, e) => x.isError || x.errorOr(e) == e | ||
|
||
val prop_map: (Validation[Int], Int => Int, Int) => Boolean = | ||
val map: (Validation[Int], Int => Int, Int) => Boolean = | ||
(x, f, n) => f(x.valueOr(n)) == x.map(f).valueOr(f(n)) | ||
|
||
|
||
} |
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 |
---|---|---|
@@ -1,90 +1,87 @@ | ||
package L02 | ||
|
||
import org.scalatest.FeatureSpec | ||
import org.scalatest.prop.Checkers | ||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Arbitrary.arbitrary | ||
import org.scalacheck.Gen.oneOf | ||
|
||
import org.scalacheck.Gen | ||
import org.specs2.mutable.Specification | ||
import org.specs2.ScalaCheck | ||
import List._ | ||
|
||
class ListTests extends FeatureSpec with Checkers { | ||
class ListTests extends Specification with ScalaCheck { | ||
|
||
"List" >> { | ||
"headOr defaults with Nil" >> prop(headOr_Nil) | ||
"headOr uses head with non-empty" >> prop(headOr_Cons) | ||
"sum reduces to zero with subtraction" >> prop(suum) | ||
"length reduces to zero with subtraction" >> prop(len) | ||
"map obeys law of identity" >> prop(map_identity) | ||
"map obeys law of composition" >> prop(map_composition) | ||
"filter leaves only valid values" >> prop(fiilter) | ||
"append produces a list with the sum of the lengths">> prop(append) | ||
"flatten sums the lengths" >> prop(flattenList) | ||
"flatMap obeys law of left identity" >> prop(flatMap_left_identity) | ||
"flatMap obeys law of right identity" >> prop(flatMap_right_identity) | ||
"flatMap obeys law of associativity" >> prop(flatMap_associativity) | ||
"flatMap with id flattens" >> prop(flatMap_id_flattens) | ||
"flatMap obeys functor relationship" >> prop(flatMap_functor) | ||
"rev with single value" >> prop(rev_single_value) | ||
"appending reverse is equal to reversing appended" >> prop(rev_append) | ||
} | ||
|
||
implicit def arbitraryListInt(implicit a: Arbitrary[Int]): Arbitrary[List[Int]] = | ||
Arbitrary(oneOf(arbitrary[Int] map (x => x |: Nil()), | ||
arbitrary[Int] map (x => Nil()))) | ||
Arbitrary(Gen.oneOf(arbitrary[Int] map (x => x |: Nil()), | ||
arbitrary[Int] map (x => Nil()))) | ||
|
||
implicit def arbitraryListListInt(implicit a: Arbitrary[Int]): Arbitrary[List[List[Int]]] = | ||
Arbitrary(arbitraryListInt.arbitrary.map(x => x |: Nil())) | ||
|
||
feature("List") { | ||
scenario("headOr defaults with Nil") (check(prop_headOr_Nil)) | ||
scenario("headOr uses head with non-empty") (check(prop_headOr_Cons)) | ||
scenario("sum reduces to zero with subtraction") (check(prop_suum)) | ||
scenario("length reduces to zero with subtraction") (check(prop_len)) | ||
scenario("map obeys law of identity") (check(prop_map_identity)) | ||
scenario("map obeys law of composition") (check(prop_map_composition)) | ||
scenario("filter leaves only valid values") (check(prop_fiilter)) | ||
scenario("append produces a list with the sum of the lengths") (check(prop_append)) | ||
scenario("flatten sums the lengths") (check(prop_flatten)) | ||
scenario("flatMap obeys law of left identity") (check(prop_flatMap_left_identity)) | ||
scenario("flatMap obeys law of right identity") (check(prop_flatMap_right_identity)) | ||
scenario("flatMap obeys law of associativity") (check(prop_flatMap_associativity)) | ||
scenario("flatMap with id flattens") (check(prop_flatMap_id_flattens)) | ||
scenario("flatMap obeys functor relationship") (check(prop_flatMap_functor)) | ||
scenario("rev with single value") (check(prop_rev_single_value)) | ||
scenario("appending reverse is equal to reversing appended") (check(prop_rev_append)) | ||
} | ||
|
||
val prop_headOr_Nil: Int => Boolean = | ||
val headOr_Nil: Int => Boolean = | ||
x => Nil[Int]().headOr(x) == x | ||
|
||
val prop_headOr_Cons: (Int, List[Int], Int) => Boolean = | ||
val headOr_Cons: (Int, List[Int], Int) => Boolean = | ||
(h, t, x) => (h |: t).headOr(x) == h | ||
|
||
val prop_suum: List[Int] => Boolean = | ||
val suum: List[Int] => Boolean = | ||
x => x.foldLeft[Int](a => b => a - b, sum(x)) == 0 | ||
|
||
val prop_len: List[Int] => Boolean = | ||
val len: List[Int] => Boolean = | ||
x => x.foldLeft[Int](a => b => a - 1, x.len) == 0 | ||
|
||
val prop_map_identity: List[Int] => Boolean = | ||
val map_identity: List[Int] => Boolean = | ||
x => x.map(identity) == x | ||
|
||
val prop_map_composition: ((Int => Int), (Int => Int), List[Int]) => Boolean = | ||
val map_composition: ((Int => Int), (Int => Int), List[Int]) => Boolean = | ||
(f, g, x) => x.map(g).map(f) == x.map(f compose g) | ||
|
||
val not: Boolean => Boolean = x => !x | ||
|
||
val prop_fiilter: ((Int => Boolean), List[Int]) => Boolean = | ||
val fiilter: ((Int => Boolean), List[Int]) => Boolean = | ||
(p, x) => x.filter(p).foldRight[Boolean](a => b => p(a) && b, true) && | ||
x.filter(not compose p).foldRight[Boolean](a => b => not (p(a) && b), true) | ||
x.filter((!(_:Boolean)) compose p).foldRight[Boolean](a => b => !(p(a) && b), true) | ||
|
||
val prop_append: (List[Int], List[Int]) => Boolean = | ||
val append: (List[Int], List[Int]) => Boolean = | ||
(x, y) => x.len + y.len == (x append y).len | ||
|
||
val prop_flatten: List[List[Int]] => Boolean = | ||
val flattenList: List[List[Int]] => Boolean = | ||
x => flatten(x).len == sum(x.map(_.len)) | ||
|
||
val prop_flatMap_right_identity: List[Int] => Boolean = | ||
val flatMap_right_identity: List[Int] => Boolean = | ||
x => x.flatMap(n => n |: Nil()) == x | ||
|
||
val prop_flatMap_left_identity: ((Int => List[Int]), Int) => Boolean = | ||
val flatMap_left_identity: ((Int => List[Int]), Int) => Boolean = | ||
(f, n) => (n |: Nil()).flatMap(f) == f(n) | ||
|
||
val prop_flatMap_associativity: ((Int => List[Int]), (Int => List[Int]), List[Int]) => Boolean = | ||
val flatMap_associativity: ((Int => List[Int]), (Int => List[Int]), List[Int]) => Boolean = | ||
(f, g, x) => x.flatMap(f).flatMap(g) == x.flatMap(a => f(a).flatMap(g)) | ||
|
||
val prop_flatMap_id_flattens: List[List[Int]] => Boolean = | ||
val flatMap_id_flattens: List[List[Int]] => Boolean = | ||
x => x.flatMap(identity) == flatten(x) | ||
|
||
val prop_flatMap_functor: ((Int => Int), List[Int]) => Boolean = | ||
val flatMap_functor: ((Int => Int), List[Int]) => Boolean = | ||
(f, x) => x.map(f) == x.flatMap(w => f(w) |: Nil()) | ||
|
||
val prop_rev_single_value: Int => Boolean = | ||
val rev_single_value: Int => Boolean = | ||
n => (n |: Nil()).rev == (n |: Nil()) | ||
|
||
val prop_rev_append: (List[Int], List[Int]) => Boolean = | ||
val rev_append: (List[Int], List[Int]) => Boolean = | ||
(x, y) => ((x.rev) append (y.rev)) == y.append(x).rev | ||
|
||
} |