Browse Source
Replace ICSharpCode.SharpDevelop.Func<...> with System.Func<...>; use System.Linq.Enumerable instead of the ICSharpCode.SharpDevelop.Linq helper class. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2628 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
46 changed files with 132 additions and 270 deletions
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections; |
||||
using System.Collections.ObjectModel; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace ICSharpCode.SharpDevelop |
||||
{ |
||||
/// <summary>
|
||||
/// Extension methods used in SharpDevelop.
|
||||
/// </summary>
|
||||
public static class ExtensionMethods |
||||
{ |
||||
/// <summary>
|
||||
/// Applies an action to all elements in the input.
|
||||
/// </summary>
|
||||
public static void Apply<T>(this IEnumerable<T> input, Action<T> action) |
||||
{ |
||||
if (input == null) |
||||
throw new ArgumentNullException("input"); |
||||
foreach (T element in input) { |
||||
action(element); |
||||
} |
||||
} |
||||
|
||||
public static ReadOnlyCollection<T> AsReadOnly<T>(this T[] arr) |
||||
{ |
||||
return Array.AsReadOnly(arr); |
||||
} |
||||
} |
||||
} |
@ -1,184 +0,0 @@
@@ -1,184 +0,0 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace ICSharpCode.SharpDevelop |
||||
{ |
||||
/// <summary>
|
||||
/// A set of methods that replicate some of the LINQ functionality.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Will be removed when SharpDevelop is compiled with C# 3.0.
|
||||
/// </remarks>
|
||||
public static class Linq |
||||
{ |
||||
/// <summary>
|
||||
/// Applies a conversion function to all elements in the input.
|
||||
/// </summary>
|
||||
public static IEnumerable<S> Select<T, S>(IEnumerable<T> input, Converter<T, S> converter) |
||||
{ |
||||
foreach (T element in input) { |
||||
yield return converter(element); |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Applies an action to all elements in the input.
|
||||
/// </summary>
|
||||
public static void Apply<T>(IEnumerable<T> input, Action<T> action) |
||||
{ |
||||
foreach (T element in input) { |
||||
action(element); |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Returns only the elements in input for which filter is true.
|
||||
/// </summary>
|
||||
public static IEnumerable<T> Where<T>(IEnumerable<T> input, Predicate<T> filter) |
||||
{ |
||||
foreach (T element in input) { |
||||
if (filter(element)) |
||||
yield return element; |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Returns the elements of type T inside input by running
|
||||
/// "if (element is T) yield return (T)element;" on each element.
|
||||
/// </summary>
|
||||
public static IEnumerable<T> OfType<T>(IEnumerable input) |
||||
{ |
||||
foreach (object element in input) { |
||||
if (element is T) |
||||
yield return (T)element; |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Casts a non-generic enumeration into a generic enumeration.
|
||||
/// </summary>
|
||||
public static IEnumerable<T> CastTo<T>(IEnumerable input) |
||||
{ |
||||
foreach (object element in input) { |
||||
yield return (T)element; |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Returns the first element in input for which filter is true.
|
||||
/// Returns default(T) if no element matches the filter.
|
||||
/// </summary>
|
||||
public static T Find<T>(IEnumerable<T> input, Predicate<T> filter) |
||||
{ |
||||
foreach (T element in input) { |
||||
if (filter(element)) |
||||
return element; |
||||
} |
||||
return default(T); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Gets if any element in the input matches the filter.
|
||||
/// </summary>
|
||||
public static bool Exists<T>(IEnumerable<T> input, Predicate<T> filter) |
||||
{ |
||||
foreach (T element in input) { |
||||
if (filter(element)) |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Returns the first element from <paramref name="input"/>.
|
||||
/// </summary>
|
||||
public static T First<T>(IEnumerable<T> input) |
||||
{ |
||||
if (input == null) |
||||
throw new ArgumentNullException("input"); |
||||
foreach (T item in input) { |
||||
return item; |
||||
} |
||||
throw new ArgumentException("input must not be an empty collection", "input"); |
||||
} |
||||
|
||||
public static List<T> ToList<T>(IEnumerable<T> input) |
||||
{ |
||||
return new List<T>(input); |
||||
} |
||||
|
||||
public static T[] ToArray<T>(IEnumerable<T> input) |
||||
{ |
||||
if (input is ICollection<T>) { |
||||
ICollection<T> c = (ICollection<T>)input; |
||||
T[] arr = new T[c.Count]; |
||||
c.CopyTo(arr, 0); |
||||
return arr; |
||||
} else { |
||||
return new List<T>(input).ToArray(); |
||||
} |
||||
} |
||||
|
||||
public static int Count<T>(IEnumerable<T> input) |
||||
{ |
||||
if (input is ICollection<T>) { |
||||
return ((ICollection<T>)input).Count; |
||||
} |
||||
int count = 0; |
||||
using (IEnumerator<T> e = input.GetEnumerator()) { |
||||
while (e.MoveNext()) |
||||
count++; |
||||
} |
||||
return count; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Concatenates the specified enumerables.
|
||||
/// </summary>
|
||||
public static IEnumerable<T> Concat<T>(IEnumerable<T> input1, IEnumerable<T> input2) |
||||
{ |
||||
foreach (T element in input1) { |
||||
yield return element; |
||||
} |
||||
foreach (T element in input2) { |
||||
yield return element; |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Concatenates the specified enumerables.
|
||||
/// </summary>
|
||||
public static IEnumerable<T> Concat<T>(IEnumerable<IEnumerable<T>> inputs) |
||||
{ |
||||
foreach (IEnumerable<T> input in inputs) { |
||||
foreach (T element in input) { |
||||
yield return element; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Outputs distinct elements only, filtering all duplicates.
|
||||
/// </summary>
|
||||
public static IEnumerable<T> Distinct<T>(IEnumerable<T> input) |
||||
{ |
||||
// store elements already seen
|
||||
Dictionary<T, object> elements = new Dictionary<T, object>(); |
||||
|
||||
foreach (T element in input) { |
||||
if (!elements.ContainsKey(element)) { |
||||
elements.Add(element, null); |
||||
yield return element; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -1,21 +0,0 @@
@@ -1,21 +0,0 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.SharpDevelop |
||||
{ |
||||
// define some delegates for functional programming
|
||||
public delegate void Action(); |
||||
// void Action<A>(A arg1) is already defined in System.
|
||||
public delegate void Action<A, B>(A arg1, B arg2); |
||||
public delegate void Action<A, B, C>(A arg1, B arg2, C arg3); |
||||
public delegate R Func<R>(); |
||||
public delegate R Func<A, R>(A arg1); |
||||
public delegate R Func<A, B, R>(A arg1, B arg2); |
||||
public delegate R Func<A, B, C, R>(A arg1, B arg2, C arg3); |
||||
} |
Loading…
Reference in new issue