[{"data":1,"prerenderedAt":513},["ShallowReactive",2],{"post:array-higher-order-funcs":3},{"id":4,"title":5,"body":6,"date":498,"description":499,"draft":500,"extension":501,"meta":502,"navigation":503,"path":504,"seo":505,"stem":506,"tags":507,"__hash__":512},"posts\u002Fposts\u002Farray-higher-order-funcs.md","Using ArraySome and ArrayEvery Higher-Order Functions in Lucee",{"type":7,"value":8,"toc":491},"minimark",[9,27,32,60,68,72,87,90,168,187,196,200,216,222,346,355,359,362,417,426,467,470,474,487],[10,11,12,13,17,18,22,23,26],"p",{},"In this post I'm going to take two ",[14,15,16],"em",{},"Lucee only"," higher-order array functions, ",[19,20,21],"code",{},"arraySome()"," and ",[19,24,25],{},"arrayEvery()",", and explain their use and purpose. I'll also review the concept of a higher-order function, as well as the Super Cool (yet very little used) lambda expression available in Lucee 5.",[28,29,31],"h2",{"id":30},"what-is-a-higher-order-function","What Is a Higher-Order Function?",[10,33,34,35,42,43,22,45,47,48,51,52,55,56,59],{},"According to ",[36,37,41],"a",{"href":38,"rel":39},"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FHigher-order_function",[40],"nofollow","Wikipedia",", higher-order functions are functions that either 1. take a function as an argument or 2. return a function as a result. These two functions we'll be looking at, ",[19,44,21],{},[19,46,25],{},", are just two of many \"Higher order functions\" defined in Coldfusion. Some other examples of higher-order functions in CF are ",[19,49,50],{},"arrayMap()",", ",[19,53,54],{},"listEach()"," or ",[19,57,58],{},"structFilter()",", all of which take a piece of data along with a function and apply the function to each data item in some way.",[10,61,62,63,22,65,67],{},"Now that we understand the concept a little, let's dig in to ",[19,64,21],{},[19,66,25],{},"!",[28,69,71],{"id":70},"arraysome","ArraySome",[10,73,74,76,77,80,81,83,84,86],{},[19,75,21],{}," executes a given function upon every element in a given array. If the function ever returns ",[19,78,79],{},"true",", then ",[19,82,21],{}," itself will return ",[19,85,79],{}," - making this function great for conditionals.",[10,88,89],{},"Here's a quick example:",[91,92,97],"pre",{"className":93,"code":94,"language":95,"meta":96,"style":96},"language-js shiki shiki-themes github-light github-dark","var checkBadWord = function(badWord) {\n    return form.comments CONTAINS badWord\n};\nvar isVulgar = arraySome(allBadWords, checkBadWord);\n","js","",[19,98,99,129,145,151],{"__ignoreMap":96},[100,101,104,108,112,115,118,122,126],"span",{"class":102,"line":103},"line",1,[100,105,107],{"class":106},"szBVR","var",[100,109,111],{"class":110},"sScJk"," checkBadWord",[100,113,114],{"class":106}," =",[100,116,117],{"class":106}," function",[100,119,121],{"class":120},"sVt8B","(",[100,123,125],{"class":124},"s4XuR","badWord",[100,127,128],{"class":120},") {\n",[100,130,132,135,138,142],{"class":102,"line":131},2,[100,133,134],{"class":106},"    return",[100,136,137],{"class":120}," form.comments ",[100,139,141],{"class":140},"sj4cs","CONTAINS",[100,143,144],{"class":120}," badWord\n",[100,146,148],{"class":102,"line":147},3,[100,149,150],{"class":120},"};\n",[100,152,154,156,159,162,165],{"class":102,"line":153},4,[100,155,107],{"class":106},[100,157,158],{"class":120}," isVulgar ",[100,160,161],{"class":106},"=",[100,163,164],{"class":110}," arraySome",[100,166,167],{"class":120},"(allBadWords, checkBadWord);\n",[10,169,170,171,174,175,178,179,182,183,186],{},"This example runs the ",[19,172,173],{},"checkBadWord"," function upon every item in the ",[19,176,177],{},"allBadWords"," array. Then the submitted ",[19,180,181],{},"comments"," field posted from a comment form gets checked for the presence of the bad word, and if any are found the ",[19,184,185],{},"isVulgar"," variable is set to true.",[10,188,189,190,195],{},"For a full reference, go check out ",[36,191,194],{"href":192,"rel":193},"https:\u002F\u002Fcfdocs.org\u002Farraysome",[40],"CFDocs.org\u002Farraysome",".",[28,197,199],{"id":198},"arrayevery","ArrayEvery",[10,201,202,203,205,206,208,209,211,212,215],{},"Much like ",[19,204,21],{},", this ",[19,207,25],{}," function is great for conditionals because it evaluates to a boolean - it iterates upon the provided array, executes the given function, and returns ",[19,210,79],{}," if ",[14,213,214],{},"all"," iterations (closure executions) returned true.",[10,217,218,219,221],{},"Here's a real-world example of how you might use ",[19,220,25],{}," to parse contact information:",[91,223,225],{"className":93,"code":224,"language":95,"meta":96,"style":96},"var contacts = [\n    { name: \"Michael\", email: \"info@test.com\" },\n    { name: \"Travis\", email: \"xyz\" },\n];\nvar isValidEmail = function( contact ) {\n    return isValid(\"email\",contact.email);\n}\nif ( arrayEvery(contacts, isValidEmail) ) {\n    \u002F\u002F SEND EMAIL\n}\n",[19,226,227,239,257,271,276,297,313,319,334,341],{"__ignoreMap":96},[100,228,229,231,234,236],{"class":102,"line":103},[100,230,107],{"class":106},[100,232,233],{"class":120}," contacts ",[100,235,161],{"class":106},[100,237,238],{"class":120}," [\n",[100,240,241,244,248,251,254],{"class":102,"line":131},[100,242,243],{"class":120},"    { name: ",[100,245,247],{"class":246},"sZZnC","\"Michael\"",[100,249,250],{"class":120},", email: ",[100,252,253],{"class":246},"\"info@test.com\"",[100,255,256],{"class":120}," },\n",[100,258,259,261,264,266,269],{"class":102,"line":147},[100,260,243],{"class":120},[100,262,263],{"class":246},"\"Travis\"",[100,265,250],{"class":120},[100,267,268],{"class":246},"\"xyz\"",[100,270,256],{"class":120},[100,272,273],{"class":102,"line":153},[100,274,275],{"class":120},"];\n",[100,277,279,281,284,286,288,291,294],{"class":102,"line":278},5,[100,280,107],{"class":106},[100,282,283],{"class":110}," isValidEmail",[100,285,114],{"class":106},[100,287,117],{"class":106},[100,289,290],{"class":120},"( ",[100,292,293],{"class":124},"contact",[100,295,296],{"class":120}," ) {\n",[100,298,300,302,305,307,310],{"class":102,"line":299},6,[100,301,134],{"class":106},[100,303,304],{"class":110}," isValid",[100,306,121],{"class":120},[100,308,309],{"class":246},"\"email\"",[100,311,312],{"class":120},",contact.email);\n",[100,314,316],{"class":102,"line":315},7,[100,317,318],{"class":120},"}\n",[100,320,322,325,328,331],{"class":102,"line":321},8,[100,323,324],{"class":106},"if",[100,326,327],{"class":120}," ( ",[100,329,330],{"class":110},"arrayEvery",[100,332,333],{"class":120},"(contacts, isValidEmail) ) {\n",[100,335,337],{"class":102,"line":336},9,[100,338,340],{"class":339},"sJ8bj","    \u002F\u002F SEND EMAIL\n",[100,342,344],{"class":102,"line":343},10,[100,345,318],{"class":120},[10,347,348,349,351,352,354],{},"This shows us how we can use ",[19,350,25],{}," to confirm that ",[14,353,214],{}," items in an array have a valid email address before we go blindly sending emails to those addresses.",[28,356,358],{"id":357},"lambda-expressions","Lambda Expressions",[10,360,361],{},"Now that you know how these work, let's take another look at that code snippet and see if we can clean it up a little.",[91,363,365],{"className":93,"code":364,"language":95,"meta":96,"style":96},"var isValidEmail = function( contact ) {\n    return isValid(\"email\",contact.email);\n}\nif ( arrayEvery(contacts, isValidEmail) ) {\n    \u002F\u002F SEND EMAIL\n}\n",[19,366,367,383,395,399,409,413],{"__ignoreMap":96},[100,368,369,371,373,375,377,379,381],{"class":102,"line":103},[100,370,107],{"class":106},[100,372,283],{"class":110},[100,374,114],{"class":106},[100,376,117],{"class":106},[100,378,290],{"class":120},[100,380,293],{"class":124},[100,382,296],{"class":120},[100,384,385,387,389,391,393],{"class":102,"line":131},[100,386,134],{"class":106},[100,388,304],{"class":110},[100,390,121],{"class":120},[100,392,309],{"class":246},[100,394,312],{"class":120},[100,396,397],{"class":102,"line":147},[100,398,318],{"class":120},[100,400,401,403,405,407],{"class":102,"line":153},[100,402,324],{"class":106},[100,404,327],{"class":120},[100,406,330],{"class":110},[100,408,333],{"class":120},[100,410,411],{"class":102,"line":278},[100,412,340],{"class":339},[100,414,415],{"class":102,"line":299},[100,416,318],{"class":120},[10,418,419,420,425],{},"As you can see, there's a decent bit of overhead with the function definition and so forth. This is the perfect case for a ",[36,421,424],{"href":422,"rel":423},"https:\u002F\u002Fdocs.lucee.org\u002Fguides\u002Flucee-5\u002Flambda.html",[40],"lambda expression",", introduced in Lucee 5. Lambda expressions offer a much shorter way to write functions, and are especially useful for functional programming. Let's see an example.",[91,427,429],{"className":93,"code":428,"language":95,"meta":96,"style":96},"if ( arrayEvery(contacts, (contact) => isValid(\"email\",contact.email) ) ) {\n    \u002F\u002F SEND EMAIL\n}\n",[19,430,431,459,463],{"__ignoreMap":96},[100,432,433,435,437,439,442,444,447,450,452,454,456],{"class":102,"line":103},[100,434,324],{"class":106},[100,436,327],{"class":120},[100,438,330],{"class":110},[100,440,441],{"class":120},"(contacts, (",[100,443,293],{"class":124},[100,445,446],{"class":120},") ",[100,448,449],{"class":106},"=>",[100,451,304],{"class":110},[100,453,121],{"class":120},[100,455,309],{"class":246},[100,457,458],{"class":120},",contact.email) ) ) {\n",[100,460,461],{"class":102,"line":131},[100,462,340],{"class":339},[100,464,465],{"class":102,"line":147},[100,466,318],{"class":120},[10,468,469],{},"There - using lambda expressions is a much cleaner, shorter way to write the closure.",[28,471,473],{"id":472},"wrap-up","Wrap Up",[10,475,476,477,22,479,481,482,67],{},"There you have it - a brief overview of higher-order functions, how to use ",[19,478,21],{},[19,480,25],{},", and even how to use lambda expressions to streamline the code examples. Any questions? Feel free to",[36,483,486],{"href":484,"rel":485},"https:\u002F\u002Fmichaelborn.me",[40]," contact me",[488,489,490],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":96,"searchDepth":131,"depth":131,"links":492},[493,494,495,496,497],{"id":30,"depth":131,"text":31},{"id":70,"depth":131,"text":71},{"id":198,"depth":131,"text":199},{"id":357,"depth":131,"text":358},{"id":472,"depth":131,"text":473},"2019-05-07T16:13:57-04:00","In this post I'm going to take two Lucee only higher-order array functions, arraySome() and arrayEvery(), and explain their use and purpose. I'll also review the concept of a higher-order function, as well as the Super Cool (yet very little used) lambda expression available in Lucee 5.",false,"md",{},true,"\u002Fposts\u002Farray-higher-order-funcs",{"title":5,"description":499},"posts\u002Farray-higher-order-funcs",[508,509,510,511],"coldfusion","cfml","lucee","lambdas","N1slGex557RJ1maxOij1HfmXLXI4YW96zDQIeJ2zXrI",1790471527931]