{"id":234,"date":"2009-03-27T00:00:23","date_gmt":"2009-03-27T07:00:23","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1065"},"modified":"2009-03-27T00:00:23","modified_gmt":"2009-03-27T07:00:23","slug":"interesting-function-composition","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=234","title":{"rendered":"Interesting Function Composition"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar multBy2Add3:Function = add(3, mult(2));\r\n\r\ntrace(multBy2Add3(10));\r\n\/\/ 10 * 2  = 20\r\n\/\/  20 + 3 = 23\r\n\r\nvar add2AndSquare = sq(add(2));\r\n\r\ntrace(add2AndSquare(8));\r\n\/\/ 8 + 2 = 10\r\n\/\/ 10 * 10 = 100\r\n\r\nvar multBy2_3times:Function = repeat(3,mult(2));\r\n\r\ntrace(multBy2_3times(3));\r\n\/\/ 3 * 2 = 6;\r\n\/\/ 6 * 2 = 12;\r\n\/\/ 12 * 2 = 24\r\n\r\n\/\/ you can also chain for even less readability\r\ntrace(sq(mult(5,add(1)))(4));\r\n\/\/ 4 + 1 = 5\r\n\/\/ 5 * 5 = 25;\r\n\/\/ 25 * 25 = 625;\r\n\r\n\/*\r\noutputs: \r\n12\r\n100\r\n24\r\n625\r\n*\/\r\n\r\n\/\/ function  composition\r\nconst F:Function = function(a:*):*{return a};\r\nfunction mult(scalar:Number, f:Function=null):Function{\r\n\tif (f == null) f = F;\r\n\t return function(n:Number){\r\n\t\treturn f(n) * scalar;\r\n\t}\r\n}\r\n\r\nfunction add(off:Number, f:Function=null):Function{\r\n\t if (f == null) f = F;\r\n\t return function(n:Number){\r\n\t\treturn f(n) + off;\r\n\t}\r\n}\r\nfunction sq(f:Function=null):Function{\r\n\t if (f == null) f = F;\r\n\t return function(n:Number){\r\n\t\t var v:Number = f(n);\r\n\t\treturn  v * v;\r\n\t}\r\n}\r\nfunction repeat(times:int, f:Function):Function {\r\n\t if (f == null) f = F;\r\n\t return function (n:Number){\r\n\t\t var v:Number = n;\r\n\t\t for (var i:int = 0; i&lt;times; i++) v = f(v);\r\n\t\t return v;\r\n\t }\r\n}\r\n<\/pre>\n<p>The above shows some interesting function composition&#8230; this demo contains the following functions:<\/p>\n<p>mult();<br \/>\nadd();<br \/>\nsq();<br \/>\nrepeat();<\/p>\n<p>These functions are designed to be combined to create new functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var multBy2Add3:Function = add(3, mult(2)); trace(multBy2Add3(10)); \/\/ 10 * 2 = 20 \/\/ 20 + 3 = 23 var add2AndSquare = sq(add(2)); trace(add2AndSquare(8)); \/\/ 8 + 2 = 10 \/\/ 10 * 10 = 100 var multBy2_3times:Function = repeat(3,mult(2)); trace(multBy2_3times(3)); \/\/ 3 * 2 = 6; \/\/ 6 * 2 = 12; \/\/ 12 * [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,121],"tags":[3,4],"class_list":["post-234","post","type-post","status-publish","format-standard","hentry","category-functions","category-math","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=234"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/234\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}