I'm AdamGCDevonTwitterandbefore I was a softwareengineer, I didattendschoolformusic, specificallymusiccomposition, and I wasalwaysdrawntotheanalyticalthinkingaspectofmusictheoryandperhapssurprisingnobody.
I had a toughtimefinding a jobinmusic, andso I wentintoprogramming, which I hadsomeexperiencewith.
Andyearslater, when I startedtoelearn a functionalprogramming, I foundthatthesameitchwasscratchedregardingtheanalyticalthinking.
Sowhen I startedThiotrytogetbackintomusicas a hobby, I decidedtousefunctionalproprogramming.
Sohere's myintroductiontoprogrammingmyintroductiontofunctionalprogrammingfrom a musicalperspective.
Thenonfunctionalprogrammingdefinitionmightbesomethinglike a sequenceofcommandsattractedinto a reusableunit.
But a morefunctionaldefinitionis a relationshipbetween a setofinputsandoutputs, andthisissometimesrefertoas a purefunction, anditcomesfromthemathematicaldefinitionof a function.
Andifyourbrainhastokeeptrackofwhatstate A currentvariablesinitsMaurtimethatyourbrainisworkingonthatandnotspendingontherestofyourprogram, italsomutabilitypreventsunintentionalsideeffects.
Youaccidentallychanging a variableinthefunctioncanbe a reallyhardbugtotrackdown, anditpreservesstate.
ToneJsis a frameworkthat's builtontopoftheWebaudio, a p I, andplayingouthereis a higherorderfunction.
Itaccepts a noteanditreturns a functionplaymiddle.
See, hereistheresultofthatthatfunctionbeingcalledwith C fourateventlistenerhereexcept a functionasanargument.
Soatevent, listenerandplaynotearebothhigherorderplaynotebecausethereturnsoffunctionandateventlistenerbecauseitaccepts a functionnowmanipulatingraisesanimportantpartofallparadigmsandfunctionalprogramminghas a lotofreallygreattoolsfordealingwith a raise.
Thehigherorderfunctionsbecauseeachofthesemethods, except a functionandimmuneability, becauseeachofthesemethodsthat I'm mentioningahwillreturn a newvalueratherthanmanipulatingthecurrentone.
So a raidatFilteritaccepts a functionthat's runagainsteveryelement.
Thatfunctionreceivestheelementtheindexinthearray, anditreturns a subsetoftheoriginalrain.
Sowedefine a melodyandwedidfind a functionishigh, whichtakes a noteandreturns a Boolean.
Butinadditiontotheaccumulator, I thinkintheadditiontotheelementindexandarray, italsoacceptanaccumulatorvalue, anditwillreturnthetotalaccumulatedvalueafterpassingthrougheveryelementandinsomelanguages.
Thisisknownasfoldandaccumulate, anditis a moregenerictoolthanfilterormap.
I oftenhearthatreducetakesanarrayandreturns a singlevalue, andthisistrue.
But I don't considerittobe a terriblyusefuldistinctionbecauseallfunctionsandjobscriptorturn a singlevalue s so I liketothinkofitas a genericutilityfunctionthatcanreturnanytype.
Well, therewouldbeanotherarray.
Um, infact, youcanwrite, filterormapusing a reduce, oritcanreturn a singlestringor a singleinterredherb.
So I findthebestwaytolearnreduceisreallybyah, byexample.
Sostartingwiththeinitialvalueofzerowegothroughandeachnoteslength, it's addedtothetotalzeropa's 00.1 to 5 0.25 plus 0.1 to 5 this 50.25 point +375 untilwegettothetotallengthof 1.6 to 5 seconds.
Sohere, withourmelodyway, get 1.6 to 5 seconds.
Allright, nowthatwe'redonewith a raise, partialapplicationisanincrediblyusefultoolforwritingreusableandgenericfunctions, anditissickeningtopresetorfixsomeoftheargumentsof a function.
Soherewehave a transposedfunction.
Ittakes a directioneitherupordownanintervalsuchas a majorthirdor a minorseventhandyouknowwhichwillbetransposed.
A majorthirdis a functionthatis a partiallyappliedtransposedwesetup, andwesetmajorthird, butwewaittoreceivethenote.
Sohere, whenpassingin C fourupmajorthirdwillreturnan E fouror a B flat, toowillreturn a D threeandthereis a wayinStageoneoftheTC 39.
There's a, uh, thereis a syntaxfornativepartialapplicationthatwillletyoutwopersonallyapply.
Um, partiallyapplyargumentsto a functionbyusingthequestionmarkforwhatyoudon't wanttoapplysodown.
MinorSeventhcanbe a partiallypietransposedbyleavingthenoteas a questionmark.
Soherewecanmapthesepartiallyappliedfunctionstoanarrayofnotesandhearthetranspositionorup a majorthird, ortransposeddown a minorseventh.
Sopartialapplicationis a greattoolforwritingreusablefunctionsthatarepartiallyapplied.
Buttherialbreadandbutterah, functionalprogrammingiscomposition, andcompositionis a techniquetocombinemultiplefunctionsinto a newfunction.
Soherewe'regonnatake a notenameandwewillreturnthefrequency.
But d tunedslightly.
Sowehave a functionconverting a notenameto a frequency, forexample, intheinputof a fourwouldreturn a numberoffourthpretty, whichisthefrequencyandhurtsofin a fourdetuneupwillbedefined a functiontomultiplyby a smallpercentage, soinputof 440 hurtswouldreturn 444.4 andthencomposehere, takestwofunctionsandcallsthemfromrighttoleftsogetdetained.
Frequencyis a functionthatiscreatedbycomposing D Turnupandfrequency.
Soininputof a fourwouldreturn 444.4.
Andthere's a pipelineoperatorthat's currentlyinstageoneoftheTC 39 suspectaswell.
Soapplyspectakesanobjectshapewithfunctions, andeachstringinthearraywillbepassedintoeachofthesefunctions, anditwillbemappedtoanobjectof a matchingshape.
Sowhatwillbereturnedisanobjectwith a namewithwiththevaluethattakeseachofthosestringsandpassittoparsenameforthenamepartsoctavefortheactivepartdurationforthelengthandparttimeforthetime.
I likefunctionalprogrammingbecauseitforcesyoutobreakdownproblemsintotheirsmallestpartswhenconverting a stringintomusic.
Wehadtothinkofhowtoparsethisshorthandin a waythathumanthatthecomputercouldreadit.
Andthere's a lotofpartsthere, butwedidn't write.
Verymanyfunctionsweretherewasonlyonefunction.
I wasactuallywritten.
Mostofthefunctionsweredefinedbyjustpipingtogetherverysmallfunctionslikelikerejectsmatchor a or a firstor a ahconversionfrom a stringto a numberoranad, soitforcesyoutobreakdownproblemsintotheirsmallestparts.