This should do what you want (although it uses the mutation internally, it feels functional in terms of subscribers):
public List<Tuple<T, double>> Replace(List<Tuple<T, double>> collection, T term, double value) { var result = collection.Where(x => !x.Item1.Equals(term)).ToList(); result.Add(Tuple.Create(term, value)); return result; }
An alternative way to do this is to use "map" ( select in LINQ):
public List<Tuple<T, double>> Replace(List<Tuple<T, double>> collection, T term, double value) { return collection.Select(x => Tuple.Create( x.Item1, x.Item1.Equals(term) ? value : x.Item2)).ToList(); }
But this may give you different results than your original intention. Although, for me, this is what I think when I see a method called Replace , which is a replacement for the place.
UPDATE
You can also create what you want:
public List<Tuple<T, double>> Replace(List<Tuple<T, double>> collection, T term, double value) { return collection. Where(x => !x.Item1.Equals(term)). Append(Tuple.Create(term, value)). ToList(); }
Using Concat as indicated by Oded:
public static class EnumerableEx { public static IEnumerable<T> Append<T>(this IEnumerable<T> source, T item) { return source.Concat(new T[] { item }); } }