Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
sesiuni:scala:lab6 [2016/07/04 16:28]
dciocirlan [Practice]
sesiuni:scala:lab6 [2016/07/04 20:07] (current)
dciocirlan
Line 26: Line 26:
  
   - Let's write a monad equivalent with ''​Option''​. Let's call this ''​Maybe'',​ as in Haskell.   - Let's write a monad equivalent with ''​Option''​. Let's call this ''​Maybe'',​ as in Haskell.
-    * create a trait ''​Maybe[T]''​ (remember the ''​[T]''​ is equivalent to Java's ''<​T>''​ syntax for generics)+    * create a trait ''​Maybe[+A]''​ (remember the ''​[A]''​ is equivalent to Java's ''<​A>''​ syntax for generics ​and ignore the + for now)
     * add some functional primitives as methods to ''​Maybe'':​     * add some functional primitives as methods to ''​Maybe'':​
       * the monad requirements:​ ''​map'',​ ''​flatMap'',​ ''​filter''​       * the monad requirements:​ ''​map'',​ ''​flatMap'',​ ''​filter''​
-      * ''​zipWith'',​ which receives a ''​Maybe[A]''​ and returns a ''​Maybe[(T,A)]''​\\ The signature is just a tiny bit more complex:\\ <code scala>​def zipWith[A](other: Maybe[A]): Maybe[(T,A)]</​code>​ If either ''​this''​ or ''​other''​ is empty, the result will be an empty ''​Option''​. +      * ''​zipWith'',​ which receives a ''​Maybe[B]''​ and returns a ''​Maybe[(A,B)]''​\\ The signature is just a tiny bit more complex:\\ <code scala>​def zipWith[B](other: Maybe[B]): Maybe[(A,B)]</​code>​ If either ''​this''​ or ''​other''​ is empty, the result will be an empty ''​Maybe''​. 
-      * ''​exists'',​ which receives a predicate (a function of to Boolean) and returns true if this instance contains an element which satisfies the predicate +      * ''​exists'',​ which receives a predicate (a function of to Boolean) and returns true if this instance contains an element which satisfies the predicate 
-      * ''​orElse'',​ a special method for ''​Maybe'',​ which returns this instance if it's not empty, or an alternative otherwise; the signature will be\\ <code scala>​def orElse(alternative: ​Option[T]): Option[T]</​code>​ How can you //delay// the computation of the alternative until you actually need it? (hint: call by value/call by name)+      * ''​orElse'',​ a special method for ''​Maybe'',​ which returns this instance if it's not empty, or an alternative otherwise; the signature will be\\ <code scala>​def orElse[B >: A](alternative: ​Maybe[B]): Maybe[B]</​code> ​(ignore the ''>:''​ for now)\\ ​How can you //delay// the computation of the alternative until you actually need it? (hint: call by value/call by name)
     * add some utility methods to the ''​Maybe''​ trait:     * add some utility methods to the ''​Maybe''​ trait:
       * ''​isEmpty''​       * ''​isEmpty''​
       * ''​toList''​       * ''​toList''​
       * ''​size''​       * ''​size''​
-    * create two case subclasses: ''​Just[T]''​ and ''​None''​ and implement the methods +    * create two case subclasses: ''​Just[A]''​ and ''​Empty''​ and implement the methods 
-      * what is None, actually? a case class? a case object?+      * what is ''​Empty''​, actually? a case class? a case object?
       * methods should be quick to write and probably take two lines of code, at most       * methods should be quick to write and probably take two lines of code, at most
-  - An equivalent to Scala'​s ''​Try''​ monad, call this ''​Attempt[T]''​. +  - An equivalent to Scala'​s ''​Try''​ monad, call this ''​Attempt[A]''​. 
-    * as before, create a trait ''​Attempt[T]''​ and two case subclasses, ''​Success[T]''​ and ''​Fail''​ +    * as before, create a trait ''​Attempt[A]''​ and two case subclasses, ''​Success[A]''​ and ''​Fail''​ 
-    * ''​Success''​ has a member of type ''​T'',​ Fail has a member of type ''​Throwable''​+    * ''​Success''​ has a member of type ''​A'',​ Fail has a member of type ''​Throwable''​
     * add some functional primitives to your ''​Attempt''​ and implement them in the subclasses:     * add some functional primitives to your ''​Attempt''​ and implement them in the subclasses:
       * ''​map'',​ ''​flatMap'',​ ''​filter''​       * ''​map'',​ ''​flatMap'',​ ''​filter''​
sesiuni/scala/lab6.1467638896.txt.gz · Last modified: 2016/07/04 16:28 by dciocirlan