{"id":426,"date":"2016-02-16T04:43:21","date_gmt":"2016-02-16T04:43:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/developer\/?p=426"},"modified":"2016-02-23T18:27:41","modified_gmt":"2016-02-23T18:27:41","slug":"inversion-of-control","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/developer\/2016\/02\/16\/inversion-of-control\/","title":{"rendered":"Invert Your Inner Control Freak!"},"content":{"rendered":"\r\n<div class=\"content\"><p>Arvind's <a href=\"https:\/\/blogs.mathworks.com\/developer\/2015\/12\/18\/open-and-extensible\">recent<\/a> <a href=\"https:\/\/blogs.mathworks.com\/developer\/2016\/01\/16\/making-code-usable-useful-and-testable\/\">posts<\/a> have had me thinking. While the spellchecker he outlines is really just for illustrative purposes, its actually an example that is great to show the power, ease of use, and connectivity of MATLAB. I'd like to add to that the ability to showcase a software design pattern or two. Today I'd like to continue Arvind's spellchecker example to meander a bit around the topic of inversion of control.<\/p><p>Inversion of control is sometimes called the Hollywood principle:<\/p><p><i>\"Don't call us, we'll call you!\"<\/i><\/p><p>It is a technique for structuring software that encourages creating abstractions and avoiding direct dependencies on the components and services that actually make the software work. When first encountered it can be a bit counterintuitive, but it facilitates flexibility and modularity like nobody's business! The big takeaway is that components should really avoid doing work themselves and instead rely upon the abstractions that collaborators can fulfill. Got it? Not really? Lemme show by example. Take a look at the start of the spellcheck constructor from last post:<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">function<\/span> obj = spellcheck(varargin)\r\n    \r\n    <span class=\"comment\">% Make Jazzy available to MATLAB<\/span>\r\n    import <span class=\"string\">com.mathworks.spellcheck.*<\/span>;\r\n    \r\n    <span class=\"comment\">% Setup a default language.<\/span>\r\n    obj.Dictionary = which(<span class=\"string\">'en_USx.dic'<\/span>); <span class=\"comment\">% default<\/span>\r\n    \r\n    <span class=\"comment\">% Create a jazzy spellchecker<\/span>\r\n    obj.Handle = SpellCheck();\r\n    obj.Handle.setDictionary(obj.Dictionary);\r\n    obj.Handle.verbose = true;\r\n    \r\n<span class=\"keyword\">end<\/span>\r\n\r\n<\/pre><p>This looks innocuous enough but it does not practice the principle of the day. Here in this constructor snippet we are setting the <b><tt>Dictionary<\/tt><\/b> property explicitly as well as creating our spell checking engine (Jazzy). This is OK for small programs, but this approach leads us to rigidity in larger systems. The spellchecker now has a concrete dependency on both the Jazzy library and this particular dictionary. This means that we can't use this spellchecker technology in the absence of Jazzy (like with a different spellchecking engine, perhaps in <a href=\"https:\/\/www.mathworks.com\/help\/compiler\/matlab-runtime-startup-options.html?searchHighlight=nojvm\"><tt>-nojvm<\/tt><\/a> mode), or in the absence of the English dictionary.<\/p><p>\"Well of course!\" you might say, \"How else would we get anything done?\" you might ask. After all, we need to invoke this code somehow somewhere, why not in the constructor? Well, let's remove one of these hard dependencies to see how its done. Today I'll show this for the dictionary file, although the same process would still apply to the spell checking engine just as well.<\/p><p>The approach needed is to create an abstraction, and rather than hardcoding <i>which<\/i> dictionary in the constructor, we should pass in the abstraction. We ask the creator of our instance to provide what we depend on rather than ourselves creating a concrete version of what we depend on directly in the constructor. The nice benefit here is that when we ask for this to be provided in the constructor inputs, many more possibilities open up in the future for which things can be passed in. As the world evolves around us we see that this leads to much more flexibility and ability to react to change and reuse our software in contexts not envisioned when it was created.<\/p><p>For our spellchecker, let's remove our explicit reference of the dictionary. The first step is to create an abstraction:<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">classdef<\/span> Dictionary &lt; matlab.mixin.Heterogeneous\r\n    \r\n    <span class=\"keyword\">properties<\/span>(Abstract)\r\n        DictionaryFile\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n<span class=\"keyword\">end<\/span>\r\n\r\n<\/pre><p>Note, I've marked it <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.mixin.heterogeneous-class.html\">Heterogeneous<\/a> because I am going to want to take advantage of MATLAB's fundamental array nature, but I'd like to do so with different dictionary implementations.<\/p><p>Now that we have the abstraction we can just operate on that abstraction by asking that it be passed into the constructor:<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">function<\/span> checker = spellcheck(dictionary)\r\n\r\n    <span class=\"comment\">% Hold onto our dictionary<\/span>\r\n    checker.Dictionary = dictionary;\r\n\r\n    <span class=\"comment\">% Create the jazzy spellchecker<\/span>\r\n    checker.Handle = com.mathworks.spellcheck.SpellCheck();\r\n    checker.Handle.setDictionary(dictionary.DictionaryFile);\r\n    checker.Handle.verbose = true;\r\n\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<\/pre><p>It's totally subtle!...but I love seeing this. In my mind the ideal constructor is doing nothing but setting properties from the values passed in. If we did this exercise once more for the Handle property we would also pass in the <b><tt>SpellCheckEngine<\/tt><\/b> or some such and all the constructor would do would set these two properties.<\/p><p>For reference here is a complete simple version of this spellchecker. (note depends on the small java code developed <a href=\"https:\/\/blogs.mathworks.com\/developer\/2015\/12\/18\/open-and-extensible\/\">a couple posts ago<\/a>).<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">classdef<\/span> spellcheck\r\n    \r\n    <span class=\"keyword\">properties<\/span>\r\n        Dictionary;\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n    <span class=\"keyword\">properties<\/span>(GetAccess=private, SetAccess=immutable)\r\n        Handle;\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n    <span class=\"keyword\">methods<\/span>\r\n        <span class=\"keyword\">function<\/span> checker = spellcheck(dictionary)\r\n            \r\n            <span class=\"comment\">% Hold onto our dictionary<\/span>\r\n            checker.Dictionary = dictionary;\r\n            \r\n            <span class=\"comment\">% Create the jazzy spellchecker<\/span>\r\n            checker.Handle = com.mathworks.spellcheck.SpellCheck();\r\n            checker.Handle.setDictionary(dictionary.DictionaryFile);\r\n            checker.Handle.verbose = true;\r\n            \r\n        <span class=\"keyword\">end<\/span>\r\n        \r\n<span class=\"comment\">        %% CHECK Method to check an input string<\/span>\r\n        <span class=\"keyword\">function<\/span> check(checker, inputStr)\r\n            checker.Handle.checkSpelling(inputStr);\r\n        <span class=\"keyword\">end<\/span>\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n    \r\n<span class=\"keyword\">end<\/span> \r\n\r\n<\/pre><p>Anyway, now that we are operating on this abstraction we can start creating some dictionaries to fulfill it:<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">classdef<\/span> AmericanEnglishDictionary &lt; Dictionary\r\n    \r\n    <span class=\"keyword\">properties<\/span>\r\n        DictionaryFile = <span class=\"string\">'en_USx.dic'<\/span>;\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n<span class=\"keyword\">end<\/span>\r\n    \r\n\r\n<\/pre><pre class=\"codeinput\">checker = spellcheck(AmericanEnglishDictionary);\r\nchecker.check(<span class=\"string\">'Do you speak MALTAB?'<\/span>);\r\n<\/pre><pre class=\"codeoutput\">Input : MALTAB\r\n\tSuggestion: MATLAB\r\n<\/pre><p>Oops! Indeed:<\/p><pre class=\"codeinput\">checker.check(<span class=\"string\">'Do you speak MATLAB?'<\/span>);\r\n<\/pre><p>Silence is golden.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/2016DoYouSpeakMATLAB.jpg\" alt=\"\"> <\/p><p>But this doesn't work:<\/p><pre class=\"codeinput\">checker.check(<span class=\"string\">'\u00bfHablas MATLAB?'<\/span>);\r\n<\/pre><pre class=\"codeoutput\">Input : Hablas\r\n\tSuggestion: tablas\r\n<\/pre><p>...and nor should it, because we are speaking a different language. No problem, let's do the same thing for Spanish and we are good to go:<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">classdef<\/span> MexicanSpanishDictionary &lt; Dictionary\r\n    \r\n    <span class=\"keyword\">properties<\/span>\r\n        DictionaryFile = <span class=\"string\">'es_MXx.dic'<\/span>;\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n<span class=\"keyword\">end<\/span>\r\n    \r\n\r\n<\/pre><pre class=\"codeinput\">checker = spellcheck(MexicanSpanishDictionary);\r\nchecker.check(<span class=\"string\">'\u00bfHablas MATLAB?'<\/span>);\r\n<\/pre><p>El silencio es oro.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/2016HablasMATLAB.jpg\" alt=\"\"> <\/p><p>Well, now that we are running at full throttle we can take this abstraction however far we want. We no longer have to be tied to an individual language file, the implementing dictionary can do whatever it wants as long as it stays true to the <b><tt>Dictionary<\/tt><\/b> interface. For example, let's see if we can use this to take care of an entire continent, namely North America. First, we'll need a couple more dictionaries for the Canadians, one more English:<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">classdef<\/span> CanadianEnglishDictionary &lt; Dictionary\r\n    \r\n    <span class=\"keyword\">properties<\/span>\r\n        DictionaryFile = <span class=\"string\">'en_CAx.dic'<\/span>;\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n<span class=\"keyword\">end<\/span>\r\n    \r\n\r\n<\/pre><p>and a French dictionary. My apologies French Canadians! I know this is not correct, but it seems the Jazzy libraries with which we are demonstrating this concept don't have a fr_CAx.dic file. Sorry, but humor me this will get us through the example.<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">classdef<\/span> CanadianFrenchDictionary &lt; Dictionary\r\n    \r\n    <span class=\"keyword\">properties<\/span>\r\n        DictionaryFile = <span class=\"string\">'fr_FRx.dic'<\/span>;\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n<span class=\"keyword\">end<\/span>\r\n    \r\n\r\n<\/pre><p>Up to this point we could only operate on a single dictionary. However, now that we are operating on an outside abstraction our code still works with totally different implementations of this abstraction. For example, nothing now stops us from implementing this reusable multi-lingual dictionary that builds off of all the dictionaries we already have:<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">classdef<\/span> MultiLingualDictionary &lt; Dictionary\r\n    \r\n    <span class=\"keyword\">properties<\/span>\r\n        DictionaryFile;\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n    <span class=\"keyword\">methods<\/span>\r\n        <span class=\"keyword\">function<\/span> dict = MultiLingualDictionary(file, dictionaries)\r\n            dict.DictionaryFile = file;\r\n            \r\n            <span class=\"comment\">% Open our dictionary file and append all dictionary languages to it. Need<\/span>\r\n            <span class=\"comment\">% to open using UTF-8 encoding, which is the format for Jazzy dictionaries.<\/span>\r\n            [fileID, msg] = fopen(file,<span class=\"string\">'w'<\/span>,<span class=\"string\">'native'<\/span>,<span class=\"string\">'UTF-8'<\/span>);\r\n            assert(fileID &gt; 0, msg);\r\n            cleaner = onCleanup(@() fclose(fileID));\r\n            <span class=\"keyword\">for<\/span> idx = 1:numel(dictionaries)\r\n                fprintf(fileID, fileread(dictionaries(idx).DictionaryFile));\r\n            <span class=\"keyword\">end<\/span>\r\n        <span class=\"keyword\">end<\/span>\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n    \r\n<span class=\"keyword\">end<\/span>\r\n    \r\n\r\n<\/pre><p>and right off the bat I am going to use this to conquer the mighty North American spell checking problem!<\/p><pre class=\"codeinput\">northAmericanDictionaries = [MexicanSpanishDictionary AmericanEnglishDictionary CanadianEnglishDictionary CanadianFrenchDictionary]\r\n<\/pre><pre class=\"codeoutput\">\r\nnorthAmericanDictionaries = \r\n\r\n  1x4 heterogeneous Dictionary (MexicanSpanishDictionary, AmericanEnglishDictionary, CanadianEnglishDictionary, ...) array with no properties.\r\n\r\n<\/pre><p>Let's now pass that array of dictionaries into our new <b><tt>MultiLingualDictionary<\/tt><\/b> and we are good to go.<\/p><pre class=\"codeinput\">checker =spellcheck(MultiLingualDictionary(<span class=\"string\">'northAmerican.dic'<\/span>, northAmericanDictionaries));\r\n\r\nchecker.check(<span class=\"string\">'\u00bfHablas MATLAB?'<\/span>);\r\nchecker.check(<span class=\"string\">'Do you speak MATLAB?'<\/span>);\r\nchecker.check(<span class=\"string\">'Parlez-vous MATLAB?'<\/span>);\r\nchecker.check(<span class=\"string\">'Do you speak MATLAB, eh?'<\/span>);\r\n<\/pre><p>I kinda <i>wanted<\/i> that last one to yell at me. That's two strikes against me from my Canadian friends today.<\/p><p>So there we go, a much more flexible system we have now, at least as the dictionary management goes. I leave for you the exercise to do the same thing for the spellchecking engine.<\/p><p>What do you think? Are you convinced? Are you an inversion of control expert already? Tell me how you apply the technique in MATLAB.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/2016ParlezVousMATLAB.jpg\" alt=\"\"> <\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_a366d968bd6f4046b3aae174d3be58fa() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='a366d968bd6f4046b3aae174d3be58fa ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a366d968bd6f4046b3aae174d3be58fa';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_a366d968bd6f4046b3aae174d3be58fa()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015b<br><\/p><\/div><!--\r\na366d968bd6f4046b3aae174d3be58fa ##### SOURCE BEGIN #####\r\n%% \r\n%\r\n% Arvind's\r\n% <https:\/\/blogs.mathworks.com\/developer\/2015\/12\/18\/open-and-extensible\r\n% recent>\r\n% <https:\/\/blogs.mathworks.com\/developer\/2016\/01\/16\/making-code-usable-useful-and-testable\/\r\n% posts> have had me thinking. While the spellchecker he outlines is really\r\n% just for illustrative purposes, its actually an example that is great to\r\n% show the power, ease of use, and connectivity of MATLAB. I'd like to add\r\n% to that the ability to showcase a software design pattern or two. Today\r\n% I'd like to continue Arvind's spellchecker example to meander a bit\r\n% around the topic of inversion of control.\r\n%\r\n% Inversion of control is sometimes called the Hollywood principle:\r\n%\r\n% _\"Don't call us, we'll call you!\"_\r\n%\r\n% It is a technique for structuring software that encourages creating\r\n% abstractions and avoiding direct dependencies on the components and\r\n% services that actually make the software work. When first encountered it\r\n% can be a bit counterintuitive, but it facilitates flexibility and\r\n% modularity like nobody's business! The big takeaway is that components\r\n% should really avoid doing work themselves and instead rely upon the\r\n% abstractions that collaborators can fulfill. Got it? Not really? Lemme\r\n% show by example. Take a look at the start of the spellcheck constructor\r\n% from last post:\r\n%\r\n% <include>originalConstructor.m<\/include>\r\n%\r\n% This looks innocuous enough but it does not practice the principle of the\r\n% day. Here in this constructor snippet we are setting the *|Dictionary|*\r\n% property explicitly as well as creating our spell checking engine\r\n% (Jazzy). This is OK for small programs, but this approach leads us to\r\n% rigidity in larger systems. The spellchecker now has a concrete\r\n% dependency on both the Jazzy library and this particular dictionary. This\r\n% means that we can't use this spellchecker technology in the absence of\r\n% Jazzy (like with a different spellchecking engine, perhaps in\r\n% <https:\/\/www.mathworks.com\/help\/compiler\/matlab-runtime-startup-options.html?searchHighlight=nojvm\r\n% |-nojvm|> mode), or in the absence of the English dictionary.\r\n% \r\n% \"Well of course!\" you might say, \"How else would we get anything done?\"\r\n% you might ask. After all, we need to invoke this code somehow somewhere,\r\n% why not in the constructor? Well, let's remove one of these hard\r\n% dependencies to see how its done. Today I'll show this for the dictionary\r\n% file, although the same process would still apply to the spell checking\r\n% engine just as well.\r\n%\r\n% The approach needed is to create an abstraction, and rather than\r\n% hardcoding _which_ dictionary in the constructor, we should pass in the\r\n% abstraction. We ask the creator of our instance to provide what we depend\r\n% on rather than ourselves creating a concrete version of what we depend on\r\n% directly in the constructor. The nice benefit here is that when we ask\r\n% for this to be provided in the constructor inputs, many more\r\n% possibilities open up in the future for which things can be passed in. As\r\n% the world evolves around us we see that this leads to much more\r\n% flexibility and ability to react to change and reuse our software in\r\n% contexts not envisioned when it was created.\r\n%\r\n% For our spellchecker, let's remove our explicit reference of the\r\n% dictionary. The first step is to create an abstraction:\r\n%\r\n% <include>Dictionary.m<\/include>\r\n%\r\n% Note, I've marked it\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.mixin.heterogeneous-class.html\r\n% Heterogeneous> because I am going to want to take advantage of MATLAB's\r\n% fundamental array nature, but I'd like to do so with different\r\n% dictionary implementations.\r\n%\r\n% Now that we have the abstraction we can just operate on that abstraction\r\n% by asking that it be passed into the constructor:\r\n%\r\n% <include>newConstructor.m<\/include>\r\n%\r\n% It's totally subtle!...but I love seeing this. In my mind the ideal\r\n% constructor is doing nothing but setting properties from the values\r\n% passed in. If we did this exercise once more for the Handle property we\r\n% would also pass in the *|SpellCheckEngine|* or some such and all the\r\n% constructor would do would set these two properties.\r\n%\r\n% For reference here is a complete simple version of this spellchecker.\r\n% (note depends on the small java code developed\r\n% <https:\/\/blogs.mathworks.com\/developer\/2015\/12\/18\/open-and-extensible\/ a\r\n% couple posts ago>).\r\n%\r\n% <include>spellcheck<\/include>\r\n%\r\n% Anyway, now that we are operating on this abstraction we can start\r\n% creating some dictionaries to fulfill it:\r\n%\r\n% <include>AmericanEnglishDictionary.m<\/include>\r\n%\r\nchecker = spellcheck(AmericanEnglishDictionary);\r\nchecker.check('Do you speak MALTAB?');\r\n\r\n%%\r\n% Oops! Indeed:\r\nchecker.check('Do you speak MATLAB?');\r\n\r\n%%\r\n% Silence is golden.\r\n%\r\n% <<2016DoYouSpeakMATLAB.jpg>>\r\n%\r\n% But this doesn't work:\r\n%\r\nchecker.check('\u00bfHablas MATLAB?');\r\n\r\n%%\r\n% ...and nor should it, because we are speaking a different language. No\r\n% problem, let's do the same thing for Spanish and we are good to go:\r\n%\r\n% <include>MexicanSpanishDictionary.m<\/include>\r\n%\r\nchecker = spellcheck(MexicanSpanishDictionary);\r\nchecker.check('\u00bfHablas MATLAB?');\r\n\r\n%%\r\n% El silencio es oro.\r\n%\r\n% <<2016HablasMATLAB.jpg>>\r\n%\r\n% Well, now that we are running at full throttle we can take this\r\n% abstraction however far we want. We no longer have to be tied to an\r\n% individual language file, the implementing dictionary can do whatever it\r\n% wants as long as it stays true to the *|Dictionary|* interface. For example,\r\n% let's see if we can use this to take care of an entire continent, namely\r\n% North America. First, we'll need a couple more dictionaries for the\r\n% Canadians, one more English:\r\n%\r\n% <include>CanadianEnglishDictionary.m<\/include>\r\n%\r\n% and a French dictionary. My apologies French Canadians! I know this is\r\n% not correct, but it seems the Jazzy libraries with which we are\r\n% demonstrating this concept don't have a fr_CAx.dic file. Sorry, but humor\r\n% me this will get us through the example.\r\n%\r\n% <include>CanadianFrenchDictionary.m<\/include>\r\n%\r\n% Up to this point we could only operate on a single dictionary. However,\r\n% now that we are operating on an outside abstraction our code still works\r\n% with totally different implementations of this abstraction. For example,\r\n% nothing now stops us from implementing this reusable multi-lingual\r\n% dictionary that builds off of all the dictionaries we already have:\r\n%\r\n% <include>MultiLingualDictionary.m<\/include>\r\n%\r\n% and right off the bat I am going to use this to conquer the mighty North American\r\n% spell checking problem!\r\n%\r\nnorthAmericanDictionaries = [MexicanSpanishDictionary AmericanEnglishDictionary CanadianEnglishDictionary CanadianFrenchDictionary]\r\n\r\n\r\n%%\r\n% Let's now pass that array of dictionaries into our new\r\n% *|MultiLingualDictionary|* and we are good to go.\r\nchecker =spellcheck(MultiLingualDictionary('northAmerican.dic', northAmericanDictionaries));\r\n\r\nchecker.check('\u00bfHablas MATLAB?');\r\nchecker.check('Do you speak MATLAB?');\r\nchecker.check('Parlez-vous MATLAB?');\r\nchecker.check('Do you speak MATLAB, eh?');\r\n\r\n%% \r\n% I kinda _wanted_ that last one to yell at me. That's two strikes against\r\n% me from my Canadian friends today.\r\n%\r\n% So there we go, a much more flexible system we have now, at least as the\r\n% dictionary management goes. I leave for you the exercise to do the same\r\n% thing for the spellchecking engine.\r\n%\r\n% What do you think? Are you convinced? Are you an inversion of control\r\n% expert already? Tell me how you apply the technique in MATLAB.\r\n%\r\n% <<2016ParlezVousMATLAB.jpg>>\r\n\r\n\r\n\r\n##### SOURCE END ##### a366d968bd6f4046b3aae174d3be58fa\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/developer\/files\/2016DoYouSpeakMATLAB.jpg\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>\r\nArvind's recent posts have had me thinking. While the spellchecker he outlines is really just for illustrative purposes, its actually an example that is great to show the power, ease of use, and... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/developer\/2016\/02\/16\/inversion-of-control\/\">read more >><\/a><\/p>","protected":false},"author":90,"featured_media":443,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,5],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/426"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/users\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/comments?post=426"}],"version-history":[{"count":16,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/426\/revisions"}],"predecessor-version":[{"id":446,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/426\/revisions\/446"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/media\/443"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/media?parent=426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/categories?post=426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/tags?post=426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}