{"id":22296,"date":"2014-07-29T11:47:53","date_gmt":"2014-07-29T09:47:53","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=22296"},"modified":"2014-07-29T11:47:53","modified_gmt":"2014-07-29T09:47:53","slug":"the-goal-challenge","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2014\/07\/29\/the-goal-challenge\/","title":{"rendered":"The goal() challenge"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>I came across <a href=\"https:\/\/github.com\/eatnumber1\/goal\">the goal() challenge<\/a> this morning.<\/p>\n<blockquote><p><em>g()(&#8216;al&#8217;)<\/em> is a challenge whereby you need to write in as many languages as possible code which enables the code <em>g()(&#8216;al&#8217;)<\/em> to return the string &#8220;goal&#8221;, the <em>code g()()(&#8216;al&#8217;)<\/em> to return the string &#8220;gooal&#8221;, the code <em>g()()()(&#8216;al&#8217;)<\/em> return the string &#8220;goooal&#8221;, etc.<\/p><\/blockquote>\n<p>Interestingly enough, the table of <a href=\"https:\/\/github.com\/eatnumber1\/goal#languages\">solutions for different languages<\/a> lists my three least liked languages &#8211; Java, Ruby, and PHP &#8211; as the ones to be improbably used. These languages don&#8217;t support the necessary syntax to make it work.<\/p>\n<p>Given that I spend most of my days around PHP developers, I decided to play around and see if I can figure out a way to do it in PHP. \u00a0After all, the challenge allows breaking rules in a smart way. \u00a0What exactly is smart &#8211; that&#8217;s not up to me.<\/p>\n<p>My initial approach was to apply the technique I learned in my Perl days &#8211; using <a href=\"http:\/\/perldoc.perl.org\/perldata.html#Special-Literals\">__DATA__ or __END__<\/a> special literals &#8211; to stop the processing of the script and adding more data to it. \u00a0I&#8217;ve never tried that in PHP, but that turned out to be quite possible with the use of <a href=\"http:\/\/php.net\/manual\/en\/function.halt-compiler.php\">__halt_compiler()<\/a> function. \u00a0I&#8217;ve submitted <a href=\"https:\/\/github.com\/eatnumber1\/goal\/pull\/153\">the Pull Request<\/a>, but it still felt like cheating.<\/p>\n<p>So my second approach was completely different. \u00a0If PHP doesn&#8217;t support the necessary syntax, why not just add it such support? \u00a0A quick search for how write a lexer in PHP turned up with <a href=\"http:\/\/nitschinger.at\/Writing-a-simple-lexer-in-PHP\">this handy article<\/a>. \u00a0I pretty much did a copy-paste of it and find-and-replace for the needs of the challenge. \u00a0Hence <a href=\"https:\/\/github.com\/eatnumber1\/goal\/pull\/156\">the second Pull Request<\/a>.<\/p>\n<p>While both of the solutions aren&#8217;t exactly what the challenge was about, I think this was still useful as I&#8217;ve learned a thing or two.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>I came across the goal() challenge this morning. g()(&#8216;al&#8217;) is a challenge whereby you need to write in as many languages as possible code which enables the code g()(&#8216;al&#8217;) to return the string &#8220;goal&#8221;, the code g()()(&#8216;al&#8217;) to return the string &#8220;gooal&#8221;, the code g()()()(&#8216;al&#8217;) return the string &#8220;goooal&#8221;, etc. Interestingly enough, the table of &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2014\/07\/29\/the-goal-challenge\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The goal() challenge<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18,62],"tags":[38,1330],"keyring_services":[],"class_list":["post-22296","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","tag-php","tag-web-development"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":15407,"url":"https:\/\/mamchenkov.net\/wordpress\/2011\/08\/18\/php-regular-expression-to-match-englishlatin-characters-only\/","url_meta":{"origin":22296,"position":0},"title":"PHP regular expression to match English\/Latin characters only","author":"Leonid Mamchenkov","date":"August 18, 2011","format":false,"excerpt":"Today at work I came across a task which turned out to be much easier and simpler than I originally thought it would. \u00a0We have have a site with some user registration forms. \u00a0The site is translated into a number of languages, but due to the regulatory procedures, we have\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":14892,"url":"https:\/\/mamchenkov.net\/wordpress\/2011\/05\/17\/partial-string-replacement-with-fixed-length-in-php\/","url_meta":{"origin":22296,"position":1},"title":"Partial string replacement with fixed length in PHP","author":"Leonid Mamchenkov","date":"May 17, 2011","format":false,"excerpt":"Today I came across one of those problems that look simple and straight forward at first, but then somehow consume a good part of an hour to solve. \u00a0I wanted to replace a part of the string (in the middle), with given character, without changing the length of the string\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27105,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/12\/11\/php-static-analysis-tool-discover-bugs-in-your-code-without-running-it\/","url_meta":{"origin":22296,"position":2},"title":"PHP Static Analysis Tool &#8211; discover bugs in your code without running it!","author":"Leonid Mamchenkov","date":"December 11, 2016","format":false,"excerpt":"Ond\u0159ej Mirtes shares the idea behind the creation of\u00a0PHPStan - a static analysis tool for PHP: Compiled languages need to know about the type of every variable, return type of every method etc. before the program runs. This is why the compiler needs to make sure that the program is\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27394,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/06\/language-detection-library-for-php\/","url_meta":{"origin":22296,"position":3},"title":"Language Detection Library for PHP","author":"Leonid Mamchenkov","date":"March 6, 2017","format":false,"excerpt":"patrickschur\/language-detection - is a language detection library for PHP, which detects the language from a given text string. \u00a0Now, a bit more detailed: This library can detect the language of a given text string. It can parse given training text in many different idioms into a sequence of N-grams and\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11461,"url":"https:\/\/mamchenkov.net\/wordpress\/2008\/12\/12\/perl-vs-php-variable-scoping\/","url_meta":{"origin":22296,"position":4},"title":"Perl vs. PHP : variable scoping","author":"Leonid Mamchenkov","date":"December 12, 2008","format":false,"excerpt":"I've mentioned quite a few times that I am a big fan of Perl programming languge.\u00a0 However, most of my programming time these days is spent in PHP.\u00a0 The languages are often similar, with PHP having its roots in Perl, and Perl being such a influence in the world of\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":36247,"url":"https:\/\/mamchenkov.net\/wordpress\/2019\/02\/26\/refactoring-guru-design-patterns-php\/","url_meta":{"origin":22296,"position":5},"title":"Refactoring.Guru : Design Patterns + PHP","author":"Leonid Mamchenkov","date":"February 26, 2019","format":false,"excerpt":"Refactoring.Guru is a great resource for learning about refactoring best practices and design patterns. A lot of the website's content is also available as Dive into Design Patterns ebook. Today I came across this GitHub repository, which makes this resource even better specifically for PHP developers. Yup, that's right, the\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2019\/02\/refactoring.guru_.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2019\/02\/refactoring.guru_.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2019\/02\/refactoring.guru_.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2019\/02\/refactoring.guru_.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/22296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=22296"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/22296\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=22296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=22296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=22296"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=22296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}