~bas-dotbas/do/win32-next

« back to all changes in this revision

Viewing changes to Do/src/Enumerable.cs

  • Committer: David Siegel
  • Date: 2008-10-22 00:11:15 UTC
  • mto: (565.5.2 do-future)
  • mto: This revision was merged to the branch mainline in revision 597.
  • Revision ID: david@david-desktop-20081022001115-ea01c0eoce3gzart
Mono 2.0 transition...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Enumerable.cs created with MonoDevelop
 
2
// User: david at 12:49 PM 10/21/2008
 
3
//
 
4
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
 
5
//
 
6
 
 
7
using System;
 
8
using System.Collections.Generic;
 
9
 
 
10
namespace Do
 
11
{
 
12
        public static class Enumerable
 
13
        {
 
14
 
 
15
                public static T Aggregate<T> (IEnumerable<T> self, Func<T, T, T> f)
 
16
                {
 
17
                        T acc;
 
18
                        bool first = true;
 
19
                        foreach (T x in self) {
 
20
                                acc = first ? x : f (acc, x);
 
21
                                first = false;
 
22
                        }
 
23
                        return acc;
 
24
                }
 
25
 
 
26
                public static T2 Aggregate<T1, T2> (IEnumerable<T1> self, T2 acc, Func<T2, T1, T2> f)
 
27
                {
 
28
                        foreach (T1 x in self)
 
29
                                acc = f (acc, x);
 
30
                        return acc;
 
31
                }
 
32
 
 
33
                public static T3 Aggregate<T1, T2, T3> (IEnumerable<T1> self, T2 acc, Func<T2, T1, T2> f, Func<T2, T3> f2)
 
34
                {
 
35
                        return f2 (Aggregate (self, acc, f));
 
36
                }
 
37
                        
 
38
                public static bool All<T> (this IEnumerable<T> self, Func<T, bool> p)
 
39
                {
 
40
                        foreach (T x in self)
 
41
                                if (!p (x)) return false;
 
42
                        return true;
 
43
                }
 
44
 
 
45
                public static bool Any<T> (this IEnumerable<T> self)
 
46
                {
 
47
                        foreach (T x in self)
 
48
                                return true;
 
49
                        return false;
 
50
                }
 
51
                
 
52
                public static bool Any<T> (this IEnumerable<T> self, Func<T, bool> p)
 
53
                {
 
54
                        foreach (T x in self)
 
55
                                if (p (x)) return true;
 
56
                        return false;
 
57
                }
 
58
 
 
59
                public static IEnumerable<T2> Select<T1, T2> (this IEnumerable<T1> self, Func<T1, T2> f)
 
60
                {
 
61
                        List<T2> xs = new List<T2> ();
 
62
                        foreach (T1 x in self)
 
63
                                xs.Add (f (x));
 
64
                        return xs;
 
65
                }
 
66
 
 
67
                public static IEnumerable<T> Where<T> (this IEnumerable<T> self, Func<T, bool> p)
 
68
                {
 
69
                        List<T> xs = new List<T> ();
 
70
                        foreach (T x in self)
 
71
                                if (p (x)) xs.Add (x);
 
72
                        return xs;
 
73
                }
 
74
        }
 
75
}