{"id":1163,"date":"2014-01-24T22:37:12","date_gmt":"2014-01-24T22:37:12","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1163"},"modified":"2014-01-24T22:37:12","modified_gmt":"2014-01-24T22:37:12","slug":"tuples-tuples-and-more-tuples","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/tuples-tuples-and-more-tuples\/","title":{"rendered":"Tuples, tuples and more tuples"},"content":{"rendered":"<p>Tuples are used a lot in F#. Let&#8217;s start with a definition <\/p>\n<p><strong>A <em>tuple<\/em> is a grouping of unnamed but ordered values, possibly of different type<\/strong><\/p>\n<p>This was take from <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd233200.aspx\" title=\"Tuples (F#)\" target=\"_blank\">Tuples (F#)<\/a> and I&#8217;d highly recommend reading this. I will undoubtedly recreate some of the information from this document here, but hopefully add something of use.<\/p>\n<p><strong>Declaring tuples<\/strong><\/p>\n<p>So we&#8217;ve got a definition (above) for what a tuple is, now let&#8217;s have a look at some<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet tupleInts = (1, 2, 3, 4)\r\nlet tupleStrings = (&quot;one&quot;, &quot;two&quot;, &quot;three&quot;)\r\nlet tupleValues = (a, b, c, d)\r\nlet tupleMixed = (&quot;one&quot;, 2, c)\r\nlet tupleExpressions = (a + 1, b + 2, c + 3)\r\n<\/pre>\n<p>The above should be pretty self explanatory.<\/p>\n<p>If we take the first tuple, name tupleInts, and run this in the F# interactive window we&#8217;ll see that this translates to <em>val tupleInts : int * int * int * int = (1, 2, 3, 4)<\/em>. The * denoting a tuple. In this case you can see that each item\/element in the tuple is of type <em>int<\/em>.<\/p>\n<p><strong>Getting values from a tuple<\/strong><\/p>\n<p>Using let bindings<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet (a, b) = (1, 2)\r\n<\/pre>\n<p>Using let bindings but ignore one or more elements (i.e. using the _ wildcard character)<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet (a, b, _) = (1, 2, 3)\r\n\r\n\/\/ or\r\n\r\nlet a, b, _ = (1, 2, 3)\r\n<\/pre>\n<p>Using pattern matching<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet tuple = (1, 2, 3)\r\n\r\nlet pattern t =\r\n    match t with\r\n    | (a, b, c) -&gt; printfn &quot;%d&quot; a\r\n\r\npattern tuple\r\n<\/pre>\n<p>We can use the <em>fst<\/em> and <em>snd<\/em> functions which get the first or second elements from the tuple, i.e.<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet a = fst (1, 2)\r\nlet b = snd (1, 2)\r\n<\/pre>\n<p>Obviously <em>a<\/em> will have the value 1 and <em>b<\/em will have the value 2, in the above. There is no third or fourth function. However using the knowledge from the let bindings (in the examples above) we can see it's easy enough to get the third or fourth values in the following manner\n\n[code language=\"fsharp\"]\nlet third (_, _, c) = c\nlet fourth (_, _, _, d) = d\n[\/code]\n\nUsing reflection we can also use \n\n[code language=\"fsharp\"]\nopen Microsoft.FSharp.Reflection\n\nlet tuple = (1, 2, 3)\nlet secondValue = FSharpValue.GetTupleField (tuple, 1)\n[\/code]\n\nI would expect (although I've not, at this time tried to profile this) that reflection may be slower than the other techniques shown, but nevertheless the option is there.\n\nWe can also use the following\n\n[code language=\"fsharp\"]\nopen Microsoft.FSharp.Reflection\n\nlet tuple = (1, 2, 3)\nlet arrayOfValues = FSharpValue.GetTupleFields (tuple)\n[\/code]\n\nwhich turns the tuple into an array of objects and ofcourse we can use the following code to get a value from the resultant array using the index of the item\n\n[code language=\"fsharp\"]\nlet secondValue = FSharpValue.GetTupleFields(tuple).[1]\n[\/code]\n\nand for completeness we can also get the types from a tuple into an array using\n\n[code language=\"fsharp\"]\nlet typeArray = tuple.GetType() |&gt; FSharpType.GetTupleElements\n\n\/\/ or if you prefer\n\nlet typeArray2 = FSharpType.GetTupleElements (tuple.GetType())\n[\/code]\n\n<strong>Returning multiple values<\/strong><\/p>\n<p>We can obviously pass multiple values into a function using a tuple, but we can also return a tuple and hence return multiple values as per<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet inc a b = (a + 1, b + 1)\r\n<\/pre>\n<p>where the return is a tuple of the values (a + 1, b + 1)<\/p>\n<p><strong>Cross language<\/strong><\/p>\n<p>In C# (.NET 4.0) we have the Tuple generic class which we can create in one of two ways<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nTuple&lt;int, int&gt; tuple = mew Tuple&lt;int, int&gt;(1, 2);\r\n\r\n\/\/ or using the helper method\r\n\r\nvar tuple = Tuple.Create(1, 2);\r\n<\/pre>\n<p>F# uses the same Tuple class internally (we can confirm this by checking the IL using ILSpy or the likes) to represent it&#8217;s tuples. However, as seen, it&#8217;s more of a language feature in F#. Hence we have the syntactic sugar to allow us to create the tuple just using <em>(item1, item2)<\/em>. <\/p>\n<p>Obviously if we can create a Tuple in C# and the underlying representation of a tuple in F# is the same, we can pass data to and from each language using this mechanism. So for example if we had this rather contrived C# code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static class Data\r\n{\r\n   public static Tuple&lt;int, string&gt; Get()\r\n   {\r\n      return Tuple.Create(123, &quot;One Two Three&quot;);\r\n   }\r\n\r\n   public static void Set(Tuple&lt;int, string&gt; tuple)\r\n   {\r\n      Console.WriteLine(tuple.Item1 + &quot; - &quot; + tuple.Item2);\r\n   }\r\n}\r\n<\/pre>\n<p>and in F# we can interact with these methods as follows<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet (num, value) = CSharpLib.Data.Get()\r\nCSharpLib.Data.Set(321, &quot;Three Two One&quot;);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Tuples are used a lot in F#. Let&#8217;s start with a definition A tuple is a grouping of unnamed but ordered values, possibly of different type This was take from Tuples (F#) and I&#8217;d highly recommend reading this. I will undoubtedly recreate some of the information from this document here, but hopefully add something of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1163","post","type-post","status-publish","format-standard","hentry","category-f"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1163"}],"version-history":[{"count":15,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1163\/revisions"}],"predecessor-version":[{"id":1179,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1163\/revisions\/1179"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}