@@ -4,6 +4,8 @@ package net.degoes.abstractions
4
4
5
5
import scalaz ._
6
6
import Scalaz ._
7
+ import net .degoes .abstractions .functor .Leaf
8
+ import net .degoes .abstractions .functor .Fork
7
9
8
10
object algebra {
9
11
type ??? = Nothing
@@ -143,8 +145,7 @@ object algebra {
143
145
new Monoid [Map [K , V ]] {
144
146
def zero : Map [K , V ] = ???
145
147
146
- def append (l : Map [K , V ], r : => Map [K , V ]): Map [K , V ] =
147
- ???
148
+ def append (l : Map [K , V ], r : => Map [K , V ]): Map [K , V ] = ???
148
149
}
149
150
150
151
//
@@ -230,8 +231,7 @@ object functor {
230
231
final case class Fork [A ](left : BTree [A ], right : BTree [A ]) extends BTree [A ]
231
232
implicit val BTreeFunctor : Functor [BTree ] =
232
233
new Functor [BTree ] {
233
- def map [A , B ](fa : BTree [A ])(f : A => B ): BTree [B ] =
234
- ???
234
+ def map [A , B ](fa : BTree [A ])(f : A => B ): BTree [B ] = ???
235
235
}
236
236
237
237
//
@@ -353,8 +353,7 @@ object functor {
353
353
new Zip [Option ] {
354
354
def map [A , B ](fa : Option [A ])(f : A => B ) = fa map f
355
355
356
- def zip [A , B ](l : Option [A ], r : Option [B ]): Option [(A , B )] =
357
- ???
356
+ def zip [A , B ](l : Option [A ], r : Option [B ]): Option [(A , B )] = ???
358
357
}
359
358
}
360
359
implicit class ZipSyntax [F [_], A ](left : F [A ]) {
@@ -568,7 +567,7 @@ object parser {
568
567
Parser (input => Left (e))
569
568
570
569
def succeed [A ](a : => A ): Parser [Nothing , A ] =
571
- ???
570
+ Parser (input => Right ((input, a)))
572
571
573
572
def maybeChar : Parser [Nothing , Option [Char ]] = char ?
574
573
@@ -613,11 +612,9 @@ object foldable {
613
612
// Define an instance of `Foldable` for `List`
614
613
//
615
614
implicit val FoldableList : Foldable [List ] = new Foldable [List ] {
616
- def foldMap [A , B : Monoid ](fa : List [A ])(f : A => B ): B =
617
- ???
615
+ def foldMap [A , B : Monoid ](fa : List [A ])(f : A => B ): B = ???
618
616
619
- def foldRight [A , B ](fa : List [A ], z : => B )(f : (A , => B ) => B ): B =
620
- ???
617
+ def foldRight [A , B ](fa : List [A ], z : => B )(f : (A , => B ) => B ): B = ???
621
618
}
622
619
623
620
//
@@ -705,6 +702,9 @@ object optics {
705
702
}
706
703
case class Employee (name : String , dob : java.time.Instant , salary : BigDecimal , address : Address )
707
704
object Employee {
705
+ val name : Lens [Employee , String ] =
706
+ Lens [Employee , String ](_.name, n => _.copy(name = n))
707
+
708
708
val salary : Lens [Employee , BigDecimal ] =
709
709
Lens [Employee , BigDecimal ](_.salary, s => _.copy(salary = s))
710
710
}
@@ -720,8 +720,7 @@ object optics {
720
720
get : S => A ,
721
721
set : A => (S => S )
722
722
) { self =>
723
- def ⋅ [B ](that : Lens [A , B ]): Lens [S , B ] =
724
- ???
723
+ def ⋅ [B ](that : Lens [A , B ]): Lens [S , B ] = ???
725
724
726
725
def ⋅ [B ](that : Optional [A , B ]): Optional [S , B ] = ???
727
726
@@ -749,16 +748,15 @@ object optics {
749
748
import Org .site
750
749
import Site .manager
751
750
import Employee .salary
752
- val org2_lens : Org = ???
751
+ lazy val org2_lens : Org = ???
753
752
754
753
//
755
754
// EXERCISE 3
756
755
//
757
756
// Implement `⋅` for `Prism` for `Prism`.
758
757
//
759
758
final case class Prism [S , A ](get : S => Option [A ], set : A => S ) { self =>
760
- def ⋅ [B ](that : Prism [A , B ]): Prism [S , B ] =
761
- ???
759
+ def ⋅ [B ](that : Prism [A , B ]): Prism [S , B ] = ???
762
760
763
761
def ⋅ [B ](that : Lens [A , B ]): Optional [S , B ] = ???
764
762
@@ -773,26 +771,22 @@ object optics {
773
771
//
774
772
// Implement `_Left` and `_Right`.
775
773
//
776
- def _Left [A , B ]: Prism [Either [A , B ], A ] =
777
- ???
778
- def _Right [A , B ]: Prism [Either [A , B ], B ] =
779
- ???
774
+ def _Left [A , B ]: Prism [Either [A , B ], A ] = ???
775
+ def _Right [A , B ]: Prism [Either [A , B ], B ] = ???
780
776
781
777
//
782
778
// EXERCISE 5
783
779
//
784
780
// Implement `⋅` for `Optional` for `Optional`.
785
781
//
786
- final case class Optional [S , A ](getOrModify : S => Either [ S , A ], set : A => (S => S )) {
782
+ final case class Optional [S , A ](get : S => Option [ A ], set : A => (S => S )) {
787
783
def ⋅ [B ](that : Optional [A , B ]): Optional [S , B ] = ???
788
784
789
785
def ⋅ [B ](that : Lens [A , B ]): Optional [S , B ] = ???
790
786
791
787
def ⋅ [B ](that : Prism [A , B ]): Optional [S , B ] = ???
792
788
793
789
def ⋅ [B ](that : Traversal [A , B ]): Traversal [S , B ] = ???
794
-
795
- final def get (s : S ): Option [A ] = getOrModify(s).right.toOption
796
790
}
797
791
798
792
//
0 commit comments