Sunday 26 April 2020

Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning' where I make revision notes in the questions which I regularly review. This post contains the questions and their answers.

Chapter 1: Introduction: Answers

  1. What is a type class? 
    • It’s a way of defining some behaviour that many types can adhere to. In scala it's a trait with a parameter
  2. What is an interface syntax in cats?
  3. What do Show and Eq do in cats? 
    • Members of show can be presented as strings. 
  4. Explain covariance, contravariance and invariance. 
    • Covariance
    • Contravariance
    • Invariance
    • F[A] and F[B] are never subtypes of each other, regardless of the relationship between A and B.

Chapter 2: Monoids and Semigroups: Answers

  1. Define monoid and semigroup and give examples of each. 

Chapter 3: Functors: Answers

  1. What is a type constructor? 
    • A type constructor is like a type but it has a 'hole to fill'. Eg List is a type constructor because it has one 'hole' to fill. Note the difference between List (a type constructor) and List[A] (a type, using a type parameter) and List[Int] (a concrete type).
  2. What  is a  covariant functor? 
  3. Write a method 'power' which can operate on either List(1,2,3) or Option(2) and returns List(1,4,9) and Option(4) respectively. 
  4. What is a contravariant functor? 

Chapter 4: Monads

  1. What is a monad? 
  2. What is the 'Id' monad for? 
    • Imagine you have a method which operates on a Monad of Ints, eg power method so for Option(2) it returns Option(4) and for List(1,2) it returns List(1,4). To use this method on regular ints we need to wrap our plain Ints in a monad, hence Id. 
  3. What is the 'Eval' monad for? 
    • This is for abstracting over different modes of computation, eager or lazy. Memoized computations run once then are cached, and can run lazily or eagerly. vals are eager and memoized. Defs are lazy and non memoized. Lazy vals are lazy and memoized. Eval has three subtypes, Now, Later and Always. 
    • It also has 'defer' which can be used to avoid stack overflow in recursive methods.
  4. What does each line print? 
    • 'Now' is eager and memoized (val). 'Later' is lazy and memoized (lazy val). 'Always' is lazy and not memoized (def). 
  5. What is the output of the following code snippet and why? 
    • "Now" is calculated eagerly so is printed first. However mapping functions are always called like defs so the output is 'a, ---, b, c, Adding!, b, c, Adding!' There is a memoize function that can be used when chaining. 
  6. What is the writer monad for?
    • This is for carrying a log along with some computation. It's particularly useful in multithreaded computation where normal log messages might get interleaved. Writer[W,A] has a log of type W and a result of type A.
  7. What is the reader monad for? 
    • Reader[A,B] represents a function A => B. It allows sequencing of operations that depend on some input. 
  8. What is the state monad for? 
    • The state monad allows us to pass additional state around as part of a computation. We can use it to model mutable state in a purely functional way, without the mutation. State[S, A] represents functions of type S => (S, A). S is the type of the state and A is the result type. 

Chapter 5: Monad Transformers

  1. What are monad transformers used for? 
    • These are for composing monads, eg for giving us nice ways of handling Future[Option[T]]


Scala with Cats: Revision Questions



Earlier this year I read a fantastic book about Ultralearning. One tip from the book was to make notes in the form of questions without the answers rather than making notes to browse. Therefore, to review my work I can answer the questions and test my recall rather than just reading information and hope it goes in. 

The answers are in another post

Chapter 1: Introduction

  1. What is a type class? 
  2. What is an interface syntax in cats?
  3. What do Show and Eq do in cats? 
  4. Explain covariance, contravariance and invariance. 

Chapter 2: Monoids and Semigroups

  1. Define monoid and semigroup and give examples of each. 

Chapter 3: Functors

  1. What is a type constructor? 
  2. What is a covariant functor?
  3. Write a method 'power' which can operate on either List(1,2,3) or Option(2) and returns List(1,4,9) and Option(4) respectively. 
  4. What is a contravariant functor? 

Chapter 4: Monads

  1. What is a monad? 
  2. What is the 'Id' monad for? 
  3. What is the 'Eval' monad for? 
  4. What does each line print? 
  5. What is the output of the following code snippet? 
  6. What is the writer monad for?
  7. What is the reader monad for? 
  8. What is the state monad for? 




Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning&#...