{"id":278,"date":"2016-07-07T05:00:43","date_gmt":"2016-07-07T05:00:43","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=278"},"modified":"2016-07-07T05:06:58","modified_gmt":"2016-07-07T05:06:58","slug":"multimethods-in-julia","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2016\/07\/07\/multimethods-in-julia\/","title":{"rendered":"Multimethods in Julia"},"content":{"rendered":"<p>I got interested in Julia programming language quite recently, primarily because of a project involving image processing and machine learning. The language is still evolving, but already has a rich set of features and a good collection of external libraries\u00a0covering many areas. One of the highlights of the language is its highly optimised code generation, thus paving way for faster execution.<\/p>\n<p>The first thing that caught my attention was Julia\u2019s support for Multimethods, inspired by Lisp. You may want to watch <a href=\"https:\/\/www.youtube.com\/watch?v=dK3zRXhrFZY\" target=\"_blank\">this\u00a0video<\/a>.<\/p>\n<p>In October 2015, I had written a post on <a href=\"http:\/\/www.rangakrish.com\/index.php\/2015\/10\/13\/multimethods-in-lisp\/\" target=\"_blank\">Multimethods in Lisp<\/a>\u00a0(it might be a good idea to read that article before continuing). Recall that a multimethod is a method that is selected based on the runtime types of all of its arguments (not just the first argument).<\/p>\n<p>My development platform\u00a0is <strong>Windows 10<\/strong>. <strong>Atom<\/strong> is my IDE with <strong>Juno<\/strong> plug-in.<\/p>\n<p>So how easy is it to implement that example in Julia? Here is the hierarchy from the earlier example:<\/p>\n<figure id=\"attachment_55\" aria-describedby=\"caption-attachment-55\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"55\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2015\/10\/13\/multimethods-in-lisp\/multimethods-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png\" data-orig-size=\"617,349\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Multimethods\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Multimethods Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png\" class=\"wp-image-55\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?resize=400%2C226\" alt=\"Multimethods Example\" width=\"400\" height=\"226\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?w=617&amp;ssl=1 617w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?resize=300%2C170&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?resize=352%2C198&amp;ssl=1 352w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-55\" class=\"wp-caption-text\">Multimethods Example<\/figcaption><\/figure>\n<p>Interestingly, Julia does not allow deriving from a concrete class. (I do not support this decision, but without getting into a debate on this, let us move on.) So I had to make <strong>Planet<\/strong> an <em><strong>abstract<\/strong><\/em> class for the code to work.<\/p>\n<p>The implementation is otherwise pretty straightforward:<\/p>\n<figure id=\"attachment_279\" aria-describedby=\"caption-attachment-279\" style=\"width: 591px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-Multimethods.jpg\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"279\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2016\/07\/07\/multimethods-in-julia\/julia-multimethods\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-Multimethods.jpg\" data-orig-size=\"591,886\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Rangarajan&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1467881619&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Julia Multimethods\" data-image-description=\"&lt;p&gt;Julia Multimethods&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Julia Multimethods&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-Multimethods.jpg\" class=\"size-full wp-image-279\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-Multimethods.jpg?resize=591%2C886\" alt=\"Julia Multimethods\" width=\"591\" height=\"886\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-Multimethods.jpg?w=591&amp;ssl=1 591w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-Multimethods.jpg?resize=200%2C300&amp;ssl=1 200w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/a><figcaption id=\"caption-attachment-279\" class=\"wp-caption-text\">Julia Multimethods<\/figcaption><\/figure>\n<p>You can download the code <a href=\"http:\/\/www.rangakrish.com\/downloads\/Multimethods Example.jl\" target=\"_blank\">here<\/a>.<\/p>\n<p>Watch out for more on Julia in future articles!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I got interested in Julia programming language quite recently, primarily because of a project involving image processing and machine learning. The language is still evolving, but already has a rich set of features and a good collection of external libraries\u00a0covering many areas. One of the highlights of the language is its highly optimised code generation, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[63,17],"tags":[64,21],"class_list":["post-278","post","type-post","status-publish","format-standard","hentry","category-julia","category-programming","tag-julia","tag-multimethods"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-4u","jetpack-related-posts":[{"id":47,"url":"https:\/\/www.rangakrish.com\/index.php\/2015\/10\/13\/multimethods-in-lisp\/","url_meta":{"origin":278,"position":0},"title":"Multimethods in Lisp","author":"admin","date":"October 13, 2015","format":false,"excerpt":"In object-oriented languages such as C++ and Java, virtual functions are dynamically dispatched based on the runtime type of the receiving object. But if such virtual functions have an argument that is itself based on a class hierarchy, there is no way to associate dynamic dispatch based on both the\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Multimethods Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3555,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/11\/22\/multiobjective-optimization-moo-in-lisp-and-prolog\/","url_meta":{"origin":278,"position":1},"title":"Multiobjective Optimization (MOO) in Lisp and Prolog","author":"admin","date":"November 22, 2024","format":false,"excerpt":"Recently I came across a nice article\u00a0by Jose Crespo, where the author stresses that the future of programming revolves around the application of math concepts such as Functor, Monads, Folds, etc. In addition, he argues that familiarity with C\/C++ is essential in this modern AI age. The author goes through\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Lisp Code for Pareto Front","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/11\/lisp-code-249x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3350,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/03\/09\/the-hy-programming-language-part-2\/","url_meta":{"origin":278,"position":2},"title":"The Hy Programming Language &#8211; Part 2","author":"admin","date":"March 9, 2024","format":false,"excerpt":"I wrote about \u201cHy\u201d language in an earlier article. Since then I have been exploring the language some more, trying to get a better understanding of its features.\u00a0 The following example uses inheritance to model different types of \u201cSpace\u201d objects such as Asteroid, SpaceShip and Planet. It is taken from\u2026","rel":"","context":"In &quot;Hy Language&quot;","block_context":{"text":"Hy Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/hy-language\/"},"img":{"alt_text":"Inheritance Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example-201x300.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example-201x300.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example-201x300.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3017,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/02\/16\/calling-external-dll-functions-from-julia\/","url_meta":{"origin":278,"position":3},"title":"Calling External DLL Functions from Julia","author":"admin","date":"February 16, 2023","format":false,"excerpt":"Julia supports calling external functions, especially those written in \u201cC\u201d language. As Julia documentation says, such calls do not involve any \u201cboilerplate\u201d code and hence are efficient. In today\u2019s article, I am going to show how to call functions defined in a DLL (Windows 64 bit). Instead of using direct\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"The \"lispEval\" Function","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3171,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/23\/the-operator-in-julia\/","url_meta":{"origin":278,"position":4},"title":"The &#8220;===&#8221; Operator in Julia","author":"admin","date":"July 23, 2023","format":false,"excerpt":"In addition to the conventional \"==\" operator that checks if two objects are equal, Julia has the \"===\" operator to check for equality. What is the difference between these two operators? The \"==\" operator checks whether two objects have the same value. Here are some examples: Cell 5 shows that\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"The \"==\" Operator","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/examples1-153x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3035,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/","url_meta":{"origin":278,"position":5},"title":"Julia: Defining Functions Dynamically","author":"admin","date":"March 5, 2023","format":false,"excerpt":"One of the widely discussed features of Julia is its support for metaprogramming. This feature makes it possible to generate and inspect the code at runtime. In essence, metaprogramming blurs the distinction between code and data. When used carefully, it can contribute to good code. Of course, Julia is not\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"Defining A Function Dynamically","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1-300x195.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1-300x195.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1-300x195.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=278"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/278\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}