Skip to content

Instantly share code, notes, and snippets.

@mausch
Last active December 15, 2015 09:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mausch/5241729 to your computer and use it in GitHub Desktop.
Save mausch/5241729 to your computer and use it in GitHub Desktop.
Anonymous iterators in C# with Ix
var hello = "";
var enumerable =
EnumerableEx.Create<int>(async Yield => {
await Yield.Return(100);
await Yield.Return(200);
hello = "hello world";
await Yield.Return(300);
});
Console.WriteLine(enumerable.ElementAt(1)); // 200
Console.WriteLine(hello); // empty
Console.WriteLine(enumerable.ElementAt(2)); // 300
Console.WriteLine(hello); // "hello world"
let mutable hello = ""
let enumerable =
seq {
yield 100
yield 200
hello <- "hello world"
yield 300
}
printfn "%d" (enumerable.ElementAt 1) // 200
printfn "%s" hello // empty
printfn "%d" (enumerable.ElementAt 2) // 300
printfn "%s" hello // "hello world"
Dim hello = ""
Dim enumerable =
Iterator Function()
Yield 100
Yield 200
hello = "hello world"
Yield 300
End Function
Console.WriteLine(enumerable().ElementAt(1)) ' 200
Console.WriteLine(hello) ' empty
Console.WriteLine(enumerable().ElementAt(2)) ' 300
Console.WriteLine(hello) ' "hello world"
@akimboyko
Copy link

I got following InnerException on line 10 while using Ix-Main 0.9.0.2:

StackTrace
at System.Linq.EnumerableEx.d__a61.MoveNext() at System.Linq.Enumerable.ElementAt[TSource](IEnumerable1 source, Int32 index)
at UserQuery.RunUserAuthoredQuery() in Source System.Interactive

Message
Inheritance security rules violated while overriding member: 'System.Linq.Yielder`1.UnsafeOnCompleted(System.Action)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.

TypeName
System.Linq.Yielder`1.UnsafeOnCompleted(System.Action)

@mausch
Copy link
Author

mausch commented Mar 26, 2013

I copied the code directly from the diff Erik posted: http://rx.codeplex.com/SourceControl/changeset/8f1e73bca96e474ea73f40de1af9b8fc9307fddb , it worked ok.
I didn't use the Nuget package (didn't know this was released). It looks like they forgot to set one of those security rules/attributes that I never understand ( http://blogs.msdn.com/b/shawnfa/archive/2009/11/12/differences-between-the-security-rule-sets.aspx ).

@mausch
Copy link
Author

mausch commented Mar 26, 2013

Unhandled Exception: System.TypeLoadException: Inheritance security rules violated while overriding member: 'System.Linq.Yielder`1<T>.UnsafeOnCompleted(System.Action)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.
   at System.Linq.EnumerableEx.<Create>d__a6`1.MoveNext()
   at System.Linq.Enumerable.ElementAt[TSource](IEnumerable`1 source, Int32 index)
   at ConsoleApplication21.Program.Main(String[] args) in g:\prg\ConsoleApplication21\ConsoleApplication21\Program.cs:line 15

@mausch
Copy link
Author

mausch commented Mar 26, 2013

Created a new issue: http://rx.codeplex.com/workitem/20 , going to sleep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment