I read an article about partial unificaton and the author mentioned about Left-biased and Right-biased datatypes.
Could someone please explain it to me, what are Left-biased and Right-biased datatypes?
What it has to do with Function1?
Is Either
Left-biased or Right-biased?
Claims of media bias in the United States include claims of liberal bias, conservative bias, mainstream bias, corporate bias and activist/cause bias. To combat this, a variety of watchdog groups that attempt to find the facts behind both biased reporting and unfounded claims of bias have been founded.
Political bias is a bias or perceived bias involving the slanting or altering of information to make a political position or political candidate seem more attractive.
I'll give a bit broader answer, hope you don't mind.
ADTs, or Algebraic Data Types, can be divided into sum types and product types. This is not specific to Scala, but a general functional programming concept.
Product types are such that include several underlying types with an implicit boolean and between them. For example, a Student is a FirstName and LastName and Age and CoursesEnrolled. Sum types on the other hand have an implicit or between them. For example, a Car is a Porsche or Audi or Ferrari.
In Scala, product types are most commonly modelled as case classes, and sum types are modelled as hierarchies where base trait or abstract class gets inherited by all the possible instances (often case classes). Given our previous examples, Student would be a
case class Student(firstName: String, lastName: String, age: Int, classesEnrolled: Vector[Course])
and Car would be a
sealed trait Car
trait Porsche extends Car
trait Audi extends Car
trait Ferrari extends Car
In case of sum types, there are a lot of small design choices whether to e.g. use a trait or an abstract class for the base, whether to use a trait or a case class for the instances, etc. I'm not going to go into that now. Also, small FYI: in FP-oriented Scala libraries such as scalaz, cats, shapeless etc. there are other handy representations for product and sum types.
Sum types can have two or more different values; our Car example had three. Often we only need two - Option, Either, Future, Try are only some examples of sum types from vanilla Scala which only have two possible values. In these cases, we can use map
and flatMap
to apply a function to the value in case of a "happy scenario" (Some for Option, Right for Either etc) and leave it unchanged otherwise (in case of None, Left etc).
But, this is only possible if the sum type in question is right biased. Right biased means that functions such as map
and flatMap
apply only to the "right side" or "happy side" and leave the other one untouched. Up until Scala 2.12, Either was unbiased, which meant that you couldn't just take an Either of something and map it. It would be unclear whether the mapping function should be applied to the Right or Left. You would need to use the "right projection" in order to make it right-biased and therefore mappable and flatmappable (that also means that it can be used in for comprehensions etc. so it's really handy).
But as I said, with Scala 2.12. it became right-biased, which is IMHO a better design choice, and fits perfectly with other similar sum types from other libraries. For example, it's very easy now to go from Either to \/ (scalaz dicjuntion) now that there is no bias mismatch. They are completely isomorphic.
I'm not sure what you mean by "what it has to do with Function1
". It's a function of one parameter. It's what you use to map
and flatMap
the two-value data types from previous examples (Option, Either etc).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With