summaryrefslogtreecommitdiff
path: root/lib/MappingExtensions.cs
blob: 17fbdd5972d108891730c8a656433581731ad03e (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
using System;
using System.Collections.Generic;
using System.Linq;

namespace jive
{
  static public class MappingExtensions
  {
    static public Output map_using<Input, Output>(this Input item, Converter<Input, Output> conversion)
    {
      return conversion(item);
    }

    static public Output map_using<Input, Output>(this Input item, Mapper<Input, Output> mapper)
    {
      return map_using(item, x => mapper.map_from(x));
    }

    static public Output map_using<Input, Output>(this Input item, Mapper mapper)
    {
      return map_using(item, x => mapper.map_from<Input, Output>(x));
    }

    static public IEnumerable<Output> map_all_using<Input, Output>(this IEnumerable<Input> items,
        Converter<Input, Output> mapper)
    {
      return null == items ? Enumerable.Empty<Output>() : items.Select(x => mapper(x));
    }

    static public IEnumerable<Output> map_all_using<Input, Output>(this IEnumerable<Input> items,
        Mapper<Input, Output> mapper)
    {
      return map_all_using(items, x => mapper.map_from(x));
    }

    static public IEnumerable<Output> map_all_using<Input, Output>(this IEnumerable<Input> items,
        Mapper mapper)
    {
      return map_all_using(items, x => mapper.map_from<Input, Output>(x));
    }

    static public Mapper<Left, Right> then<Left, Middle, Right>(this Mapper<Left, Middle> left,
        Mapper<Middle, Right> right)
    {
      return new ChainedMapper<Left, Middle, Right>(left, right);
    }
  }
}