Scala Monoids, Functors & Monads interview questions are very popular & form the basis for the functional programming. Q01. Given the below code, is it possible to implement the map(..) method with flatMap(…) within the Monad trait?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.monad object MyMonad extends App { trait Monoid[A] { def identity: A def combine(a: A, b: A): A } trait Functor[F[_]] { def map[A, B](a: F[A])(f: A => B): F[B] } trait Monad[M[_]] extends Functor[M] /* with Monoid[_ => M[_]] */ { def pure[A](a: A): M[A] def flatMap[A, B](a: M[A])(f: A => M[B]): M[B] } } |
A01. Yes. … Read more ›...