summary refs log tree commit diff
path: root/src/libstd/either.rs
blob: b6da93f9d40ab53b6fdf189ab631a23dfedf81c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A type that represents one of two alternatives

#[allow(missing_doc)];

use container::Container;
use cmp::Eq;
use kinds::Copy;
use iterator::IteratorUtil;
use result::Result;
use result;
use vec;
use vec::{OwnedVector, ImmutableVector};

/// The either type
#[deriving(Clone, Eq)]
pub enum Either<T, U> {
    Left(T),
    Right(U)
}

/// Applies a function based on the given either value
///
/// If `value` is left(T) then `f_left` is applied to its contents, if
/// `value` is right(U) then `f_right` is applied to its contents, and the
/// result is returned.
#[inline]
pub fn either<T, U, V>(f_left: &fn(&T) -> V,
                       f_right: &fn(&U) -> V, value: &Either<T, U>) -> V {
    match *value {
        Left(ref l) => f_left(l),
        Right(ref r) => f_right(r)
    }
}

/// Extracts from a vector of either all the left values
pub fn lefts<T:Copy,U>(eithers: &[Either<T, U>]) -> ~[T] {
    do vec::build_sized(eithers.len()) |push| {
        for eithers.iter().advance |elt| {
            match *elt {
                Left(ref l) => { push(copy *l); }
                _ => { /* fallthrough */ }
            }
        }
    }
}

/// Extracts from a vector of either all the right values
pub fn rights<T, U: Copy>(eithers: &[Either<T, U>]) -> ~[U] {
    do vec::build_sized(eithers.len()) |push| {
        for eithers.iter().advance |elt| {
            match *elt {
                Right(ref r) => { push(copy *r); }
                _ => { /* fallthrough */ }
            }
        }
    }
}

/// Extracts from a vector of either all the left values and right values
///
/// Returns a structure containing a vector of left values and a vector of
/// right values.
pub fn partition<T, U>(eithers: ~[Either<T, U>]) -> (~[T], ~[U]) {
    let mut lefts: ~[T] = ~[];
    let mut rights: ~[U] = ~[];
    do vec::consume(eithers) |_i, elt| {
        match elt {
            Left(l) => lefts.push(l),
            Right(r) => rights.push(r)
        }
    }
    return (lefts, rights);
}

/// Flips between left and right of a given either
#[inline]
pub fn flip<T, U>(eith: Either<T, U>) -> Either<U, T> {
    match eith {
        Right(r) => Left(r),
        Left(l) => Right(l)
    }
}

/// Converts either::t to a result::t
///
/// Converts an `either` type to a `result` type, making the "right" choice
/// an ok result, and the "left" choice a fail
#[inline]
pub fn to_result<T, U>(eith: Either<T, U>) -> Result<U, T> {
    match eith {
        Right(r) => result::Ok(r),
        Left(l) => result::Err(l)
    }
}

/// Checks whether the given value is a left
#[inline]
pub fn is_left<T, U>(eith: &Either<T, U>) -> bool {
    match *eith {
        Left(_) => true,
        _ => false
    }
}

/// Checks whether the given value is a right
#[inline]
pub fn is_right<T, U>(eith: &Either<T, U>) -> bool {
    match *eith {
        Right(_) => true,
        _ => false
    }
}

/// Retrieves the value in the left branch. Fails if the either is Right.
#[inline]
pub fn unwrap_left<T,U>(eith: Either<T,U>) -> T {
    match eith {
        Left(x) => x,
        Right(_) => fail!("either::unwrap_left Right")
    }
}

/// Retrieves the value in the right branch. Fails if the either is Left.
#[inline]
pub fn unwrap_right<T,U>(eith: Either<T,U>) -> U {
    match eith {
        Right(x) => x,
        Left(_) => fail!("either::unwrap_right Left")
    }
}

impl<T, U> Either<T, U> {
    #[inline]
    pub fn either<V>(&self, f_left: &fn(&T) -> V, f_right: &fn(&U) -> V) -> V {
        either(f_left, f_right, self)
    }

    #[inline]
    pub fn flip(self) -> Either<U, T> { flip(self) }

    #[inline]
    pub fn to_result(self) -> Result<U, T> { to_result(self) }

    #[inline]
    pub fn is_left(&self) -> bool { is_left(self) }

    #[inline]
    pub fn is_right(&self) -> bool { is_right(self) }

    #[inline]
    pub fn unwrap_left(self) -> T { unwrap_left(self) }

    #[inline]
    pub fn unwrap_right(self) -> U { unwrap_right(self) }
}

#[test]
fn test_either_left() {
    let val = Left(10);
    fn f_left(x: &int) -> bool { *x == 10 }
    fn f_right(_x: &uint) -> bool { false }
    assert!((either(f_left, f_right, &val)));
}

#[test]
fn test_either_right() {
    let val = Right(10u);
    fn f_left(_x: &int) -> bool { false }
    fn f_right(x: &uint) -> bool { *x == 10u }
    assert!((either(f_left, f_right, &val)));
}

#[test]
fn test_lefts() {
    let input = ~[Left(10), Right(11), Left(12), Right(13), Left(14)];
    let result = lefts(input);
    assert_eq!(result, ~[10, 12, 14]);
}

#[test]
fn test_lefts_none() {
    let input: ~[Either<int, int>] = ~[Right(10), Right(10)];
    let result = lefts(input);
    assert_eq!(result.len(), 0u);
}

#[test]
fn test_lefts_empty() {
    let input: ~[Either<int, int>] = ~[];
    let result = lefts(input);
    assert_eq!(result.len(), 0u);
}

#[test]
fn test_rights() {
    let input = ~[Left(10), Right(11), Left(12), Right(13), Left(14)];
    let result = rights(input);
    assert_eq!(result, ~[11, 13]);
}

#[test]
fn test_rights_none() {
    let input: ~[Either<int, int>] = ~[Left(10), Left(10)];
    let result = rights(input);
    assert_eq!(result.len(), 0u);
}

#[test]
fn test_rights_empty() {
    let input: ~[Either<int, int>] = ~[];
    let result = rights(input);
    assert_eq!(result.len(), 0u);
}

#[test]
fn test_partition() {
    let input = ~[Left(10), Right(11), Left(12), Right(13), Left(14)];
    let (lefts, rights) = partition(input);
    assert_eq!(lefts[0], 10);
    assert_eq!(lefts[1], 12);
    assert_eq!(lefts[2], 14);
    assert_eq!(rights[0], 11);
    assert_eq!(rights[1], 13);
}

#[test]
fn test_partition_no_lefts() {
    let input: ~[Either<int, int>] = ~[Right(10), Right(11)];
    let (lefts, rights) = partition(input);
    assert_eq!(lefts.len(), 0u);
    assert_eq!(rights.len(), 2u);
}

#[test]
fn test_partition_no_rights() {
    let input: ~[Either<int, int>] = ~[Left(10), Left(11)];
    let (lefts, rights) = partition(input);
    assert_eq!(lefts.len(), 2u);
    assert_eq!(rights.len(), 0u);
}

#[test]
fn test_partition_empty() {
    let input: ~[Either<int, int>] = ~[];
    let (lefts, rights) = partition(input);
    assert_eq!(lefts.len(), 0u);
    assert_eq!(rights.len(), 0u);
}