Learning Goal: I’m working on a c# question and need a sample draft to help me learn.
using System;
using System.Collections.Generic;
namespace cse465 {
class DelegatesExample {
public delegate R Function<R, T>(T value);
public delegate R BinaryFunction<R, T1, T2>(T1 arg1, T2 arg2);
public static List<R> Map<R, T>(List<T> theList, Function<R, T> f) {
// TODO: implement the map operation, which you
// should be familiar with already from scheme
// But use a loop — not recursion!
List<R> result = new List<R>();
//Console.WriteLine(theList[1]);
// for (int i=0;i<theList.length;i++){
// result[i]=theList[i];
// }
return result;
}
public static R FoldLeft<R, T>(List<T> values, BinaryFunction<R, T, R> f, R init) {
// TODO: Implement the Fold Left
// You should be familar with it from scheme
// In case you arent — it is this pseudocode:
//
// for each item in the list, in reverse order:
// init = f(item, init)
// return init
return init;
}
public static void printIfEven(int x) {
if ((x % 2) == 0) {
Console.WriteLine($”{x,-6} – Even”);
} else {
Console.WriteLine($”{x,-6} – Odd”);
}
}
public static void printIfPositive(int x) {
if (x > 0) {
Console.WriteLine($”{x,-6} – Positive”);
} else if (x < 0) {
Console.WriteLine($”{x,-6} – Negative”);
} else {
Console.WriteLine($”{x,-6} – Zero”);
}
}
delegate void NumberHandler(int x);
public static void Main() {
List<string> values = new List<string>(Console.ReadLine().Split(‘,’));
Console.WriteLine($”Read in: {string.Join(“,”, values)}”);
List<int> numbers = Map<int, string>(values, Int32.Parse);
Console.WriteLine($”To Int: {string.Join(“,”, numbers)}”);
// Cube each number using a lambda expression
numbers = Map<int, int>(numbers, (x) => x*x*x );
Console.WriteLine($”Cubed: {string.Join(“,”, numbers)}”);
// TODO — add printIfEvens to the delegate `handler`
// TODO — add printIfPositive to the delegate `handler`
// This demonstrates the way delegate can be used as callbacks in C#
foreach (int n in numbers) {
if (handler != null){
handler(n);
}
}
}
}
}
Expect: Read in: 1,-2,3,-4,10,11,-7,18 To Int: 1,-2,3,-4,10,11,-7,18 Cubed: 1,-8,27,-64,1000,1331,-343,5832 1 - Odd 1 - Positive -8 - Even -8 - Negative 27 - Odd 27 - Positive -64 - Even -64 - Negative 1000 - Even 1000 - Positive 1331 - Odd 1331 - Positive -343 - Odd -343 - Negative 5832 - Even 5832 - Positive
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more
Recent Comments