|
use extension methods from derived class: msg#00008lang.nemerle.devel
Hi there is the following extension methods. module Extensions { public All [T] (this objs : IEnumerable[T], fn : T -> bool) : bool { return : { foreach (o in objs) when (!fn (o)) return (false); return (true); } } } and use this. def ls = $ [0, 2 .. 10]; def ar = array [1, 2, 3, 4, 5]; ls.All (x => x % 2 == 0); ar.All (x => x < 10); but compile error occurred. however, def ls = $ [0, 2 .. 10] : IEnumerable [int]; def ar = array [1, 2, 3, 4, 5] : IEnumerable [int]; then compile ok. C#3.0 (LINQ) do not need that cast. -- sample.cs for C#3.0 using System; using System.Collections.Generic; public delegate bool Pred<T>(T t); public static class Extensions { public static bool All<T> (this IEnumerable<T> objs, Pred<T> pred) { foreach (T o in objs) if (!pred(o)) return false; return true; } } public class App { public static void Main () { int[] arr = new [] { 2, 4, 6 }; if (arr.All<int>(delegate (int x) { return x % 2 == 0; })) Console.WriteLine ("All !"); } } I'd like to do this. -- akiramei <mei@xxxxxxxxxxxxxxxx>
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: List comprehensions ranges, Sandro Magi |
|---|---|
| Next by Date: | Re: use extension methods from derived class, Kamil Skalski |
| Previous by Thread: | List comprehensions ranges, mei |
| Next by Thread: | Re: use extension methods from derived class, Kamil Skalski |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |