{"id":509,"date":"2008-10-12T14:08:00","date_gmt":"2008-10-12T12:08:00","guid":{"rendered":"http:\/\/www.navision-blog.de\/2008\/10\/12\/f-option-types-in-c-verwenden\/"},"modified":"2008-10-12T14:28:19","modified_gmt":"2008-10-12T12:28:19","slug":"f-option-types-und-generische-listen-in-c-verwenden","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2008\/10\/12\/f-option-types-und-generische-listen-in-c-verwenden\/","title":{"rendered":"F# option types und generische Listen in C# verwenden"},"content":{"rendered":"<p><a href=\"http:\/\/langexplr.blogspot.com\/\">Luis Fallas<\/a> beschreibt in seinem Blog (\u201cExploring Beautiful Languages\u201d) an einem sehr sch&#246;nen Beispiel, wie man die <a href=\"http:\/\/langexplr.blogspot.com\/2008\/06\/using-f-option-types-in-c.html\">F# option types mit Hilfe von Extension Methods in C# verwenden<\/a> kann.<\/p>\n<p>Hier ist eine generische Variante zu seiner Exists()-Methode:<\/p>\n<p><span style=\"color: blue\">open<\/span> System.Runtime.CompilerServices <\/p>\n<div style=\"font-size: 10pt; background: white; color: black; font-family: courier new\">\n<pre style=\"margin: 0px\">[&lt;Extension&gt;]<\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: blue\">module<\/span> Extensions =<\/pre>\n<pre style=\"margin: 0px\">&#160; [&lt;Extension&gt;]<\/pre>\n<pre style=\"margin: 0px\">&#160; <span style=\"color: blue\">let<\/span> Exists(opt : 'a option) =<\/pre>\n<pre style=\"margin: 0px\">&#160;&#160;&#160; <span style=\"color: blue\">match<\/span> opt <span style=\"color: blue\">with<\/span><\/pre>\n<pre style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; | Some _ <span style=\"color: blue\">-&gt;<\/span> <span style=\"color: blue\">true<\/span><\/pre>\n<pre style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; | None \u2013<span style=\"color: blue\">&gt;<\/span> <span style=\"color: blue\">false<\/span><\/pre>\n<\/div>\n<p>Auf &#228;hnlichem Wege kann man &#252;brigens auch die <a href=\"http:\/\/diditwith.net\/2008\/03\/03\/WhyILoveFListsTheBasics.aspx\">generischen F#-Listen<\/a> in System.Collections.Generic.List&lt;T&gt; umwandeln:<\/p>\n<div style=\"font-size: 10pt; background: white; color: black; font-family: courier new\">\n<pre style=\"margin: 0px\">[&lt;Extension&gt;]&#160;&#160;&#160; <\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: blue\">let<\/span> ToCSharpList(list : 'a list) =<\/pre>\n<pre style=\"margin: 0px\">&#160; <span style=\"color: blue\">let<\/span> csharpList = <\/pre>\n<pre style=\"margin: 0px\">    <span style=\"color: blue\">new<\/span> System.Collections.Generic.List&lt;'a&gt;()<\/pre>\n<pre style=\"margin: 0px\">&#160; list |&gt; List.iter (<span style=\"color: blue\">fun<\/span> item <span style=\"color: blue\">-&gt;<\/span> csharpList.Add item)<\/pre>\n<pre style=\"margin: 0px\">&#160; csharpList<\/pre>\n<\/div>\n<p>Der umgekehrte Weg (von C# nach F#) ist fast analog, allerdings muss man die Liste drehen:<\/p>\n<div style=\"font-size: 10pt; background: white; color: black; font-family: courier new\">\n<pre style=\"margin: 0px\"><span style=\"color: blue\">static<\/span> <span style=\"color: blue\">class<\/span> <span style=\"color: #2b91af\">Extensions<\/span><\/pre>\n<pre style=\"margin: 0px\">{<\/pre>\n<div style=\"font-size: 10pt; background: white; color: black; font-family: courier new\">\n<pre style=\"margin: 0px\"><span style=\"color: gray\">  \/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;summary&gt;<\/span><\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: gray\">  \/\/\/<\/span><span style=\"color: green\"> Converts a System.Collections.Generic.List<\/span><span style=\"color: gray\">&lt;T&gt;<\/span><span style=\"color: green\"> <\/span><\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: green\">  <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span>in the corresponding F# list.<\/span><\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: gray\">  \/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;\/summary&gt;<\/span><\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: gray\">  <\/span><span style=\"color: blue\">public<\/span> <span style=\"color: blue\">static<\/span> Microsoft.FSharp.Collections.<span style=\"color: #2b91af\">List<\/span>&lt;T&gt; <\/pre>\n<\/p><\/div>\n<pre style=\"margin: 0px\">     ToFSharpList&lt;T&gt;(<span style=\"color: blue\">this<\/span> <span style=\"color: #2b91af\">List<\/span>&lt;T&gt; list)<\/pre>\n<pre style=\"margin: 0px\">&#160; {<\/pre>\n<pre style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; <span style=\"color: blue\">var<\/span> fSharpList = <\/pre>\n<pre style=\"margin: 0px\">        Microsoft.FSharp.Collections.<span style=\"color: #2b91af\">List<\/span>&lt;T&gt;.Empty;<\/pre>\n<pre style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; <span style=\"color: blue\">for<\/span> (<span style=\"color: blue\">int<\/span> i = list.Count - 1; i &gt;= 0; i--)<\/pre>\n<pre style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160; fSharpList = <\/pre>\n<pre style=\"margin: 0px\">          Microsoft.FSharp.Collections.<span style=\"color: #2b91af\">List<\/span>&lt;T&gt;.Cons(<\/pre>\n<pre style=\"margin: 0px\">            list[i], <\/pre>\n<pre style=\"margin: 0px\">            fSharpList);<\/pre>\n<pre style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; <span style=\"color: blue\">return<\/span> fSharpList;<\/pre>\n<pre style=\"margin: 0px\">&#160; }<\/pre>\n<pre style=\"margin: 0px\">}<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Luis Fallas beschreibt in seinem Blog (\u201cExploring Beautiful Languages\u201d) an einem sehr sch&#246;nen Beispiel, wie man die F# option types mit Hilfe von Extension Methods in C# verwenden kann. Hier ist eine generische Variante zu seiner Exists()-Methode: open System.Runtime.CompilerServices [&lt;Extension&gt;] module Extensions = &#160; [&lt;Extension&gt;] &#160; let Exists(opt : &#8216;a option) = &#160;&#160;&#160; match opt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,448],"tags":[81,456,455,664,454,452,453],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/509"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=509"}],"version-history":[{"count":9,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/509\/revisions"}],"predecessor-version":[{"id":521,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/509\/revisions\/521"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=509"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}