{"id":2937,"date":"2018-12-17T12:27:49","date_gmt":"2018-12-17T10:27:49","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=2937"},"modified":"2019-03-04T00:25:16","modified_gmt":"2019-03-03T22:25:16","slug":"the-groovy-spaceship-operator-explained","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/fr\/2018\/12\/17\/the-groovy-spaceship-operator-explained\/","title":{"rendered":"L&rsquo;op\u00e9rateur de spaceship Groovy a expliqu\u00e9"},"content":{"rendered":"<p><em>Have you ever heard of the spaceship operator? If it is the case, how does it look like? To be honest, I have known it recently when I used this comparison operator to sort an array list of objects. In this post, I will show you what the operator does and how could we apply it into realistic situations.<\/em><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a507454c2494\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a507454c2494\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.itersdesktop.com\/fr\/2018\/12\/17\/the-groovy-spaceship-operator-explained\/#what-is-it\" >What is it?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.itersdesktop.com\/fr\/2018\/12\/17\/the-groovy-spaceship-operator-explained\/#spaceship-operator-in-groovy\" >Spaceship operator In Groovy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.itersdesktop.com\/fr\/2018\/12\/17\/the-groovy-spaceship-operator-explained\/#references\" >References<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"what-is-it\"><\/span>What is it?<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Spaceship operator, originally called three-way comparison and spelt &lt;=&gt;, is used for comparing two objects A and B. This operator returns the values \u22121, 0, or 1 depending on whether A &lt; B, A = B, or A &gt; B, respectively. There have been a few reasons why it was historically named \u00ab\u00a0<strong>spaceship operator<\/strong>\u00a0\u00bb [1]. In my honest opinion, the two symbols &lt; and &gt; look the heads of a spaceship while the symbol = seems to be the body of the spaceship.<\/p>\n<p>PHP 7 introduced the spaceship operator as a new feature [4, 5]. The upcoming C++ spaceship operator has been approved in\u00a0the proposals for the next C++ standardisation [6, 7].<\/p>\n<h1><span class=\"ez-toc-section\" id=\"spaceship-operator-in-groovy\"><\/span>Spaceship operator In Groovy<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Let&rsquo;s give a piece of code listed below.<\/p>\n<pre class=\"lang:default decode:true\" title=\"An example of using spaceship operator in Groovy\">def today = new Date()\r\nprintln today\r\ndef yesterday = today - 1\r\ndef tomorrow = today + 1\r\nprintln today &lt;=&gt; today\r\nprintln today &lt;=&gt; yesterday\r\nprintln today &lt;=&gt; tomorrow<\/pre>\n<p>That piece of code aims to compare <span class=\"lang:default decode:true crayon-inline \">Date<\/span>\u00a0objects. Obviously, we can evaluate the output of the last three <span class=\"lang:default decode:true crayon-inline\">println<\/span>statement. They produce 0, 1, -1, respectively.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2978\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2018\/12\/Screenshot-2019-03-03-at-21.24.33-1024x193.png\" alt=\"\" width=\"620\" height=\"117\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2018\/12\/Screenshot-2019-03-03-at-21.24.33-1024x193.png 1024w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2018\/12\/Screenshot-2019-03-03-at-21.24.33-300x56.png 300w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2018\/12\/Screenshot-2019-03-03-at-21.24.33-768x145.png 768w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>To illustrate what we have done in our work, I will present below how to sort entries of Model of The Month by the publication date. Let&rsquo;s look at the following lines:<\/p>\n<pre class=\"lang:java decode:true \">List&lt;ModelOfTheMonthTransportCommand&gt; momEntries = list()\r\nmomEntries.sort { m1, m2 -&gt; m2.publicationDate &lt;=&gt; m1.publicationDate }<\/pre>\n<p>This results <a href=\"https:\/\/www.ebi.ac.uk\/biomodels\/modelOfTheMonth\/rss\" target=\"_blank\" rel=\"noopener\">RSS feeds<\/a> where all entries of Model of The Month were ordered descending by the publication date. See the screenshot below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2977\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2018\/12\/Screenshot-2019-03-03-at-21.22.58-1002x1024.png\" alt=\"\" width=\"620\" height=\"634\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2018\/12\/Screenshot-2019-03-03-at-21.22.58-1002x1024.png 1002w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2018\/12\/Screenshot-2019-03-03-at-21.22.58-294x300.png 294w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2018\/12\/Screenshot-2019-03-03-at-21.22.58-768x784.png 768w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2018\/12\/Screenshot-2019-03-03-at-21.22.58.png 1584w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>In practice, the spaceship operator is often used for sorting objects in arrays or lists.\u00a0To write this article, I have read the section Spaceship Operator in Groovy&rsquo;s documentation [2] and a glorious explanation [3].<\/p>\n<h1><span class=\"ez-toc-section\" id=\"references\"><\/span>References<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>[1]\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Three-way_comparison\" target=\"_blank\" rel=\"noopener\">Three-way comparison<\/a><\/p>\n<p>[2]\u00a0<a href=\"http:\/\/www.groovy-lang.org\/operators.html#_spaceship_operator\" target=\"_blank\" rel=\"noopener\">Spaceship Operator<\/a><\/p>\n<p>[3]\u00a0<a href=\"https:\/\/objectpartners.com\/2010\/02\/08\/the-groovy-spaceship-operator-explained\/\" target=\"_blank\" rel=\"noopener\">The Groovy Spaceship Operator Explained<\/a><\/p>\n<p>[4]\u00a0<a href=\"http:\/\/php.net\/manual\/en\/migration70.new-features.php\" target=\"_blank\" rel=\"noopener\">New features in PHP7<\/a><\/p>\n<p>[5]\u00a0<a href=\"https:\/\/stackoverflow.com\/questions\/3737139\/reference-what-does-this-symbol-mean-in-php\/31298778\" target=\"_blank\" rel=\"noopener\">What does this symbol mean in PHP?<\/a><\/p>\n<p>[6]\u00a0<a href=\"https:\/\/foonathan.net\/blog\/2018\/10\/13\/spaceship-proposals.html\" target=\"_blank\" rel=\"noopener\">Proposals to Fix the Spaceship Operator<\/a><\/p>\n<p>[7]\u00a0<a href=\"https:\/\/foonathan.net\/blog\/2018\/06\/20\/equivalence-relations.html\" target=\"_blank\" rel=\"noopener\">Mathematics behind Comparison #1: Equality and Equivalence Relations<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Have you ever heard of the spaceship operator? If it is the case, how does it look like? To be honest, I have known it recently when I used this&hellip; <\/p>\n","protected":false},"author":2,"featured_media":2978,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[595],"tags":[655,656],"class_list":["post-2937","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-groovy","tag-spaceship-operator","tag-three-way-comparison"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/2937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/comments?post=2937"}],"version-history":[{"count":6,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/2937\/revisions"}],"predecessor-version":[{"id":2982,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/2937\/revisions\/2982"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media\/2978"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media?parent=2937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/categories?post=2937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/tags?post=2937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}