{"id":1551,"date":"2022-03-02T10:18:53","date_gmt":"2022-03-02T10:18:53","guid":{"rendered":"http:\/\/sdi.thoughtstorms.info\/?p=1551"},"modified":"2022-03-02T10:18:53","modified_gmt":"2022-03-02T10:18:53","slug":"why-i-love-clojure","status":"publish","type":"post","link":"https:\/\/blog.thoughtstorms.info\/?p=1551","title":{"rendered":"Why I love Clojure"},"content":{"rendered":"\n<p>Here&#8217;s some code I&#8217;m writing for my day-job. <\/p>\n\n\n\n<p>I have an iterator of IScoreObjects (basically things that live on a musical score or timeline).<br \/><br \/>I want an iterator of only the Notes. Where Notes are one of the things that implement the IScoreObject interface and can live on the score.<\/p>\n\n\n\n<p>Here&#8217;s my code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>import <\/strong>java.util.Iterator;\n\n<strong>public class <\/strong>NoteIterator <strong>implements <\/strong>Iterator&lt;Note> {\n    <em>\/**\n     * Takes an iterator of IScoreObjects and returns an Iterator of only the Notes\n     *\/\n    <\/em>Iterator&lt;IScoreObject> <strong>isoIterator<\/strong>;\n    Note _<strong>next<\/strong>;\n    <strong>boolean _hasNext<\/strong>;\n\n    <strong>public <\/strong>NoteIterator(Iterator&lt;IScoreObject> isoi) {\n        <strong>isoIterator <\/strong>= isoi;\n        findNext();\n    }\n\n    <strong>private void <\/strong>findNext() {\n        <strong>while <\/strong>(<strong>isoIterator<\/strong>.hasNext()) {\n            IScoreObject n = <strong>isoIterator<\/strong>.next();\n            <strong>if <\/strong>(n.isNote()) {\n                <strong>_next <\/strong>= (Note)n;\n                <strong>_hasNext <\/strong>= <strong>true<\/strong>;\n                <strong>return<\/strong>;\n            }\n        }\n        <strong>_next <\/strong>= <strong>null<\/strong>;\n        <strong>_hasNext <\/strong>= <strong>false<\/strong>;\n    }\n\n    @Override\n    <strong>public boolean <\/strong>hasNext() {\n        <strong>return _hasNext<\/strong>;\n    }\n\n    @Override\n    <strong>public <\/strong>Note next() {\n        Note rv = _<strong>next<\/strong>;\n        findNext();\n        <strong>return <\/strong>rv;\n    }\n}<\/pre>\n\n\n\n<p>On the other hand, here&#8217;s how I&#8217;d do it in Clojure<\/p>\n\n\n\n<pre class=\"wp-code-block\"><code>(defn just-the-notes [score-objects] (filter .isNote score-objects))<\/code><\/pre>\n\n\n\n<p>This is literally something like a 20:1 ratio in line count. And the Java needs an extra file of its own. That is insane. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s some code I&#8217;m writing for my day-job. I have an iterator of IScoreObjects (basically things that live on a musical score or timeline). I want an iterator of only the Notes. Where Notes are one of the things that implement the IScoreObject interface and can live on the score. Here&#8217;s my code. import java.util.Iterator; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[67,68,219],"class_list":["post-1551","post","type-post","status-publish","format-standard","hentry","category-opinion","tag-clojurescript","tag-clojure-vs-java","tag-java"],"_links":{"self":[{"href":"https:\/\/blog.thoughtstorms.info\/index.php?rest_route=\/wp\/v2\/posts\/1551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.thoughtstorms.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.thoughtstorms.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.thoughtstorms.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.thoughtstorms.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1551"}],"version-history":[{"count":0,"href":"https:\/\/blog.thoughtstorms.info\/index.php?rest_route=\/wp\/v2\/posts\/1551\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.thoughtstorms.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.thoughtstorms.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.thoughtstorms.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}