Dziwne te Wasze rozwiązania. Moja wersja:
using System;
using System.Linq;
using System.Collections.Generic;
namespace Program {
public interface IValueConverter<T, R> {
R Convert(T input);
}
public class StringToIntValueConverter : IValueConverter<string, int> {
public int Convert(string input) {
return int.Parse(input);
}
}
public interface ISupplier<T> {
T Get { get; }
}
public class EndoPairSupplier<T> : ISupplier<Tuple<T, T>> {
public EndoPairSupplier(T first, T second) {
_first = first;
_second = second;
}
public Tuple<T, T> Get {
get { return Tuple.Create(_first, _second); }
}
public EndoPairSupplier<R> Each<R>(Func<T, R> fun) {
return new EndoPairSupplier<R>(fun(_first), fun(_second));
}
private readonly T _first;
private readonly T _second;
}
public interface IReverseStrategy<T> {
T Reverse(T input);
}
public class SimpleStringReverseStrategy : IReverseStrategy<string> {
public string Reverse(string input) {
var charArray = input.ToCharArray();
Array.Reverse(charArray);
return new String(charArray);
}
}
public interface IPairsGenerator<T, T1, T2> {
IEnumerable<ISupplier<Tuple<T1, T2>>> Generate(params T[] input);
}
public class PairsGenerator<T, R> : IPairsGenerator<T, R, R> {
public PairsGenerator(IReverseStrategy<T> reverser, IValueConverter<T, R> converter) {
_reverser = reverser;
_converter = converter;
}
public IEnumerable<ISupplier<Tuple<R, R>>> Generate(params T[] input) {
foreach(var value in input) {
var reversedValue = _reverser.Reverse(value);
var pairSupplier = new EndoPairSupplier<T>(value, reversedValue);
yield return pairSupplier.Each(v => _converter.Convert(v));
}
}
private readonly IReverseStrategy<T> _reverser;
private readonly IValueConverter<T, R> _converter;
}
public class MainClass {
public static ISupplier<Tuple<int, int>> MyFunc(string numStr) {
var pairsGenerator = new PairsGenerator<string, int>(
new SimpleStringReverseStrategy(),
new StringToIntValueConverter()
);
var result = pairsGenerator.Generate(numStr);
return result.FirstOrDefault();
}
public static void Main(string[] args) {
var numStr = "123";
var supplier = MyFunc(numStr);
Console.WriteLine(supplier.Get);
}
}
}
[Inny język, ale wszystkie konstrukcje występują w jakiś sposób w C++].