Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9250807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:32:12+00:00 2026-06-18T10:32:12+00:00

Using Scala 2.9.1, consider the following two instances of Either : scala> val er:

  • 0

Using Scala 2.9.1, consider the following two instances of Either:

scala> val er: Either[String, Int] = Right(1)
er: Either[String,Int] = Right(1)

scala> val el: Either[String, Int] = Left("a")
el: Either[String,Int] = Left(a)

It’s great that, by using the left and right projections, one can use for-comprehensions (via biased monad of the projected Either):

scala> for { r <- er.right } yield r * 2
res6: Product with Either[String,Int] with Serializable = Right(2)

scala> for { r <- el.right } yield r * 2
res7: Product with Either[String,Int] with Serializable = Left(a)

Can someone explain to me why the decision was made not to have the filter method return an Either? I would have expected the following to work:

scala> for { r <- er.right if r > 2 } yield r * 2 // r is NOT greater than 2!
res8: Product with Either[String,Int] with Serializable = Left(a)

Instead you get the following error:
:9: error: value * is not a member of Either[Nothing,Int]
for { r <- er.right if r > 2 } yield r * 2

It appears that the underlying call in Either.RightProjection#filter actually returns an Option:

scala> er.right.filter(_ > 2)
res9: Option[Either[Nothing,Int]] = None

This defeats the use of the if clause in the for-comprehension, at least the way I was trying to use it.

Does anyone have an explanation of why this design is the way it is?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:32:14+00:00Added an answer on June 18, 2026 at 10:32 am

    It boils down to the fact that if you have a Right(b), but your filter predicate fails, you have no value to put in a Left.

    You might imagine an implementation that works for your case of Either[String, Int], by failing with a default value of Left(""). The Scala standard library doesn’t have the facility to to produce a value for you, because it doesn’t include a concept such as a monoid which would determine the “empty” value for a type.

    The Scalaz library does include a monoid typeclass, and version 7 also includes a right-biased disjunction type, \/[A, B] (isomorphic to Either[A, B]) which has a filter method iff the left type is a monoid:

    scala> \/.right[String, Int](1).filter(_ > 2)
    res1: scalaz.\/[String,Int] = -\/()
    

    But you couldn’t do this for the general case – if you had an Either[Nothing, Int], you could never produce a left value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following: val stuff = Map[String, Int](apple -> 5, orange -> 1, banana
Please consider the following test program (using scala 2.9.0.1) object test { def main(args:Array[String])
Consider the following: scala> val myset = Set(1,2) myset: scala.collection.immutable.Set[Int] = Set(1, 2) scala>
Using scala-2.10.0-M7, consider the following Scala program: import reflect.runtime.universe._ object ScalaApplication { def main(args:
Consider the following sketch for using a loop to empty a queue in Scala:
Consider the following: scala> val a:java.lang.Boolean = true a: java.lang.Boolean = true scala> val
I am currently using scala 2.9.1. I create a mutable.HashMap using : > val
I am trying to parallelise a code using scala actors. That is my first
Consider the following two scenarios: //Data Contract public class MyValue { } Scenario 1:
Consider the following C# class: public class Role { public string Id { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.