title: Kotlin - How to properly map result using Either?
body: <p>I'm trying to read a list of objects from the database and mapping it to another type of list.</p>
  
  <pre><code>// Returns either a Failure or the expected result
  suspend fun getCountries(): Either&lt;Failure, List&lt;CountryItem&gt;&gt; {
      // Get the result from the database
      val result = countryLocalDataSource.getCountries()
  
      // Left means Failure
      if (result.isLeft) {
          // Retrieve the error from the database
          lateinit var error: Failure
          result.either({
              error = it
          }, {})
  
          // Return the result
          return Either.Left(error)
      }
  
      // The database returns a List of Country objects, we need to map it to another object (CountryItem)
      val countryItems: MutableList&lt;CountryItem&gt; = mutableListOf()
  
      // Iterate the Country List and construct a new List of CountryItems
      result.map { countries -&gt; {
          countries.forEach {
              // Assign some values from an Enum (localized string resources)
              val countryEnumValue = Countries.fromId(it.id)
              countryEnumValue?.let { countryIt -&gt;
                  val countryStringNameRes = countryIt.nameStringRes;
  
                  // Create the new CountryItem object (@StringRes value: Int, isSelected: Bool)
                  countryItems.add(CountryItem(countryStringNameRes, false))
              }
          }
      } }
  
      // Because this is a success, return as Right with the newly created List of CountryItems
      return Either.Right(countryItems)
  }
  </code></pre>
  
  <p>For the sake of readability I didn't included the whole <code>Repository</code> or the <code>DAO</code> classes and I have left comments in the code snippet above.</p>
  
  <p><strong>In a nutshell</strong>: I'm using Kotlin's <a href="https://kotlinlang.org/docs/reference/coroutines-overview.html" rel="noreferrer">Coroutines</a> for accessing the database in a separated thread and I'm handling the response on the <code>UI Thread</code>. Using the <a href="https://github.com/android10/Android-CleanArchitecture-Kotlin/blob/master/app/src/main/kotlin/com/fernandocejas/sample/core/functional/Either.kt" rel="noreferrer">Either</a> class in order to return two different results (failure or success).</p>
  
  <p><strong>The above code works, however It's too ugly. Is this the right approach to deliver the result?</strong></p>
  
  <p>What I'm trying to do is to refactor the code above.</p>
  
  <p>The whole problem is caused by the two different object types. The <code>Database Data Source API</code> is returning an <code>Either&lt;Failure, List&lt;Country&gt;&gt;</code>, meanwhile the function is expected to return an <code>Either&lt;Failure, List&lt;CountryItem&gt;&gt;</code>.</p>
  
  <p>I can't deliver a <code>List&lt;CountryItem&gt;</code> directly from the <code>Database Data Source API</code>, because Android Studio doesn't let me compile the project (entities implementing interfaces, compile error, etc.). What I'm trying to achieve is to map the <code>Either</code> result in a nicer way.</p>