Skip to content
Permalink
Browse files

FIx the rest of exercises

Add in the end of every section a exercise
  • Loading branch information...
AdrianRaFo committed Mar 28, 2017
1 parent 78d8b5a commit 64885c4782422491db5c244f4e78e41690255ee3
@@ -157,9 +157,9 @@ object IsoExercises extends FlatSpec with Matchers with Section {
def roundTripOtherWay[S, A](i: Iso[S, A], a: A): Boolean =
i.get(i.reverseGet(a)) == a

roundTripOneWay(personToTuple, Person("Zoey",25)) should be res0
roundTripOneWay(personToTuple, Person("Zoey",25)) should be (res0)

roundTripOtherWay(personToTuple,("Zoe",52)) should be res1
roundTripOtherWay(personToTuple,("Zoe",52)) should be (res1)
}


@@ -1,17 +1,16 @@
package monocle
package monocleex


import monocle.{Iso, Lens}
import monocle.IsoHelper.{Person, personToTuple}
import org.scalatest._
import org.scalaexercises.definitions._
import monocle.Lens
import monocle.macros.GenLens
import org.scalaexercises.definitions._
import org.scalatest._

object LensHelper {
case class Address(streetNumber: Int, streetName: String)
case class Address(strNumber : Int, streetName : String)

val address = Address(10, "High Street")
val streetNumber = GenLens[Address](_.streetNumber)
val streetNumber = GenLens[Address](_.strNumber)

def neighbors(n: Int): List[Int] =
if(n > 0) List(n - 1, n + 1) else List(n + 1)
@@ -30,23 +29,23 @@ object LensHelper {
*
* Let’s take a simple case class with two fields:
* {{{
* case class Address(streetNumber: Int, streetName: String)
* case class Address(strNumber: Int, streetName: String)
* }}}
*
* We can create a `Lens[Address, Int]` which zooms from an `Address` to its field `streetNumber` by supplying a pair of functions:
* We can create a `Lens[Address, Int]` which zooms from an `Address` to its field `strNumber` by supplying a pair of functions:
*
* - `get: Address => Int`
* - `set: Int => Address => Address`
*
* {{{
* import monocle.Lens
* val streetNumber = Lens[Address, Int](_.streetNumber)(n => a => a.copy(streetNumber = n))
* val strNumber = Lens[Address, Int](_.strNumber)(n => a => a.copy(strNumber = n))
* }}}
*
* This case is really straightforward so we automated the generation of `Lenses` from case classes using a macro:
* {{{
* import monocle.macros.GenLens
* val streetNumber = GenLens[Address](_.streetNumber)
* val strNumber = GenLens[Address](_.strNumber)
* }}}
*
* @param name lens
@@ -105,7 +104,7 @@ object LensExercises extends FlatSpec with Matchers with Section {
* def updateNumber(n: Int): Future[Int] = Future.successful(n + 1)
* }}}
* {{{
* streetNumber.modifyF(updateNumber)(address)
* strNumber.modifyF(updateNumber)(address)
* // res9: scala.concurrent.Future[Address] = Future(<not completed>)
* }}}
*
@@ -151,16 +150,16 @@ object LensExercises extends FlatSpec with Matchers with Section {
*/
def exerciseLaws(res0 : Boolean, res1 : Boolean) =
{
val streetNumber = Lens[Address, Int](_.streetNumber)(n => a => a.copy(streetNumber = n))
val streetNumber = Lens[Address, Int](_.strNumber)(n => a => a.copy(strNumber = n))

def getSet[S, A](l: Lens[S, A], s: S): Boolean =
l.set(l.get(s))(s) == s

def setGet[S, A](l: Lens[S, A], s: S, a: A): Boolean =
l.get(l.set(a)(s)) == a

getSet(streetNumber) should be res0
getSet(streetNumber,address) should be (res0)

getSet(streetNumber) should be res1
setGet(streetNumber,address,20) should be (res1)
}
}
@@ -1,7 +1,8 @@
package monocle

import org.scalatest._
import monocle.law.OptionalLaws
import org.scalaexercises.definitions._
import org.scalatest._


