Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c60a6a9

Browse files
committed
!sips/pending/_posts/2012-06-29}-fixing-either.md: should now be ready to submit -sips/pending/_posts/2012-06-13-fixing-eithers-left-rightprojection.md
1 parent 6b33da2 commit c60a6a9

File tree

2 files changed

+28
-111
lines changed

2 files changed

+28
-111
lines changed

sips/pending/_posts/2012-06-13-fixing-eithers-left-rightprojection.md

Lines changed: 0 additions & 91 deletions
This file was deleted.

sips/pending/_posts/2012-06-29-fixing-either.md

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ title: SIP-20 Fixing Either
88

99
This proposal is the sole initiative of the author, based on his
1010
attempts, [first][enhance] to understand why `scala.Either` is not
11-
more widely used, and to enhance it, [and then][fix] to try to fix
12-
it. It is also being made in the light of two mailing-list debates
13-
which have taken place - one about [right-biasing Either][debate1],
14-
which was inconclusive, and a subsequent one about [fixing
15-
Either][debate2].
11+
more widely used, and to enhance it, and then ([here][fix] and
12+
[here][vs]) to try to fix it. It is also being made in the light of
13+
two mailing-list debates which have taken place - one (initiated by
14+
Jason Zaugg) about [right-biasing Either][debate1], which was
15+
inconclusive, and a subsequent one about [fixing Either][debate2].
1616

1717
## Motivation ##
1818

@@ -21,11 +21,11 @@ eschewed in favour of alternatives:
2121

2222
1. `for` comprehensions involving `Either` behave oddly.
2323

24-
2. The alternatives are simpler to use, by virtue of their being 'biased'
25-
towards one of their possible result types. In the case of
24+
2. The alternatives are simpler to use, by virtue of their being
25+
'biased' towards one of their possible result types. In the case of
2626
`Either`, it is necessary to specify which of the two types (`Left`
27-
or `Right`) should have its value passed to `foreach`, `map` or
28-
`flatMap`.
27+
or `Right`) should have its value passed to the function passed to
28+
`foreach`, `map` or `flatMap`.
2929

3030
3. The alternative may offer 'added value', such as `scalaz.Validation`'s
3131
ability to accumulate failures.
@@ -45,7 +45,7 @@ definitions are not supported (as first reported [here][report]),
4545
c = b + 1
4646
} ... // do something with c
4747

48-
and secondly (as reported [here][fix]), `if` cannot be used together
48+
and secondly (as mentioned [here][fix]), `if` cannot be used together
4949
with multiple generators and `yield`:
5050

5151
for {
@@ -59,12 +59,12 @@ with multiple generators and `yield`:
5959
} yield c
6060

6161
The lack of support for definitions was traced to the fact that the
62-
`map` method of LeftProjection and RightProjection returns an
62+
`map` method of `LeftProjection` and `RightProjection` returns an
6363
`Either`, which does not have a `foreach`, or `map` method itself.
6464

6565
The proposed solution is the one proposed [here][fix], whereby the
66-
`map` method of `LeftProjection` returns another `LeftProjection`, and
67-
that of `RightProjection` returns another `RightProjection`.
66+
`map` method of `LeftProjection` (`RightProjection`) returns another
67+
`LeftProjection` (`RightProjection`).
6868

6969
Also, and as a conseqence, the respective `flatMap` methods must be
7070
changed by substituting the respective projection in place of
@@ -106,7 +106,7 @@ becomes
106106
case Right(b) => LeftProj(Right(b))
107107
}
108108

109-
Note that `.e` must be appended to the value the for-comprehension then
109+
Note that `.e` must be appended to the value the `for` comprehension then
110110
yields, in order to obtain the corresponding `Either` value.
111111

112112
Regarding the second example of odd behaviour, involving `if`, this was
@@ -126,10 +126,10 @@ pattern-matching in `for` comprehensions involving (projections of)
126126

127127
Therefore, a third solution has been investigated, whereby `LeftProj`
128128
(`RightProj`) has a `withFilter` method that returns a `LeftProj`
129-
containing a `Right` (`RightProj` containing a `Left`) if the
130-
predicate is `false`, and where the contents of that `Right` (`Left`)
131-
is obtained using an implicit conversion passed to the `withFilter`
132-
method in a second parameter list:
129+
(`RightProj`) containing a `Right` (`Left`) if the predicate is
130+
`false`, and where the contents of that `Right` (`Left`) is obtained
131+
using an implicit conversion passed to the `withFilter` method in a
132+
second parameter list:
133133

134134
def withFilter[BB >: B](p: A => Boolean)
135135
(implicit aToB: Right.Convert => BB): LeftProj[A, BB] = {
@@ -147,9 +147,9 @@ when `if` or pattern-matching features in a `for` comprehension:
147147
implicit def f(convert: Right.Convert) = convert.any.toString
148148

149149
Note that `Convert` is a simple case class which serves to ensure that
150-
the implicit conversion is properly targetted.
150+
the implicit conversion is properly targeted.
151151

152-
This third solution has been demonstrated to work well, and is the one
152+
This third solution has been [demonstrated][project] to work well, and is the one
153153
proposed here.
154154

155155
## Part 2: Simplifying use in for-comprehensions by *adding* right-biased capability ##
@@ -183,11 +183,19 @@ Finally, note that Part 2 does not render Part 1 redundant; although
183183
appropriate in `for` comprehensions involving the `RightProjection`,
184184
but not the `LeftProjection`.
185185

186+
## Trial version ##
187+
188+
A trial verson of `Either` incorporating the proposed enhancements,
189+
and complete with test suites, is maintained [here][project].
190+
186191
[enhance]: http://robsscala.blogspot.co.uk/2012/04/validation-without-scalaz.html
187192
[fix]:
188193
http://robsscala.blogspot.co.uk/2012/05/fixing-scalaeither-leftrightmap-returns.html
194+
[vs]:
195+
http://robsscala.blogspot.co.uk/2012/06/fixing-scalaeither-unbiased-vs-biased.html
189196
[debate1]:
190197
https://groups.google.com/group/scala-debate/browse_thread/thread/2bac2fe8aa6124ad?hl=en
191198
[debate2]:
192199
https://groups.google.com/forum/?fromgroups#!topic/scala-debate/XlN-oqbslS0
193200
[report]: https://issues.scala-lang.org/browse/SI-5793
201+
[project]: https://github.com/robcd/scala-either-proj-map-returns-proj/tree/add_right-bias_2-10_withFilter

0 commit comments

Comments
 (0)