summaryrefslogtreecommitdiff
path: root/mcs/tests/gtest-217.cs
blob: bc86539667963e561dce021a9310bafb590c0175 (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
using System;
using System.Collections.Generic;

public delegate R Fun<A1,R>(A1 x);

class MyTest {
  public static void Main(String[] args) {
    foreach (Object d in Map<int,int,String,Object>
	                    (delegate (int x) { return x.ToString(); }, 
			     FromTo(10,20)))
      Console.WriteLine(d);
  }

  // Map with argument/result co/contravariance:
  // Aa=argument, Rr=result, Af=f's argument, Rf=f's result

  public static IEnumerable<Rr> Map<Aa,Af,Rf,Rr>(Fun<Af,Rf> f, 
                                                 IEnumerable<Aa> xs) 
    where Aa : Af 
    where Rf : Rr 
  { 
    foreach (Aa x in xs)
      yield return f(x);    // gmcs 1.1.9 bug: cannot convert Aa to Af
  }

  // FromTo : int * int -> int stream

  public static IEnumerable<int> FromTo(int from, int to) { 
    for (int i=from; i<=to; i++)
      yield return i;
  }
}