object OptionalHelper {
@@ -111,24 +112,39 @@ object OptionalExercises extends FlatSpec with Matchers with Section {
/**
* == Laws ==
*
* {{{
* class OptionalLaws[S, A](optional: Optional[S, A]) {
*
* def getOptionSet(s: S): Boolean =
* optional.getOrModify(s).fold(identity, optional.set(_)(s)) == s
*
* def setGetOption(s: S, a: A): Boolean =
* optional.getOption(optional.set(a)(s)) == optional.getOption(s).map(_ => a)
* }
* }}}
*
* An `Optional` must satisfy all properties defined in `OptionalLaws` in core module. You can check the validity of your own `Optional` using `OptionalTests` in law module.
*
* `getOptionSet` states that if you `getOrModify` a value `A` from `S` and then `set` it back in, the result is an object identical to the original one.
*
* `setGetOption` states that if you `set` a value, you always `getOption` the same value back.
*/
def conclusion(): Unit = ()
def exerciseLaws(res0 : Boolean, res1 : Boolean) =
{
val head = Optional[List[Int], Int] {
case Nil => None
case x :: xs => Some(x)
}
{
a => {
case Nil => Nil
case x :: xs => a :: xs
}
}
class OptionalLaws[S, A](optional: Optional[S, A]) {

def getOptionSet(s: S): Boolean =
optional.getOrModify(s).fold(identity, optional.set(_)(s)) == s

def setGetOption(s: S, a: A): Boolean =
optional.getOption(optional.set(a)(s)) == optional.getOption(s).map(_ => a)

}
new OptionalLaws(head).getOptionSet(List(1,2,3)) should be (res0)

new OptionalLaws(head).setGetOption(List(1,2,3),20) should be (res1)

}



}
@@ -1,7 +1,7 @@
package monocle

import org.scalatest._
import org.scalaexercises.definitions._
import org.scalatest._


object PrismHelper {
@@ -177,18 +177,25 @@ object PrismExercises extends FlatSpec with Matchers with Section {
*
* In particular, a `Prism` must verify that `getOption` and `reverseGet` allow a full round trip if the Prism matches i.e. if `getOption` returns a `Some`.
*
* {{{
* def partialRoundTripOneWay[S, A](p: Prism[S, A], s: S): Boolean =
* p.getOption(s) match {
* case None => true // nothing to prove
* case Some(a) => p.reverseGet(a) == s
* }
*
* def partialRoundTripOneWay[S, A](p: Prism[S, A], a: A): Boolean =
* p.getOption(p.reverseGet(a)) == Some(a)
* }}}
*
*/
def conclusion(): Unit = ()
def exerciseLaws(res0 : Boolean, res1 : Boolean) =
{
val jStr = Prism.partial[Json, String]{case JStr(v) => v}(JStr)

def partialRoundTripOneWay[S, A](p: Prism[S, A], s: S): Boolean =
p.getOption(s) match {
case None => true // nothing to prove
case Some(a) => p.reverseGet(a) == s
}

def partialRoundTripOtherWay[S, A](p: Prism[S, A], a: A): Boolean =
p.getOption(p.reverseGet(a)) == Some(a)

partialRoundTripOneWay(jStr,JStr("Hi")) should be (res0)

partialRoundTripOtherWay(jStr, "Hi") should be (res1)

}

}
@@ -21,7 +21,6 @@ object TraversalHelper {
import scalaz.syntax.traverse._
import scalaz.syntax.applicative._


def filterKey[K, V](predicate: K => Boolean): Traversal[Map[K, V], V] =
new Traversal[Map[K, V], V]{
def modifyF[F[_]: Applicative](f: V => F[V])(s: Map[K, V]): F[Map[K, V]] =
@@ -133,19 +132,26 @@ object TraversalExercises extends FlatSpec with Matchers with Section {
*
* In particular, a `Traversal` must respect the `modifyGetAll` law which checks that you can modify all elements targeted by a `Traversal`
*
* {{{
* def modifyGetAll[S, A](t: Traversal[S, A], s: S, f: A => A): Boolean =
* t.getAll(t.modify(f)(s)) == t.getAll(s).map(f)
* }}}
*
* Another important `law` is `composeModify` also known as fusion law:
*
* {{{
* def composeModify[S, A](t: Traversal[S, A], s: S, f: A => A, g: A => A): Boolean =
* t.modify(g)(t.modify(f)(s)) == t.modify(g compose f)(s)
* }}}
*/
def conclusion(): Unit = ()
def exerciseLaws(res0 : Boolean, res1 : Boolean) =
{

val eachL = Traversal.fromTraverse[List, Int]

def modifyGetAll[S, A](t: Traversal[S, A], s: S, f: A => A): Boolean =
t.getAll(t.modify(f)(s)) == t.getAll(s).map(f)


def composeModify[S, A](t: Traversal[S, A], s: S, f: A => A, g: A => A): Boolean =
t.modify(g)(t.modify(f)(s)) == t.modify(g compose f)(s)

modifyGetAll(eachL,List(1,2,3),(x:Int)=>x+1) should be (res0)

composeModify(eachL, List(1,2,3),(x:Int)=>x+1,(y:Int)=>y+2) should be (res1)

}


}
@@ -1,7 +1,7 @@
package monocle

import monocleex.LensExercises
import monocleex.LensHelper.{Person, Address}
import monocleex.LensHelper.{Address, Person}
import org.scalacheck.Shapeless._
import org.scalaexercises.Test
import org.scalatest.FunSuite
@@ -59,7 +59,7 @@ class LensSpec extends FunSuite with Checkers {
test("exercise laws") {
check(
Test.testSuccess(
IsoExercises.exerciseLaws _,
LensExercises.exerciseLaws _,
true :: true:: HNil
)
)
@@ -46,4 +46,14 @@ class OptionalSpec extends FunSuite with Checkers {
)
)
}


test("exercise laws") {
check(
Test.testSuccess(
OptionalExercises.exerciseLaws _,
true :: true:: HNil
)
)
}
}
@@ -66,4 +66,12 @@ class PrismSpec extends FunSuite with Checkers {
)
}

test("exercise laws") {
check(
Test.testSuccess(
PrismExercises.exerciseLaws _,
true :: true:: HNil
)
)
}
}
@@ -47,4 +47,12 @@ class TraversalSpec extends FunSuite with Checkers {
)
}

test("exercise laws") {
check(
Test.testSuccess(
TraversalExercises.exerciseLaws _,
true :: true:: HNil
)
)
}
}

0 comments on commit 64885c4

Please sign in to comment.
You can’t perform that action at this time.