{"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-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;credit&quot;:&quot;Rangarajan&quot;,&quot;created_timestamp&quot;:&quot;1467881619&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_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_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_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-4u","jetpack-related-posts":[{"id":3017,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/02\/16\/calling-external-dll-functions-from-julia\/","url_meta":{"origin":278,"position":0},"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":1},"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":2},"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":[]},{"id":2927,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/20\/why-learn-lisp\/","url_meta":{"origin":278,"position":3},"title":"Why Learn Lisp?","author":"admin","date":"October 20, 2022","format":false,"excerpt":"In the last article, I had shared my views on why programmers should learn Prolog, preferably as the first language. What language should one learn next? I strongly pitch for Lisp, to be precise, \u201cCommon Lisp\u201d. Lisp happens to be the second oldest (1958) programming language, only after Fortran (1957)!\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":41,"url":"https:\/\/www.rangakrish.com\/index.php\/2015\/10\/08\/learning-lisp-resources\/","url_meta":{"origin":278,"position":4},"title":"Learning LISP &#8211; Resources","author":"admin","date":"October 8, 2015","format":false,"excerpt":"During my talks on Lisp Programming, I am invariably asked to suggest good books to start learning the language. Here is a (partial) list that I readily recommend. Beginner Level: ANSI Common Lisp, Paul Graham, Prentice Hall, 1996. Common LISP: A Tutorial, Wendy L.Milner, Prentice Hall, 1988. Land of LISP:\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2645,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/12\/25\/calling-lisp-functions-from-d-language\/","url_meta":{"origin":278,"position":5},"title":"Calling Lisp Functions from D Language","author":"admin","date":"December 25, 2021","format":false,"excerpt":"After exploring \"newLisp\" in the past few posts, I would like to start looking at the \"D Programming Language\"\u00a0(DLang). DLang\u00a0has been around since 2001. It was originally created by Walter Bright and later Andrei Alexandrescu joined the team in 2007. The main inspiration for DLang was C++, although it uses\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Lisp Function Called from D","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_featured_media_url":"","_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}]}}