ramda.js 314 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398
  1. // Ramda v0.24.1
  2. // https://github.com/ramda/ramda
  3. // (c) 2013-2017 Scott Sauyet, Michael Hurley, and David Chambers
  4. // Ramda may be freely distributed under the MIT license.
  5. ;(function() {
  6. 'use strict';
  7. /**
  8. * A special placeholder value used to specify "gaps" within curried functions,
  9. * allowing partial application of any combination of arguments, regardless of
  10. * their positions.
  11. *
  12. * If `g` is a curried ternary function and `_` is `R.__`, the following are
  13. * equivalent:
  14. *
  15. * - `g(1, 2, 3)`
  16. * - `g(_, 2, 3)(1)`
  17. * - `g(_, _, 3)(1)(2)`
  18. * - `g(_, _, 3)(1, 2)`
  19. * - `g(_, 2, _)(1, 3)`
  20. * - `g(_, 2)(1)(3)`
  21. * - `g(_, 2)(1, 3)`
  22. * - `g(_, 2)(_, 3)(1)`
  23. *
  24. * @constant
  25. * @memberOf R
  26. * @since v0.6.0
  27. * @category Function
  28. * @example
  29. *
  30. * var greet = R.replace('{name}', R.__, 'Hello, {name}!');
  31. * greet('Alice'); //=> 'Hello, Alice!'
  32. */
  33. var __ = { '@@functional/placeholder': true };
  34. var _aperture = function _aperture(n, list) {
  35. var idx = 0;
  36. var limit = list.length - (n - 1);
  37. var acc = new Array(limit >= 0 ? limit : 0);
  38. while (idx < limit) {
  39. acc[idx] = Array.prototype.slice.call(list, idx, idx + n);
  40. idx += 1;
  41. }
  42. return acc;
  43. };
  44. /* eslint-disable no-unused-vars */
  45. var _arity = function _arity(n, fn) {
  46. /* eslint-disable no-unused-vars */
  47. switch (n) {
  48. case 0:
  49. return function () {
  50. return fn.apply(this, arguments);
  51. };
  52. case 1:
  53. return function (a0) {
  54. return fn.apply(this, arguments);
  55. };
  56. case 2:
  57. return function (a0, a1) {
  58. return fn.apply(this, arguments);
  59. };
  60. case 3:
  61. return function (a0, a1, a2) {
  62. return fn.apply(this, arguments);
  63. };
  64. case 4:
  65. return function (a0, a1, a2, a3) {
  66. return fn.apply(this, arguments);
  67. };
  68. case 5:
  69. return function (a0, a1, a2, a3, a4) {
  70. return fn.apply(this, arguments);
  71. };
  72. case 6:
  73. return function (a0, a1, a2, a3, a4, a5) {
  74. return fn.apply(this, arguments);
  75. };
  76. case 7:
  77. return function (a0, a1, a2, a3, a4, a5, a6) {
  78. return fn.apply(this, arguments);
  79. };
  80. case 8:
  81. return function (a0, a1, a2, a3, a4, a5, a6, a7) {
  82. return fn.apply(this, arguments);
  83. };
  84. case 9:
  85. return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
  86. return fn.apply(this, arguments);
  87. };
  88. case 10:
  89. return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
  90. return fn.apply(this, arguments);
  91. };
  92. default:
  93. throw new Error('First argument to _arity must be a non-negative integer no greater than ten');
  94. }
  95. };
  96. var _arrayFromIterator = function _arrayFromIterator(iter) {
  97. var list = [];
  98. var next;
  99. while (!(next = iter.next()).done) {
  100. list.push(next.value);
  101. }
  102. return list;
  103. };
  104. var _cloneRegExp = function _cloneRegExp(pattern) {
  105. return new RegExp(pattern.source, (pattern.global ? 'g' : '') + (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : '') + (pattern.sticky ? 'y' : '') + (pattern.unicode ? 'u' : ''));
  106. };
  107. var _complement = function _complement(f) {
  108. return function () {
  109. return !f.apply(this, arguments);
  110. };
  111. };
  112. /**
  113. * Private `concat` function to merge two array-like objects.
  114. *
  115. * @private
  116. * @param {Array|Arguments} [set1=[]] An array-like object.
  117. * @param {Array|Arguments} [set2=[]] An array-like object.
  118. * @return {Array} A new, merged array.
  119. * @example
  120. *
  121. * _concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
  122. */
  123. var _concat = function _concat(set1, set2) {
  124. set1 = set1 || [];
  125. set2 = set2 || [];
  126. var idx;
  127. var len1 = set1.length;
  128. var len2 = set2.length;
  129. var result = [];
  130. idx = 0;
  131. while (idx < len1) {
  132. result[result.length] = set1[idx];
  133. idx += 1;
  134. }
  135. idx = 0;
  136. while (idx < len2) {
  137. result[result.length] = set2[idx];
  138. idx += 1;
  139. }
  140. return result;
  141. };
  142. var _containsWith = function _containsWith(pred, x, list) {
  143. var idx = 0;
  144. var len = list.length;
  145. while (idx < len) {
  146. if (pred(x, list[idx])) {
  147. return true;
  148. }
  149. idx += 1;
  150. }
  151. return false;
  152. };
  153. var _dropLastWhile = function dropLastWhile(pred, list) {
  154. var idx = list.length - 1;
  155. while (idx >= 0 && pred(list[idx])) {
  156. idx -= 1;
  157. }
  158. return Array.prototype.slice.call(list, 0, idx + 1);
  159. };
  160. var _filter = function _filter(fn, list) {
  161. var idx = 0;
  162. var len = list.length;
  163. var result = [];
  164. while (idx < len) {
  165. if (fn(list[idx])) {
  166. result[result.length] = list[idx];
  167. }
  168. idx += 1;
  169. }
  170. return result;
  171. };
  172. var _forceReduced = function _forceReduced(x) {
  173. return {
  174. '@@transducer/value': x,
  175. '@@transducer/reduced': true
  176. };
  177. };
  178. // String(x => x) evaluates to "x => x", so the pattern may not match.
  179. var _functionName = function _functionName(f) {
  180. // String(x => x) evaluates to "x => x", so the pattern may not match.
  181. var match = String(f).match(/^function (\w*)/);
  182. return match == null ? '' : match[1];
  183. };
  184. var _has = function _has(prop, obj) {
  185. return Object.prototype.hasOwnProperty.call(obj, prop);
  186. };
  187. var _identity = function _identity(x) {
  188. return x;
  189. };
  190. var _isArguments = function () {
  191. var toString = Object.prototype.toString;
  192. return toString.call(arguments) === '[object Arguments]' ? function _isArguments(x) {
  193. return toString.call(x) === '[object Arguments]';
  194. } : function _isArguments(x) {
  195. return _has('callee', x);
  196. };
  197. }();
  198. /**
  199. * Tests whether or not an object is an array.
  200. *
  201. * @private
  202. * @param {*} val The object to test.
  203. * @return {Boolean} `true` if `val` is an array, `false` otherwise.
  204. * @example
  205. *
  206. * _isArray([]); //=> true
  207. * _isArray(null); //=> false
  208. * _isArray({}); //=> false
  209. */
  210. var _isArray = Array.isArray || function _isArray(val) {
  211. return val != null && val.length >= 0 && Object.prototype.toString.call(val) === '[object Array]';
  212. };
  213. var _isFunction = function _isFunction(x) {
  214. return Object.prototype.toString.call(x) === '[object Function]';
  215. };
  216. /**
  217. * Determine if the passed argument is an integer.
  218. *
  219. * @private
  220. * @param {*} n
  221. * @category Type
  222. * @return {Boolean}
  223. */
  224. var _isInteger = Number.isInteger || function _isInteger(n) {
  225. return n << 0 === n;
  226. };
  227. var _isNumber = function _isNumber(x) {
  228. return Object.prototype.toString.call(x) === '[object Number]';
  229. };
  230. var _isObject = function _isObject(x) {
  231. return Object.prototype.toString.call(x) === '[object Object]';
  232. };
  233. var _isPlaceholder = function _isPlaceholder(a) {
  234. return a != null && typeof a === 'object' && a['@@functional/placeholder'] === true;
  235. };
  236. var _isRegExp = function _isRegExp(x) {
  237. return Object.prototype.toString.call(x) === '[object RegExp]';
  238. };
  239. var _isString = function _isString(x) {
  240. return Object.prototype.toString.call(x) === '[object String]';
  241. };
  242. var _isTransformer = function _isTransformer(obj) {
  243. return typeof obj['@@transducer/step'] === 'function';
  244. };
  245. var _map = function _map(fn, functor) {
  246. var idx = 0;
  247. var len = functor.length;
  248. var result = Array(len);
  249. while (idx < len) {
  250. result[idx] = fn(functor[idx]);
  251. idx += 1;
  252. }
  253. return result;
  254. };
  255. // Based on https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
  256. var _objectAssign = function _objectAssign(target) {
  257. if (target == null) {
  258. throw new TypeError('Cannot convert undefined or null to object');
  259. }
  260. var output = Object(target);
  261. var idx = 1;
  262. var length = arguments.length;
  263. while (idx < length) {
  264. var source = arguments[idx];
  265. if (source != null) {
  266. for (var nextKey in source) {
  267. if (_has(nextKey, source)) {
  268. output[nextKey] = source[nextKey];
  269. }
  270. }
  271. }
  272. idx += 1;
  273. }
  274. return output;
  275. };
  276. var _of = function _of(x) {
  277. return [x];
  278. };
  279. var _pipe = function _pipe(f, g) {
  280. return function () {
  281. return g.call(this, f.apply(this, arguments));
  282. };
  283. };
  284. var _pipeP = function _pipeP(f, g) {
  285. return function () {
  286. var ctx = this;
  287. return f.apply(ctx, arguments).then(function (x) {
  288. return g.call(ctx, x);
  289. });
  290. };
  291. };
  292. // \b matches word boundary; [\b] matches backspace
  293. var _quote = function _quote(s) {
  294. var escaped = s.replace(/\\/g, '\\\\').replace(/[\b]/g, '\\b') // \b matches word boundary; [\b] matches backspace
  295. .replace(/\f/g, '\\f').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t').replace(/\v/g, '\\v').replace(/\0/g, '\\0');
  296. return '"' + escaped.replace(/"/g, '\\"') + '"';
  297. };
  298. var _reduced = function _reduced(x) {
  299. return x && x['@@transducer/reduced'] ? x : {
  300. '@@transducer/value': x,
  301. '@@transducer/reduced': true
  302. };
  303. };
  304. /**
  305. * Polyfill from <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString>.
  306. */
  307. var _toISOString = function () {
  308. var pad = function pad(n) {
  309. return (n < 10 ? '0' : '') + n;
  310. };
  311. return typeof Date.prototype.toISOString === 'function' ? function _toISOString(d) {
  312. return d.toISOString();
  313. } : function _toISOString(d) {
  314. return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) + 'T' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds()) + '.' + (d.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) + 'Z';
  315. };
  316. }();
  317. var _xfBase = {
  318. init: function () {
  319. return this.xf['@@transducer/init']();
  320. },
  321. result: function (result) {
  322. return this.xf['@@transducer/result'](result);
  323. }
  324. };
  325. var _xwrap = function () {
  326. function XWrap(fn) {
  327. this.f = fn;
  328. }
  329. XWrap.prototype['@@transducer/init'] = function () {
  330. throw new Error('init not implemented on XWrap');
  331. };
  332. XWrap.prototype['@@transducer/result'] = function (acc) {
  333. return acc;
  334. };
  335. XWrap.prototype['@@transducer/step'] = function (acc, x) {
  336. return this.f(acc, x);
  337. };
  338. return function _xwrap(fn) {
  339. return new XWrap(fn);
  340. };
  341. }();
  342. var _assign = typeof Object.assign === 'function' ? Object.assign : _objectAssign;
  343. /**
  344. * This checks whether a function has a [methodname] function. If it isn't an
  345. * array it will execute that function otherwise it will default to the ramda
  346. * implementation.
  347. *
  348. * @private
  349. * @param {Function} fn ramda implemtation
  350. * @param {String} methodname property to check for a custom implementation
  351. * @return {Object} Whatever the return value of the method is.
  352. */
  353. var _checkForMethod = function _checkForMethod(methodname, fn) {
  354. return function () {
  355. var length = arguments.length;
  356. if (length === 0) {
  357. return fn();
  358. }
  359. var obj = arguments[length - 1];
  360. return _isArray(obj) || typeof obj[methodname] !== 'function' ? fn.apply(this, arguments) : obj[methodname].apply(obj, Array.prototype.slice.call(arguments, 0, length - 1));
  361. };
  362. };
  363. /**
  364. * Optimized internal one-arity curry function.
  365. *
  366. * @private
  367. * @category Function
  368. * @param {Function} fn The function to curry.
  369. * @return {Function} The curried function.
  370. */
  371. var _curry1 = function _curry1(fn) {
  372. return function f1(a) {
  373. if (arguments.length === 0 || _isPlaceholder(a)) {
  374. return f1;
  375. } else {
  376. return fn.apply(this, arguments);
  377. }
  378. };
  379. };
  380. /**
  381. * Optimized internal two-arity curry function.
  382. *
  383. * @private
  384. * @category Function
  385. * @param {Function} fn The function to curry.
  386. * @return {Function} The curried function.
  387. */
  388. var _curry2 = function _curry2(fn) {
  389. return function f2(a, b) {
  390. switch (arguments.length) {
  391. case 0:
  392. return f2;
  393. case 1:
  394. return _isPlaceholder(a) ? f2 : _curry1(function (_b) {
  395. return fn(a, _b);
  396. });
  397. default:
  398. return _isPlaceholder(a) && _isPlaceholder(b) ? f2 : _isPlaceholder(a) ? _curry1(function (_a) {
  399. return fn(_a, b);
  400. }) : _isPlaceholder(b) ? _curry1(function (_b) {
  401. return fn(a, _b);
  402. }) : fn(a, b);
  403. }
  404. };
  405. };
  406. /**
  407. * Optimized internal three-arity curry function.
  408. *
  409. * @private
  410. * @category Function
  411. * @param {Function} fn The function to curry.
  412. * @return {Function} The curried function.
  413. */
  414. var _curry3 = function _curry3(fn) {
  415. return function f3(a, b, c) {
  416. switch (arguments.length) {
  417. case 0:
  418. return f3;
  419. case 1:
  420. return _isPlaceholder(a) ? f3 : _curry2(function (_b, _c) {
  421. return fn(a, _b, _c);
  422. });
  423. case 2:
  424. return _isPlaceholder(a) && _isPlaceholder(b) ? f3 : _isPlaceholder(a) ? _curry2(function (_a, _c) {
  425. return fn(_a, b, _c);
  426. }) : _isPlaceholder(b) ? _curry2(function (_b, _c) {
  427. return fn(a, _b, _c);
  428. }) : _curry1(function (_c) {
  429. return fn(a, b, _c);
  430. });
  431. default:
  432. return _isPlaceholder(a) && _isPlaceholder(b) && _isPlaceholder(c) ? f3 : _isPlaceholder(a) && _isPlaceholder(b) ? _curry2(function (_a, _b) {
  433. return fn(_a, _b, c);
  434. }) : _isPlaceholder(a) && _isPlaceholder(c) ? _curry2(function (_a, _c) {
  435. return fn(_a, b, _c);
  436. }) : _isPlaceholder(b) && _isPlaceholder(c) ? _curry2(function (_b, _c) {
  437. return fn(a, _b, _c);
  438. }) : _isPlaceholder(a) ? _curry1(function (_a) {
  439. return fn(_a, b, c);
  440. }) : _isPlaceholder(b) ? _curry1(function (_b) {
  441. return fn(a, _b, c);
  442. }) : _isPlaceholder(c) ? _curry1(function (_c) {
  443. return fn(a, b, _c);
  444. }) : fn(a, b, c);
  445. }
  446. };
  447. };
  448. /**
  449. * Internal curryN function.
  450. *
  451. * @private
  452. * @category Function
  453. * @param {Number} length The arity of the curried function.
  454. * @param {Array} received An array of arguments received thus far.
  455. * @param {Function} fn The function to curry.
  456. * @return {Function} The curried function.
  457. */
  458. var _curryN = function _curryN(length, received, fn) {
  459. return function () {
  460. var combined = [];
  461. var argsIdx = 0;
  462. var left = length;
  463. var combinedIdx = 0;
  464. while (combinedIdx < received.length || argsIdx < arguments.length) {
  465. var result;
  466. if (combinedIdx < received.length && (!_isPlaceholder(received[combinedIdx]) || argsIdx >= arguments.length)) {
  467. result = received[combinedIdx];
  468. } else {
  469. result = arguments[argsIdx];
  470. argsIdx += 1;
  471. }
  472. combined[combinedIdx] = result;
  473. if (!_isPlaceholder(result)) {
  474. left -= 1;
  475. }
  476. combinedIdx += 1;
  477. }
  478. return left <= 0 ? fn.apply(this, combined) : _arity(left, _curryN(length, combined, fn));
  479. };
  480. };
  481. /**
  482. * Returns a function that dispatches with different strategies based on the
  483. * object in list position (last argument). If it is an array, executes [fn].
  484. * Otherwise, if it has a function with one of the given method names, it will
  485. * execute that function (functor case). Otherwise, if it is a transformer,
  486. * uses transducer [xf] to return a new transformer (transducer case).
  487. * Otherwise, it will default to executing [fn].
  488. *
  489. * @private
  490. * @param {Array} methodNames properties to check for a custom implementation
  491. * @param {Function} xf transducer to initialize if object is transformer
  492. * @param {Function} fn default ramda implementation
  493. * @return {Function} A function that dispatches on object in list position
  494. */
  495. var _dispatchable = function _dispatchable(methodNames, xf, fn) {
  496. return function () {
  497. if (arguments.length === 0) {
  498. return fn();
  499. }
  500. var args = Array.prototype.slice.call(arguments, 0);
  501. var obj = args.pop();
  502. if (!_isArray(obj)) {
  503. var idx = 0;
  504. while (idx < methodNames.length) {
  505. if (typeof obj[methodNames[idx]] === 'function') {
  506. return obj[methodNames[idx]].apply(obj, args);
  507. }
  508. idx += 1;
  509. }
  510. if (_isTransformer(obj)) {
  511. var transducer = xf.apply(null, args);
  512. return transducer(obj);
  513. }
  514. }
  515. return fn.apply(this, arguments);
  516. };
  517. };
  518. /**
  519. * Tests whether or not an object is similar to an array.
  520. *
  521. * @private
  522. * @category Type
  523. * @category List
  524. * @sig * -> Boolean
  525. * @param {*} x The object to test.
  526. * @return {Boolean} `true` if `x` has a numeric length property and extreme indices defined; `false` otherwise.
  527. * @example
  528. *
  529. * _isArrayLike([]); //=> true
  530. * _isArrayLike(true); //=> false
  531. * _isArrayLike({}); //=> false
  532. * _isArrayLike({length: 10}); //=> false
  533. * _isArrayLike({0: 'zero', 9: 'nine', length: 10}); //=> true
  534. */
  535. var _isArrayLike = _curry1(function isArrayLike(x) {
  536. if (_isArray(x)) {
  537. return true;
  538. }
  539. if (!x) {
  540. return false;
  541. }
  542. if (typeof x !== 'object') {
  543. return false;
  544. }
  545. if (_isString(x)) {
  546. return false;
  547. }
  548. if (x.nodeType === 1) {
  549. return !!x.length;
  550. }
  551. if (x.length === 0) {
  552. return true;
  553. }
  554. if (x.length > 0) {
  555. return x.hasOwnProperty(0) && x.hasOwnProperty(x.length - 1);
  556. }
  557. return false;
  558. });
  559. /**
  560. * `_makeFlat` is a helper function that returns a one-level or fully recursive
  561. * function based on the flag passed in.
  562. *
  563. * @private
  564. */
  565. var _makeFlat = function _makeFlat(recursive) {
  566. return function flatt(list) {
  567. var value, jlen, j;
  568. var result = [];
  569. var idx = 0;
  570. var ilen = list.length;
  571. while (idx < ilen) {
  572. if (_isArrayLike(list[idx])) {
  573. value = recursive ? flatt(list[idx]) : list[idx];
  574. j = 0;
  575. jlen = value.length;
  576. while (j < jlen) {
  577. result[result.length] = value[j];
  578. j += 1;
  579. }
  580. } else {
  581. result[result.length] = list[idx];
  582. }
  583. idx += 1;
  584. }
  585. return result;
  586. };
  587. };
  588. var _xall = function () {
  589. function XAll(f, xf) {
  590. this.xf = xf;
  591. this.f = f;
  592. this.all = true;
  593. }
  594. XAll.prototype['@@transducer/init'] = _xfBase.init;
  595. XAll.prototype['@@transducer/result'] = function (result) {
  596. if (this.all) {
  597. result = this.xf['@@transducer/step'](result, true);
  598. }
  599. return this.xf['@@transducer/result'](result);
  600. };
  601. XAll.prototype['@@transducer/step'] = function (result, input) {
  602. if (!this.f(input)) {
  603. this.all = false;
  604. result = _reduced(this.xf['@@transducer/step'](result, false));
  605. }
  606. return result;
  607. };
  608. return _curry2(function _xall(f, xf) {
  609. return new XAll(f, xf);
  610. });
  611. }();
  612. var _xany = function () {
  613. function XAny(f, xf) {
  614. this.xf = xf;
  615. this.f = f;
  616. this.any = false;
  617. }
  618. XAny.prototype['@@transducer/init'] = _xfBase.init;
  619. XAny.prototype['@@transducer/result'] = function (result) {
  620. if (!this.any) {
  621. result = this.xf['@@transducer/step'](result, false);
  622. }
  623. return this.xf['@@transducer/result'](result);
  624. };
  625. XAny.prototype['@@transducer/step'] = function (result, input) {
  626. if (this.f(input)) {
  627. this.any = true;
  628. result = _reduced(this.xf['@@transducer/step'](result, true));
  629. }
  630. return result;
  631. };
  632. return _curry2(function _xany(f, xf) {
  633. return new XAny(f, xf);
  634. });
  635. }();
  636. var _xaperture = function () {
  637. function XAperture(n, xf) {
  638. this.xf = xf;
  639. this.pos = 0;
  640. this.full = false;
  641. this.acc = new Array(n);
  642. }
  643. XAperture.prototype['@@transducer/init'] = _xfBase.init;
  644. XAperture.prototype['@@transducer/result'] = function (result) {
  645. this.acc = null;
  646. return this.xf['@@transducer/result'](result);
  647. };
  648. XAperture.prototype['@@transducer/step'] = function (result, input) {
  649. this.store(input);
  650. return this.full ? this.xf['@@transducer/step'](result, this.getCopy()) : result;
  651. };
  652. XAperture.prototype.store = function (input) {
  653. this.acc[this.pos] = input;
  654. this.pos += 1;
  655. if (this.pos === this.acc.length) {
  656. this.pos = 0;
  657. this.full = true;
  658. }
  659. };
  660. XAperture.prototype.getCopy = function () {
  661. return _concat(Array.prototype.slice.call(this.acc, this.pos), Array.prototype.slice.call(this.acc, 0, this.pos));
  662. };
  663. return _curry2(function _xaperture(n, xf) {
  664. return new XAperture(n, xf);
  665. });
  666. }();
  667. var _xdrop = function () {
  668. function XDrop(n, xf) {
  669. this.xf = xf;
  670. this.n = n;
  671. }
  672. XDrop.prototype['@@transducer/init'] = _xfBase.init;
  673. XDrop.prototype['@@transducer/result'] = _xfBase.result;
  674. XDrop.prototype['@@transducer/step'] = function (result, input) {
  675. if (this.n > 0) {
  676. this.n -= 1;
  677. return result;
  678. }
  679. return this.xf['@@transducer/step'](result, input);
  680. };
  681. return _curry2(function _xdrop(n, xf) {
  682. return new XDrop(n, xf);
  683. });
  684. }();
  685. var _xdropLast = function () {
  686. function XDropLast(n, xf) {
  687. this.xf = xf;
  688. this.pos = 0;
  689. this.full = false;
  690. this.acc = new Array(n);
  691. }
  692. XDropLast.prototype['@@transducer/init'] = _xfBase.init;
  693. XDropLast.prototype['@@transducer/result'] = function (result) {
  694. this.acc = null;
  695. return this.xf['@@transducer/result'](result);
  696. };
  697. XDropLast.prototype['@@transducer/step'] = function (result, input) {
  698. if (this.full) {
  699. result = this.xf['@@transducer/step'](result, this.acc[this.pos]);
  700. }
  701. this.store(input);
  702. return result;
  703. };
  704. XDropLast.prototype.store = function (input) {
  705. this.acc[this.pos] = input;
  706. this.pos += 1;
  707. if (this.pos === this.acc.length) {
  708. this.pos = 0;
  709. this.full = true;
  710. }
  711. };
  712. return _curry2(function _xdropLast(n, xf) {
  713. return new XDropLast(n, xf);
  714. });
  715. }();
  716. var _xdropRepeatsWith = function () {
  717. function XDropRepeatsWith(pred, xf) {
  718. this.xf = xf;
  719. this.pred = pred;
  720. this.lastValue = undefined;
  721. this.seenFirstValue = false;
  722. }
  723. XDropRepeatsWith.prototype['@@transducer/init'] = _xfBase.init;
  724. XDropRepeatsWith.prototype['@@transducer/result'] = _xfBase.result;
  725. XDropRepeatsWith.prototype['@@transducer/step'] = function (result, input) {
  726. var sameAsLast = false;
  727. if (!this.seenFirstValue) {
  728. this.seenFirstValue = true;
  729. } else if (this.pred(this.lastValue, input)) {
  730. sameAsLast = true;
  731. }
  732. this.lastValue = input;
  733. return sameAsLast ? result : this.xf['@@transducer/step'](result, input);
  734. };
  735. return _curry2(function _xdropRepeatsWith(pred, xf) {
  736. return new XDropRepeatsWith(pred, xf);
  737. });
  738. }();
  739. var _xdropWhile = function () {
  740. function XDropWhile(f, xf) {
  741. this.xf = xf;
  742. this.f = f;
  743. }
  744. XDropWhile.prototype['@@transducer/init'] = _xfBase.init;
  745. XDropWhile.prototype['@@transducer/result'] = _xfBase.result;
  746. XDropWhile.prototype['@@transducer/step'] = function (result, input) {
  747. if (this.f) {
  748. if (this.f(input)) {
  749. return result;
  750. }
  751. this.f = null;
  752. }
  753. return this.xf['@@transducer/step'](result, input);
  754. };
  755. return _curry2(function _xdropWhile(f, xf) {
  756. return new XDropWhile(f, xf);
  757. });
  758. }();
  759. var _xfilter = function () {
  760. function XFilter(f, xf) {
  761. this.xf = xf;
  762. this.f = f;
  763. }
  764. XFilter.prototype['@@transducer/init'] = _xfBase.init;
  765. XFilter.prototype['@@transducer/result'] = _xfBase.result;
  766. XFilter.prototype['@@transducer/step'] = function (result, input) {
  767. return this.f(input) ? this.xf['@@transducer/step'](result, input) : result;
  768. };
  769. return _curry2(function _xfilter(f, xf) {
  770. return new XFilter(f, xf);
  771. });
  772. }();
  773. var _xfind = function () {
  774. function XFind(f, xf) {
  775. this.xf = xf;
  776. this.f = f;
  777. this.found = false;
  778. }
  779. XFind.prototype['@@transducer/init'] = _xfBase.init;
  780. XFind.prototype['@@transducer/result'] = function (result) {
  781. if (!this.found) {
  782. result = this.xf['@@transducer/step'](result, void 0);
  783. }
  784. return this.xf['@@transducer/result'](result);
  785. };
  786. XFind.prototype['@@transducer/step'] = function (result, input) {
  787. if (this.f(input)) {
  788. this.found = true;
  789. result = _reduced(this.xf['@@transducer/step'](result, input));
  790. }
  791. return result;
  792. };
  793. return _curry2(function _xfind(f, xf) {
  794. return new XFind(f, xf);
  795. });
  796. }();
  797. var _xfindIndex = function () {
  798. function XFindIndex(f, xf) {
  799. this.xf = xf;
  800. this.f = f;
  801. this.idx = -1;
  802. this.found = false;
  803. }
  804. XFindIndex.prototype['@@transducer/init'] = _xfBase.init;
  805. XFindIndex.prototype['@@transducer/result'] = function (result) {
  806. if (!this.found) {
  807. result = this.xf['@@transducer/step'](result, -1);
  808. }
  809. return this.xf['@@transducer/result'](result);
  810. };
  811. XFindIndex.prototype['@@transducer/step'] = function (result, input) {
  812. this.idx += 1;
  813. if (this.f(input)) {
  814. this.found = true;
  815. result = _reduced(this.xf['@@transducer/step'](result, this.idx));
  816. }
  817. return result;
  818. };
  819. return _curry2(function _xfindIndex(f, xf) {
  820. return new XFindIndex(f, xf);
  821. });
  822. }();
  823. var _xfindLast = function () {
  824. function XFindLast(f, xf) {
  825. this.xf = xf;
  826. this.f = f;
  827. }
  828. XFindLast.prototype['@@transducer/init'] = _xfBase.init;
  829. XFindLast.prototype['@@transducer/result'] = function (result) {
  830. return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.last));
  831. };
  832. XFindLast.prototype['@@transducer/step'] = function (result, input) {
  833. if (this.f(input)) {
  834. this.last = input;
  835. }
  836. return result;
  837. };
  838. return _curry2(function _xfindLast(f, xf) {
  839. return new XFindLast(f, xf);
  840. });
  841. }();
  842. var _xfindLastIndex = function () {
  843. function XFindLastIndex(f, xf) {
  844. this.xf = xf;
  845. this.f = f;
  846. this.idx = -1;
  847. this.lastIdx = -1;
  848. }
  849. XFindLastIndex.prototype['@@transducer/init'] = _xfBase.init;
  850. XFindLastIndex.prototype['@@transducer/result'] = function (result) {
  851. return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.lastIdx));
  852. };
  853. XFindLastIndex.prototype['@@transducer/step'] = function (result, input) {
  854. this.idx += 1;
  855. if (this.f(input)) {
  856. this.lastIdx = this.idx;
  857. }
  858. return result;
  859. };
  860. return _curry2(function _xfindLastIndex(f, xf) {
  861. return new XFindLastIndex(f, xf);
  862. });
  863. }();
  864. var _xmap = function () {
  865. function XMap(f, xf) {
  866. this.xf = xf;
  867. this.f = f;
  868. }
  869. XMap.prototype['@@transducer/init'] = _xfBase.init;
  870. XMap.prototype['@@transducer/result'] = _xfBase.result;
  871. XMap.prototype['@@transducer/step'] = function (result, input) {
  872. return this.xf['@@transducer/step'](result, this.f(input));
  873. };
  874. return _curry2(function _xmap(f, xf) {
  875. return new XMap(f, xf);
  876. });
  877. }();
  878. var _xreduceBy = function () {
  879. function XReduceBy(valueFn, valueAcc, keyFn, xf) {
  880. this.valueFn = valueFn;
  881. this.valueAcc = valueAcc;
  882. this.keyFn = keyFn;
  883. this.xf = xf;
  884. this.inputs = {};
  885. }
  886. XReduceBy.prototype['@@transducer/init'] = _xfBase.init;
  887. XReduceBy.prototype['@@transducer/result'] = function (result) {
  888. var key;
  889. for (key in this.inputs) {
  890. if (_has(key, this.inputs)) {
  891. result = this.xf['@@transducer/step'](result, this.inputs[key]);
  892. if (result['@@transducer/reduced']) {
  893. result = result['@@transducer/value'];
  894. break;
  895. }
  896. }
  897. }
  898. this.inputs = null;
  899. return this.xf['@@transducer/result'](result);
  900. };
  901. XReduceBy.prototype['@@transducer/step'] = function (result, input) {
  902. var key = this.keyFn(input);
  903. this.inputs[key] = this.inputs[key] || [
  904. key,
  905. this.valueAcc
  906. ];
  907. this.inputs[key][1] = this.valueFn(this.inputs[key][1], input);
  908. return result;
  909. };
  910. return _curryN(4, [], function _xreduceBy(valueFn, valueAcc, keyFn, xf) {
  911. return new XReduceBy(valueFn, valueAcc, keyFn, xf);
  912. });
  913. }();
  914. var _xtake = function () {
  915. function XTake(n, xf) {
  916. this.xf = xf;
  917. this.n = n;
  918. this.i = 0;
  919. }
  920. XTake.prototype['@@transducer/init'] = _xfBase.init;
  921. XTake.prototype['@@transducer/result'] = _xfBase.result;
  922. XTake.prototype['@@transducer/step'] = function (result, input) {
  923. this.i += 1;
  924. var ret = this.n === 0 ? result : this.xf['@@transducer/step'](result, input);
  925. return this.n >= 0 && this.i >= this.n ? _reduced(ret) : ret;
  926. };
  927. return _curry2(function _xtake(n, xf) {
  928. return new XTake(n, xf);
  929. });
  930. }();
  931. var _xtakeWhile = function () {
  932. function XTakeWhile(f, xf) {
  933. this.xf = xf;
  934. this.f = f;
  935. }
  936. XTakeWhile.prototype['@@transducer/init'] = _xfBase.init;
  937. XTakeWhile.prototype['@@transducer/result'] = _xfBase.result;
  938. XTakeWhile.prototype['@@transducer/step'] = function (result, input) {
  939. return this.f(input) ? this.xf['@@transducer/step'](result, input) : _reduced(result);
  940. };
  941. return _curry2(function _xtakeWhile(f, xf) {
  942. return new XTakeWhile(f, xf);
  943. });
  944. }();
  945. /**
  946. * Adds two values.
  947. *
  948. * @func
  949. * @memberOf R
  950. * @since v0.1.0
  951. * @category Math
  952. * @sig Number -> Number -> Number
  953. * @param {Number} a
  954. * @param {Number} b
  955. * @return {Number}
  956. * @see R.subtract
  957. * @example
  958. *
  959. * R.add(2, 3); //=> 5
  960. * R.add(7)(10); //=> 17
  961. */
  962. var add = _curry2(function add(a, b) {
  963. return Number(a) + Number(b);
  964. });
  965. /**
  966. * Applies a function to the value at the given index of an array, returning a
  967. * new copy of the array with the element at the given index replaced with the
  968. * result of the function application.
  969. *
  970. * @func
  971. * @memberOf R
  972. * @since v0.14.0
  973. * @category List
  974. * @sig (a -> a) -> Number -> [a] -> [a]
  975. * @param {Function} fn The function to apply.
  976. * @param {Number} idx The index.
  977. * @param {Array|Arguments} list An array-like object whose value
  978. * at the supplied index will be replaced.
  979. * @return {Array} A copy of the supplied array-like object with
  980. * the element at index `idx` replaced with the value
  981. * returned by applying `fn` to the existing element.
  982. * @see R.update
  983. * @example
  984. *
  985. * R.adjust(R.add(10), 1, [1, 2, 3]); //=> [1, 12, 3]
  986. * R.adjust(R.add(10))(1)([1, 2, 3]); //=> [1, 12, 3]
  987. * @symb R.adjust(f, -1, [a, b]) = [a, f(b)]
  988. * @symb R.adjust(f, 0, [a, b]) = [f(a), b]
  989. */
  990. var adjust = _curry3(function adjust(fn, idx, list) {
  991. if (idx >= list.length || idx < -list.length) {
  992. return list;
  993. }
  994. var start = idx < 0 ? list.length : 0;
  995. var _idx = start + idx;
  996. var _list = _concat(list);
  997. _list[_idx] = fn(list[_idx]);
  998. return _list;
  999. });
  1000. /**
  1001. * Returns `true` if all elements of the list match the predicate, `false` if
  1002. * there are any that don't.
  1003. *
  1004. * Dispatches to the `all` method of the second argument, if present.
  1005. *
  1006. * Acts as a transducer if a transformer is given in list position.
  1007. *
  1008. * @func
  1009. * @memberOf R
  1010. * @since v0.1.0
  1011. * @category List
  1012. * @sig (a -> Boolean) -> [a] -> Boolean
  1013. * @param {Function} fn The predicate function.
  1014. * @param {Array} list The array to consider.
  1015. * @return {Boolean} `true` if the predicate is satisfied by every element, `false`
  1016. * otherwise.
  1017. * @see R.any, R.none, R.transduce
  1018. * @example
  1019. *
  1020. * var equals3 = R.equals(3);
  1021. * R.all(equals3)([3, 3, 3, 3]); //=> true
  1022. * R.all(equals3)([3, 3, 1, 3]); //=> false
  1023. */
  1024. var all = _curry2(_dispatchable(['all'], _xall, function all(fn, list) {
  1025. var idx = 0;
  1026. while (idx < list.length) {
  1027. if (!fn(list[idx])) {
  1028. return false;
  1029. }
  1030. idx += 1;
  1031. }
  1032. return true;
  1033. }));
  1034. /**
  1035. * Returns a function that always returns the given value. Note that for
  1036. * non-primitives the value returned is a reference to the original value.
  1037. *
  1038. * This function is known as `const`, `constant`, or `K` (for K combinator) in
  1039. * other languages and libraries.
  1040. *
  1041. * @func
  1042. * @memberOf R
  1043. * @since v0.1.0
  1044. * @category Function
  1045. * @sig a -> (* -> a)
  1046. * @param {*} val The value to wrap in a function
  1047. * @return {Function} A Function :: * -> val.
  1048. * @example
  1049. *
  1050. * var t = R.always('Tee');
  1051. * t(); //=> 'Tee'
  1052. */
  1053. var always = _curry1(function always(val) {
  1054. return function () {
  1055. return val;
  1056. };
  1057. });
  1058. /**
  1059. * Returns `true` if both arguments are `true`; `false` otherwise.
  1060. *
  1061. * @func
  1062. * @memberOf R
  1063. * @since v0.1.0
  1064. * @category Logic
  1065. * @sig a -> b -> a | b
  1066. * @param {Any} a
  1067. * @param {Any} b
  1068. * @return {Any} the first argument if it is falsy, otherwise the second argument.
  1069. * @see R.both
  1070. * @example
  1071. *
  1072. * R.and(true, true); //=> true
  1073. * R.and(true, false); //=> false
  1074. * R.and(false, true); //=> false
  1075. * R.and(false, false); //=> false
  1076. */
  1077. var and = _curry2(function and(a, b) {
  1078. return a && b;
  1079. });
  1080. /**
  1081. * Returns `true` if at least one of elements of the list match the predicate,
  1082. * `false` otherwise.
  1083. *
  1084. * Dispatches to the `any` method of the second argument, if present.
  1085. *
  1086. * Acts as a transducer if a transformer is given in list position.
  1087. *
  1088. * @func
  1089. * @memberOf R
  1090. * @since v0.1.0
  1091. * @category List
  1092. * @sig (a -> Boolean) -> [a] -> Boolean
  1093. * @param {Function} fn The predicate function.
  1094. * @param {Array} list The array to consider.
  1095. * @return {Boolean} `true` if the predicate is satisfied by at least one element, `false`
  1096. * otherwise.
  1097. * @see R.all, R.none, R.transduce
  1098. * @example
  1099. *
  1100. * var lessThan0 = R.flip(R.lt)(0);
  1101. * var lessThan2 = R.flip(R.lt)(2);
  1102. * R.any(lessThan0)([1, 2]); //=> false
  1103. * R.any(lessThan2)([1, 2]); //=> true
  1104. */
  1105. var any = _curry2(_dispatchable(['any'], _xany, function any(fn, list) {
  1106. var idx = 0;
  1107. while (idx < list.length) {
  1108. if (fn(list[idx])) {
  1109. return true;
  1110. }
  1111. idx += 1;
  1112. }
  1113. return false;
  1114. }));
  1115. /**
  1116. * Returns a new list, composed of n-tuples of consecutive elements. If `n` is
  1117. * greater than the length of the list, an empty list is returned.
  1118. *
  1119. * Acts as a transducer if a transformer is given in list position.
  1120. *
  1121. * @func
  1122. * @memberOf R
  1123. * @since v0.12.0
  1124. * @category List
  1125. * @sig Number -> [a] -> [[a]]
  1126. * @param {Number} n The size of the tuples to create
  1127. * @param {Array} list The list to split into `n`-length tuples
  1128. * @return {Array} The resulting list of `n`-length tuples
  1129. * @see R.transduce
  1130. * @example
  1131. *
  1132. * R.aperture(2, [1, 2, 3, 4, 5]); //=> [[1, 2], [2, 3], [3, 4], [4, 5]]
  1133. * R.aperture(3, [1, 2, 3, 4, 5]); //=> [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
  1134. * R.aperture(7, [1, 2, 3, 4, 5]); //=> []
  1135. */
  1136. var aperture = _curry2(_dispatchable([], _xaperture, _aperture));
  1137. /**
  1138. * Returns a new list containing the contents of the given list, followed by
  1139. * the given element.
  1140. *
  1141. * @func
  1142. * @memberOf R
  1143. * @since v0.1.0
  1144. * @category List
  1145. * @sig a -> [a] -> [a]
  1146. * @param {*} el The element to add to the end of the new list.
  1147. * @param {Array} list The list of elements to add a new item to.
  1148. * list.
  1149. * @return {Array} A new list containing the elements of the old list followed by `el`.
  1150. * @see R.prepend
  1151. * @example
  1152. *
  1153. * R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
  1154. * R.append('tests', []); //=> ['tests']
  1155. * R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]
  1156. */
  1157. var append = _curry2(function append(el, list) {
  1158. return _concat(list, [el]);
  1159. });
  1160. /**
  1161. * Applies function `fn` to the argument list `args`. This is useful for
  1162. * creating a fixed-arity function from a variadic function. `fn` should be a
  1163. * bound function if context is significant.
  1164. *
  1165. * @func
  1166. * @memberOf R
  1167. * @since v0.7.0
  1168. * @category Function
  1169. * @sig (*... -> a) -> [*] -> a
  1170. * @param {Function} fn The function which will be called with `args`
  1171. * @param {Array} args The arguments to call `fn` with
  1172. * @return {*} result The result, equivalent to `fn(...args)`
  1173. * @see R.call, R.unapply
  1174. * @example
  1175. *
  1176. * var nums = [1, 2, 3, -99, 42, 6, 7];
  1177. * R.apply(Math.max, nums); //=> 42
  1178. * @symb R.apply(f, [a, b, c]) = f(a, b, c)
  1179. */
  1180. var apply = _curry2(function apply(fn, args) {
  1181. return fn.apply(this, args);
  1182. });
  1183. /**
  1184. * Makes an ascending comparator function out of a function that returns a value
  1185. * that can be compared with `<` and `>`.
  1186. *
  1187. * @func
  1188. * @memberOf R
  1189. * @since v0.23.0
  1190. * @category Function
  1191. * @sig Ord b => (a -> b) -> a -> a -> Number
  1192. * @param {Function} fn A function of arity one that returns a value that can be compared
  1193. * @param {*} a The first item to be compared.
  1194. * @param {*} b The second item to be compared.
  1195. * @return {Number} `-1` if fn(a) < fn(b), `1` if fn(b) < fn(a), otherwise `0`
  1196. * @see R.descend
  1197. * @example
  1198. *
  1199. * var byAge = R.ascend(R.prop('age'));
  1200. * var people = [
  1201. * // ...
  1202. * ];
  1203. * var peopleByYoungestFirst = R.sort(byAge, people);
  1204. */
  1205. var ascend = _curry3(function ascend(fn, a, b) {
  1206. var aa = fn(a);
  1207. var bb = fn(b);
  1208. return aa < bb ? -1 : aa > bb ? 1 : 0;
  1209. });
  1210. /**
  1211. * Makes a shallow clone of an object, setting or overriding the specified
  1212. * property with the given value. Note that this copies and flattens prototype
  1213. * properties onto the new object as well. All non-primitive properties are
  1214. * copied by reference.
  1215. *
  1216. * @func
  1217. * @memberOf R
  1218. * @since v0.8.0
  1219. * @category Object
  1220. * @sig String -> a -> {k: v} -> {k: v}
  1221. * @param {String} prop The property name to set
  1222. * @param {*} val The new value
  1223. * @param {Object} obj The object to clone
  1224. * @return {Object} A new object equivalent to the original except for the changed property.
  1225. * @see R.dissoc
  1226. * @example
  1227. *
  1228. * R.assoc('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3}
  1229. */
  1230. var assoc = _curry3(function assoc(prop, val, obj) {
  1231. var result = {};
  1232. for (var p in obj) {
  1233. result[p] = obj[p];
  1234. }
  1235. result[prop] = val;
  1236. return result;
  1237. });
  1238. /**
  1239. * Creates a function that is bound to a context.
  1240. * Note: `R.bind` does not provide the additional argument-binding capabilities of
  1241. * [Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).
  1242. *
  1243. * @func
  1244. * @memberOf R
  1245. * @since v0.6.0
  1246. * @category Function
  1247. * @category Object
  1248. * @sig (* -> *) -> {*} -> (* -> *)
  1249. * @param {Function} fn The function to bind to context
  1250. * @param {Object} thisObj The context to bind `fn` to
  1251. * @return {Function} A function that will execute in the context of `thisObj`.
  1252. * @see R.partial
  1253. * @example
  1254. *
  1255. * var log = R.bind(console.log, console);
  1256. * R.pipe(R.assoc('a', 2), R.tap(log), R.assoc('a', 3))({a: 1}); //=> {a: 3}
  1257. * // logs {a: 2}
  1258. * @symb R.bind(f, o)(a, b) = f.call(o, a, b)
  1259. */
  1260. var bind = _curry2(function bind(fn, thisObj) {
  1261. return _arity(fn.length, function () {
  1262. return fn.apply(thisObj, arguments);
  1263. });
  1264. });
  1265. /**
  1266. * Restricts a number to be within a range.
  1267. *
  1268. * Also works for other ordered types such as Strings and Dates.
  1269. *
  1270. * @func
  1271. * @memberOf R
  1272. * @since v0.20.0
  1273. * @category Relation
  1274. * @sig Ord a => a -> a -> a -> a
  1275. * @param {Number} minimum The lower limit of the clamp (inclusive)
  1276. * @param {Number} maximum The upper limit of the clamp (inclusive)
  1277. * @param {Number} value Value to be clamped
  1278. * @return {Number} Returns `minimum` when `val < minimum`, `maximum` when `val > maximum`, returns `val` otherwise
  1279. * @example
  1280. *
  1281. * R.clamp(1, 10, -5) // => 1
  1282. * R.clamp(1, 10, 15) // => 10
  1283. * R.clamp(1, 10, 4) // => 4
  1284. */
  1285. var clamp = _curry3(function clamp(min, max, value) {
  1286. if (min > max) {
  1287. throw new Error('min must not be greater than max in clamp(min, max, value)');
  1288. }
  1289. return value < min ? min : value > max ? max : value;
  1290. });
  1291. /**
  1292. * Makes a comparator function out of a function that reports whether the first
  1293. * element is less than the second.
  1294. *
  1295. * @func
  1296. * @memberOf R
  1297. * @since v0.1.0
  1298. * @category Function
  1299. * @sig (a, b -> Boolean) -> (a, b -> Number)
  1300. * @param {Function} pred A predicate function of arity two which will return `true` if the first argument
  1301. * is less than the second, `false` otherwise
  1302. * @return {Function} A Function :: a -> b -> Int that returns `-1` if a < b, `1` if b < a, otherwise `0`
  1303. * @example
  1304. *
  1305. * var byAge = R.comparator((a, b) => a.age < b.age);
  1306. * var people = [
  1307. * // ...
  1308. * ];
  1309. * var peopleByIncreasingAge = R.sort(byAge, people);
  1310. */
  1311. var comparator = _curry1(function comparator(pred) {
  1312. return function (a, b) {
  1313. return pred(a, b) ? -1 : pred(b, a) ? 1 : 0;
  1314. };
  1315. });
  1316. /**
  1317. * Returns a curried equivalent of the provided function, with the specified
  1318. * arity. The curried function has two unusual capabilities. First, its
  1319. * arguments needn't be provided one at a time. If `g` is `R.curryN(3, f)`, the
  1320. * following are equivalent:
  1321. *
  1322. * - `g(1)(2)(3)`
  1323. * - `g(1)(2, 3)`
  1324. * - `g(1, 2)(3)`
  1325. * - `g(1, 2, 3)`
  1326. *
  1327. * Secondly, the special placeholder value [`R.__`](#__) may be used to specify
  1328. * "gaps", allowing partial application of any combination of arguments,
  1329. * regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
  1330. * the following are equivalent:
  1331. *
  1332. * - `g(1, 2, 3)`
  1333. * - `g(_, 2, 3)(1)`
  1334. * - `g(_, _, 3)(1)(2)`
  1335. * - `g(_, _, 3)(1, 2)`
  1336. * - `g(_, 2)(1)(3)`
  1337. * - `g(_, 2)(1, 3)`
  1338. * - `g(_, 2)(_, 3)(1)`
  1339. *
  1340. * @func
  1341. * @memberOf R
  1342. * @since v0.5.0
  1343. * @category Function
  1344. * @sig Number -> (* -> a) -> (* -> a)
  1345. * @param {Number} length The arity for the returned function.
  1346. * @param {Function} fn The function to curry.
  1347. * @return {Function} A new, curried function.
  1348. * @see R.curry
  1349. * @example
  1350. *
  1351. * var sumArgs = (...args) => R.sum(args);
  1352. *
  1353. * var curriedAddFourNumbers = R.curryN(4, sumArgs);
  1354. * var f = curriedAddFourNumbers(1, 2);
  1355. * var g = f(3);
  1356. * g(4); //=> 10
  1357. */
  1358. var curryN = _curry2(function curryN(length, fn) {
  1359. if (length === 1) {
  1360. return _curry1(fn);
  1361. }
  1362. return _arity(length, _curryN(length, [], fn));
  1363. });
  1364. /**
  1365. * Decrements its argument.
  1366. *
  1367. * @func
  1368. * @memberOf R
  1369. * @since v0.9.0
  1370. * @category Math
  1371. * @sig Number -> Number
  1372. * @param {Number} n
  1373. * @return {Number} n - 1
  1374. * @see R.inc
  1375. * @example
  1376. *
  1377. * R.dec(42); //=> 41
  1378. */
  1379. var dec = add(-1);
  1380. /**
  1381. * Returns the second argument if it is not `null`, `undefined` or `NaN`;
  1382. * otherwise the first argument is returned.
  1383. *
  1384. * @func
  1385. * @memberOf R
  1386. * @since v0.10.0
  1387. * @category Logic
  1388. * @sig a -> b -> a | b
  1389. * @param {a} default The default value.
  1390. * @param {b} val `val` will be returned instead of `default` unless `val` is `null`, `undefined` or `NaN`.
  1391. * @return {*} The second value if it is not `null`, `undefined` or `NaN`, otherwise the default value
  1392. * @example
  1393. *
  1394. * var defaultTo42 = R.defaultTo(42);
  1395. *
  1396. * defaultTo42(null); //=> 42
  1397. * defaultTo42(undefined); //=> 42
  1398. * defaultTo42('Ramda'); //=> 'Ramda'
  1399. * // parseInt('string') results in NaN
  1400. * defaultTo42(parseInt('string')); //=> 42
  1401. */
  1402. var defaultTo = _curry2(function defaultTo(d, v) {
  1403. return v == null || v !== v ? d : v;
  1404. });
  1405. /**
  1406. * Makes a descending comparator function out of a function that returns a value
  1407. * that can be compared with `<` and `>`.
  1408. *
  1409. * @func
  1410. * @memberOf R
  1411. * @since v0.23.0
  1412. * @category Function
  1413. * @sig Ord b => (a -> b) -> a -> a -> Number
  1414. * @param {Function} fn A function of arity one that returns a value that can be compared
  1415. * @param {*} a The first item to be compared.
  1416. * @param {*} b The second item to be compared.
  1417. * @return {Number} `-1` if fn(a) > fn(b), `1` if fn(b) > fn(a), otherwise `0`
  1418. * @see R.ascend
  1419. * @example
  1420. *
  1421. * var byAge = R.descend(R.prop('age'));
  1422. * var people = [
  1423. * // ...
  1424. * ];
  1425. * var peopleByOldestFirst = R.sort(byAge, people);
  1426. */
  1427. var descend = _curry3(function descend(fn, a, b) {
  1428. var aa = fn(a);
  1429. var bb = fn(b);
  1430. return aa > bb ? -1 : aa < bb ? 1 : 0;
  1431. });
  1432. /**
  1433. * Finds the set (i.e. no duplicates) of all elements in the first list not
  1434. * contained in the second list. Duplication is determined according to the
  1435. * value returned by applying the supplied predicate to two list elements.
  1436. *
  1437. * @func
  1438. * @memberOf R
  1439. * @since v0.1.0
  1440. * @category Relation
  1441. * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
  1442. * @param {Function} pred A predicate used to test whether two items are equal.
  1443. * @param {Array} list1 The first list.
  1444. * @param {Array} list2 The second list.
  1445. * @return {Array} The elements in `list1` that are not in `list2`.
  1446. * @see R.difference, R.symmetricDifference, R.symmetricDifferenceWith
  1447. * @example
  1448. *
  1449. * var cmp = (x, y) => x.a === y.a;
  1450. * var l1 = [{a: 1}, {a: 2}, {a: 3}];
  1451. * var l2 = [{a: 3}, {a: 4}];
  1452. * R.differenceWith(cmp, l1, l2); //=> [{a: 1}, {a: 2}]
  1453. */
  1454. var differenceWith = _curry3(function differenceWith(pred, first, second) {
  1455. var out = [];
  1456. var idx = 0;
  1457. var firstLen = first.length;
  1458. while (idx < firstLen) {
  1459. if (!_containsWith(pred, first[idx], second) && !_containsWith(pred, first[idx], out)) {
  1460. out.push(first[idx]);
  1461. }
  1462. idx += 1;
  1463. }
  1464. return out;
  1465. });
  1466. /**
  1467. * Returns a new object that does not contain a `prop` property.
  1468. *
  1469. * @func
  1470. * @memberOf R
  1471. * @since v0.10.0
  1472. * @category Object
  1473. * @sig String -> {k: v} -> {k: v}
  1474. * @param {String} prop The name of the property to dissociate
  1475. * @param {Object} obj The object to clone
  1476. * @return {Object} A new object equivalent to the original but without the specified property
  1477. * @see R.assoc
  1478. * @example
  1479. *
  1480. * R.dissoc('b', {a: 1, b: 2, c: 3}); //=> {a: 1, c: 3}
  1481. */
  1482. var dissoc = _curry2(function dissoc(prop, obj) {
  1483. var result = {};
  1484. for (var p in obj) {
  1485. result[p] = obj[p];
  1486. }
  1487. delete result[prop];
  1488. return result;
  1489. });
  1490. /**
  1491. * Divides two numbers. Equivalent to `a / b`.
  1492. *
  1493. * @func
  1494. * @memberOf R
  1495. * @since v0.1.0
  1496. * @category Math
  1497. * @sig Number -> Number -> Number
  1498. * @param {Number} a The first value.
  1499. * @param {Number} b The second value.
  1500. * @return {Number} The result of `a / b`.
  1501. * @see R.multiply
  1502. * @example
  1503. *
  1504. * R.divide(71, 100); //=> 0.71
  1505. *
  1506. * var half = R.divide(R.__, 2);
  1507. * half(42); //=> 21
  1508. *
  1509. * var reciprocal = R.divide(1);
  1510. * reciprocal(4); //=> 0.25
  1511. */
  1512. var divide = _curry2(function divide(a, b) {
  1513. return a / b;
  1514. });
  1515. /**
  1516. * Returns a new list excluding the leading elements of a given list which
  1517. * satisfy the supplied predicate function. It passes each value to the supplied
  1518. * predicate function, skipping elements while the predicate function returns
  1519. * `true`. The predicate function is applied to one argument: *(value)*.
  1520. *
  1521. * Dispatches to the `dropWhile` method of the second argument, if present.
  1522. *
  1523. * Acts as a transducer if a transformer is given in list position.
  1524. *
  1525. * @func
  1526. * @memberOf R
  1527. * @since v0.9.0
  1528. * @category List
  1529. * @sig (a -> Boolean) -> [a] -> [a]
  1530. * @param {Function} fn The function called per iteration.
  1531. * @param {Array} list The collection to iterate over.
  1532. * @return {Array} A new array.
  1533. * @see R.takeWhile, R.transduce, R.addIndex
  1534. * @example
  1535. *
  1536. * var lteTwo = x => x <= 2;
  1537. *
  1538. * R.dropWhile(lteTwo, [1, 2, 3, 4, 3, 2, 1]); //=> [3, 4, 3, 2, 1]
  1539. */
  1540. var dropWhile = _curry2(_dispatchable(['dropWhile'], _xdropWhile, function dropWhile(pred, list) {
  1541. var idx = 0;
  1542. var len = list.length;
  1543. while (idx < len && pred(list[idx])) {
  1544. idx += 1;
  1545. }
  1546. return Array.prototype.slice.call(list, idx);
  1547. }));
  1548. /**
  1549. * Returns the empty value of its argument's type. Ramda defines the empty
  1550. * value of Array (`[]`), Object (`{}`), String (`''`), and Arguments. Other
  1551. * types are supported if they define `<Type>.empty` and/or
  1552. * `<Type>.prototype.empty`.
  1553. *
  1554. * Dispatches to the `empty` method of the first argument, if present.
  1555. *
  1556. * @func
  1557. * @memberOf R
  1558. * @since v0.3.0
  1559. * @category Function
  1560. * @sig a -> a
  1561. * @param {*} x
  1562. * @return {*}
  1563. * @example
  1564. *
  1565. * R.empty(Just(42)); //=> Nothing()
  1566. * R.empty([1, 2, 3]); //=> []
  1567. * R.empty('unicorns'); //=> ''
  1568. * R.empty({x: 1, y: 2}); //=> {}
  1569. */
  1570. // else
  1571. var empty = _curry1(function empty(x) {
  1572. return x != null && typeof x['fantasy-land/empty'] === 'function' ? x['fantasy-land/empty']() : x != null && x.constructor != null && typeof x.constructor['fantasy-land/empty'] === 'function' ? x.constructor['fantasy-land/empty']() : x != null && typeof x.empty === 'function' ? x.empty() : x != null && x.constructor != null && typeof x.constructor.empty === 'function' ? x.constructor.empty() : _isArray(x) ? [] : _isString(x) ? '' : _isObject(x) ? {} : _isArguments(x) ? function () {
  1573. return arguments;
  1574. }() : // else
  1575. void 0;
  1576. });
  1577. /**
  1578. * Creates a new object by recursively evolving a shallow copy of `object`,
  1579. * according to the `transformation` functions. All non-primitive properties
  1580. * are copied by reference.
  1581. *
  1582. * A `transformation` function will not be invoked if its corresponding key
  1583. * does not exist in the evolved object.
  1584. *
  1585. * @func
  1586. * @memberOf R
  1587. * @since v0.9.0
  1588. * @category Object
  1589. * @sig {k: (v -> v)} -> {k: v} -> {k: v}
  1590. * @param {Object} transformations The object specifying transformation functions to apply
  1591. * to the object.
  1592. * @param {Object} object The object to be transformed.
  1593. * @return {Object} The transformed object.
  1594. * @example
  1595. *
  1596. * var tomato = {firstName: ' Tomato ', data: {elapsed: 100, remaining: 1400}, id:123};
  1597. * var transformations = {
  1598. * firstName: R.trim,
  1599. * lastName: R.trim, // Will not get invoked.
  1600. * data: {elapsed: R.add(1), remaining: R.add(-1)}
  1601. * };
  1602. * R.evolve(transformations, tomato); //=> {firstName: 'Tomato', data: {elapsed: 101, remaining: 1399}, id:123}
  1603. */
  1604. var evolve = _curry2(function evolve(transformations, object) {
  1605. var result = {};
  1606. var transformation, key, type;
  1607. for (key in object) {
  1608. transformation = transformations[key];
  1609. type = typeof transformation;
  1610. result[key] = type === 'function' ? transformation(object[key]) : transformation && type === 'object' ? evolve(transformation, object[key]) : object[key];
  1611. }
  1612. return result;
  1613. });
  1614. /**
  1615. * Returns the first element of the list which matches the predicate, or
  1616. * `undefined` if no element matches.
  1617. *
  1618. * Dispatches to the `find` method of the second argument, if present.
  1619. *
  1620. * Acts as a transducer if a transformer is given in list position.
  1621. *
  1622. * @func
  1623. * @memberOf R
  1624. * @since v0.1.0
  1625. * @category List
  1626. * @sig (a -> Boolean) -> [a] -> a | undefined
  1627. * @param {Function} fn The predicate function used to determine if the element is the
  1628. * desired one.
  1629. * @param {Array} list The array to consider.
  1630. * @return {Object} The element found, or `undefined`.
  1631. * @see R.transduce
  1632. * @example
  1633. *
  1634. * var xs = [{a: 1}, {a: 2}, {a: 3}];
  1635. * R.find(R.propEq('a', 2))(xs); //=> {a: 2}
  1636. * R.find(R.propEq('a', 4))(xs); //=> undefined
  1637. */
  1638. var find = _curry2(_dispatchable(['find'], _xfind, function find(fn, list) {
  1639. var idx = 0;
  1640. var len = list.length;
  1641. while (idx < len) {
  1642. if (fn(list[idx])) {
  1643. return list[idx];
  1644. }
  1645. idx += 1;
  1646. }
  1647. }));
  1648. /**
  1649. * Returns the index of the first element of the list which matches the
  1650. * predicate, or `-1` if no element matches.
  1651. *
  1652. * Acts as a transducer if a transformer is given in list position.
  1653. *
  1654. * @func
  1655. * @memberOf R
  1656. * @since v0.1.1
  1657. * @category List
  1658. * @sig (a -> Boolean) -> [a] -> Number
  1659. * @param {Function} fn The predicate function used to determine if the element is the
  1660. * desired one.
  1661. * @param {Array} list The array to consider.
  1662. * @return {Number} The index of the element found, or `-1`.
  1663. * @see R.transduce
  1664. * @example
  1665. *
  1666. * var xs = [{a: 1}, {a: 2}, {a: 3}];
  1667. * R.findIndex(R.propEq('a', 2))(xs); //=> 1
  1668. * R.findIndex(R.propEq('a', 4))(xs); //=> -1
  1669. */
  1670. var findIndex = _curry2(_dispatchable([], _xfindIndex, function findIndex(fn, list) {
  1671. var idx = 0;
  1672. var len = list.length;
  1673. while (idx < len) {
  1674. if (fn(list[idx])) {
  1675. return idx;
  1676. }
  1677. idx += 1;
  1678. }
  1679. return -1;
  1680. }));
  1681. /**
  1682. * Returns the last element of the list which matches the predicate, or
  1683. * `undefined` if no element matches.
  1684. *
  1685. * Acts as a transducer if a transformer is given in list position.
  1686. *
  1687. * @func
  1688. * @memberOf R
  1689. * @since v0.1.1
  1690. * @category List
  1691. * @sig (a -> Boolean) -> [a] -> a | undefined
  1692. * @param {Function} fn The predicate function used to determine if the element is the
  1693. * desired one.
  1694. * @param {Array} list The array to consider.
  1695. * @return {Object} The element found, or `undefined`.
  1696. * @see R.transduce
  1697. * @example
  1698. *
  1699. * var xs = [{a: 1, b: 0}, {a:1, b: 1}];
  1700. * R.findLast(R.propEq('a', 1))(xs); //=> {a: 1, b: 1}
  1701. * R.findLast(R.propEq('a', 4))(xs); //=> undefined
  1702. */
  1703. var findLast = _curry2(_dispatchable([], _xfindLast, function findLast(fn, list) {
  1704. var idx = list.length - 1;
  1705. while (idx >= 0) {
  1706. if (fn(list[idx])) {
  1707. return list[idx];
  1708. }
  1709. idx -= 1;
  1710. }
  1711. }));
  1712. /**
  1713. * Returns the index of the last element of the list which matches the
  1714. * predicate, or `-1` if no element matches.
  1715. *
  1716. * Acts as a transducer if a transformer is given in list position.
  1717. *
  1718. * @func
  1719. * @memberOf R
  1720. * @since v0.1.1
  1721. * @category List
  1722. * @sig (a -> Boolean) -> [a] -> Number
  1723. * @param {Function} fn The predicate function used to determine if the element is the
  1724. * desired one.
  1725. * @param {Array} list The array to consider.
  1726. * @return {Number} The index of the element found, or `-1`.
  1727. * @see R.transduce
  1728. * @example
  1729. *
  1730. * var xs = [{a: 1, b: 0}, {a:1, b: 1}];
  1731. * R.findLastIndex(R.propEq('a', 1))(xs); //=> 1
  1732. * R.findLastIndex(R.propEq('a', 4))(xs); //=> -1
  1733. */
  1734. var findLastIndex = _curry2(_dispatchable([], _xfindLastIndex, function findLastIndex(fn, list) {
  1735. var idx = list.length - 1;
  1736. while (idx >= 0) {
  1737. if (fn(list[idx])) {
  1738. return idx;
  1739. }
  1740. idx -= 1;
  1741. }
  1742. return -1;
  1743. }));
  1744. /**
  1745. * Returns a new list by pulling every item out of it (and all its sub-arrays)
  1746. * and putting them in a new array, depth-first.
  1747. *
  1748. * @func
  1749. * @memberOf R
  1750. * @since v0.1.0
  1751. * @category List
  1752. * @sig [a] -> [b]
  1753. * @param {Array} list The array to consider.
  1754. * @return {Array} The flattened list.
  1755. * @see R.unnest
  1756. * @example
  1757. *
  1758. * R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
  1759. * //=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  1760. */
  1761. var flatten = _curry1(_makeFlat(true));
  1762. /**
  1763. * Iterate over an input `list`, calling a provided function `fn` for each
  1764. * element in the list.
  1765. *
  1766. * `fn` receives one argument: *(value)*.
  1767. *
  1768. * Note: `R.forEach` does not skip deleted or unassigned indices (sparse
  1769. * arrays), unlike the native `Array.prototype.forEach` method. For more
  1770. * details on this behavior, see:
  1771. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description
  1772. *
  1773. * Also note that, unlike `Array.prototype.forEach`, Ramda's `forEach` returns
  1774. * the original array. In some libraries this function is named `each`.
  1775. *
  1776. * Dispatches to the `forEach` method of the second argument, if present.
  1777. *
  1778. * @func
  1779. * @memberOf R
  1780. * @since v0.1.1
  1781. * @category List
  1782. * @sig (a -> *) -> [a] -> [a]
  1783. * @param {Function} fn The function to invoke. Receives one argument, `value`.
  1784. * @param {Array} list The list to iterate over.
  1785. * @return {Array} The original list.
  1786. * @see R.addIndex
  1787. * @example
  1788. *
  1789. * var printXPlusFive = x => console.log(x + 5);
  1790. * R.forEach(printXPlusFive, [1, 2, 3]); //=> [1, 2, 3]
  1791. * // logs 6
  1792. * // logs 7
  1793. * // logs 8
  1794. * @symb R.forEach(f, [a, b, c]) = [a, b, c]
  1795. */
  1796. var forEach = _curry2(_checkForMethod('forEach', function forEach(fn, list) {
  1797. var len = list.length;
  1798. var idx = 0;
  1799. while (idx < len) {
  1800. fn(list[idx]);
  1801. idx += 1;
  1802. }
  1803. return list;
  1804. }));
  1805. /**
  1806. * Creates a new object from a list key-value pairs. If a key appears in
  1807. * multiple pairs, the rightmost pair is included in the object.
  1808. *
  1809. * @func
  1810. * @memberOf R
  1811. * @since v0.3.0
  1812. * @category List
  1813. * @sig [[k,v]] -> {k: v}
  1814. * @param {Array} pairs An array of two-element arrays that will be the keys and values of the output object.
  1815. * @return {Object} The object made by pairing up `keys` and `values`.
  1816. * @see R.toPairs, R.pair
  1817. * @example
  1818. *
  1819. * R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3}
  1820. */
  1821. var fromPairs = _curry1(function fromPairs(pairs) {
  1822. var result = {};
  1823. var idx = 0;
  1824. while (idx < pairs.length) {
  1825. result[pairs[idx][0]] = pairs[idx][1];
  1826. idx += 1;
  1827. }
  1828. return result;
  1829. });
  1830. /**
  1831. * Takes a list and returns a list of lists where each sublist's elements are
  1832. * all satisfied pairwise comparison according to the provided function.
  1833. * Only adjacent elements are passed to the comparison function.
  1834. *
  1835. * @func
  1836. * @memberOf R
  1837. * @since v0.21.0
  1838. * @category List
  1839. * @sig ((a, a) → Boolean) → [a] → [[a]]
  1840. * @param {Function} fn Function for determining whether two given (adjacent)
  1841. * elements should be in the same group
  1842. * @param {Array} list The array to group. Also accepts a string, which will be
  1843. * treated as a list of characters.
  1844. * @return {List} A list that contains sublists of elements,
  1845. * whose concatenations are equal to the original list.
  1846. * @example
  1847. *
  1848. * R.groupWith(R.equals, [0, 1, 1, 2, 3, 5, 8, 13, 21])
  1849. * //=> [[0], [1, 1], [2], [3], [5], [8], [13], [21]]
  1850. *
  1851. * R.groupWith((a, b) => a + 1 === b, [0, 1, 1, 2, 3, 5, 8, 13, 21])
  1852. * //=> [[0, 1], [1, 2, 3], [5], [8], [13], [21]]
  1853. *
  1854. * R.groupWith((a, b) => a % 2 === b % 2, [0, 1, 1, 2, 3, 5, 8, 13, 21])
  1855. * //=> [[0], [1, 1], [2], [3, 5], [8], [13, 21]]
  1856. *
  1857. * R.groupWith(R.eqBy(isVowel), 'aestiou')
  1858. * //=> ['ae', 'st', 'iou']
  1859. */
  1860. var groupWith = _curry2(function (fn, list) {
  1861. var res = [];
  1862. var idx = 0;
  1863. var len = list.length;
  1864. while (idx < len) {
  1865. var nextidx = idx + 1;
  1866. while (nextidx < len && fn(list[nextidx - 1], list[nextidx])) {
  1867. nextidx += 1;
  1868. }
  1869. res.push(list.slice(idx, nextidx));
  1870. idx = nextidx;
  1871. }
  1872. return res;
  1873. });
  1874. /**
  1875. * Returns `true` if the first argument is greater than the second; `false`
  1876. * otherwise.
  1877. *
  1878. * @func
  1879. * @memberOf R
  1880. * @since v0.1.0
  1881. * @category Relation
  1882. * @sig Ord a => a -> a -> Boolean
  1883. * @param {*} a
  1884. * @param {*} b
  1885. * @return {Boolean}
  1886. * @see R.lt
  1887. * @example
  1888. *
  1889. * R.gt(2, 1); //=> true
  1890. * R.gt(2, 2); //=> false
  1891. * R.gt(2, 3); //=> false
  1892. * R.gt('a', 'z'); //=> false
  1893. * R.gt('z', 'a'); //=> true
  1894. */
  1895. var gt = _curry2(function gt(a, b) {
  1896. return a > b;
  1897. });
  1898. /**
  1899. * Returns `true` if the first argument is greater than or equal to the second;
  1900. * `false` otherwise.
  1901. *
  1902. * @func
  1903. * @memberOf R
  1904. * @since v0.1.0
  1905. * @category Relation
  1906. * @sig Ord a => a -> a -> Boolean
  1907. * @param {Number} a
  1908. * @param {Number} b
  1909. * @return {Boolean}
  1910. * @see R.lte
  1911. * @example
  1912. *
  1913. * R.gte(2, 1); //=> true
  1914. * R.gte(2, 2); //=> true
  1915. * R.gte(2, 3); //=> false
  1916. * R.gte('a', 'z'); //=> false
  1917. * R.gte('z', 'a'); //=> true
  1918. */
  1919. var gte = _curry2(function gte(a, b) {
  1920. return a >= b;
  1921. });
  1922. /**
  1923. * Returns whether or not an object has an own property with the specified name
  1924. *
  1925. * @func
  1926. * @memberOf R
  1927. * @since v0.7.0
  1928. * @category Object
  1929. * @sig s -> {s: x} -> Boolean
  1930. * @param {String} prop The name of the property to check for.
  1931. * @param {Object} obj The object to query.
  1932. * @return {Boolean} Whether the property exists.
  1933. * @example
  1934. *
  1935. * var hasName = R.has('name');
  1936. * hasName({name: 'alice'}); //=> true
  1937. * hasName({name: 'bob'}); //=> true
  1938. * hasName({}); //=> false
  1939. *
  1940. * var point = {x: 0, y: 0};
  1941. * var pointHas = R.has(R.__, point);
  1942. * pointHas('x'); //=> true
  1943. * pointHas('y'); //=> true
  1944. * pointHas('z'); //=> false
  1945. */
  1946. var has = _curry2(_has);
  1947. /**
  1948. * Returns whether or not an object or its prototype chain has a property with
  1949. * the specified name
  1950. *
  1951. * @func
  1952. * @memberOf R
  1953. * @since v0.7.0
  1954. * @category Object
  1955. * @sig s -> {s: x} -> Boolean
  1956. * @param {String} prop The name of the property to check for.
  1957. * @param {Object} obj The object to query.
  1958. * @return {Boolean} Whether the property exists.
  1959. * @example
  1960. *
  1961. * function Rectangle(width, height) {
  1962. * this.width = width;
  1963. * this.height = height;
  1964. * }
  1965. * Rectangle.prototype.area = function() {
  1966. * return this.width * this.height;
  1967. * };
  1968. *
  1969. * var square = new Rectangle(2, 2);
  1970. * R.hasIn('width', square); //=> true
  1971. * R.hasIn('area', square); //=> true
  1972. */
  1973. var hasIn = _curry2(function hasIn(prop, obj) {
  1974. return prop in obj;
  1975. });
  1976. /**
  1977. * Returns true if its arguments are identical, false otherwise. Values are
  1978. * identical if they reference the same memory. `NaN` is identical to `NaN`;
  1979. * `0` and `-0` are not identical.
  1980. *
  1981. * @func
  1982. * @memberOf R
  1983. * @since v0.15.0
  1984. * @category Relation
  1985. * @sig a -> a -> Boolean
  1986. * @param {*} a
  1987. * @param {*} b
  1988. * @return {Boolean}
  1989. * @example
  1990. *
  1991. * var o = {};
  1992. * R.identical(o, o); //=> true
  1993. * R.identical(1, 1); //=> true
  1994. * R.identical(1, '1'); //=> false
  1995. * R.identical([], []); //=> false
  1996. * R.identical(0, -0); //=> false
  1997. * R.identical(NaN, NaN); //=> true
  1998. */
  1999. // SameValue algorithm
  2000. // Steps 1-5, 7-10
  2001. // Steps 6.b-6.e: +0 != -0
  2002. // Step 6.a: NaN == NaN
  2003. var identical = _curry2(function identical(a, b) {
  2004. // SameValue algorithm
  2005. if (a === b) {
  2006. // Steps 1-5, 7-10
  2007. // Steps 6.b-6.e: +0 != -0
  2008. return a !== 0 || 1 / a === 1 / b;
  2009. } else {
  2010. // Step 6.a: NaN == NaN
  2011. return a !== a && b !== b;
  2012. }
  2013. });
  2014. /**
  2015. * A function that does nothing but return the parameter supplied to it. Good
  2016. * as a default or placeholder function.
  2017. *
  2018. * @func
  2019. * @memberOf R
  2020. * @since v0.1.0
  2021. * @category Function
  2022. * @sig a -> a
  2023. * @param {*} x The value to return.
  2024. * @return {*} The input value, `x`.
  2025. * @example
  2026. *
  2027. * R.identity(1); //=> 1
  2028. *
  2029. * var obj = {};
  2030. * R.identity(obj) === obj; //=> true
  2031. * @symb R.identity(a) = a
  2032. */
  2033. var identity = _curry1(_identity);
  2034. /**
  2035. * Creates a function that will process either the `onTrue` or the `onFalse`
  2036. * function depending upon the result of the `condition` predicate.
  2037. *
  2038. * @func
  2039. * @memberOf R
  2040. * @since v0.8.0
  2041. * @category Logic
  2042. * @sig (*... -> Boolean) -> (*... -> *) -> (*... -> *) -> (*... -> *)
  2043. * @param {Function} condition A predicate function
  2044. * @param {Function} onTrue A function to invoke when the `condition` evaluates to a truthy value.
  2045. * @param {Function} onFalse A function to invoke when the `condition` evaluates to a falsy value.
  2046. * @return {Function} A new unary function that will process either the `onTrue` or the `onFalse`
  2047. * function depending upon the result of the `condition` predicate.
  2048. * @see R.unless, R.when
  2049. * @example
  2050. *
  2051. * var incCount = R.ifElse(
  2052. * R.has('count'),
  2053. * R.over(R.lensProp('count'), R.inc),
  2054. * R.assoc('count', 1)
  2055. * );
  2056. * incCount({}); //=> { count: 1 }
  2057. * incCount({ count: 1 }); //=> { count: 2 }
  2058. */
  2059. var ifElse = _curry3(function ifElse(condition, onTrue, onFalse) {
  2060. return curryN(Math.max(condition.length, onTrue.length, onFalse.length), function _ifElse() {
  2061. return condition.apply(this, arguments) ? onTrue.apply(this, arguments) : onFalse.apply(this, arguments);
  2062. });
  2063. });
  2064. /**
  2065. * Increments its argument.
  2066. *
  2067. * @func
  2068. * @memberOf R
  2069. * @since v0.9.0
  2070. * @category Math
  2071. * @sig Number -> Number
  2072. * @param {Number} n
  2073. * @return {Number} n + 1
  2074. * @see R.dec
  2075. * @example
  2076. *
  2077. * R.inc(42); //=> 43
  2078. */
  2079. var inc = add(1);
  2080. /**
  2081. * Takes a predicate `pred`, a list `xs`, and a list `ys`, and returns a list
  2082. * `xs'` comprising each of the elements of `xs` which is equal to one or more
  2083. * elements of `ys` according to `pred`.
  2084. *
  2085. * `pred` must be a binary function expecting an element from each list.
  2086. *
  2087. * `xs`, `ys`, and `xs'` are treated as sets, semantically, so ordering should
  2088. * not be significant, but since `xs'` is ordered the implementation guarantees
  2089. * that its values are in the same order as they appear in `xs`. Duplicates are
  2090. * not removed, so `xs'` may contain duplicates if `xs` contains duplicates.
  2091. *
  2092. * @func
  2093. * @memberOf R
  2094. * @since v0.24.0
  2095. * @category Relation
  2096. * @sig (a -> b -> Boolean) -> [a] -> [b] -> [a]
  2097. * @param {Function} pred
  2098. * @param {Array} xs
  2099. * @param {Array} ys
  2100. * @return {Array}
  2101. * @see R.intersection
  2102. * @example
  2103. *
  2104. * R.innerJoin(
  2105. * (record, id) => record.id === id,
  2106. * [{id: 824, name: 'Richie Furay'},
  2107. * {id: 956, name: 'Dewey Martin'},
  2108. * {id: 313, name: 'Bruce Palmer'},
  2109. * {id: 456, name: 'Stephen Stills'},
  2110. * {id: 177, name: 'Neil Young'}],
  2111. * [177, 456, 999]
  2112. * );
  2113. * //=> [{id: 456, name: 'Stephen Stills'}, {id: 177, name: 'Neil Young'}]
  2114. */
  2115. var innerJoin = _curry3(function innerJoin(pred, xs, ys) {
  2116. return _filter(function (x) {
  2117. return _containsWith(pred, x, ys);
  2118. }, xs);
  2119. });
  2120. /**
  2121. * Inserts the supplied element into the list, at the specified `index`. _Note that
  2122. * this is not destructive_: it returns a copy of the list with the changes.
  2123. * <small>No lists have been harmed in the application of this function.</small>
  2124. *
  2125. * @func
  2126. * @memberOf R
  2127. * @since v0.2.2
  2128. * @category List
  2129. * @sig Number -> a -> [a] -> [a]
  2130. * @param {Number} index The position to insert the element
  2131. * @param {*} elt The element to insert into the Array
  2132. * @param {Array} list The list to insert into
  2133. * @return {Array} A new Array with `elt` inserted at `index`.
  2134. * @example
  2135. *
  2136. * R.insert(2, 'x', [1,2,3,4]); //=> [1,2,'x',3,4]
  2137. */
  2138. var insert = _curry3(function insert(idx, elt, list) {
  2139. idx = idx < list.length && idx >= 0 ? idx : list.length;
  2140. var result = Array.prototype.slice.call(list, 0);
  2141. result.splice(idx, 0, elt);
  2142. return result;
  2143. });
  2144. /**
  2145. * Inserts the sub-list into the list, at the specified `index`. _Note that this is not
  2146. * destructive_: it returns a copy of the list with the changes.
  2147. * <small>No lists have been harmed in the application of this function.</small>
  2148. *
  2149. * @func
  2150. * @memberOf R
  2151. * @since v0.9.0
  2152. * @category List
  2153. * @sig Number -> [a] -> [a] -> [a]
  2154. * @param {Number} index The position to insert the sub-list
  2155. * @param {Array} elts The sub-list to insert into the Array
  2156. * @param {Array} list The list to insert the sub-list into
  2157. * @return {Array} A new Array with `elts` inserted starting at `index`.
  2158. * @example
  2159. *
  2160. * R.insertAll(2, ['x','y','z'], [1,2,3,4]); //=> [1,2,'x','y','z',3,4]
  2161. */
  2162. var insertAll = _curry3(function insertAll(idx, elts, list) {
  2163. idx = idx < list.length && idx >= 0 ? idx : list.length;
  2164. return [].concat(Array.prototype.slice.call(list, 0, idx), elts, Array.prototype.slice.call(list, idx));
  2165. });
  2166. /**
  2167. * Creates a new list with the separator interposed between elements.
  2168. *
  2169. * Dispatches to the `intersperse` method of the second argument, if present.
  2170. *
  2171. * @func
  2172. * @memberOf R
  2173. * @since v0.14.0
  2174. * @category List
  2175. * @sig a -> [a] -> [a]
  2176. * @param {*} separator The element to add to the list.
  2177. * @param {Array} list The list to be interposed.
  2178. * @return {Array} The new list.
  2179. * @example
  2180. *
  2181. * R.intersperse('n', ['ba', 'a', 'a']); //=> ['ba', 'n', 'a', 'n', 'a']
  2182. */
  2183. var intersperse = _curry2(_checkForMethod('intersperse', function intersperse(separator, list) {
  2184. var out = [];
  2185. var idx = 0;
  2186. var length = list.length;
  2187. while (idx < length) {
  2188. if (idx === length - 1) {
  2189. out.push(list[idx]);
  2190. } else {
  2191. out.push(list[idx], separator);
  2192. }
  2193. idx += 1;
  2194. }
  2195. return out;
  2196. }));
  2197. /**
  2198. * See if an object (`val`) is an instance of the supplied constructor. This
  2199. * function will check up the inheritance chain, if any.
  2200. *
  2201. * @func
  2202. * @memberOf R
  2203. * @since v0.3.0
  2204. * @category Type
  2205. * @sig (* -> {*}) -> a -> Boolean
  2206. * @param {Object} ctor A constructor
  2207. * @param {*} val The value to test
  2208. * @return {Boolean}
  2209. * @example
  2210. *
  2211. * R.is(Object, {}); //=> true
  2212. * R.is(Number, 1); //=> true
  2213. * R.is(Object, 1); //=> false
  2214. * R.is(String, 's'); //=> true
  2215. * R.is(String, new String('')); //=> true
  2216. * R.is(Object, new String('')); //=> true
  2217. * R.is(Object, 's'); //=> false
  2218. * R.is(Number, {}); //=> false
  2219. */
  2220. var is = _curry2(function is(Ctor, val) {
  2221. return val != null && val.constructor === Ctor || val instanceof Ctor;
  2222. });
  2223. /**
  2224. * Checks if the input value is `null` or `undefined`.
  2225. *
  2226. * @func
  2227. * @memberOf R
  2228. * @since v0.9.0
  2229. * @category Type
  2230. * @sig * -> Boolean
  2231. * @param {*} x The value to test.
  2232. * @return {Boolean} `true` if `x` is `undefined` or `null`, otherwise `false`.
  2233. * @example
  2234. *
  2235. * R.isNil(null); //=> true
  2236. * R.isNil(undefined); //=> true
  2237. * R.isNil(0); //=> false
  2238. * R.isNil([]); //=> false
  2239. */
  2240. var isNil = _curry1(function isNil(x) {
  2241. return x == null;
  2242. });
  2243. /**
  2244. * Returns a list containing the names of all the enumerable own properties of
  2245. * the supplied object.
  2246. * Note that the order of the output array is not guaranteed to be consistent
  2247. * across different JS platforms.
  2248. *
  2249. * @func
  2250. * @memberOf R
  2251. * @since v0.1.0
  2252. * @category Object
  2253. * @sig {k: v} -> [k]
  2254. * @param {Object} obj The object to extract properties from
  2255. * @return {Array} An array of the object's own properties.
  2256. * @see R.keysIn, R.values
  2257. * @example
  2258. *
  2259. * R.keys({a: 1, b: 2, c: 3}); //=> ['a', 'b', 'c']
  2260. */
  2261. // cover IE < 9 keys issues
  2262. // Safari bug
  2263. var keys = function () {
  2264. // cover IE < 9 keys issues
  2265. var hasEnumBug = !{ toString: null }.propertyIsEnumerable('toString');
  2266. var nonEnumerableProps = [
  2267. 'constructor',
  2268. 'valueOf',
  2269. 'isPrototypeOf',
  2270. 'toString',
  2271. 'propertyIsEnumerable',
  2272. 'hasOwnProperty',
  2273. 'toLocaleString'
  2274. ];
  2275. // Safari bug
  2276. var hasArgsEnumBug = function () {
  2277. 'use strict';
  2278. return arguments.propertyIsEnumerable('length');
  2279. }();
  2280. var contains = function contains(list, item) {
  2281. var idx = 0;
  2282. while (idx < list.length) {
  2283. if (list[idx] === item) {
  2284. return true;
  2285. }
  2286. idx += 1;
  2287. }
  2288. return false;
  2289. };
  2290. return typeof Object.keys === 'function' && !hasArgsEnumBug ? _curry1(function keys(obj) {
  2291. return Object(obj) !== obj ? [] : Object.keys(obj);
  2292. }) : _curry1(function keys(obj) {
  2293. if (Object(obj) !== obj) {
  2294. return [];
  2295. }
  2296. var prop, nIdx;
  2297. var ks = [];
  2298. var checkArgsLength = hasArgsEnumBug && _isArguments(obj);
  2299. for (prop in obj) {
  2300. if (_has(prop, obj) && (!checkArgsLength || prop !== 'length')) {
  2301. ks[ks.length] = prop;
  2302. }
  2303. }
  2304. if (hasEnumBug) {
  2305. nIdx = nonEnumerableProps.length - 1;
  2306. while (nIdx >= 0) {
  2307. prop = nonEnumerableProps[nIdx];
  2308. if (_has(prop, obj) && !contains(ks, prop)) {
  2309. ks[ks.length] = prop;
  2310. }
  2311. nIdx -= 1;
  2312. }
  2313. }
  2314. return ks;
  2315. });
  2316. }();
  2317. /**
  2318. * Returns a list containing the names of all the properties of the supplied
  2319. * object, including prototype properties.
  2320. * Note that the order of the output array is not guaranteed to be consistent
  2321. * across different JS platforms.
  2322. *
  2323. * @func
  2324. * @memberOf R
  2325. * @since v0.2.0
  2326. * @category Object
  2327. * @sig {k: v} -> [k]
  2328. * @param {Object} obj The object to extract properties from
  2329. * @return {Array} An array of the object's own and prototype properties.
  2330. * @see R.keys, R.valuesIn
  2331. * @example
  2332. *
  2333. * var F = function() { this.x = 'X'; };
  2334. * F.prototype.y = 'Y';
  2335. * var f = new F();
  2336. * R.keysIn(f); //=> ['x', 'y']
  2337. */
  2338. var keysIn = _curry1(function keysIn(obj) {
  2339. var prop;
  2340. var ks = [];
  2341. for (prop in obj) {
  2342. ks[ks.length] = prop;
  2343. }
  2344. return ks;
  2345. });
  2346. /**
  2347. * Returns the number of elements in the array by returning `list.length`.
  2348. *
  2349. * @func
  2350. * @memberOf R
  2351. * @since v0.3.0
  2352. * @category List
  2353. * @sig [a] -> Number
  2354. * @param {Array} list The array to inspect.
  2355. * @return {Number} The length of the array.
  2356. * @example
  2357. *
  2358. * R.length([]); //=> 0
  2359. * R.length([1, 2, 3]); //=> 3
  2360. */
  2361. var length = _curry1(function length(list) {
  2362. return list != null && _isNumber(list.length) ? list.length : NaN;
  2363. });
  2364. /**
  2365. * Returns `true` if the first argument is less than the second; `false`
  2366. * otherwise.
  2367. *
  2368. * @func
  2369. * @memberOf R
  2370. * @since v0.1.0
  2371. * @category Relation
  2372. * @sig Ord a => a -> a -> Boolean
  2373. * @param {*} a
  2374. * @param {*} b
  2375. * @return {Boolean}
  2376. * @see R.gt
  2377. * @example
  2378. *
  2379. * R.lt(2, 1); //=> false
  2380. * R.lt(2, 2); //=> false
  2381. * R.lt(2, 3); //=> true
  2382. * R.lt('a', 'z'); //=> true
  2383. * R.lt('z', 'a'); //=> false
  2384. */
  2385. var lt = _curry2(function lt(a, b) {
  2386. return a < b;
  2387. });
  2388. /**
  2389. * Returns `true` if the first argument is less than or equal to the second;
  2390. * `false` otherwise.
  2391. *
  2392. * @func
  2393. * @memberOf R
  2394. * @since v0.1.0
  2395. * @category Relation
  2396. * @sig Ord a => a -> a -> Boolean
  2397. * @param {Number} a
  2398. * @param {Number} b
  2399. * @return {Boolean}
  2400. * @see R.gte
  2401. * @example
  2402. *
  2403. * R.lte(2, 1); //=> false
  2404. * R.lte(2, 2); //=> true
  2405. * R.lte(2, 3); //=> true
  2406. * R.lte('a', 'z'); //=> true
  2407. * R.lte('z', 'a'); //=> false
  2408. */
  2409. var lte = _curry2(function lte(a, b) {
  2410. return a <= b;
  2411. });
  2412. /**
  2413. * The `mapAccum` function behaves like a combination of map and reduce; it
  2414. * applies a function to each element of a list, passing an accumulating
  2415. * parameter from left to right, and returning a final value of this
  2416. * accumulator together with the new list.
  2417. *
  2418. * The iterator function receives two arguments, *acc* and *value*, and should
  2419. * return a tuple *[acc, value]*.
  2420. *
  2421. * @func
  2422. * @memberOf R
  2423. * @since v0.10.0
  2424. * @category List
  2425. * @sig (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
  2426. * @param {Function} fn The function to be called on every element of the input `list`.
  2427. * @param {*} acc The accumulator value.
  2428. * @param {Array} list The list to iterate over.
  2429. * @return {*} The final, accumulated value.
  2430. * @see R.addIndex, R.mapAccumRight
  2431. * @example
  2432. *
  2433. * var digits = ['1', '2', '3', '4'];
  2434. * var appender = (a, b) => [a + b, a + b];
  2435. *
  2436. * R.mapAccum(appender, 0, digits); //=> ['01234', ['01', '012', '0123', '01234']]
  2437. * @symb R.mapAccum(f, a, [b, c, d]) = [
  2438. * f(f(f(a, b)[0], c)[0], d)[0],
  2439. * [
  2440. * f(a, b)[1],
  2441. * f(f(a, b)[0], c)[1],
  2442. * f(f(f(a, b)[0], c)[0], d)[1]
  2443. * ]
  2444. * ]
  2445. */
  2446. var mapAccum = _curry3(function mapAccum(fn, acc, list) {
  2447. var idx = 0;
  2448. var len = list.length;
  2449. var result = [];
  2450. var tuple = [acc];
  2451. while (idx < len) {
  2452. tuple = fn(tuple[0], list[idx]);
  2453. result[idx] = tuple[1];
  2454. idx += 1;
  2455. }
  2456. return [
  2457. tuple[0],
  2458. result
  2459. ];
  2460. });
  2461. /**
  2462. * The `mapAccumRight` function behaves like a combination of map and reduce; it
  2463. * applies a function to each element of a list, passing an accumulating
  2464. * parameter from right to left, and returning a final value of this
  2465. * accumulator together with the new list.
  2466. *
  2467. * Similar to [`mapAccum`](#mapAccum), except moves through the input list from
  2468. * the right to the left.
  2469. *
  2470. * The iterator function receives two arguments, *value* and *acc*, and should
  2471. * return a tuple *[value, acc]*.
  2472. *
  2473. * @func
  2474. * @memberOf R
  2475. * @since v0.10.0
  2476. * @category List
  2477. * @sig (x-> acc -> (y, acc)) -> acc -> [x] -> ([y], acc)
  2478. * @param {Function} fn The function to be called on every element of the input `list`.
  2479. * @param {*} acc The accumulator value.
  2480. * @param {Array} list The list to iterate over.
  2481. * @return {*} The final, accumulated value.
  2482. * @see R.addIndex, R.mapAccum
  2483. * @example
  2484. *
  2485. * var digits = ['1', '2', '3', '4'];
  2486. * var append = (a, b) => [a + b, a + b];
  2487. *
  2488. * R.mapAccumRight(append, 5, digits); //=> [['12345', '2345', '345', '45'], '12345']
  2489. * @symb R.mapAccumRight(f, a, [b, c, d]) = [
  2490. * [
  2491. * f(b, f(c, f(d, a)[0])[0])[1],
  2492. * f(c, f(d, a)[0])[1],
  2493. * f(d, a)[1],
  2494. * ]
  2495. * f(b, f(c, f(d, a)[0])[0])[0],
  2496. * ]
  2497. */
  2498. var mapAccumRight = _curry3(function mapAccumRight(fn, acc, list) {
  2499. var idx = list.length - 1;
  2500. var result = [];
  2501. var tuple = [acc];
  2502. while (idx >= 0) {
  2503. tuple = fn(list[idx], tuple[0]);
  2504. result[idx] = tuple[1];
  2505. idx -= 1;
  2506. }
  2507. return [
  2508. result,
  2509. tuple[0]
  2510. ];
  2511. });
  2512. /**
  2513. * Tests a regular expression against a String. Note that this function will
  2514. * return an empty array when there are no matches. This differs from
  2515. * [`String.prototype.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)
  2516. * which returns `null` when there are no matches.
  2517. *
  2518. * @func
  2519. * @memberOf R
  2520. * @since v0.1.0
  2521. * @category String
  2522. * @sig RegExp -> String -> [String | Undefined]
  2523. * @param {RegExp} rx A regular expression.
  2524. * @param {String} str The string to match against
  2525. * @return {Array} The list of matches or empty array.
  2526. * @see R.test
  2527. * @example
  2528. *
  2529. * R.match(/([a-z]a)/g, 'bananas'); //=> ['ba', 'na', 'na']
  2530. * R.match(/a/, 'b'); //=> []
  2531. * R.match(/a/, null); //=> TypeError: null does not have a method named "match"
  2532. */
  2533. var match = _curry2(function match(rx, str) {
  2534. return str.match(rx) || [];
  2535. });
  2536. /**
  2537. * `mathMod` behaves like the modulo operator should mathematically, unlike the
  2538. * `%` operator (and by extension, [`R.modulo`](#modulo)). So while
  2539. * `-17 % 5` is `-2`, `mathMod(-17, 5)` is `3`. `mathMod` requires Integer
  2540. * arguments, and returns NaN when the modulus is zero or negative.
  2541. *
  2542. * @func
  2543. * @memberOf R
  2544. * @since v0.3.0
  2545. * @category Math
  2546. * @sig Number -> Number -> Number
  2547. * @param {Number} m The dividend.
  2548. * @param {Number} p the modulus.
  2549. * @return {Number} The result of `b mod a`.
  2550. * @see R.modulo
  2551. * @example
  2552. *
  2553. * R.mathMod(-17, 5); //=> 3
  2554. * R.mathMod(17, 5); //=> 2
  2555. * R.mathMod(17, -5); //=> NaN
  2556. * R.mathMod(17, 0); //=> NaN
  2557. * R.mathMod(17.2, 5); //=> NaN
  2558. * R.mathMod(17, 5.3); //=> NaN
  2559. *
  2560. * var clock = R.mathMod(R.__, 12);
  2561. * clock(15); //=> 3
  2562. * clock(24); //=> 0
  2563. *
  2564. * var seventeenMod = R.mathMod(17);
  2565. * seventeenMod(3); //=> 2
  2566. * seventeenMod(4); //=> 1
  2567. * seventeenMod(10); //=> 7
  2568. */
  2569. var mathMod = _curry2(function mathMod(m, p) {
  2570. if (!_isInteger(m)) {
  2571. return NaN;
  2572. }
  2573. if (!_isInteger(p) || p < 1) {
  2574. return NaN;
  2575. }
  2576. return (m % p + p) % p;
  2577. });
  2578. /**
  2579. * Returns the larger of its two arguments.
  2580. *
  2581. * @func
  2582. * @memberOf R
  2583. * @since v0.1.0
  2584. * @category Relation
  2585. * @sig Ord a => a -> a -> a
  2586. * @param {*} a
  2587. * @param {*} b
  2588. * @return {*}
  2589. * @see R.maxBy, R.min
  2590. * @example
  2591. *
  2592. * R.max(789, 123); //=> 789
  2593. * R.max('a', 'b'); //=> 'b'
  2594. */
  2595. var max = _curry2(function max(a, b) {
  2596. return b > a ? b : a;
  2597. });
  2598. /**
  2599. * Takes a function and two values, and returns whichever value produces the
  2600. * larger result when passed to the provided function.
  2601. *
  2602. * @func
  2603. * @memberOf R
  2604. * @since v0.8.0
  2605. * @category Relation
  2606. * @sig Ord b => (a -> b) -> a -> a -> a
  2607. * @param {Function} f
  2608. * @param {*} a
  2609. * @param {*} b
  2610. * @return {*}
  2611. * @see R.max, R.minBy
  2612. * @example
  2613. *
  2614. * // square :: Number -> Number
  2615. * var square = n => n * n;
  2616. *
  2617. * R.maxBy(square, -3, 2); //=> -3
  2618. *
  2619. * R.reduce(R.maxBy(square), 0, [3, -5, 4, 1, -2]); //=> -5
  2620. * R.reduce(R.maxBy(square), 0, []); //=> 0
  2621. */
  2622. var maxBy = _curry3(function maxBy(f, a, b) {
  2623. return f(b) > f(a) ? b : a;
  2624. });
  2625. /**
  2626. * A customisable version of [`R.memoize`](#memoize). `memoizeWith` takes an
  2627. * additional function that will be applied to a given argument set and used to
  2628. * create the cache key under which the results of the function to be memoized
  2629. * will be stored. Care must be taken when implementing key generation to avoid
  2630. * clashes that may overwrite previous entries erroneously.
  2631. *
  2632. *
  2633. * @func
  2634. * @memberOf R
  2635. * @since v0.24.0
  2636. * @category Function
  2637. * @sig (*... -> String) -> (*... -> a) -> (*... -> a)
  2638. * @param {Function} fn The function to generate the cache key.
  2639. * @param {Function} fn The function to memoize.
  2640. * @return {Function} Memoized version of `fn`.
  2641. * @see R.memoize
  2642. * @example
  2643. *
  2644. * let count = 0;
  2645. * const factorial = R.memoizeWith(R.identity, n => {
  2646. * count += 1;
  2647. * return R.product(R.range(1, n + 1));
  2648. * });
  2649. * factorial(5); //=> 120
  2650. * factorial(5); //=> 120
  2651. * factorial(5); //=> 120
  2652. * count; //=> 1
  2653. */
  2654. var memoizeWith = _curry2(function memoizeWith(mFn, fn) {
  2655. var cache = {};
  2656. return _arity(fn.length, function () {
  2657. var key = mFn.apply(this, arguments);
  2658. if (!_has(key, cache)) {
  2659. cache[key] = fn.apply(this, arguments);
  2660. }
  2661. return cache[key];
  2662. });
  2663. });
  2664. /**
  2665. * Create a new object with the own properties of the first object merged with
  2666. * the own properties of the second object. If a key exists in both objects,
  2667. * the value from the second object will be used.
  2668. *
  2669. * @func
  2670. * @memberOf R
  2671. * @since v0.1.0
  2672. * @category Object
  2673. * @sig {k: v} -> {k: v} -> {k: v}
  2674. * @param {Object} l
  2675. * @param {Object} r
  2676. * @return {Object}
  2677. * @see R.mergeDeepRight, R.mergeWith, R.mergeWithKey
  2678. * @example
  2679. *
  2680. * R.merge({ 'name': 'fred', 'age': 10 }, { 'age': 40 });
  2681. * //=> { 'name': 'fred', 'age': 40 }
  2682. *
  2683. * var resetToDefault = R.merge(R.__, {x: 0});
  2684. * resetToDefault({x: 5, y: 2}); //=> {x: 0, y: 2}
  2685. * @symb R.merge({ x: 1, y: 2 }, { y: 5, z: 3 }) = { x: 1, y: 5, z: 3 }
  2686. */
  2687. var merge = _curry2(function merge(l, r) {
  2688. return _assign({}, l, r);
  2689. });
  2690. /**
  2691. * Merges a list of objects together into one object.
  2692. *
  2693. * @func
  2694. * @memberOf R
  2695. * @since v0.10.0
  2696. * @category List
  2697. * @sig [{k: v}] -> {k: v}
  2698. * @param {Array} list An array of objects
  2699. * @return {Object} A merged object.
  2700. * @see R.reduce
  2701. * @example
  2702. *
  2703. * R.mergeAll([{foo:1},{bar:2},{baz:3}]); //=> {foo:1,bar:2,baz:3}
  2704. * R.mergeAll([{foo:1},{foo:2},{bar:2}]); //=> {foo:2,bar:2}
  2705. * @symb R.mergeAll([{ x: 1 }, { y: 2 }, { z: 3 }]) = { x: 1, y: 2, z: 3 }
  2706. */
  2707. var mergeAll = _curry1(function mergeAll(list) {
  2708. return _assign.apply(null, [{}].concat(list));
  2709. });
  2710. /**
  2711. * Creates a new object with the own properties of the two provided objects. If
  2712. * a key exists in both objects, the provided function is applied to the key
  2713. * and the values associated with the key in each object, with the result being
  2714. * used as the value associated with the key in the returned object.
  2715. *
  2716. * @func
  2717. * @memberOf R
  2718. * @since v0.19.0
  2719. * @category Object
  2720. * @sig (String -> a -> a -> a) -> {a} -> {a} -> {a}
  2721. * @param {Function} fn
  2722. * @param {Object} l
  2723. * @param {Object} r
  2724. * @return {Object}
  2725. * @see R.mergeDeepWithKey, R.merge, R.mergeWith
  2726. * @example
  2727. *
  2728. * let concatValues = (k, l, r) => k == 'values' ? R.concat(l, r) : r
  2729. * R.mergeWithKey(concatValues,
  2730. * { a: true, thing: 'foo', values: [10, 20] },
  2731. * { b: true, thing: 'bar', values: [15, 35] });
  2732. * //=> { a: true, b: true, thing: 'bar', values: [10, 20, 15, 35] }
  2733. * @symb R.mergeWithKey(f, { x: 1, y: 2 }, { y: 5, z: 3 }) = { x: 1, y: f('y', 2, 5), z: 3 }
  2734. */
  2735. var mergeWithKey = _curry3(function mergeWithKey(fn, l, r) {
  2736. var result = {};
  2737. var k;
  2738. for (k in l) {
  2739. if (_has(k, l)) {
  2740. result[k] = _has(k, r) ? fn(k, l[k], r[k]) : l[k];
  2741. }
  2742. }
  2743. for (k in r) {
  2744. if (_has(k, r) && !_has(k, result)) {
  2745. result[k] = r[k];
  2746. }
  2747. }
  2748. return result;
  2749. });
  2750. /**
  2751. * Returns the smaller of its two arguments.
  2752. *
  2753. * @func
  2754. * @memberOf R
  2755. * @since v0.1.0
  2756. * @category Relation
  2757. * @sig Ord a => a -> a -> a
  2758. * @param {*} a
  2759. * @param {*} b
  2760. * @return {*}
  2761. * @see R.minBy, R.max
  2762. * @example
  2763. *
  2764. * R.min(789, 123); //=> 123
  2765. * R.min('a', 'b'); //=> 'a'
  2766. */
  2767. var min = _curry2(function min(a, b) {
  2768. return b < a ? b : a;
  2769. });
  2770. /**
  2771. * Takes a function and two values, and returns whichever value produces the
  2772. * smaller result when passed to the provided function.
  2773. *
  2774. * @func
  2775. * @memberOf R
  2776. * @since v0.8.0
  2777. * @category Relation
  2778. * @sig Ord b => (a -> b) -> a -> a -> a
  2779. * @param {Function} f
  2780. * @param {*} a
  2781. * @param {*} b
  2782. * @return {*}
  2783. * @see R.min, R.maxBy
  2784. * @example
  2785. *
  2786. * // square :: Number -> Number
  2787. * var square = n => n * n;
  2788. *
  2789. * R.minBy(square, -3, 2); //=> 2
  2790. *
  2791. * R.reduce(R.minBy(square), Infinity, [3, -5, 4, 1, -2]); //=> 1
  2792. * R.reduce(R.minBy(square), Infinity, []); //=> Infinity
  2793. */
  2794. var minBy = _curry3(function minBy(f, a, b) {
  2795. return f(b) < f(a) ? b : a;
  2796. });
  2797. /**
  2798. * Divides the first parameter by the second and returns the remainder. Note
  2799. * that this function preserves the JavaScript-style behavior for modulo. For
  2800. * mathematical modulo see [`mathMod`](#mathMod).
  2801. *
  2802. * @func
  2803. * @memberOf R
  2804. * @since v0.1.1
  2805. * @category Math
  2806. * @sig Number -> Number -> Number
  2807. * @param {Number} a The value to the divide.
  2808. * @param {Number} b The pseudo-modulus
  2809. * @return {Number} The result of `b % a`.
  2810. * @see R.mathMod
  2811. * @example
  2812. *
  2813. * R.modulo(17, 3); //=> 2
  2814. * // JS behavior:
  2815. * R.modulo(-17, 3); //=> -2
  2816. * R.modulo(17, -3); //=> 2
  2817. *
  2818. * var isOdd = R.modulo(R.__, 2);
  2819. * isOdd(42); //=> 0
  2820. * isOdd(21); //=> 1
  2821. */
  2822. var modulo = _curry2(function modulo(a, b) {
  2823. return a % b;
  2824. });
  2825. /**
  2826. * Multiplies two numbers. Equivalent to `a * b` but curried.
  2827. *
  2828. * @func
  2829. * @memberOf R
  2830. * @since v0.1.0
  2831. * @category Math
  2832. * @sig Number -> Number -> Number
  2833. * @param {Number} a The first value.
  2834. * @param {Number} b The second value.
  2835. * @return {Number} The result of `a * b`.
  2836. * @see R.divide
  2837. * @example
  2838. *
  2839. * var double = R.multiply(2);
  2840. * var triple = R.multiply(3);
  2841. * double(3); //=> 6
  2842. * triple(4); //=> 12
  2843. * R.multiply(2, 5); //=> 10
  2844. */
  2845. var multiply = _curry2(function multiply(a, b) {
  2846. return a * b;
  2847. });
  2848. /**
  2849. * Wraps a function of any arity (including nullary) in a function that accepts
  2850. * exactly `n` parameters. Any extraneous parameters will not be passed to the
  2851. * supplied function.
  2852. *
  2853. * @func
  2854. * @memberOf R
  2855. * @since v0.1.0
  2856. * @category Function
  2857. * @sig Number -> (* -> a) -> (* -> a)
  2858. * @param {Number} n The desired arity of the new function.
  2859. * @param {Function} fn The function to wrap.
  2860. * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
  2861. * arity `n`.
  2862. * @see R.binary, R.unary
  2863. * @example
  2864. *
  2865. * var takesTwoArgs = (a, b) => [a, b];
  2866. *
  2867. * takesTwoArgs.length; //=> 2
  2868. * takesTwoArgs(1, 2); //=> [1, 2]
  2869. *
  2870. * var takesOneArg = R.nAry(1, takesTwoArgs);
  2871. * takesOneArg.length; //=> 1
  2872. * // Only `n` arguments are passed to the wrapped function
  2873. * takesOneArg(1, 2); //=> [1, undefined]
  2874. * @symb R.nAry(0, f)(a, b) = f()
  2875. * @symb R.nAry(1, f)(a, b) = f(a)
  2876. * @symb R.nAry(2, f)(a, b) = f(a, b)
  2877. */
  2878. var nAry = _curry2(function nAry(n, fn) {
  2879. switch (n) {
  2880. case 0:
  2881. return function () {
  2882. return fn.call(this);
  2883. };
  2884. case 1:
  2885. return function (a0) {
  2886. return fn.call(this, a0);
  2887. };
  2888. case 2:
  2889. return function (a0, a1) {
  2890. return fn.call(this, a0, a1);
  2891. };
  2892. case 3:
  2893. return function (a0, a1, a2) {
  2894. return fn.call(this, a0, a1, a2);
  2895. };
  2896. case 4:
  2897. return function (a0, a1, a2, a3) {
  2898. return fn.call(this, a0, a1, a2, a3);
  2899. };
  2900. case 5:
  2901. return function (a0, a1, a2, a3, a4) {
  2902. return fn.call(this, a0, a1, a2, a3, a4);
  2903. };
  2904. case 6:
  2905. return function (a0, a1, a2, a3, a4, a5) {
  2906. return fn.call(this, a0, a1, a2, a3, a4, a5);
  2907. };
  2908. case 7:
  2909. return function (a0, a1, a2, a3, a4, a5, a6) {
  2910. return fn.call(this, a0, a1, a2, a3, a4, a5, a6);
  2911. };
  2912. case 8:
  2913. return function (a0, a1, a2, a3, a4, a5, a6, a7) {
  2914. return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7);
  2915. };
  2916. case 9:
  2917. return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
  2918. return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8);
  2919. };
  2920. case 10:
  2921. return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
  2922. return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
  2923. };
  2924. default:
  2925. throw new Error('First argument to nAry must be a non-negative integer no greater than ten');
  2926. }
  2927. });
  2928. /**
  2929. * Negates its argument.
  2930. *
  2931. * @func
  2932. * @memberOf R
  2933. * @since v0.9.0
  2934. * @category Math
  2935. * @sig Number -> Number
  2936. * @param {Number} n
  2937. * @return {Number}
  2938. * @example
  2939. *
  2940. * R.negate(42); //=> -42
  2941. */
  2942. var negate = _curry1(function negate(n) {
  2943. return -n;
  2944. });
  2945. /**
  2946. * Returns `true` if no elements of the list match the predicate, `false`
  2947. * otherwise.
  2948. *
  2949. * Dispatches to the `any` method of the second argument, if present.
  2950. *
  2951. * @func
  2952. * @memberOf R
  2953. * @since v0.12.0
  2954. * @category List
  2955. * @sig (a -> Boolean) -> [a] -> Boolean
  2956. * @param {Function} fn The predicate function.
  2957. * @param {Array} list The array to consider.
  2958. * @return {Boolean} `true` if the predicate is not satisfied by every element, `false` otherwise.
  2959. * @see R.all, R.any
  2960. * @example
  2961. *
  2962. * var isEven = n => n % 2 === 0;
  2963. *
  2964. * R.none(isEven, [1, 3, 5, 7, 9, 11]); //=> true
  2965. * R.none(isEven, [1, 3, 5, 7, 8, 11]); //=> false
  2966. */
  2967. var none = _curry2(_complement(_dispatchable(['any'], _xany, any)));
  2968. /**
  2969. * A function that returns the `!` of its argument. It will return `true` when
  2970. * passed false-y value, and `false` when passed a truth-y one.
  2971. *
  2972. * @func
  2973. * @memberOf R
  2974. * @since v0.1.0
  2975. * @category Logic
  2976. * @sig * -> Boolean
  2977. * @param {*} a any value
  2978. * @return {Boolean} the logical inverse of passed argument.
  2979. * @see R.complement
  2980. * @example
  2981. *
  2982. * R.not(true); //=> false
  2983. * R.not(false); //=> true
  2984. * R.not(0); //=> true
  2985. * R.not(1); //=> false
  2986. */
  2987. var not = _curry1(function not(a) {
  2988. return !a;
  2989. });
  2990. /**
  2991. * Returns the nth element of the given list or string. If n is negative the
  2992. * element at index length + n is returned.
  2993. *
  2994. * @func
  2995. * @memberOf R
  2996. * @since v0.1.0
  2997. * @category List
  2998. * @sig Number -> [a] -> a | Undefined
  2999. * @sig Number -> String -> String
  3000. * @param {Number} offset
  3001. * @param {*} list
  3002. * @return {*}
  3003. * @example
  3004. *
  3005. * var list = ['foo', 'bar', 'baz', 'quux'];
  3006. * R.nth(1, list); //=> 'bar'
  3007. * R.nth(-1, list); //=> 'quux'
  3008. * R.nth(-99, list); //=> undefined
  3009. *
  3010. * R.nth(2, 'abc'); //=> 'c'
  3011. * R.nth(3, 'abc'); //=> ''
  3012. * @symb R.nth(-1, [a, b, c]) = c
  3013. * @symb R.nth(0, [a, b, c]) = a
  3014. * @symb R.nth(1, [a, b, c]) = b
  3015. */
  3016. var nth = _curry2(function nth(offset, list) {
  3017. var idx = offset < 0 ? list.length + offset : offset;
  3018. return _isString(list) ? list.charAt(idx) : list[idx];
  3019. });
  3020. /**
  3021. * Returns a function which returns its nth argument.
  3022. *
  3023. * @func
  3024. * @memberOf R
  3025. * @since v0.9.0
  3026. * @category Function
  3027. * @sig Number -> *... -> *
  3028. * @param {Number} n
  3029. * @return {Function}
  3030. * @example
  3031. *
  3032. * R.nthArg(1)('a', 'b', 'c'); //=> 'b'
  3033. * R.nthArg(-1)('a', 'b', 'c'); //=> 'c'
  3034. * @symb R.nthArg(-1)(a, b, c) = c
  3035. * @symb R.nthArg(0)(a, b, c) = a
  3036. * @symb R.nthArg(1)(a, b, c) = b
  3037. */
  3038. var nthArg = _curry1(function nthArg(n) {
  3039. var arity = n < 0 ? 1 : n + 1;
  3040. return curryN(arity, function () {
  3041. return nth(n, arguments);
  3042. });
  3043. });
  3044. /**
  3045. * `o` is a curried composition function that returns a unary function.
  3046. * Like [`compose`](#compose), `o` performs right-to-left function composition.
  3047. * Unlike [`compose`](#compose), the rightmost function passed to `o` will be
  3048. * invoked with only one argument.
  3049. *
  3050. * @func
  3051. * @memberOf R
  3052. * @since v0.24.0
  3053. * @category Function
  3054. * @sig (b -> c) -> (a -> b) -> a -> c
  3055. * @param {Function} f
  3056. * @param {Function} g
  3057. * @return {Function}
  3058. * @see R.compose, R.pipe
  3059. * @example
  3060. *
  3061. * var classyGreeting = name => "The name's " + name.last + ", " + name.first + " " + lastName
  3062. * var yellGreeting = R.o(R.toUpper, classyGreeting);
  3063. * yellGreeting({first: 'James', last: 'Bond'}); //=> "THE NAME'S BOND, JAMES BOND"
  3064. *
  3065. * R.o(R.multiply(10), R.add(10))(-4) //=> 60
  3066. *
  3067. * @symb R.o(f, g, x) = f(g(x))
  3068. */
  3069. var o = _curry3(function o(f, g, x) {
  3070. return f(g(x));
  3071. });
  3072. /**
  3073. * Creates an object containing a single key:value pair.
  3074. *
  3075. * @func
  3076. * @memberOf R
  3077. * @since v0.18.0
  3078. * @category Object
  3079. * @sig String -> a -> {String:a}
  3080. * @param {String} key
  3081. * @param {*} val
  3082. * @return {Object}
  3083. * @see R.pair
  3084. * @example
  3085. *
  3086. * var matchPhrases = R.compose(
  3087. * R.objOf('must'),
  3088. * R.map(R.objOf('match_phrase'))
  3089. * );
  3090. * matchPhrases(['foo', 'bar', 'baz']); //=> {must: [{match_phrase: 'foo'}, {match_phrase: 'bar'}, {match_phrase: 'baz'}]}
  3091. */
  3092. var objOf = _curry2(function objOf(key, val) {
  3093. var obj = {};
  3094. obj[key] = val;
  3095. return obj;
  3096. });
  3097. /**
  3098. * Returns a singleton array containing the value provided.
  3099. *
  3100. * Note this `of` is different from the ES6 `of`; See
  3101. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
  3102. *
  3103. * @func
  3104. * @memberOf R
  3105. * @since v0.3.0
  3106. * @category Function
  3107. * @sig a -> [a]
  3108. * @param {*} x any value
  3109. * @return {Array} An array wrapping `x`.
  3110. * @example
  3111. *
  3112. * R.of(null); //=> [null]
  3113. * R.of([42]); //=> [[42]]
  3114. */
  3115. var of = _curry1(_of);
  3116. /**
  3117. * Accepts a function `fn` and returns a function that guards invocation of
  3118. * `fn` such that `fn` can only ever be called once, no matter how many times
  3119. * the returned function is invoked. The first value calculated is returned in
  3120. * subsequent invocations.
  3121. *
  3122. * @func
  3123. * @memberOf R
  3124. * @since v0.1.0
  3125. * @category Function
  3126. * @sig (a... -> b) -> (a... -> b)
  3127. * @param {Function} fn The function to wrap in a call-only-once wrapper.
  3128. * @return {Function} The wrapped function.
  3129. * @example
  3130. *
  3131. * var addOneOnce = R.once(x => x + 1);
  3132. * addOneOnce(10); //=> 11
  3133. * addOneOnce(addOneOnce(50)); //=> 11
  3134. */
  3135. var once = _curry1(function once(fn) {
  3136. var called = false;
  3137. var result;
  3138. return _arity(fn.length, function () {
  3139. if (called) {
  3140. return result;
  3141. }
  3142. called = true;
  3143. result = fn.apply(this, arguments);
  3144. return result;
  3145. });
  3146. });
  3147. /**
  3148. * Returns `true` if one or both of its arguments are `true`. Returns `false`
  3149. * if both arguments are `false`.
  3150. *
  3151. * @func
  3152. * @memberOf R
  3153. * @since v0.1.0
  3154. * @category Logic
  3155. * @sig a -> b -> a | b
  3156. * @param {Any} a
  3157. * @param {Any} b
  3158. * @return {Any} the first argument if truthy, otherwise the second argument.
  3159. * @see R.either
  3160. * @example
  3161. *
  3162. * R.or(true, true); //=> true
  3163. * R.or(true, false); //=> true
  3164. * R.or(false, true); //=> true
  3165. * R.or(false, false); //=> false
  3166. */
  3167. var or = _curry2(function or(a, b) {
  3168. return a || b;
  3169. });
  3170. /**
  3171. * Returns the result of "setting" the portion of the given data structure
  3172. * focused by the given lens to the result of applying the given function to
  3173. * the focused value.
  3174. *
  3175. * @func
  3176. * @memberOf R
  3177. * @since v0.16.0
  3178. * @category Object
  3179. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  3180. * @sig Lens s a -> (a -> a) -> s -> s
  3181. * @param {Lens} lens
  3182. * @param {*} v
  3183. * @param {*} x
  3184. * @return {*}
  3185. * @see R.prop, R.lensIndex, R.lensProp
  3186. * @example
  3187. *
  3188. * var headLens = R.lensIndex(0);
  3189. *
  3190. * R.over(headLens, R.toUpper, ['foo', 'bar', 'baz']); //=> ['FOO', 'bar', 'baz']
  3191. */
  3192. // `Identity` is a functor that holds a single value, where `map` simply
  3193. // transforms the held value with the provided function.
  3194. // The value returned by the getter function is first transformed with `f`,
  3195. // then set as the value of an `Identity`. This is then mapped over with the
  3196. // setter function of the lens.
  3197. var over = function () {
  3198. // `Identity` is a functor that holds a single value, where `map` simply
  3199. // transforms the held value with the provided function.
  3200. var Identity = function (x) {
  3201. return {
  3202. value: x,
  3203. map: function (f) {
  3204. return Identity(f(x));
  3205. }
  3206. };
  3207. };
  3208. return _curry3(function over(lens, f, x) {
  3209. // The value returned by the getter function is first transformed with `f`,
  3210. // then set as the value of an `Identity`. This is then mapped over with the
  3211. // setter function of the lens.
  3212. return lens(function (y) {
  3213. return Identity(f(y));
  3214. })(x).value;
  3215. });
  3216. }();
  3217. /**
  3218. * Takes two arguments, `fst` and `snd`, and returns `[fst, snd]`.
  3219. *
  3220. * @func
  3221. * @memberOf R
  3222. * @since v0.18.0
  3223. * @category List
  3224. * @sig a -> b -> (a,b)
  3225. * @param {*} fst
  3226. * @param {*} snd
  3227. * @return {Array}
  3228. * @see R.objOf, R.of
  3229. * @example
  3230. *
  3231. * R.pair('foo', 'bar'); //=> ['foo', 'bar']
  3232. */
  3233. var pair = _curry2(function pair(fst, snd) {
  3234. return [
  3235. fst,
  3236. snd
  3237. ];
  3238. });
  3239. /**
  3240. * Retrieve the value at a given path.
  3241. *
  3242. * @func
  3243. * @memberOf R
  3244. * @since v0.2.0
  3245. * @category Object
  3246. * @typedefn Idx = String | Int
  3247. * @sig [Idx] -> {a} -> a | Undefined
  3248. * @param {Array} path The path to use.
  3249. * @param {Object} obj The object to retrieve the nested property from.
  3250. * @return {*} The data at `path`.
  3251. * @see R.prop
  3252. * @example
  3253. *
  3254. * R.path(['a', 'b'], {a: {b: 2}}); //=> 2
  3255. * R.path(['a', 'b'], {c: {b: 2}}); //=> undefined
  3256. */
  3257. var path = _curry2(function path(paths, obj) {
  3258. var val = obj;
  3259. var idx = 0;
  3260. while (idx < paths.length) {
  3261. if (val == null) {
  3262. return;
  3263. }
  3264. val = val[paths[idx]];
  3265. idx += 1;
  3266. }
  3267. return val;
  3268. });
  3269. /**
  3270. * If the given, non-null object has a value at the given path, returns the
  3271. * value at that path. Otherwise returns the provided default value.
  3272. *
  3273. * @func
  3274. * @memberOf R
  3275. * @since v0.18.0
  3276. * @category Object
  3277. * @typedefn Idx = String | Int
  3278. * @sig a -> [Idx] -> {a} -> a
  3279. * @param {*} d The default value.
  3280. * @param {Array} p The path to use.
  3281. * @param {Object} obj The object to retrieve the nested property from.
  3282. * @return {*} The data at `path` of the supplied object or the default value.
  3283. * @example
  3284. *
  3285. * R.pathOr('N/A', ['a', 'b'], {a: {b: 2}}); //=> 2
  3286. * R.pathOr('N/A', ['a', 'b'], {c: {b: 2}}); //=> "N/A"
  3287. */
  3288. var pathOr = _curry3(function pathOr(d, p, obj) {
  3289. return defaultTo(d, path(p, obj));
  3290. });
  3291. /**
  3292. * Returns `true` if the specified object property at given path satisfies the
  3293. * given predicate; `false` otherwise.
  3294. *
  3295. * @func
  3296. * @memberOf R
  3297. * @since v0.19.0
  3298. * @category Logic
  3299. * @typedefn Idx = String | Int
  3300. * @sig (a -> Boolean) -> [Idx] -> {a} -> Boolean
  3301. * @param {Function} pred
  3302. * @param {Array} propPath
  3303. * @param {*} obj
  3304. * @return {Boolean}
  3305. * @see R.propSatisfies, R.path
  3306. * @example
  3307. *
  3308. * R.pathSatisfies(y => y > 0, ['x', 'y'], {x: {y: 2}}); //=> true
  3309. */
  3310. var pathSatisfies = _curry3(function pathSatisfies(pred, propPath, obj) {
  3311. return propPath.length > 0 && pred(path(propPath, obj));
  3312. });
  3313. /**
  3314. * Returns a partial copy of an object containing only the keys specified. If
  3315. * the key does not exist, the property is ignored.
  3316. *
  3317. * @func
  3318. * @memberOf R
  3319. * @since v0.1.0
  3320. * @category Object
  3321. * @sig [k] -> {k: v} -> {k: v}
  3322. * @param {Array} names an array of String property names to copy onto a new object
  3323. * @param {Object} obj The object to copy from
  3324. * @return {Object} A new object with only properties from `names` on it.
  3325. * @see R.omit, R.props
  3326. * @example
  3327. *
  3328. * R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
  3329. * R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1}
  3330. */
  3331. var pick = _curry2(function pick(names, obj) {
  3332. var result = {};
  3333. var idx = 0;
  3334. while (idx < names.length) {
  3335. if (names[idx] in obj) {
  3336. result[names[idx]] = obj[names[idx]];
  3337. }
  3338. idx += 1;
  3339. }
  3340. return result;
  3341. });
  3342. /**
  3343. * Similar to `pick` except that this one includes a `key: undefined` pair for
  3344. * properties that don't exist.
  3345. *
  3346. * @func
  3347. * @memberOf R
  3348. * @since v0.1.0
  3349. * @category Object
  3350. * @sig [k] -> {k: v} -> {k: v}
  3351. * @param {Array} names an array of String property names to copy onto a new object
  3352. * @param {Object} obj The object to copy from
  3353. * @return {Object} A new object with only properties from `names` on it.
  3354. * @see R.pick
  3355. * @example
  3356. *
  3357. * R.pickAll(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
  3358. * R.pickAll(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, e: undefined, f: undefined}
  3359. */
  3360. var pickAll = _curry2(function pickAll(names, obj) {
  3361. var result = {};
  3362. var idx = 0;
  3363. var len = names.length;
  3364. while (idx < len) {
  3365. var name = names[idx];
  3366. result[name] = obj[name];
  3367. idx += 1;
  3368. }
  3369. return result;
  3370. });
  3371. /**
  3372. * Returns a partial copy of an object containing only the keys that satisfy
  3373. * the supplied predicate.
  3374. *
  3375. * @func
  3376. * @memberOf R
  3377. * @since v0.8.0
  3378. * @category Object
  3379. * @sig (v, k -> Boolean) -> {k: v} -> {k: v}
  3380. * @param {Function} pred A predicate to determine whether or not a key
  3381. * should be included on the output object.
  3382. * @param {Object} obj The object to copy from
  3383. * @return {Object} A new object with only properties that satisfy `pred`
  3384. * on it.
  3385. * @see R.pick, R.filter
  3386. * @example
  3387. *
  3388. * var isUpperCase = (val, key) => key.toUpperCase() === key;
  3389. * R.pickBy(isUpperCase, {a: 1, b: 2, A: 3, B: 4}); //=> {A: 3, B: 4}
  3390. */
  3391. var pickBy = _curry2(function pickBy(test, obj) {
  3392. var result = {};
  3393. for (var prop in obj) {
  3394. if (test(obj[prop], prop, obj)) {
  3395. result[prop] = obj[prop];
  3396. }
  3397. }
  3398. return result;
  3399. });
  3400. /**
  3401. * Returns a new list with the given element at the front, followed by the
  3402. * contents of the list.
  3403. *
  3404. * @func
  3405. * @memberOf R
  3406. * @since v0.1.0
  3407. * @category List
  3408. * @sig a -> [a] -> [a]
  3409. * @param {*} el The item to add to the head of the output list.
  3410. * @param {Array} list The array to add to the tail of the output list.
  3411. * @return {Array} A new array.
  3412. * @see R.append
  3413. * @example
  3414. *
  3415. * R.prepend('fee', ['fi', 'fo', 'fum']); //=> ['fee', 'fi', 'fo', 'fum']
  3416. */
  3417. var prepend = _curry2(function prepend(el, list) {
  3418. return _concat([el], list);
  3419. });
  3420. /**
  3421. * Returns a function that when supplied an object returns the indicated
  3422. * property of that object, if it exists.
  3423. *
  3424. * @func
  3425. * @memberOf R
  3426. * @since v0.1.0
  3427. * @category Object
  3428. * @sig s -> {s: a} -> a | Undefined
  3429. * @param {String} p The property name
  3430. * @param {Object} obj The object to query
  3431. * @return {*} The value at `obj.p`.
  3432. * @see R.path
  3433. * @example
  3434. *
  3435. * R.prop('x', {x: 100}); //=> 100
  3436. * R.prop('x', {}); //=> undefined
  3437. */
  3438. var prop = _curry2(function prop(p, obj) {
  3439. return obj[p];
  3440. });
  3441. /**
  3442. * Returns `true` if the specified object property is of the given type;
  3443. * `false` otherwise.
  3444. *
  3445. * @func
  3446. * @memberOf R
  3447. * @since v0.16.0
  3448. * @category Type
  3449. * @sig Type -> String -> Object -> Boolean
  3450. * @param {Function} type
  3451. * @param {String} name
  3452. * @param {*} obj
  3453. * @return {Boolean}
  3454. * @see R.is, R.propSatisfies
  3455. * @example
  3456. *
  3457. * R.propIs(Number, 'x', {x: 1, y: 2}); //=> true
  3458. * R.propIs(Number, 'x', {x: 'foo'}); //=> false
  3459. * R.propIs(Number, 'x', {}); //=> false
  3460. */
  3461. var propIs = _curry3(function propIs(type, name, obj) {
  3462. return is(type, obj[name]);
  3463. });
  3464. /**
  3465. * If the given, non-null object has an own property with the specified name,
  3466. * returns the value of that property. Otherwise returns the provided default
  3467. * value.
  3468. *
  3469. * @func
  3470. * @memberOf R
  3471. * @since v0.6.0
  3472. * @category Object
  3473. * @sig a -> String -> Object -> a
  3474. * @param {*} val The default value.
  3475. * @param {String} p The name of the property to return.
  3476. * @param {Object} obj The object to query.
  3477. * @return {*} The value of given property of the supplied object or the default value.
  3478. * @example
  3479. *
  3480. * var alice = {
  3481. * name: 'ALICE',
  3482. * age: 101
  3483. * };
  3484. * var favorite = R.prop('favoriteLibrary');
  3485. * var favoriteWithDefault = R.propOr('Ramda', 'favoriteLibrary');
  3486. *
  3487. * favorite(alice); //=> undefined
  3488. * favoriteWithDefault(alice); //=> 'Ramda'
  3489. */
  3490. var propOr = _curry3(function propOr(val, p, obj) {
  3491. return obj != null && _has(p, obj) ? obj[p] : val;
  3492. });
  3493. /**
  3494. * Returns `true` if the specified object property satisfies the given
  3495. * predicate; `false` otherwise.
  3496. *
  3497. * @func
  3498. * @memberOf R
  3499. * @since v0.16.0
  3500. * @category Logic
  3501. * @sig (a -> Boolean) -> String -> {String: a} -> Boolean
  3502. * @param {Function} pred
  3503. * @param {String} name
  3504. * @param {*} obj
  3505. * @return {Boolean}
  3506. * @see R.propEq, R.propIs
  3507. * @example
  3508. *
  3509. * R.propSatisfies(x => x > 0, 'x', {x: 1, y: 2}); //=> true
  3510. */
  3511. var propSatisfies = _curry3(function propSatisfies(pred, name, obj) {
  3512. return pred(obj[name]);
  3513. });
  3514. /**
  3515. * Acts as multiple `prop`: array of keys in, array of values out. Preserves
  3516. * order.
  3517. *
  3518. * @func
  3519. * @memberOf R
  3520. * @since v0.1.0
  3521. * @category Object
  3522. * @sig [k] -> {k: v} -> [v]
  3523. * @param {Array} ps The property names to fetch
  3524. * @param {Object} obj The object to query
  3525. * @return {Array} The corresponding values or partially applied function.
  3526. * @example
  3527. *
  3528. * R.props(['x', 'y'], {x: 1, y: 2}); //=> [1, 2]
  3529. * R.props(['c', 'a', 'b'], {b: 2, a: 1}); //=> [undefined, 1, 2]
  3530. *
  3531. * var fullName = R.compose(R.join(' '), R.props(['first', 'last']));
  3532. * fullName({last: 'Bullet-Tooth', age: 33, first: 'Tony'}); //=> 'Tony Bullet-Tooth'
  3533. */
  3534. var props = _curry2(function props(ps, obj) {
  3535. var len = ps.length;
  3536. var out = [];
  3537. var idx = 0;
  3538. while (idx < len) {
  3539. out[idx] = obj[ps[idx]];
  3540. idx += 1;
  3541. }
  3542. return out;
  3543. });
  3544. /**
  3545. * Returns a list of numbers from `from` (inclusive) to `to` (exclusive).
  3546. *
  3547. * @func
  3548. * @memberOf R
  3549. * @since v0.1.0
  3550. * @category List
  3551. * @sig Number -> Number -> [Number]
  3552. * @param {Number} from The first number in the list.
  3553. * @param {Number} to One more than the last number in the list.
  3554. * @return {Array} The list of numbers in tthe set `[a, b)`.
  3555. * @example
  3556. *
  3557. * R.range(1, 5); //=> [1, 2, 3, 4]
  3558. * R.range(50, 53); //=> [50, 51, 52]
  3559. */
  3560. var range = _curry2(function range(from, to) {
  3561. if (!(_isNumber(from) && _isNumber(to))) {
  3562. throw new TypeError('Both arguments to range must be numbers');
  3563. }
  3564. var result = [];
  3565. var n = from;
  3566. while (n < to) {
  3567. result.push(n);
  3568. n += 1;
  3569. }
  3570. return result;
  3571. });
  3572. /**
  3573. * Returns a single item by iterating through the list, successively calling
  3574. * the iterator function and passing it an accumulator value and the current
  3575. * value from the array, and then passing the result to the next call.
  3576. *
  3577. * Similar to [`reduce`](#reduce), except moves through the input list from the
  3578. * right to the left.
  3579. *
  3580. * The iterator function receives two values: *(value, acc)*, while the arguments'
  3581. * order of `reduce`'s iterator function is *(acc, value)*.
  3582. *
  3583. * Note: `R.reduceRight` does not skip deleted or unassigned indices (sparse
  3584. * arrays), unlike the native `Array.prototype.reduceRight` method. For more details
  3585. * on this behavior, see:
  3586. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight#Description
  3587. *
  3588. * @func
  3589. * @memberOf R
  3590. * @since v0.1.0
  3591. * @category List
  3592. * @sig (a, b -> b) -> b -> [a] -> b
  3593. * @param {Function} fn The iterator function. Receives two values, the current element from the array
  3594. * and the accumulator.
  3595. * @param {*} acc The accumulator value.
  3596. * @param {Array} list The list to iterate over.
  3597. * @return {*} The final, accumulated value.
  3598. * @see R.reduce, R.addIndex
  3599. * @example
  3600. *
  3601. * R.reduceRight(R.subtract, 0, [1, 2, 3, 4]) // => (1 - (2 - (3 - (4 - 0)))) = -2
  3602. * - -2
  3603. * / \ / \
  3604. * 1 - 1 3
  3605. * / \ / \
  3606. * 2 - ==> 2 -1
  3607. * / \ / \
  3608. * 3 - 3 4
  3609. * / \ / \
  3610. * 4 0 4 0
  3611. *
  3612. * @symb R.reduceRight(f, a, [b, c, d]) = f(b, f(c, f(d, a)))
  3613. */
  3614. var reduceRight = _curry3(function reduceRight(fn, acc, list) {
  3615. var idx = list.length - 1;
  3616. while (idx >= 0) {
  3617. acc = fn(list[idx], acc);
  3618. idx -= 1;
  3619. }
  3620. return acc;
  3621. });
  3622. /**
  3623. * Returns a value wrapped to indicate that it is the final value of the reduce
  3624. * and transduce functions. The returned value should be considered a black
  3625. * box: the internal structure is not guaranteed to be stable.
  3626. *
  3627. * Note: this optimization is unavailable to functions not explicitly listed
  3628. * above. For instance, it is not currently supported by
  3629. * [`reduceRight`](#reduceRight).
  3630. *
  3631. * @func
  3632. * @memberOf R
  3633. * @since v0.15.0
  3634. * @category List
  3635. * @sig a -> *
  3636. * @param {*} x The final value of the reduce.
  3637. * @return {*} The wrapped value.
  3638. * @see R.reduce, R.transduce
  3639. * @example
  3640. *
  3641. * R.reduce(
  3642. * (acc, item) => item > 3 ? R.reduced(acc) : acc.concat(item),
  3643. * [],
  3644. * [1, 2, 3, 4, 5]) // [1, 2, 3]
  3645. */
  3646. var reduced = _curry1(_reduced);
  3647. /**
  3648. * Removes the sub-list of `list` starting at index `start` and containing
  3649. * `count` elements. _Note that this is not destructive_: it returns a copy of
  3650. * the list with the changes.
  3651. * <small>No lists have been harmed in the application of this function.</small>
  3652. *
  3653. * @func
  3654. * @memberOf R
  3655. * @since v0.2.2
  3656. * @category List
  3657. * @sig Number -> Number -> [a] -> [a]
  3658. * @param {Number} start The position to start removing elements
  3659. * @param {Number} count The number of elements to remove
  3660. * @param {Array} list The list to remove from
  3661. * @return {Array} A new Array with `count` elements from `start` removed.
  3662. * @example
  3663. *
  3664. * R.remove(2, 3, [1,2,3,4,5,6,7,8]); //=> [1,2,6,7,8]
  3665. */
  3666. var remove = _curry3(function remove(start, count, list) {
  3667. var result = Array.prototype.slice.call(list, 0);
  3668. result.splice(start, count);
  3669. return result;
  3670. });
  3671. /**
  3672. * Replace a substring or regex match in a string with a replacement.
  3673. *
  3674. * @func
  3675. * @memberOf R
  3676. * @since v0.7.0
  3677. * @category String
  3678. * @sig RegExp|String -> String -> String -> String
  3679. * @param {RegExp|String} pattern A regular expression or a substring to match.
  3680. * @param {String} replacement The string to replace the matches with.
  3681. * @param {String} str The String to do the search and replacement in.
  3682. * @return {String} The result.
  3683. * @example
  3684. *
  3685. * R.replace('foo', 'bar', 'foo foo foo'); //=> 'bar foo foo'
  3686. * R.replace(/foo/, 'bar', 'foo foo foo'); //=> 'bar foo foo'
  3687. *
  3688. * // Use the "g" (global) flag to replace all occurrences:
  3689. * R.replace(/foo/g, 'bar', 'foo foo foo'); //=> 'bar bar bar'
  3690. */
  3691. var replace = _curry3(function replace(regex, replacement, str) {
  3692. return str.replace(regex, replacement);
  3693. });
  3694. /**
  3695. * Returns a new list or string with the elements or characters in reverse
  3696. * order.
  3697. *
  3698. * @func
  3699. * @memberOf R
  3700. * @since v0.1.0
  3701. * @category List
  3702. * @sig [a] -> [a]
  3703. * @sig String -> String
  3704. * @param {Array|String} list
  3705. * @return {Array|String}
  3706. * @example
  3707. *
  3708. * R.reverse([1, 2, 3]); //=> [3, 2, 1]
  3709. * R.reverse([1, 2]); //=> [2, 1]
  3710. * R.reverse([1]); //=> [1]
  3711. * R.reverse([]); //=> []
  3712. *
  3713. * R.reverse('abc'); //=> 'cba'
  3714. * R.reverse('ab'); //=> 'ba'
  3715. * R.reverse('a'); //=> 'a'
  3716. * R.reverse(''); //=> ''
  3717. */
  3718. var reverse = _curry1(function reverse(list) {
  3719. return _isString(list) ? list.split('').reverse().join('') : Array.prototype.slice.call(list, 0).reverse();
  3720. });
  3721. /**
  3722. * Scan is similar to [`reduce`](#reduce), but returns a list of successively
  3723. * reduced values from the left
  3724. *
  3725. * @func
  3726. * @memberOf R
  3727. * @since v0.10.0
  3728. * @category List
  3729. * @sig (a,b -> a) -> a -> [b] -> [a]
  3730. * @param {Function} fn The iterator function. Receives two values, the accumulator and the
  3731. * current element from the array
  3732. * @param {*} acc The accumulator value.
  3733. * @param {Array} list The list to iterate over.
  3734. * @return {Array} A list of all intermediately reduced values.
  3735. * @see R.reduce
  3736. * @example
  3737. *
  3738. * var numbers = [1, 2, 3, 4];
  3739. * var factorials = R.scan(R.multiply, 1, numbers); //=> [1, 1, 2, 6, 24]
  3740. * @symb R.scan(f, a, [b, c]) = [a, f(a, b), f(f(a, b), c)]
  3741. */
  3742. var scan = _curry3(function scan(fn, acc, list) {
  3743. var idx = 0;
  3744. var len = list.length;
  3745. var result = [acc];
  3746. while (idx < len) {
  3747. acc = fn(acc, list[idx]);
  3748. result[idx + 1] = acc;
  3749. idx += 1;
  3750. }
  3751. return result;
  3752. });
  3753. /**
  3754. * Returns the result of "setting" the portion of the given data structure
  3755. * focused by the given lens to the given value.
  3756. *
  3757. * @func
  3758. * @memberOf R
  3759. * @since v0.16.0
  3760. * @category Object
  3761. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  3762. * @sig Lens s a -> a -> s -> s
  3763. * @param {Lens} lens
  3764. * @param {*} v
  3765. * @param {*} x
  3766. * @return {*}
  3767. * @see R.prop, R.lensIndex, R.lensProp
  3768. * @example
  3769. *
  3770. * var xLens = R.lensProp('x');
  3771. *
  3772. * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
  3773. * R.set(xLens, 8, {x: 1, y: 2}); //=> {x: 8, y: 2}
  3774. */
  3775. var set = _curry3(function set(lens, v, x) {
  3776. return over(lens, always(v), x);
  3777. });
  3778. /**
  3779. * Returns the elements of the given list or string (or object with a `slice`
  3780. * method) from `fromIndex` (inclusive) to `toIndex` (exclusive).
  3781. *
  3782. * Dispatches to the `slice` method of the third argument, if present.
  3783. *
  3784. * @func
  3785. * @memberOf R
  3786. * @since v0.1.4
  3787. * @category List
  3788. * @sig Number -> Number -> [a] -> [a]
  3789. * @sig Number -> Number -> String -> String
  3790. * @param {Number} fromIndex The start index (inclusive).
  3791. * @param {Number} toIndex The end index (exclusive).
  3792. * @param {*} list
  3793. * @return {*}
  3794. * @example
  3795. *
  3796. * R.slice(1, 3, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
  3797. * R.slice(1, Infinity, ['a', 'b', 'c', 'd']); //=> ['b', 'c', 'd']
  3798. * R.slice(0, -1, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c']
  3799. * R.slice(-3, -1, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
  3800. * R.slice(0, 3, 'ramda'); //=> 'ram'
  3801. */
  3802. var slice = _curry3(_checkForMethod('slice', function slice(fromIndex, toIndex, list) {
  3803. return Array.prototype.slice.call(list, fromIndex, toIndex);
  3804. }));
  3805. /**
  3806. * Returns a copy of the list, sorted according to the comparator function,
  3807. * which should accept two values at a time and return a negative number if the
  3808. * first value is smaller, a positive number if it's larger, and zero if they
  3809. * are equal. Please note that this is a **copy** of the list. It does not
  3810. * modify the original.
  3811. *
  3812. * @func
  3813. * @memberOf R
  3814. * @since v0.1.0
  3815. * @category List
  3816. * @sig (a,a -> Number) -> [a] -> [a]
  3817. * @param {Function} comparator A sorting function :: a -> b -> Int
  3818. * @param {Array} list The list to sort
  3819. * @return {Array} a new array with its elements sorted by the comparator function.
  3820. * @example
  3821. *
  3822. * var diff = function(a, b) { return a - b; };
  3823. * R.sort(diff, [4,2,7,5]); //=> [2, 4, 5, 7]
  3824. */
  3825. var sort = _curry2(function sort(comparator, list) {
  3826. return Array.prototype.slice.call(list, 0).sort(comparator);
  3827. });
  3828. /**
  3829. * Sorts the list according to the supplied function.
  3830. *
  3831. * @func
  3832. * @memberOf R
  3833. * @since v0.1.0
  3834. * @category Relation
  3835. * @sig Ord b => (a -> b) -> [a] -> [a]
  3836. * @param {Function} fn
  3837. * @param {Array} list The list to sort.
  3838. * @return {Array} A new list sorted by the keys generated by `fn`.
  3839. * @example
  3840. *
  3841. * var sortByFirstItem = R.sortBy(R.prop(0));
  3842. * var sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name')));
  3843. * var pairs = [[-1, 1], [-2, 2], [-3, 3]];
  3844. * sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
  3845. * var alice = {
  3846. * name: 'ALICE',
  3847. * age: 101
  3848. * };
  3849. * var bob = {
  3850. * name: 'Bob',
  3851. * age: -10
  3852. * };
  3853. * var clara = {
  3854. * name: 'clara',
  3855. * age: 314.159
  3856. * };
  3857. * var people = [clara, bob, alice];
  3858. * sortByNameCaseInsensitive(people); //=> [alice, bob, clara]
  3859. */
  3860. var sortBy = _curry2(function sortBy(fn, list) {
  3861. return Array.prototype.slice.call(list, 0).sort(function (a, b) {
  3862. var aa = fn(a);
  3863. var bb = fn(b);
  3864. return aa < bb ? -1 : aa > bb ? 1 : 0;
  3865. });
  3866. });
  3867. /**
  3868. * Sorts a list according to a list of comparators.
  3869. *
  3870. * @func
  3871. * @memberOf R
  3872. * @since v0.23.0
  3873. * @category Relation
  3874. * @sig [a -> a -> Number] -> [a] -> [a]
  3875. * @param {Array} functions A list of comparator functions.
  3876. * @param {Array} list The list to sort.
  3877. * @return {Array} A new list sorted according to the comarator functions.
  3878. * @example
  3879. *
  3880. * var alice = {
  3881. * name: 'alice',
  3882. * age: 40
  3883. * };
  3884. * var bob = {
  3885. * name: 'bob',
  3886. * age: 30
  3887. * };
  3888. * var clara = {
  3889. * name: 'clara',
  3890. * age: 40
  3891. * };
  3892. * var people = [clara, bob, alice];
  3893. * var ageNameSort = R.sortWith([
  3894. * R.descend(R.prop('age')),
  3895. * R.ascend(R.prop('name'))
  3896. * ]);
  3897. * ageNameSort(people); //=> [alice, clara, bob]
  3898. */
  3899. var sortWith = _curry2(function sortWith(fns, list) {
  3900. return Array.prototype.slice.call(list, 0).sort(function (a, b) {
  3901. var result = 0;
  3902. var i = 0;
  3903. while (result === 0 && i < fns.length) {
  3904. result = fns[i](a, b);
  3905. i += 1;
  3906. }
  3907. return result;
  3908. });
  3909. });
  3910. /**
  3911. * Splits a given list or string at a given index.
  3912. *
  3913. * @func
  3914. * @memberOf R
  3915. * @since v0.19.0
  3916. * @category List
  3917. * @sig Number -> [a] -> [[a], [a]]
  3918. * @sig Number -> String -> [String, String]
  3919. * @param {Number} index The index where the array/string is split.
  3920. * @param {Array|String} array The array/string to be split.
  3921. * @return {Array}
  3922. * @example
  3923. *
  3924. * R.splitAt(1, [1, 2, 3]); //=> [[1], [2, 3]]
  3925. * R.splitAt(5, 'hello world'); //=> ['hello', ' world']
  3926. * R.splitAt(-1, 'foobar'); //=> ['fooba', 'r']
  3927. */
  3928. var splitAt = _curry2(function splitAt(index, array) {
  3929. return [
  3930. slice(0, index, array),
  3931. slice(index, length(array), array)
  3932. ];
  3933. });
  3934. /**
  3935. * Splits a collection into slices of the specified length.
  3936. *
  3937. * @func
  3938. * @memberOf R
  3939. * @since v0.16.0
  3940. * @category List
  3941. * @sig Number -> [a] -> [[a]]
  3942. * @sig Number -> String -> [String]
  3943. * @param {Number} n
  3944. * @param {Array} list
  3945. * @return {Array}
  3946. * @example
  3947. *
  3948. * R.splitEvery(3, [1, 2, 3, 4, 5, 6, 7]); //=> [[1, 2, 3], [4, 5, 6], [7]]
  3949. * R.splitEvery(3, 'foobarbaz'); //=> ['foo', 'bar', 'baz']
  3950. */
  3951. var splitEvery = _curry2(function splitEvery(n, list) {
  3952. if (n <= 0) {
  3953. throw new Error('First argument to splitEvery must be a positive integer');
  3954. }
  3955. var result = [];
  3956. var idx = 0;
  3957. while (idx < list.length) {
  3958. result.push(slice(idx, idx += n, list));
  3959. }
  3960. return result;
  3961. });
  3962. /**
  3963. * Takes a list and a predicate and returns a pair of lists with the following properties:
  3964. *
  3965. * - the result of concatenating the two output lists is equivalent to the input list;
  3966. * - none of the elements of the first output list satisfies the predicate; and
  3967. * - if the second output list is non-empty, its first element satisfies the predicate.
  3968. *
  3969. * @func
  3970. * @memberOf R
  3971. * @since v0.19.0
  3972. * @category List
  3973. * @sig (a -> Boolean) -> [a] -> [[a], [a]]
  3974. * @param {Function} pred The predicate that determines where the array is split.
  3975. * @param {Array} list The array to be split.
  3976. * @return {Array}
  3977. * @example
  3978. *
  3979. * R.splitWhen(R.equals(2), [1, 2, 3, 1, 2, 3]); //=> [[1], [2, 3, 1, 2, 3]]
  3980. */
  3981. var splitWhen = _curry2(function splitWhen(pred, list) {
  3982. var idx = 0;
  3983. var len = list.length;
  3984. var prefix = [];
  3985. while (idx < len && !pred(list[idx])) {
  3986. prefix.push(list[idx]);
  3987. idx += 1;
  3988. }
  3989. return [
  3990. prefix,
  3991. Array.prototype.slice.call(list, idx)
  3992. ];
  3993. });
  3994. /**
  3995. * Subtracts its second argument from its first argument.
  3996. *
  3997. * @func
  3998. * @memberOf R
  3999. * @since v0.1.0
  4000. * @category Math
  4001. * @sig Number -> Number -> Number
  4002. * @param {Number} a The first value.
  4003. * @param {Number} b The second value.
  4004. * @return {Number} The result of `a - b`.
  4005. * @see R.add
  4006. * @example
  4007. *
  4008. * R.subtract(10, 8); //=> 2
  4009. *
  4010. * var minus5 = R.subtract(R.__, 5);
  4011. * minus5(17); //=> 12
  4012. *
  4013. * var complementaryAngle = R.subtract(90);
  4014. * complementaryAngle(30); //=> 60
  4015. * complementaryAngle(72); //=> 18
  4016. */
  4017. var subtract = _curry2(function subtract(a, b) {
  4018. return Number(a) - Number(b);
  4019. });
  4020. /**
  4021. * Returns all but the first element of the given list or string (or object
  4022. * with a `tail` method).
  4023. *
  4024. * Dispatches to the `slice` method of the first argument, if present.
  4025. *
  4026. * @func
  4027. * @memberOf R
  4028. * @since v0.1.0
  4029. * @category List
  4030. * @sig [a] -> [a]
  4031. * @sig String -> String
  4032. * @param {*} list
  4033. * @return {*}
  4034. * @see R.head, R.init, R.last
  4035. * @example
  4036. *
  4037. * R.tail([1, 2, 3]); //=> [2, 3]
  4038. * R.tail([1, 2]); //=> [2]
  4039. * R.tail([1]); //=> []
  4040. * R.tail([]); //=> []
  4041. *
  4042. * R.tail('abc'); //=> 'bc'
  4043. * R.tail('ab'); //=> 'b'
  4044. * R.tail('a'); //=> ''
  4045. * R.tail(''); //=> ''
  4046. */
  4047. var tail = _curry1(_checkForMethod('tail', slice(1, Infinity)));
  4048. /**
  4049. * Returns the first `n` elements of the given list, string, or
  4050. * transducer/transformer (or object with a `take` method).
  4051. *
  4052. * Dispatches to the `take` method of the second argument, if present.
  4053. *
  4054. * @func
  4055. * @memberOf R
  4056. * @since v0.1.0
  4057. * @category List
  4058. * @sig Number -> [a] -> [a]
  4059. * @sig Number -> String -> String
  4060. * @param {Number} n
  4061. * @param {*} list
  4062. * @return {*}
  4063. * @see R.drop
  4064. * @example
  4065. *
  4066. * R.take(1, ['foo', 'bar', 'baz']); //=> ['foo']
  4067. * R.take(2, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
  4068. * R.take(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  4069. * R.take(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  4070. * R.take(3, 'ramda'); //=> 'ram'
  4071. *
  4072. * var personnel = [
  4073. * 'Dave Brubeck',
  4074. * 'Paul Desmond',
  4075. * 'Eugene Wright',
  4076. * 'Joe Morello',
  4077. * 'Gerry Mulligan',
  4078. * 'Bob Bates',
  4079. * 'Joe Dodge',
  4080. * 'Ron Crotty'
  4081. * ];
  4082. *
  4083. * var takeFive = R.take(5);
  4084. * takeFive(personnel);
  4085. * //=> ['Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan']
  4086. * @symb R.take(-1, [a, b]) = [a, b]
  4087. * @symb R.take(0, [a, b]) = []
  4088. * @symb R.take(1, [a, b]) = [a]
  4089. * @symb R.take(2, [a, b]) = [a, b]
  4090. */
  4091. var take = _curry2(_dispatchable(['take'], _xtake, function take(n, xs) {
  4092. return slice(0, n < 0 ? Infinity : n, xs);
  4093. }));
  4094. /**
  4095. * Returns a new list containing the last `n` elements of a given list, passing
  4096. * each value to the supplied predicate function, and terminating when the
  4097. * predicate function returns `false`. Excludes the element that caused the
  4098. * predicate function to fail. The predicate function is passed one argument:
  4099. * *(value)*.
  4100. *
  4101. * @func
  4102. * @memberOf R
  4103. * @since v0.16.0
  4104. * @category List
  4105. * @sig (a -> Boolean) -> [a] -> [a]
  4106. * @param {Function} fn The function called per iteration.
  4107. * @param {Array} list The collection to iterate over.
  4108. * @return {Array} A new array.
  4109. * @see R.dropLastWhile, R.addIndex
  4110. * @example
  4111. *
  4112. * var isNotOne = x => x !== 1;
  4113. *
  4114. * R.takeLastWhile(isNotOne, [1, 2, 3, 4]); //=> [2, 3, 4]
  4115. */
  4116. var takeLastWhile = _curry2(function takeLastWhile(fn, list) {
  4117. var idx = list.length - 1;
  4118. while (idx >= 0 && fn(list[idx])) {
  4119. idx -= 1;
  4120. }
  4121. return Array.prototype.slice.call(list, idx + 1);
  4122. });
  4123. /**
  4124. * Returns a new list containing the first `n` elements of a given list,
  4125. * passing each value to the supplied predicate function, and terminating when
  4126. * the predicate function returns `false`. Excludes the element that caused the
  4127. * predicate function to fail. The predicate function is passed one argument:
  4128. * *(value)*.
  4129. *
  4130. * Dispatches to the `takeWhile` method of the second argument, if present.
  4131. *
  4132. * Acts as a transducer if a transformer is given in list position.
  4133. *
  4134. * @func
  4135. * @memberOf R
  4136. * @since v0.1.0
  4137. * @category List
  4138. * @sig (a -> Boolean) -> [a] -> [a]
  4139. * @param {Function} fn The function called per iteration.
  4140. * @param {Array} list The collection to iterate over.
  4141. * @return {Array} A new array.
  4142. * @see R.dropWhile, R.transduce, R.addIndex
  4143. * @example
  4144. *
  4145. * var isNotFour = x => x !== 4;
  4146. *
  4147. * R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3]
  4148. */
  4149. var takeWhile = _curry2(_dispatchable(['takeWhile'], _xtakeWhile, function takeWhile(fn, list) {
  4150. var idx = 0;
  4151. var len = list.length;
  4152. while (idx < len && fn(list[idx])) {
  4153. idx += 1;
  4154. }
  4155. return Array.prototype.slice.call(list, 0, idx);
  4156. }));
  4157. /**
  4158. * Runs the given function with the supplied object, then returns the object.
  4159. *
  4160. * @func
  4161. * @memberOf R
  4162. * @since v0.1.0
  4163. * @category Function
  4164. * @sig (a -> *) -> a -> a
  4165. * @param {Function} fn The function to call with `x`. The return value of `fn` will be thrown away.
  4166. * @param {*} x
  4167. * @return {*} `x`.
  4168. * @example
  4169. *
  4170. * var sayX = x => console.log('x is ' + x);
  4171. * R.tap(sayX, 100); //=> 100
  4172. * // logs 'x is 100'
  4173. * @symb R.tap(f, a) = a
  4174. */
  4175. var tap = _curry2(function tap(fn, x) {
  4176. fn(x);
  4177. return x;
  4178. });
  4179. /**
  4180. * Calls an input function `n` times, returning an array containing the results
  4181. * of those function calls.
  4182. *
  4183. * `fn` is passed one argument: The current value of `n`, which begins at `0`
  4184. * and is gradually incremented to `n - 1`.
  4185. *
  4186. * @func
  4187. * @memberOf R
  4188. * @since v0.2.3
  4189. * @category List
  4190. * @sig (Number -> a) -> Number -> [a]
  4191. * @param {Function} fn The function to invoke. Passed one argument, the current value of `n`.
  4192. * @param {Number} n A value between `0` and `n - 1`. Increments after each function call.
  4193. * @return {Array} An array containing the return values of all calls to `fn`.
  4194. * @see R.repeat
  4195. * @example
  4196. *
  4197. * R.times(R.identity, 5); //=> [0, 1, 2, 3, 4]
  4198. * @symb R.times(f, 0) = []
  4199. * @symb R.times(f, 1) = [f(0)]
  4200. * @symb R.times(f, 2) = [f(0), f(1)]
  4201. */
  4202. var times = _curry2(function times(fn, n) {
  4203. var len = Number(n);
  4204. var idx = 0;
  4205. var list;
  4206. if (len < 0 || isNaN(len)) {
  4207. throw new RangeError('n must be a non-negative number');
  4208. }
  4209. list = new Array(len);
  4210. while (idx < len) {
  4211. list[idx] = fn(idx);
  4212. idx += 1;
  4213. }
  4214. return list;
  4215. });
  4216. /**
  4217. * Converts an object into an array of key, value arrays. Only the object's
  4218. * own properties are used.
  4219. * Note that the order of the output array is not guaranteed to be consistent
  4220. * across different JS platforms.
  4221. *
  4222. * @func
  4223. * @memberOf R
  4224. * @since v0.4.0
  4225. * @category Object
  4226. * @sig {String: *} -> [[String,*]]
  4227. * @param {Object} obj The object to extract from
  4228. * @return {Array} An array of key, value arrays from the object's own properties.
  4229. * @see R.fromPairs
  4230. * @example
  4231. *
  4232. * R.toPairs({a: 1, b: 2, c: 3}); //=> [['a', 1], ['b', 2], ['c', 3]]
  4233. */
  4234. var toPairs = _curry1(function toPairs(obj) {
  4235. var pairs = [];
  4236. for (var prop in obj) {
  4237. if (_has(prop, obj)) {
  4238. pairs[pairs.length] = [
  4239. prop,
  4240. obj[prop]
  4241. ];
  4242. }
  4243. }
  4244. return pairs;
  4245. });
  4246. /**
  4247. * Converts an object into an array of key, value arrays. The object's own
  4248. * properties and prototype properties are used. Note that the order of the
  4249. * output array is not guaranteed to be consistent across different JS
  4250. * platforms.
  4251. *
  4252. * @func
  4253. * @memberOf R
  4254. * @since v0.4.0
  4255. * @category Object
  4256. * @sig {String: *} -> [[String,*]]
  4257. * @param {Object} obj The object to extract from
  4258. * @return {Array} An array of key, value arrays from the object's own
  4259. * and prototype properties.
  4260. * @example
  4261. *
  4262. * var F = function() { this.x = 'X'; };
  4263. * F.prototype.y = 'Y';
  4264. * var f = new F();
  4265. * R.toPairsIn(f); //=> [['x','X'], ['y','Y']]
  4266. */
  4267. var toPairsIn = _curry1(function toPairsIn(obj) {
  4268. var pairs = [];
  4269. for (var prop in obj) {
  4270. pairs[pairs.length] = [
  4271. prop,
  4272. obj[prop]
  4273. ];
  4274. }
  4275. return pairs;
  4276. });
  4277. /**
  4278. * Transposes the rows and columns of a 2D list.
  4279. * When passed a list of `n` lists of length `x`,
  4280. * returns a list of `x` lists of length `n`.
  4281. *
  4282. *
  4283. * @func
  4284. * @memberOf R
  4285. * @since v0.19.0
  4286. * @category List
  4287. * @sig [[a]] -> [[a]]
  4288. * @param {Array} list A 2D list
  4289. * @return {Array} A 2D list
  4290. * @example
  4291. *
  4292. * R.transpose([[1, 'a'], [2, 'b'], [3, 'c']]) //=> [[1, 2, 3], ['a', 'b', 'c']]
  4293. * R.transpose([[1, 2, 3], ['a', 'b', 'c']]) //=> [[1, 'a'], [2, 'b'], [3, 'c']]
  4294. *
  4295. * // If some of the rows are shorter than the following rows, their elements are skipped:
  4296. * R.transpose([[10, 11], [20], [], [30, 31, 32]]) //=> [[10, 20, 30], [11, 31], [32]]
  4297. * @symb R.transpose([[a], [b], [c]]) = [a, b, c]
  4298. * @symb R.transpose([[a, b], [c, d]]) = [[a, c], [b, d]]
  4299. * @symb R.transpose([[a, b], [c]]) = [[a, c], [b]]
  4300. */
  4301. var transpose = _curry1(function transpose(outerlist) {
  4302. var i = 0;
  4303. var result = [];
  4304. while (i < outerlist.length) {
  4305. var innerlist = outerlist[i];
  4306. var j = 0;
  4307. while (j < innerlist.length) {
  4308. if (typeof result[j] === 'undefined') {
  4309. result[j] = [];
  4310. }
  4311. result[j].push(innerlist[j]);
  4312. j += 1;
  4313. }
  4314. i += 1;
  4315. }
  4316. return result;
  4317. });
  4318. /**
  4319. * Removes (strips) whitespace from both ends of the string.
  4320. *
  4321. * @func
  4322. * @memberOf R
  4323. * @since v0.6.0
  4324. * @category String
  4325. * @sig String -> String
  4326. * @param {String} str The string to trim.
  4327. * @return {String} Trimmed version of `str`.
  4328. * @example
  4329. *
  4330. * R.trim(' xyz '); //=> 'xyz'
  4331. * R.map(R.trim, R.split(',', 'x, y, z')); //=> ['x', 'y', 'z']
  4332. */
  4333. var trim = function () {
  4334. var ws = '\t\n\x0B\f\r \xA0\u1680\u180E\u2000\u2001\u2002\u2003' + '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028' + '\u2029\uFEFF';
  4335. var zeroWidth = '\u200B';
  4336. var hasProtoTrim = typeof String.prototype.trim === 'function';
  4337. if (!hasProtoTrim || (ws.trim() || !zeroWidth.trim())) {
  4338. return _curry1(function trim(str) {
  4339. var beginRx = new RegExp('^[' + ws + '][' + ws + ']*');
  4340. var endRx = new RegExp('[' + ws + '][' + ws + ']*$');
  4341. return str.replace(beginRx, '').replace(endRx, '');
  4342. });
  4343. } else {
  4344. return _curry1(function trim(str) {
  4345. return str.trim();
  4346. });
  4347. }
  4348. }();
  4349. /**
  4350. * `tryCatch` takes two functions, a `tryer` and a `catcher`. The returned
  4351. * function evaluates the `tryer`; if it does not throw, it simply returns the
  4352. * result. If the `tryer` *does* throw, the returned function evaluates the
  4353. * `catcher` function and returns its result. Note that for effective
  4354. * composition with this function, both the `tryer` and `catcher` functions
  4355. * must return the same type of results.
  4356. *
  4357. * @func
  4358. * @memberOf R
  4359. * @since v0.20.0
  4360. * @category Function
  4361. * @sig (...x -> a) -> ((e, ...x) -> a) -> (...x -> a)
  4362. * @param {Function} tryer The function that may throw.
  4363. * @param {Function} catcher The function that will be evaluated if `tryer` throws.
  4364. * @return {Function} A new function that will catch exceptions and send then to the catcher.
  4365. * @example
  4366. *
  4367. * R.tryCatch(R.prop('x'), R.F)({x: true}); //=> true
  4368. * R.tryCatch(R.prop('x'), R.F)(null); //=> false
  4369. */
  4370. var tryCatch = _curry2(function _tryCatch(tryer, catcher) {
  4371. return _arity(tryer.length, function () {
  4372. try {
  4373. return tryer.apply(this, arguments);
  4374. } catch (e) {
  4375. return catcher.apply(this, _concat([e], arguments));
  4376. }
  4377. });
  4378. });
  4379. /**
  4380. * Gives a single-word string description of the (native) type of a value,
  4381. * returning such answers as 'Object', 'Number', 'Array', or 'Null'. Does not
  4382. * attempt to distinguish user Object types any further, reporting them all as
  4383. * 'Object'.
  4384. *
  4385. * @func
  4386. * @memberOf R
  4387. * @since v0.8.0
  4388. * @category Type
  4389. * @sig (* -> {*}) -> String
  4390. * @param {*} val The value to test
  4391. * @return {String}
  4392. * @example
  4393. *
  4394. * R.type({}); //=> "Object"
  4395. * R.type(1); //=> "Number"
  4396. * R.type(false); //=> "Boolean"
  4397. * R.type('s'); //=> "String"
  4398. * R.type(null); //=> "Null"
  4399. * R.type([]); //=> "Array"
  4400. * R.type(/[A-z]/); //=> "RegExp"
  4401. * R.type(() => {}); //=> "Function"
  4402. */
  4403. var type = _curry1(function type(val) {
  4404. return val === null ? 'Null' : val === undefined ? 'Undefined' : Object.prototype.toString.call(val).slice(8, -1);
  4405. });
  4406. /**
  4407. * Takes a function `fn`, which takes a single array argument, and returns a
  4408. * function which:
  4409. *
  4410. * - takes any number of positional arguments;
  4411. * - passes these arguments to `fn` as an array; and
  4412. * - returns the result.
  4413. *
  4414. * In other words, `R.unapply` derives a variadic function from a function which
  4415. * takes an array. `R.unapply` is the inverse of [`R.apply`](#apply).
  4416. *
  4417. * @func
  4418. * @memberOf R
  4419. * @since v0.8.0
  4420. * @category Function
  4421. * @sig ([*...] -> a) -> (*... -> a)
  4422. * @param {Function} fn
  4423. * @return {Function}
  4424. * @see R.apply
  4425. * @example
  4426. *
  4427. * R.unapply(JSON.stringify)(1, 2, 3); //=> '[1,2,3]'
  4428. * @symb R.unapply(f)(a, b) = f([a, b])
  4429. */
  4430. var unapply = _curry1(function unapply(fn) {
  4431. return function () {
  4432. return fn(Array.prototype.slice.call(arguments, 0));
  4433. };
  4434. });
  4435. /**
  4436. * Wraps a function of any arity (including nullary) in a function that accepts
  4437. * exactly 1 parameter. Any extraneous parameters will not be passed to the
  4438. * supplied function.
  4439. *
  4440. * @func
  4441. * @memberOf R
  4442. * @since v0.2.0
  4443. * @category Function
  4444. * @sig (* -> b) -> (a -> b)
  4445. * @param {Function} fn The function to wrap.
  4446. * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
  4447. * arity 1.
  4448. * @see R.binary, R.nAry
  4449. * @example
  4450. *
  4451. * var takesTwoArgs = function(a, b) {
  4452. * return [a, b];
  4453. * };
  4454. * takesTwoArgs.length; //=> 2
  4455. * takesTwoArgs(1, 2); //=> [1, 2]
  4456. *
  4457. * var takesOneArg = R.unary(takesTwoArgs);
  4458. * takesOneArg.length; //=> 1
  4459. * // Only 1 argument is passed to the wrapped function
  4460. * takesOneArg(1, 2); //=> [1, undefined]
  4461. * @symb R.unary(f)(a, b, c) = f(a)
  4462. */
  4463. var unary = _curry1(function unary(fn) {
  4464. return nAry(1, fn);
  4465. });
  4466. /**
  4467. * Returns a function of arity `n` from a (manually) curried function.
  4468. *
  4469. * @func
  4470. * @memberOf R
  4471. * @since v0.14.0
  4472. * @category Function
  4473. * @sig Number -> (a -> b) -> (a -> c)
  4474. * @param {Number} length The arity for the returned function.
  4475. * @param {Function} fn The function to uncurry.
  4476. * @return {Function} A new function.
  4477. * @see R.curry
  4478. * @example
  4479. *
  4480. * var addFour = a => b => c => d => a + b + c + d;
  4481. *
  4482. * var uncurriedAddFour = R.uncurryN(4, addFour);
  4483. * uncurriedAddFour(1, 2, 3, 4); //=> 10
  4484. */
  4485. var uncurryN = _curry2(function uncurryN(depth, fn) {
  4486. return curryN(depth, function () {
  4487. var currentDepth = 1;
  4488. var value = fn;
  4489. var idx = 0;
  4490. var endIdx;
  4491. while (currentDepth <= depth && typeof value === 'function') {
  4492. endIdx = currentDepth === depth ? arguments.length : idx + value.length;
  4493. value = value.apply(this, Array.prototype.slice.call(arguments, idx, endIdx));
  4494. currentDepth += 1;
  4495. idx = endIdx;
  4496. }
  4497. return value;
  4498. });
  4499. });
  4500. /**
  4501. * Builds a list from a seed value. Accepts an iterator function, which returns
  4502. * either false to stop iteration or an array of length 2 containing the value
  4503. * to add to the resulting list and the seed to be used in the next call to the
  4504. * iterator function.
  4505. *
  4506. * The iterator function receives one argument: *(seed)*.
  4507. *
  4508. * @func
  4509. * @memberOf R
  4510. * @since v0.10.0
  4511. * @category List
  4512. * @sig (a -> [b]) -> * -> [b]
  4513. * @param {Function} fn The iterator function. receives one argument, `seed`, and returns
  4514. * either false to quit iteration or an array of length two to proceed. The element
  4515. * at index 0 of this array will be added to the resulting array, and the element
  4516. * at index 1 will be passed to the next call to `fn`.
  4517. * @param {*} seed The seed value.
  4518. * @return {Array} The final list.
  4519. * @example
  4520. *
  4521. * var f = n => n > 50 ? false : [-n, n + 10];
  4522. * R.unfold(f, 10); //=> [-10, -20, -30, -40, -50]
  4523. * @symb R.unfold(f, x) = [f(x)[0], f(f(x)[1])[0], f(f(f(x)[1])[1])[0], ...]
  4524. */
  4525. var unfold = _curry2(function unfold(fn, seed) {
  4526. var pair = fn(seed);
  4527. var result = [];
  4528. while (pair && pair.length) {
  4529. result[result.length] = pair[0];
  4530. pair = fn(pair[1]);
  4531. }
  4532. return result;
  4533. });
  4534. /**
  4535. * Returns a new list containing only one copy of each element in the original
  4536. * list, based upon the value returned by applying the supplied predicate to
  4537. * two list elements. Prefers the first item if two items compare equal based
  4538. * on the predicate.
  4539. *
  4540. * @func
  4541. * @memberOf R
  4542. * @since v0.2.0
  4543. * @category List
  4544. * @sig (a, a -> Boolean) -> [a] -> [a]
  4545. * @param {Function} pred A predicate used to test whether two items are equal.
  4546. * @param {Array} list The array to consider.
  4547. * @return {Array} The list of unique items.
  4548. * @example
  4549. *
  4550. * var strEq = R.eqBy(String);
  4551. * R.uniqWith(strEq)([1, '1', 2, 1]); //=> [1, 2]
  4552. * R.uniqWith(strEq)([{}, {}]); //=> [{}]
  4553. * R.uniqWith(strEq)([1, '1', 1]); //=> [1]
  4554. * R.uniqWith(strEq)(['1', 1, 1]); //=> ['1']
  4555. */
  4556. var uniqWith = _curry2(function uniqWith(pred, list) {
  4557. var idx = 0;
  4558. var len = list.length;
  4559. var result = [];
  4560. var item;
  4561. while (idx < len) {
  4562. item = list[idx];
  4563. if (!_containsWith(pred, item, result)) {
  4564. result[result.length] = item;
  4565. }
  4566. idx += 1;
  4567. }
  4568. return result;
  4569. });
  4570. /**
  4571. * Tests the final argument by passing it to the given predicate function. If
  4572. * the predicate is not satisfied, the function will return the result of
  4573. * calling the `whenFalseFn` function with the same argument. If the predicate
  4574. * is satisfied, the argument is returned as is.
  4575. *
  4576. * @func
  4577. * @memberOf R
  4578. * @since v0.18.0
  4579. * @category Logic
  4580. * @sig (a -> Boolean) -> (a -> a) -> a -> a
  4581. * @param {Function} pred A predicate function
  4582. * @param {Function} whenFalseFn A function to invoke when the `pred` evaluates
  4583. * to a falsy value.
  4584. * @param {*} x An object to test with the `pred` function and
  4585. * pass to `whenFalseFn` if necessary.
  4586. * @return {*} Either `x` or the result of applying `x` to `whenFalseFn`.
  4587. * @see R.ifElse, R.when
  4588. * @example
  4589. *
  4590. * let safeInc = R.unless(R.isNil, R.inc);
  4591. * safeInc(null); //=> null
  4592. * safeInc(1); //=> 2
  4593. */
  4594. var unless = _curry3(function unless(pred, whenFalseFn, x) {
  4595. return pred(x) ? x : whenFalseFn(x);
  4596. });
  4597. /**
  4598. * Takes a predicate, a transformation function, and an initial value,
  4599. * and returns a value of the same type as the initial value.
  4600. * It does so by applying the transformation until the predicate is satisfied,
  4601. * at which point it returns the satisfactory value.
  4602. *
  4603. * @func
  4604. * @memberOf R
  4605. * @since v0.20.0
  4606. * @category Logic
  4607. * @sig (a -> Boolean) -> (a -> a) -> a -> a
  4608. * @param {Function} pred A predicate function
  4609. * @param {Function} fn The iterator function
  4610. * @param {*} init Initial value
  4611. * @return {*} Final value that satisfies predicate
  4612. * @example
  4613. *
  4614. * R.until(R.gt(R.__, 100), R.multiply(2))(1) // => 128
  4615. */
  4616. var until = _curry3(function until(pred, fn, init) {
  4617. var val = init;
  4618. while (!pred(val)) {
  4619. val = fn(val);
  4620. }
  4621. return val;
  4622. });
  4623. /**
  4624. * Returns a new copy of the array with the element at the provided index
  4625. * replaced with the given value.
  4626. *
  4627. * @func
  4628. * @memberOf R
  4629. * @since v0.14.0
  4630. * @category List
  4631. * @sig Number -> a -> [a] -> [a]
  4632. * @param {Number} idx The index to update.
  4633. * @param {*} x The value to exist at the given index of the returned array.
  4634. * @param {Array|Arguments} list The source array-like object to be updated.
  4635. * @return {Array} A copy of `list` with the value at index `idx` replaced with `x`.
  4636. * @see R.adjust
  4637. * @example
  4638. *
  4639. * R.update(1, 11, [0, 1, 2]); //=> [0, 11, 2]
  4640. * R.update(1)(11)([0, 1, 2]); //=> [0, 11, 2]
  4641. * @symb R.update(-1, a, [b, c]) = [b, a]
  4642. * @symb R.update(0, a, [b, c]) = [a, c]
  4643. * @symb R.update(1, a, [b, c]) = [b, a]
  4644. */
  4645. var update = _curry3(function update(idx, x, list) {
  4646. return adjust(always(x), idx, list);
  4647. });
  4648. /**
  4649. * Accepts a function `fn` and a list of transformer functions and returns a
  4650. * new curried function. When the new function is invoked, it calls the
  4651. * function `fn` with parameters consisting of the result of calling each
  4652. * supplied handler on successive arguments to the new function.
  4653. *
  4654. * If more arguments are passed to the returned function than transformer
  4655. * functions, those arguments are passed directly to `fn` as additional
  4656. * parameters. If you expect additional arguments that don't need to be
  4657. * transformed, although you can ignore them, it's best to pass an identity
  4658. * function so that the new function reports the correct arity.
  4659. *
  4660. * @func
  4661. * @memberOf R
  4662. * @since v0.1.0
  4663. * @category Function
  4664. * @sig (x1 -> x2 -> ... -> z) -> [(a -> x1), (b -> x2), ...] -> (a -> b -> ... -> z)
  4665. * @param {Function} fn The function to wrap.
  4666. * @param {Array} transformers A list of transformer functions
  4667. * @return {Function} The wrapped function.
  4668. * @see R.converge
  4669. * @example
  4670. *
  4671. * R.useWith(Math.pow, [R.identity, R.identity])(3, 4); //=> 81
  4672. * R.useWith(Math.pow, [R.identity, R.identity])(3)(4); //=> 81
  4673. * R.useWith(Math.pow, [R.dec, R.inc])(3, 4); //=> 32
  4674. * R.useWith(Math.pow, [R.dec, R.inc])(3)(4); //=> 32
  4675. * @symb R.useWith(f, [g, h])(a, b) = f(g(a), h(b))
  4676. */
  4677. var useWith = _curry2(function useWith(fn, transformers) {
  4678. return curryN(transformers.length, function () {
  4679. var args = [];
  4680. var idx = 0;
  4681. while (idx < transformers.length) {
  4682. args.push(transformers[idx].call(this, arguments[idx]));
  4683. idx += 1;
  4684. }
  4685. return fn.apply(this, args.concat(Array.prototype.slice.call(arguments, transformers.length)));
  4686. });
  4687. });
  4688. /**
  4689. * Returns a list of all the enumerable own properties of the supplied object.
  4690. * Note that the order of the output array is not guaranteed across different
  4691. * JS platforms.
  4692. *
  4693. * @func
  4694. * @memberOf R
  4695. * @since v0.1.0
  4696. * @category Object
  4697. * @sig {k: v} -> [v]
  4698. * @param {Object} obj The object to extract values from
  4699. * @return {Array} An array of the values of the object's own properties.
  4700. * @see R.valuesIn, R.keys
  4701. * @example
  4702. *
  4703. * R.values({a: 1, b: 2, c: 3}); //=> [1, 2, 3]
  4704. */
  4705. var values = _curry1(function values(obj) {
  4706. var props = keys(obj);
  4707. var len = props.length;
  4708. var vals = [];
  4709. var idx = 0;
  4710. while (idx < len) {
  4711. vals[idx] = obj[props[idx]];
  4712. idx += 1;
  4713. }
  4714. return vals;
  4715. });
  4716. /**
  4717. * Returns a list of all the properties, including prototype properties, of the
  4718. * supplied object.
  4719. * Note that the order of the output array is not guaranteed to be consistent
  4720. * across different JS platforms.
  4721. *
  4722. * @func
  4723. * @memberOf R
  4724. * @since v0.2.0
  4725. * @category Object
  4726. * @sig {k: v} -> [v]
  4727. * @param {Object} obj The object to extract values from
  4728. * @return {Array} An array of the values of the object's own and prototype properties.
  4729. * @see R.values, R.keysIn
  4730. * @example
  4731. *
  4732. * var F = function() { this.x = 'X'; };
  4733. * F.prototype.y = 'Y';
  4734. * var f = new F();
  4735. * R.valuesIn(f); //=> ['X', 'Y']
  4736. */
  4737. var valuesIn = _curry1(function valuesIn(obj) {
  4738. var prop;
  4739. var vs = [];
  4740. for (prop in obj) {
  4741. vs[vs.length] = obj[prop];
  4742. }
  4743. return vs;
  4744. });
  4745. /**
  4746. * Returns a "view" of the given data structure, determined by the given lens.
  4747. * The lens's focus determines which portion of the data structure is visible.
  4748. *
  4749. * @func
  4750. * @memberOf R
  4751. * @since v0.16.0
  4752. * @category Object
  4753. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  4754. * @sig Lens s a -> s -> a
  4755. * @param {Lens} lens
  4756. * @param {*} x
  4757. * @return {*}
  4758. * @see R.prop, R.lensIndex, R.lensProp
  4759. * @example
  4760. *
  4761. * var xLens = R.lensProp('x');
  4762. *
  4763. * R.view(xLens, {x: 1, y: 2}); //=> 1
  4764. * R.view(xLens, {x: 4, y: 2}); //=> 4
  4765. */
  4766. // `Const` is a functor that effectively ignores the function given to `map`.
  4767. // Using `Const` effectively ignores the setter function of the `lens`,
  4768. // leaving the value returned by the getter function unmodified.
  4769. var view = function () {
  4770. // `Const` is a functor that effectively ignores the function given to `map`.
  4771. var Const = function (x) {
  4772. return {
  4773. value: x,
  4774. 'fantasy-land/map': function () {
  4775. return this;
  4776. }
  4777. };
  4778. };
  4779. return _curry2(function view(lens, x) {
  4780. // Using `Const` effectively ignores the setter function of the `lens`,
  4781. // leaving the value returned by the getter function unmodified.
  4782. return lens(Const)(x).value;
  4783. });
  4784. }();
  4785. /**
  4786. * Tests the final argument by passing it to the given predicate function. If
  4787. * the predicate is satisfied, the function will return the result of calling
  4788. * the `whenTrueFn` function with the same argument. If the predicate is not
  4789. * satisfied, the argument is returned as is.
  4790. *
  4791. * @func
  4792. * @memberOf R
  4793. * @since v0.18.0
  4794. * @category Logic
  4795. * @sig (a -> Boolean) -> (a -> a) -> a -> a
  4796. * @param {Function} pred A predicate function
  4797. * @param {Function} whenTrueFn A function to invoke when the `condition`
  4798. * evaluates to a truthy value.
  4799. * @param {*} x An object to test with the `pred` function and
  4800. * pass to `whenTrueFn` if necessary.
  4801. * @return {*} Either `x` or the result of applying `x` to `whenTrueFn`.
  4802. * @see R.ifElse, R.unless
  4803. * @example
  4804. *
  4805. * // truncate :: String -> String
  4806. * var truncate = R.when(
  4807. * R.propSatisfies(R.gt(R.__, 10), 'length'),
  4808. * R.pipe(R.take(10), R.append('…'), R.join(''))
  4809. * );
  4810. * truncate('12345'); //=> '12345'
  4811. * truncate('0123456789ABC'); //=> '0123456789…'
  4812. */
  4813. var when = _curry3(function when(pred, whenTrueFn, x) {
  4814. return pred(x) ? whenTrueFn(x) : x;
  4815. });
  4816. /**
  4817. * Takes a spec object and a test object; returns true if the test satisfies
  4818. * the spec. Each of the spec's own properties must be a predicate function.
  4819. * Each predicate is applied to the value of the corresponding property of the
  4820. * test object. `where` returns true if all the predicates return true, false
  4821. * otherwise.
  4822. *
  4823. * `where` is well suited to declaratively expressing constraints for other
  4824. * functions such as [`filter`](#filter) and [`find`](#find).
  4825. *
  4826. * @func
  4827. * @memberOf R
  4828. * @since v0.1.1
  4829. * @category Object
  4830. * @sig {String: (* -> Boolean)} -> {String: *} -> Boolean
  4831. * @param {Object} spec
  4832. * @param {Object} testObj
  4833. * @return {Boolean}
  4834. * @example
  4835. *
  4836. * // pred :: Object -> Boolean
  4837. * var pred = R.where({
  4838. * a: R.equals('foo'),
  4839. * b: R.complement(R.equals('bar')),
  4840. * x: R.gt(R.__, 10),
  4841. * y: R.lt(R.__, 20)
  4842. * });
  4843. *
  4844. * pred({a: 'foo', b: 'xxx', x: 11, y: 19}); //=> true
  4845. * pred({a: 'xxx', b: 'xxx', x: 11, y: 19}); //=> false
  4846. * pred({a: 'foo', b: 'bar', x: 11, y: 19}); //=> false
  4847. * pred({a: 'foo', b: 'xxx', x: 10, y: 19}); //=> false
  4848. * pred({a: 'foo', b: 'xxx', x: 11, y: 20}); //=> false
  4849. */
  4850. var where = _curry2(function where(spec, testObj) {
  4851. for (var prop in spec) {
  4852. if (_has(prop, spec) && !spec[prop](testObj[prop])) {
  4853. return false;
  4854. }
  4855. }
  4856. return true;
  4857. });
  4858. /**
  4859. * Creates a new list out of the two supplied by creating each possible pair
  4860. * from the lists.
  4861. *
  4862. * @func
  4863. * @memberOf R
  4864. * @since v0.1.0
  4865. * @category List
  4866. * @sig [a] -> [b] -> [[a,b]]
  4867. * @param {Array} as The first list.
  4868. * @param {Array} bs The second list.
  4869. * @return {Array} The list made by combining each possible pair from
  4870. * `as` and `bs` into pairs (`[a, b]`).
  4871. * @example
  4872. *
  4873. * R.xprod([1, 2], ['a', 'b']); //=> [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
  4874. * @symb R.xprod([a, b], [c, d]) = [[a, c], [a, d], [b, c], [b, d]]
  4875. */
  4876. // = xprodWith(prepend); (takes about 3 times as long...)
  4877. var xprod = _curry2(function xprod(a, b) {
  4878. // = xprodWith(prepend); (takes about 3 times as long...)
  4879. var idx = 0;
  4880. var ilen = a.length;
  4881. var j;
  4882. var jlen = b.length;
  4883. var result = [];
  4884. while (idx < ilen) {
  4885. j = 0;
  4886. while (j < jlen) {
  4887. result[result.length] = [
  4888. a[idx],
  4889. b[j]
  4890. ];
  4891. j += 1;
  4892. }
  4893. idx += 1;
  4894. }
  4895. return result;
  4896. });
  4897. /**
  4898. * Creates a new list out of the two supplied by pairing up equally-positioned
  4899. * items from both lists. The returned list is truncated to the length of the
  4900. * shorter of the two input lists.
  4901. * Note: `zip` is equivalent to `zipWith(function(a, b) { return [a, b] })`.
  4902. *
  4903. * @func
  4904. * @memberOf R
  4905. * @since v0.1.0
  4906. * @category List
  4907. * @sig [a] -> [b] -> [[a,b]]
  4908. * @param {Array} list1 The first array to consider.
  4909. * @param {Array} list2 The second array to consider.
  4910. * @return {Array} The list made by pairing up same-indexed elements of `list1` and `list2`.
  4911. * @example
  4912. *
  4913. * R.zip([1, 2, 3], ['a', 'b', 'c']); //=> [[1, 'a'], [2, 'b'], [3, 'c']]
  4914. * @symb R.zip([a, b, c], [d, e, f]) = [[a, d], [b, e], [c, f]]
  4915. */
  4916. var zip = _curry2(function zip(a, b) {
  4917. var rv = [];
  4918. var idx = 0;
  4919. var len = Math.min(a.length, b.length);
  4920. while (idx < len) {
  4921. rv[idx] = [
  4922. a[idx],
  4923. b[idx]
  4924. ];
  4925. idx += 1;
  4926. }
  4927. return rv;
  4928. });
  4929. /**
  4930. * Creates a new object out of a list of keys and a list of values.
  4931. * Key/value pairing is truncated to the length of the shorter of the two lists.
  4932. * Note: `zipObj` is equivalent to `pipe(zipWith(pair), fromPairs)`.
  4933. *
  4934. * @func
  4935. * @memberOf R
  4936. * @since v0.3.0
  4937. * @category List
  4938. * @sig [String] -> [*] -> {String: *}
  4939. * @param {Array} keys The array that will be properties on the output object.
  4940. * @param {Array} values The list of values on the output object.
  4941. * @return {Object} The object made by pairing up same-indexed elements of `keys` and `values`.
  4942. * @example
  4943. *
  4944. * R.zipObj(['a', 'b', 'c'], [1, 2, 3]); //=> {a: 1, b: 2, c: 3}
  4945. */
  4946. var zipObj = _curry2(function zipObj(keys, values) {
  4947. var idx = 0;
  4948. var len = Math.min(keys.length, values.length);
  4949. var out = {};
  4950. while (idx < len) {
  4951. out[keys[idx]] = values[idx];
  4952. idx += 1;
  4953. }
  4954. return out;
  4955. });
  4956. /**
  4957. * Creates a new list out of the two supplied by applying the function to each
  4958. * equally-positioned pair in the lists. The returned list is truncated to the
  4959. * length of the shorter of the two input lists.
  4960. *
  4961. * @function
  4962. * @memberOf R
  4963. * @since v0.1.0
  4964. * @category List
  4965. * @sig (a,b -> c) -> [a] -> [b] -> [c]
  4966. * @param {Function} fn The function used to combine the two elements into one value.
  4967. * @param {Array} list1 The first array to consider.
  4968. * @param {Array} list2 The second array to consider.
  4969. * @return {Array} The list made by combining same-indexed elements of `list1` and `list2`
  4970. * using `fn`.
  4971. * @example
  4972. *
  4973. * var f = (x, y) => {
  4974. * // ...
  4975. * };
  4976. * R.zipWith(f, [1, 2, 3], ['a', 'b', 'c']);
  4977. * //=> [f(1, 'a'), f(2, 'b'), f(3, 'c')]
  4978. * @symb R.zipWith(fn, [a, b, c], [d, e, f]) = [fn(a, d), fn(b, e), fn(c, f)]
  4979. */
  4980. var zipWith = _curry3(function zipWith(fn, a, b) {
  4981. var rv = [];
  4982. var idx = 0;
  4983. var len = Math.min(a.length, b.length);
  4984. while (idx < len) {
  4985. rv[idx] = fn(a[idx], b[idx]);
  4986. idx += 1;
  4987. }
  4988. return rv;
  4989. });
  4990. /**
  4991. * A function that always returns `false`. Any passed in parameters are ignored.
  4992. *
  4993. * @func
  4994. * @memberOf R
  4995. * @since v0.9.0
  4996. * @category Function
  4997. * @sig * -> Boolean
  4998. * @param {*}
  4999. * @return {Boolean}
  5000. * @see R.always, R.T
  5001. * @example
  5002. *
  5003. * R.F(); //=> false
  5004. */
  5005. var F = always(false);
  5006. /**
  5007. * A function that always returns `true`. Any passed in parameters are ignored.
  5008. *
  5009. * @func
  5010. * @memberOf R
  5011. * @since v0.9.0
  5012. * @category Function
  5013. * @sig * -> Boolean
  5014. * @param {*}
  5015. * @return {Boolean}
  5016. * @see R.always, R.F
  5017. * @example
  5018. *
  5019. * R.T(); //=> true
  5020. */
  5021. var T = always(true);
  5022. /**
  5023. * Copies an object.
  5024. *
  5025. * @private
  5026. * @param {*} value The value to be copied
  5027. * @param {Array} refFrom Array containing the source references
  5028. * @param {Array} refTo Array containing the copied source references
  5029. * @param {Boolean} deep Whether or not to perform deep cloning.
  5030. * @return {*} The copied value.
  5031. */
  5032. var _clone = function _clone(value, refFrom, refTo, deep) {
  5033. var copy = function copy(copiedValue) {
  5034. var len = refFrom.length;
  5035. var idx = 0;
  5036. while (idx < len) {
  5037. if (value === refFrom[idx]) {
  5038. return refTo[idx];
  5039. }
  5040. idx += 1;
  5041. }
  5042. refFrom[idx + 1] = value;
  5043. refTo[idx + 1] = copiedValue;
  5044. for (var key in value) {
  5045. copiedValue[key] = deep ? _clone(value[key], refFrom, refTo, true) : value[key];
  5046. }
  5047. return copiedValue;
  5048. };
  5049. switch (type(value)) {
  5050. case 'Object':
  5051. return copy({});
  5052. case 'Array':
  5053. return copy([]);
  5054. case 'Date':
  5055. return new Date(value.valueOf());
  5056. case 'RegExp':
  5057. return _cloneRegExp(value);
  5058. default:
  5059. return value;
  5060. }
  5061. };
  5062. var _createPartialApplicator = function _createPartialApplicator(concat) {
  5063. return _curry2(function (fn, args) {
  5064. return _arity(Math.max(0, fn.length - args.length), function () {
  5065. return fn.apply(this, concat(args, arguments));
  5066. });
  5067. });
  5068. };
  5069. var _dropLast = function dropLast(n, xs) {
  5070. return take(n < xs.length ? xs.length - n : 0, xs);
  5071. };
  5072. // Values of other types are only equal if identical.
  5073. var _equals = function _equals(a, b, stackA, stackB) {
  5074. if (identical(a, b)) {
  5075. return true;
  5076. }
  5077. if (type(a) !== type(b)) {
  5078. return false;
  5079. }
  5080. if (a == null || b == null) {
  5081. return false;
  5082. }
  5083. if (typeof a['fantasy-land/equals'] === 'function' || typeof b['fantasy-land/equals'] === 'function') {
  5084. return typeof a['fantasy-land/equals'] === 'function' && a['fantasy-land/equals'](b) && typeof b['fantasy-land/equals'] === 'function' && b['fantasy-land/equals'](a);
  5085. }
  5086. if (typeof a.equals === 'function' || typeof b.equals === 'function') {
  5087. return typeof a.equals === 'function' && a.equals(b) && typeof b.equals === 'function' && b.equals(a);
  5088. }
  5089. switch (type(a)) {
  5090. case 'Arguments':
  5091. case 'Array':
  5092. case 'Object':
  5093. if (typeof a.constructor === 'function' && _functionName(a.constructor) === 'Promise') {
  5094. return a === b;
  5095. }
  5096. break;
  5097. case 'Boolean':
  5098. case 'Number':
  5099. case 'String':
  5100. if (!(typeof a === typeof b && identical(a.valueOf(), b.valueOf()))) {
  5101. return false;
  5102. }
  5103. break;
  5104. case 'Date':
  5105. if (!identical(a.valueOf(), b.valueOf())) {
  5106. return false;
  5107. }
  5108. break;
  5109. case 'Error':
  5110. return a.name === b.name && a.message === b.message;
  5111. case 'RegExp':
  5112. if (!(a.source === b.source && a.global === b.global && a.ignoreCase === b.ignoreCase && a.multiline === b.multiline && a.sticky === b.sticky && a.unicode === b.unicode)) {
  5113. return false;
  5114. }
  5115. break;
  5116. case 'Map':
  5117. case 'Set':
  5118. if (!_equals(_arrayFromIterator(a.entries()), _arrayFromIterator(b.entries()), stackA, stackB)) {
  5119. return false;
  5120. }
  5121. break;
  5122. case 'Int8Array':
  5123. case 'Uint8Array':
  5124. case 'Uint8ClampedArray':
  5125. case 'Int16Array':
  5126. case 'Uint16Array':
  5127. case 'Int32Array':
  5128. case 'Uint32Array':
  5129. case 'Float32Array':
  5130. case 'Float64Array':
  5131. break;
  5132. case 'ArrayBuffer':
  5133. break;
  5134. default:
  5135. // Values of other types are only equal if identical.
  5136. return false;
  5137. }
  5138. var keysA = keys(a);
  5139. if (keysA.length !== keys(b).length) {
  5140. return false;
  5141. }
  5142. var idx = stackA.length - 1;
  5143. while (idx >= 0) {
  5144. if (stackA[idx] === a) {
  5145. return stackB[idx] === b;
  5146. }
  5147. idx -= 1;
  5148. }
  5149. stackA.push(a);
  5150. stackB.push(b);
  5151. idx = keysA.length - 1;
  5152. while (idx >= 0) {
  5153. var key = keysA[idx];
  5154. if (!(_has(key, b) && _equals(b[key], a[key], stackA, stackB))) {
  5155. return false;
  5156. }
  5157. idx -= 1;
  5158. }
  5159. stackA.pop();
  5160. stackB.pop();
  5161. return true;
  5162. };
  5163. var _reduce = function () {
  5164. function _arrayReduce(xf, acc, list) {
  5165. var idx = 0;
  5166. var len = list.length;
  5167. while (idx < len) {
  5168. acc = xf['@@transducer/step'](acc, list[idx]);
  5169. if (acc && acc['@@transducer/reduced']) {
  5170. acc = acc['@@transducer/value'];
  5171. break;
  5172. }
  5173. idx += 1;
  5174. }
  5175. return xf['@@transducer/result'](acc);
  5176. }
  5177. function _iterableReduce(xf, acc, iter) {
  5178. var step = iter.next();
  5179. while (!step.done) {
  5180. acc = xf['@@transducer/step'](acc, step.value);
  5181. if (acc && acc['@@transducer/reduced']) {
  5182. acc = acc['@@transducer/value'];
  5183. break;
  5184. }
  5185. step = iter.next();
  5186. }
  5187. return xf['@@transducer/result'](acc);
  5188. }
  5189. function _methodReduce(xf, acc, obj, methodName) {
  5190. return xf['@@transducer/result'](obj[methodName](bind(xf['@@transducer/step'], xf), acc));
  5191. }
  5192. var symIterator = typeof Symbol !== 'undefined' ? Symbol.iterator : '@@iterator';
  5193. return function _reduce(fn, acc, list) {
  5194. if (typeof fn === 'function') {
  5195. fn = _xwrap(fn);
  5196. }
  5197. if (_isArrayLike(list)) {
  5198. return _arrayReduce(fn, acc, list);
  5199. }
  5200. if (typeof list['fantasy-land/reduce'] === 'function') {
  5201. return _methodReduce(fn, acc, list, 'fantasy-land/reduce');
  5202. }
  5203. if (list[symIterator] != null) {
  5204. return _iterableReduce(fn, acc, list[symIterator]());
  5205. }
  5206. if (typeof list.next === 'function') {
  5207. return _iterableReduce(fn, acc, list);
  5208. }
  5209. if (typeof list.reduce === 'function') {
  5210. return _methodReduce(fn, acc, list, 'reduce');
  5211. }
  5212. throw new TypeError('reduce: list must be array or iterable');
  5213. };
  5214. }();
  5215. var _stepCat = function () {
  5216. var _stepCatArray = {
  5217. '@@transducer/init': Array,
  5218. '@@transducer/step': function (xs, x) {
  5219. xs.push(x);
  5220. return xs;
  5221. },
  5222. '@@transducer/result': _identity
  5223. };
  5224. var _stepCatString = {
  5225. '@@transducer/init': String,
  5226. '@@transducer/step': function (a, b) {
  5227. return a + b;
  5228. },
  5229. '@@transducer/result': _identity
  5230. };
  5231. var _stepCatObject = {
  5232. '@@transducer/init': Object,
  5233. '@@transducer/step': function (result, input) {
  5234. return _assign(result, _isArrayLike(input) ? objOf(input[0], input[1]) : input);
  5235. },
  5236. '@@transducer/result': _identity
  5237. };
  5238. return function _stepCat(obj) {
  5239. if (_isTransformer(obj)) {
  5240. return obj;
  5241. }
  5242. if (_isArrayLike(obj)) {
  5243. return _stepCatArray;
  5244. }
  5245. if (typeof obj === 'string') {
  5246. return _stepCatString;
  5247. }
  5248. if (typeof obj === 'object') {
  5249. return _stepCatObject;
  5250. }
  5251. throw new Error('Cannot create transformer for ' + obj);
  5252. };
  5253. }();
  5254. var _xdropLastWhile = function () {
  5255. function XDropLastWhile(fn, xf) {
  5256. this.f = fn;
  5257. this.retained = [];
  5258. this.xf = xf;
  5259. }
  5260. XDropLastWhile.prototype['@@transducer/init'] = _xfBase.init;
  5261. XDropLastWhile.prototype['@@transducer/result'] = function (result) {
  5262. this.retained = null;
  5263. return this.xf['@@transducer/result'](result);
  5264. };
  5265. XDropLastWhile.prototype['@@transducer/step'] = function (result, input) {
  5266. return this.f(input) ? this.retain(result, input) : this.flush(result, input);
  5267. };
  5268. XDropLastWhile.prototype.flush = function (result, input) {
  5269. result = _reduce(this.xf['@@transducer/step'], result, this.retained);
  5270. this.retained = [];
  5271. return this.xf['@@transducer/step'](result, input);
  5272. };
  5273. XDropLastWhile.prototype.retain = function (result, input) {
  5274. this.retained.push(input);
  5275. return result;
  5276. };
  5277. return _curry2(function _xdropLastWhile(fn, xf) {
  5278. return new XDropLastWhile(fn, xf);
  5279. });
  5280. }();
  5281. /**
  5282. * Creates a new list iteration function from an existing one by adding two new
  5283. * parameters to its callback function: the current index, and the entire list.
  5284. *
  5285. * This would turn, for instance, [`R.map`](#map) function into one that
  5286. * more closely resembles `Array.prototype.map`. Note that this will only work
  5287. * for functions in which the iteration callback function is the first
  5288. * parameter, and where the list is the last parameter. (This latter might be
  5289. * unimportant if the list parameter is not used.)
  5290. *
  5291. * @func
  5292. * @memberOf R
  5293. * @since v0.15.0
  5294. * @category Function
  5295. * @category List
  5296. * @sig ((a ... -> b) ... -> [a] -> *) -> (a ..., Int, [a] -> b) ... -> [a] -> *)
  5297. * @param {Function} fn A list iteration function that does not pass index or list to its callback
  5298. * @return {Function} An altered list iteration function that passes (item, index, list) to its callback
  5299. * @example
  5300. *
  5301. * var mapIndexed = R.addIndex(R.map);
  5302. * mapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']);
  5303. * //=> ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']
  5304. */
  5305. var addIndex = _curry1(function addIndex(fn) {
  5306. return curryN(fn.length, function () {
  5307. var idx = 0;
  5308. var origFn = arguments[0];
  5309. var list = arguments[arguments.length - 1];
  5310. var args = Array.prototype.slice.call(arguments, 0);
  5311. args[0] = function () {
  5312. var result = origFn.apply(this, _concat(arguments, [
  5313. idx,
  5314. list
  5315. ]));
  5316. idx += 1;
  5317. return result;
  5318. };
  5319. return fn.apply(this, args);
  5320. });
  5321. });
  5322. /**
  5323. * Makes a shallow clone of an object, setting or overriding the nodes required
  5324. * to create the given path, and placing the specific value at the tail end of
  5325. * that path. Note that this copies and flattens prototype properties onto the
  5326. * new object as well. All non-primitive properties are copied by reference.
  5327. *
  5328. * @func
  5329. * @memberOf R
  5330. * @since v0.8.0
  5331. * @category Object
  5332. * @typedefn Idx = String | Int
  5333. * @sig [Idx] -> a -> {a} -> {a}
  5334. * @param {Array} path the path to set
  5335. * @param {*} val The new value
  5336. * @param {Object} obj The object to clone
  5337. * @return {Object} A new object equivalent to the original except along the specified path.
  5338. * @see R.dissocPath
  5339. * @example
  5340. *
  5341. * R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}); //=> {a: {b: {c: 42}}}
  5342. *
  5343. * // Any missing or non-object keys in path will be overridden
  5344. * R.assocPath(['a', 'b', 'c'], 42, {a: 5}); //=> {a: {b: {c: 42}}}
  5345. */
  5346. var assocPath = _curry3(function assocPath(path, val, obj) {
  5347. if (path.length === 0) {
  5348. return val;
  5349. }
  5350. var idx = path[0];
  5351. if (path.length > 1) {
  5352. var nextObj = !isNil(obj) && _has(idx, obj) ? obj[idx] : _isInteger(path[1]) ? [] : {};
  5353. val = assocPath(Array.prototype.slice.call(path, 1), val, nextObj);
  5354. }
  5355. if (_isInteger(idx) && _isArray(obj)) {
  5356. var arr = [].concat(obj);
  5357. arr[idx] = val;
  5358. return arr;
  5359. } else {
  5360. return assoc(idx, val, obj);
  5361. }
  5362. });
  5363. /**
  5364. * Wraps a function of any arity (including nullary) in a function that accepts
  5365. * exactly 2 parameters. Any extraneous parameters will not be passed to the
  5366. * supplied function.
  5367. *
  5368. * @func
  5369. * @memberOf R
  5370. * @since v0.2.0
  5371. * @category Function
  5372. * @sig (* -> c) -> (a, b -> c)
  5373. * @param {Function} fn The function to wrap.
  5374. * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
  5375. * arity 2.
  5376. * @see R.nAry, R.unary
  5377. * @example
  5378. *
  5379. * var takesThreeArgs = function(a, b, c) {
  5380. * return [a, b, c];
  5381. * };
  5382. * takesThreeArgs.length; //=> 3
  5383. * takesThreeArgs(1, 2, 3); //=> [1, 2, 3]
  5384. *
  5385. * var takesTwoArgs = R.binary(takesThreeArgs);
  5386. * takesTwoArgs.length; //=> 2
  5387. * // Only 2 arguments are passed to the wrapped function
  5388. * takesTwoArgs(1, 2, 3); //=> [1, 2, undefined]
  5389. * @symb R.binary(f)(a, b, c) = f(a, b)
  5390. */
  5391. var binary = _curry1(function binary(fn) {
  5392. return nAry(2, fn);
  5393. });
  5394. /**
  5395. * Creates a deep copy of the value which may contain (nested) `Array`s and
  5396. * `Object`s, `Number`s, `String`s, `Boolean`s and `Date`s. `Function`s are
  5397. * assigned by reference rather than copied
  5398. *
  5399. * Dispatches to a `clone` method if present.
  5400. *
  5401. * @func
  5402. * @memberOf R
  5403. * @since v0.1.0
  5404. * @category Object
  5405. * @sig {*} -> {*}
  5406. * @param {*} value The object or array to clone
  5407. * @return {*} A deeply cloned copy of `val`
  5408. * @example
  5409. *
  5410. * var objects = [{}, {}, {}];
  5411. * var objectsClone = R.clone(objects);
  5412. * objects === objectsClone; //=> false
  5413. * objects[0] === objectsClone[0]; //=> false
  5414. */
  5415. var clone = _curry1(function clone(value) {
  5416. return value != null && typeof value.clone === 'function' ? value.clone() : _clone(value, [], [], true);
  5417. });
  5418. /**
  5419. * Returns a curried equivalent of the provided function. The curried function
  5420. * has two unusual capabilities. First, its arguments needn't be provided one
  5421. * at a time. If `f` is a ternary function and `g` is `R.curry(f)`, the
  5422. * following are equivalent:
  5423. *
  5424. * - `g(1)(2)(3)`
  5425. * - `g(1)(2, 3)`
  5426. * - `g(1, 2)(3)`
  5427. * - `g(1, 2, 3)`
  5428. *
  5429. * Secondly, the special placeholder value [`R.__`](#__) may be used to specify
  5430. * "gaps", allowing partial application of any combination of arguments,
  5431. * regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
  5432. * the following are equivalent:
  5433. *
  5434. * - `g(1, 2, 3)`
  5435. * - `g(_, 2, 3)(1)`
  5436. * - `g(_, _, 3)(1)(2)`
  5437. * - `g(_, _, 3)(1, 2)`
  5438. * - `g(_, 2)(1)(3)`
  5439. * - `g(_, 2)(1, 3)`
  5440. * - `g(_, 2)(_, 3)(1)`
  5441. *
  5442. * @func
  5443. * @memberOf R
  5444. * @since v0.1.0
  5445. * @category Function
  5446. * @sig (* -> a) -> (* -> a)
  5447. * @param {Function} fn The function to curry.
  5448. * @return {Function} A new, curried function.
  5449. * @see R.curryN
  5450. * @example
  5451. *
  5452. * var addFourNumbers = (a, b, c, d) => a + b + c + d;
  5453. *
  5454. * var curriedAddFourNumbers = R.curry(addFourNumbers);
  5455. * var f = curriedAddFourNumbers(1, 2);
  5456. * var g = f(3);
  5457. * g(4); //=> 10
  5458. */
  5459. var curry = _curry1(function curry(fn) {
  5460. return curryN(fn.length, fn);
  5461. });
  5462. /**
  5463. * Makes a shallow clone of an object, omitting the property at the given path.
  5464. * Note that this copies and flattens prototype properties onto the new object
  5465. * as well. All non-primitive properties are copied by reference.
  5466. *
  5467. * @func
  5468. * @memberOf R
  5469. * @since v0.11.0
  5470. * @category Object
  5471. * @typedefn Idx = String | Int
  5472. * @sig [Idx] -> {k: v} -> {k: v}
  5473. * @param {Array} path The path to the value to omit
  5474. * @param {Object} obj The object to clone
  5475. * @return {Object} A new object without the property at path
  5476. * @see R.assocPath
  5477. * @example
  5478. *
  5479. * R.dissocPath(['a', 'b', 'c'], {a: {b: {c: 42}}}); //=> {a: {b: {}}}
  5480. */
  5481. var dissocPath = _curry2(function dissocPath(path, obj) {
  5482. switch (path.length) {
  5483. case 0:
  5484. return obj;
  5485. case 1:
  5486. return _isInteger(path[0]) ? remove(path[0], 1, obj) : dissoc(path[0], obj);
  5487. default:
  5488. var head = path[0];
  5489. var tail = Array.prototype.slice.call(path, 1);
  5490. if (obj[head] == null) {
  5491. return obj;
  5492. } else if (_isInteger(path[0])) {
  5493. return update(head, dissocPath(tail, obj[head]), obj);
  5494. } else {
  5495. return assoc(head, dissocPath(tail, obj[head]), obj);
  5496. }
  5497. }
  5498. });
  5499. /**
  5500. * Returns all but the first `n` elements of the given list, string, or
  5501. * transducer/transformer (or object with a `drop` method).
  5502. *
  5503. * Dispatches to the `drop` method of the second argument, if present.
  5504. *
  5505. * @func
  5506. * @memberOf R
  5507. * @since v0.1.0
  5508. * @category List
  5509. * @sig Number -> [a] -> [a]
  5510. * @sig Number -> String -> String
  5511. * @param {Number} n
  5512. * @param {*} list
  5513. * @return {*} A copy of list without the first `n` elements
  5514. * @see R.take, R.transduce, R.dropLast, R.dropWhile
  5515. * @example
  5516. *
  5517. * R.drop(1, ['foo', 'bar', 'baz']); //=> ['bar', 'baz']
  5518. * R.drop(2, ['foo', 'bar', 'baz']); //=> ['baz']
  5519. * R.drop(3, ['foo', 'bar', 'baz']); //=> []
  5520. * R.drop(4, ['foo', 'bar', 'baz']); //=> []
  5521. * R.drop(3, 'ramda'); //=> 'da'
  5522. */
  5523. var drop = _curry2(_dispatchable(['drop'], _xdrop, function drop(n, xs) {
  5524. return slice(Math.max(0, n), Infinity, xs);
  5525. }));
  5526. /**
  5527. * Returns a list containing all but the last `n` elements of the given `list`.
  5528. *
  5529. * @func
  5530. * @memberOf R
  5531. * @since v0.16.0
  5532. * @category List
  5533. * @sig Number -> [a] -> [a]
  5534. * @sig Number -> String -> String
  5535. * @param {Number} n The number of elements of `list` to skip.
  5536. * @param {Array} list The list of elements to consider.
  5537. * @return {Array} A copy of the list with only the first `list.length - n` elements
  5538. * @see R.takeLast, R.drop, R.dropWhile, R.dropLastWhile
  5539. * @example
  5540. *
  5541. * R.dropLast(1, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
  5542. * R.dropLast(2, ['foo', 'bar', 'baz']); //=> ['foo']
  5543. * R.dropLast(3, ['foo', 'bar', 'baz']); //=> []
  5544. * R.dropLast(4, ['foo', 'bar', 'baz']); //=> []
  5545. * R.dropLast(3, 'ramda'); //=> 'ra'
  5546. */
  5547. var dropLast = _curry2(_dispatchable([], _xdropLast, _dropLast));
  5548. /**
  5549. * Returns a new list excluding all the tailing elements of a given list which
  5550. * satisfy the supplied predicate function. It passes each value from the right
  5551. * to the supplied predicate function, skipping elements until the predicate
  5552. * function returns a `falsy` value. The predicate function is applied to one argument:
  5553. * *(value)*.
  5554. *
  5555. * @func
  5556. * @memberOf R
  5557. * @since v0.16.0
  5558. * @category List
  5559. * @sig (a -> Boolean) -> [a] -> [a]
  5560. * @param {Function} predicate The function to be called on each element
  5561. * @param {Array} list The collection to iterate over.
  5562. * @return {Array} A new array without any trailing elements that return `falsy` values from the `predicate`.
  5563. * @see R.takeLastWhile, R.addIndex, R.drop, R.dropWhile
  5564. * @example
  5565. *
  5566. * var lteThree = x => x <= 3;
  5567. *
  5568. * R.dropLastWhile(lteThree, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3, 4]
  5569. */
  5570. var dropLastWhile = _curry2(_dispatchable([], _xdropLastWhile, _dropLastWhile));
  5571. /**
  5572. * Returns `true` if its arguments are equivalent, `false` otherwise. Handles
  5573. * cyclical data structures.
  5574. *
  5575. * Dispatches symmetrically to the `equals` methods of both arguments, if
  5576. * present.
  5577. *
  5578. * @func
  5579. * @memberOf R
  5580. * @since v0.15.0
  5581. * @category Relation
  5582. * @sig a -> b -> Boolean
  5583. * @param {*} a
  5584. * @param {*} b
  5585. * @return {Boolean}
  5586. * @example
  5587. *
  5588. * R.equals(1, 1); //=> true
  5589. * R.equals(1, '1'); //=> false
  5590. * R.equals([1, 2, 3], [1, 2, 3]); //=> true
  5591. *
  5592. * var a = {}; a.v = a;
  5593. * var b = {}; b.v = b;
  5594. * R.equals(a, b); //=> true
  5595. */
  5596. var equals = _curry2(function equals(a, b) {
  5597. return _equals(a, b, [], []);
  5598. });
  5599. /**
  5600. * Takes a predicate and a `Filterable`, and returns a new filterable of the
  5601. * same type containing the members of the given filterable which satisfy the
  5602. * given predicate. Filterable objects include plain objects or any object
  5603. * that has a filter method such as `Array`.
  5604. *
  5605. * Dispatches to the `filter` method of the second argument, if present.
  5606. *
  5607. * Acts as a transducer if a transformer is given in list position.
  5608. *
  5609. * @func
  5610. * @memberOf R
  5611. * @since v0.1.0
  5612. * @category List
  5613. * @sig Filterable f => (a -> Boolean) -> f a -> f a
  5614. * @param {Function} pred
  5615. * @param {Array} filterable
  5616. * @return {Array} Filterable
  5617. * @see R.reject, R.transduce, R.addIndex
  5618. * @example
  5619. *
  5620. * var isEven = n => n % 2 === 0;
  5621. *
  5622. * R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
  5623. *
  5624. * R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
  5625. */
  5626. // else
  5627. var filter = _curry2(_dispatchable(['filter'], _xfilter, function (pred, filterable) {
  5628. return _isObject(filterable) ? _reduce(function (acc, key) {
  5629. if (pred(filterable[key])) {
  5630. acc[key] = filterable[key];
  5631. }
  5632. return acc;
  5633. }, {}, keys(filterable)) : // else
  5634. _filter(pred, filterable);
  5635. }));
  5636. /**
  5637. * Returns a new function much like the supplied one, except that the first two
  5638. * arguments' order is reversed.
  5639. *
  5640. * @func
  5641. * @memberOf R
  5642. * @since v0.1.0
  5643. * @category Function
  5644. * @sig (a -> b -> c -> ... -> z) -> (b -> a -> c -> ... -> z)
  5645. * @param {Function} fn The function to invoke with its first two parameters reversed.
  5646. * @return {*} The result of invoking `fn` with its first two parameters' order reversed.
  5647. * @example
  5648. *
  5649. * var mergeThree = (a, b, c) => [].concat(a, b, c);
  5650. *
  5651. * mergeThree(1, 2, 3); //=> [1, 2, 3]
  5652. *
  5653. * R.flip(mergeThree)(1, 2, 3); //=> [2, 1, 3]
  5654. * @symb R.flip(f)(a, b, c) = f(b, a, c)
  5655. */
  5656. var flip = _curry1(function flip(fn) {
  5657. return curry(function (a, b) {
  5658. var args = Array.prototype.slice.call(arguments, 0);
  5659. args[0] = b;
  5660. args[1] = a;
  5661. return fn.apply(this, args);
  5662. });
  5663. });
  5664. /**
  5665. * Iterate over an input `object`, calling a provided function `fn` for each
  5666. * key and value in the object.
  5667. *
  5668. * `fn` receives three argument: *(value, key, obj)*.
  5669. *
  5670. * @func
  5671. * @memberOf R
  5672. * @since v0.23.0
  5673. * @category Object
  5674. * @sig ((a, String, StrMap a) -> Any) -> StrMap a -> StrMap a
  5675. * @param {Function} fn The function to invoke. Receives three argument, `value`, `key`, `obj`.
  5676. * @param {Object} obj The object to iterate over.
  5677. * @return {Object} The original object.
  5678. * @example
  5679. *
  5680. * var printKeyConcatValue = (value, key) => console.log(key + ':' + value);
  5681. * R.forEachObjIndexed(printKeyConcatValue, {x: 1, y: 2}); //=> {x: 1, y: 2}
  5682. * // logs x:1
  5683. * // logs y:2
  5684. * @symb R.forEachObjIndexed(f, {x: a, y: b}) = {x: a, y: b}
  5685. */
  5686. var forEachObjIndexed = _curry2(function forEachObjIndexed(fn, obj) {
  5687. var keyList = keys(obj);
  5688. var idx = 0;
  5689. while (idx < keyList.length) {
  5690. var key = keyList[idx];
  5691. fn(obj[key], key, obj);
  5692. idx += 1;
  5693. }
  5694. return obj;
  5695. });
  5696. /**
  5697. * Returns the first element of the given list or string. In some libraries
  5698. * this function is named `first`.
  5699. *
  5700. * @func
  5701. * @memberOf R
  5702. * @since v0.1.0
  5703. * @category List
  5704. * @sig [a] -> a | Undefined
  5705. * @sig String -> String
  5706. * @param {Array|String} list
  5707. * @return {*}
  5708. * @see R.tail, R.init, R.last
  5709. * @example
  5710. *
  5711. * R.head(['fi', 'fo', 'fum']); //=> 'fi'
  5712. * R.head([]); //=> undefined
  5713. *
  5714. * R.head('abc'); //=> 'a'
  5715. * R.head(''); //=> ''
  5716. */
  5717. var head = nth(0);
  5718. /**
  5719. * Returns all but the last element of the given list or string.
  5720. *
  5721. * @func
  5722. * @memberOf R
  5723. * @since v0.9.0
  5724. * @category List
  5725. * @sig [a] -> [a]
  5726. * @sig String -> String
  5727. * @param {*} list
  5728. * @return {*}
  5729. * @see R.last, R.head, R.tail
  5730. * @example
  5731. *
  5732. * R.init([1, 2, 3]); //=> [1, 2]
  5733. * R.init([1, 2]); //=> [1]
  5734. * R.init([1]); //=> []
  5735. * R.init([]); //=> []
  5736. *
  5737. * R.init('abc'); //=> 'ab'
  5738. * R.init('ab'); //=> 'a'
  5739. * R.init('a'); //=> ''
  5740. * R.init(''); //=> ''
  5741. */
  5742. var init = slice(0, -1);
  5743. /**
  5744. * Combines two lists into a set (i.e. no duplicates) composed of those
  5745. * elements common to both lists. Duplication is determined according to the
  5746. * value returned by applying the supplied predicate to two list elements.
  5747. *
  5748. * @func
  5749. * @memberOf R
  5750. * @since v0.1.0
  5751. * @category Relation
  5752. * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
  5753. * @param {Function} pred A predicate function that determines whether
  5754. * the two supplied elements are equal.
  5755. * @param {Array} list1 One list of items to compare
  5756. * @param {Array} list2 A second list of items to compare
  5757. * @return {Array} A new list containing those elements common to both lists.
  5758. * @see R.innerJoin
  5759. * @deprecated since v0.24.0
  5760. * @example
  5761. *
  5762. * var buffaloSpringfield = [
  5763. * {id: 824, name: 'Richie Furay'},
  5764. * {id: 956, name: 'Dewey Martin'},
  5765. * {id: 313, name: 'Bruce Palmer'},
  5766. * {id: 456, name: 'Stephen Stills'},
  5767. * {id: 177, name: 'Neil Young'}
  5768. * ];
  5769. * var csny = [
  5770. * {id: 204, name: 'David Crosby'},
  5771. * {id: 456, name: 'Stephen Stills'},
  5772. * {id: 539, name: 'Graham Nash'},
  5773. * {id: 177, name: 'Neil Young'}
  5774. * ];
  5775. *
  5776. * R.intersectionWith(R.eqBy(R.prop('id')), buffaloSpringfield, csny);
  5777. * //=> [{id: 456, name: 'Stephen Stills'}, {id: 177, name: 'Neil Young'}]
  5778. */
  5779. var intersectionWith = _curry3(function intersectionWith(pred, list1, list2) {
  5780. var lookupList, filteredList;
  5781. if (list1.length > list2.length) {
  5782. lookupList = list1;
  5783. filteredList = list2;
  5784. } else {
  5785. lookupList = list2;
  5786. filteredList = list1;
  5787. }
  5788. var results = [];
  5789. var idx = 0;
  5790. while (idx < filteredList.length) {
  5791. if (_containsWith(pred, filteredList[idx], lookupList)) {
  5792. results[results.length] = filteredList[idx];
  5793. }
  5794. idx += 1;
  5795. }
  5796. return uniqWith(pred, results);
  5797. });
  5798. /**
  5799. * Transforms the items of the list with the transducer and appends the
  5800. * transformed items to the accumulator using an appropriate iterator function
  5801. * based on the accumulator type.
  5802. *
  5803. * The accumulator can be an array, string, object or a transformer. Iterated
  5804. * items will be appended to arrays and concatenated to strings. Objects will
  5805. * be merged directly or 2-item arrays will be merged as key, value pairs.
  5806. *
  5807. * The accumulator can also be a transformer object that provides a 2-arity
  5808. * reducing iterator function, step, 0-arity initial value function, init, and
  5809. * 1-arity result extraction function result. The step function is used as the
  5810. * iterator function in reduce. The result function is used to convert the
  5811. * final accumulator into the return type and in most cases is R.identity. The
  5812. * init function is used to provide the initial accumulator.
  5813. *
  5814. * The iteration is performed with [`R.reduce`](#reduce) after initializing the
  5815. * transducer.
  5816. *
  5817. * @func
  5818. * @memberOf R
  5819. * @since v0.12.0
  5820. * @category List
  5821. * @sig a -> (b -> b) -> [c] -> a
  5822. * @param {*} acc The initial accumulator value.
  5823. * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
  5824. * @param {Array} list The list to iterate over.
  5825. * @return {*} The final, accumulated value.
  5826. * @example
  5827. *
  5828. * var numbers = [1, 2, 3, 4];
  5829. * var transducer = R.compose(R.map(R.add(1)), R.take(2));
  5830. *
  5831. * R.into([], transducer, numbers); //=> [2, 3]
  5832. *
  5833. * var intoArray = R.into([]);
  5834. * intoArray(transducer, numbers); //=> [2, 3]
  5835. */
  5836. var into = _curry3(function into(acc, xf, list) {
  5837. return _isTransformer(acc) ? _reduce(xf(acc), acc['@@transducer/init'](), list) : _reduce(xf(_stepCat(acc)), _clone(acc, [], [], false), list);
  5838. });
  5839. /**
  5840. * Same as [`R.invertObj`](#invertObj), however this accounts for objects with
  5841. * duplicate values by putting the values into an array.
  5842. *
  5843. * @func
  5844. * @memberOf R
  5845. * @since v0.9.0
  5846. * @category Object
  5847. * @sig {s: x} -> {x: [ s, ... ]}
  5848. * @param {Object} obj The object or array to invert
  5849. * @return {Object} out A new object with keys in an array.
  5850. * @see R.invertObj
  5851. * @example
  5852. *
  5853. * var raceResultsByFirstName = {
  5854. * first: 'alice',
  5855. * second: 'jake',
  5856. * third: 'alice',
  5857. * };
  5858. * R.invert(raceResultsByFirstName);
  5859. * //=> { 'alice': ['first', 'third'], 'jake':['second'] }
  5860. */
  5861. var invert = _curry1(function invert(obj) {
  5862. var props = keys(obj);
  5863. var len = props.length;
  5864. var idx = 0;
  5865. var out = {};
  5866. while (idx < len) {
  5867. var key = props[idx];
  5868. var val = obj[key];
  5869. var list = _has(val, out) ? out[val] : out[val] = [];
  5870. list[list.length] = key;
  5871. idx += 1;
  5872. }
  5873. return out;
  5874. });
  5875. /**
  5876. * Returns a new object with the keys of the given object as values, and the
  5877. * values of the given object, which are coerced to strings, as keys. Note
  5878. * that the last key found is preferred when handling the same value.
  5879. *
  5880. * @func
  5881. * @memberOf R
  5882. * @since v0.9.0
  5883. * @category Object
  5884. * @sig {s: x} -> {x: s}
  5885. * @param {Object} obj The object or array to invert
  5886. * @return {Object} out A new object
  5887. * @see R.invert
  5888. * @example
  5889. *
  5890. * var raceResults = {
  5891. * first: 'alice',
  5892. * second: 'jake'
  5893. * };
  5894. * R.invertObj(raceResults);
  5895. * //=> { 'alice': 'first', 'jake':'second' }
  5896. *
  5897. * // Alternatively:
  5898. * var raceResults = ['alice', 'jake'];
  5899. * R.invertObj(raceResults);
  5900. * //=> { 'alice': '0', 'jake':'1' }
  5901. */
  5902. var invertObj = _curry1(function invertObj(obj) {
  5903. var props = keys(obj);
  5904. var len = props.length;
  5905. var idx = 0;
  5906. var out = {};
  5907. while (idx < len) {
  5908. var key = props[idx];
  5909. out[obj[key]] = key;
  5910. idx += 1;
  5911. }
  5912. return out;
  5913. });
  5914. /**
  5915. * Returns `true` if the given value is its type's empty value; `false`
  5916. * otherwise.
  5917. *
  5918. * @func
  5919. * @memberOf R
  5920. * @since v0.1.0
  5921. * @category Logic
  5922. * @sig a -> Boolean
  5923. * @param {*} x
  5924. * @return {Boolean}
  5925. * @see R.empty
  5926. * @example
  5927. *
  5928. * R.isEmpty([1, 2, 3]); //=> false
  5929. * R.isEmpty([]); //=> true
  5930. * R.isEmpty(''); //=> true
  5931. * R.isEmpty(null); //=> false
  5932. * R.isEmpty({}); //=> true
  5933. * R.isEmpty({length: 0}); //=> false
  5934. */
  5935. var isEmpty = _curry1(function isEmpty(x) {
  5936. return x != null && equals(x, empty(x));
  5937. });
  5938. /**
  5939. * Returns the last element of the given list or string.
  5940. *
  5941. * @func
  5942. * @memberOf R
  5943. * @since v0.1.4
  5944. * @category List
  5945. * @sig [a] -> a | Undefined
  5946. * @sig String -> String
  5947. * @param {*} list
  5948. * @return {*}
  5949. * @see R.init, R.head, R.tail
  5950. * @example
  5951. *
  5952. * R.last(['fi', 'fo', 'fum']); //=> 'fum'
  5953. * R.last([]); //=> undefined
  5954. *
  5955. * R.last('abc'); //=> 'c'
  5956. * R.last(''); //=> ''
  5957. */
  5958. var last = nth(-1);
  5959. /**
  5960. * Returns the position of the last occurrence of an item in an array, or -1 if
  5961. * the item is not included in the array. [`R.equals`](#equals) is used to
  5962. * determine equality.
  5963. *
  5964. * @func
  5965. * @memberOf R
  5966. * @since v0.1.0
  5967. * @category List
  5968. * @sig a -> [a] -> Number
  5969. * @param {*} target The item to find.
  5970. * @param {Array} xs The array to search in.
  5971. * @return {Number} the index of the target, or -1 if the target is not found.
  5972. * @see R.indexOf
  5973. * @example
  5974. *
  5975. * R.lastIndexOf(3, [-1,3,3,0,1,2,3,4]); //=> 6
  5976. * R.lastIndexOf(10, [1,2,3,4]); //=> -1
  5977. */
  5978. var lastIndexOf = _curry2(function lastIndexOf(target, xs) {
  5979. if (typeof xs.lastIndexOf === 'function' && !_isArray(xs)) {
  5980. return xs.lastIndexOf(target);
  5981. } else {
  5982. var idx = xs.length - 1;
  5983. while (idx >= 0) {
  5984. if (equals(xs[idx], target)) {
  5985. return idx;
  5986. }
  5987. idx -= 1;
  5988. }
  5989. return -1;
  5990. }
  5991. });
  5992. /**
  5993. * Takes a function and
  5994. * a [functor](https://github.com/fantasyland/fantasy-land#functor),
  5995. * applies the function to each of the functor's values, and returns
  5996. * a functor of the same shape.
  5997. *
  5998. * Ramda provides suitable `map` implementations for `Array` and `Object`,
  5999. * so this function may be applied to `[1, 2, 3]` or `{x: 1, y: 2, z: 3}`.
  6000. *
  6001. * Dispatches to the `map` method of the second argument, if present.
  6002. *
  6003. * Acts as a transducer if a transformer is given in list position.
  6004. *
  6005. * Also treats functions as functors and will compose them together.
  6006. *
  6007. * @func
  6008. * @memberOf R
  6009. * @since v0.1.0
  6010. * @category List
  6011. * @sig Functor f => (a -> b) -> f a -> f b
  6012. * @param {Function} fn The function to be called on every element of the input `list`.
  6013. * @param {Array} list The list to be iterated over.
  6014. * @return {Array} The new list.
  6015. * @see R.transduce, R.addIndex
  6016. * @example
  6017. *
  6018. * var double = x => x * 2;
  6019. *
  6020. * R.map(double, [1, 2, 3]); //=> [2, 4, 6]
  6021. *
  6022. * R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
  6023. * @symb R.map(f, [a, b]) = [f(a), f(b)]
  6024. * @symb R.map(f, { x: a, y: b }) = { x: f(a), y: f(b) }
  6025. * @symb R.map(f, functor_o) = functor_o.map(f)
  6026. */
  6027. var map = _curry2(_dispatchable([
  6028. 'fantasy-land/map',
  6029. 'map'
  6030. ], _xmap, function map(fn, functor) {
  6031. switch (Object.prototype.toString.call(functor)) {
  6032. case '[object Function]':
  6033. return curryN(functor.length, function () {
  6034. return fn.call(this, functor.apply(this, arguments));
  6035. });
  6036. case '[object Object]':
  6037. return _reduce(function (acc, key) {
  6038. acc[key] = fn(functor[key]);
  6039. return acc;
  6040. }, {}, keys(functor));
  6041. default:
  6042. return _map(fn, functor);
  6043. }
  6044. }));
  6045. /**
  6046. * An Object-specific version of [`map`](#map). The function is applied to three
  6047. * arguments: *(value, key, obj)*. If only the value is significant, use
  6048. * [`map`](#map) instead.
  6049. *
  6050. * @func
  6051. * @memberOf R
  6052. * @since v0.9.0
  6053. * @category Object
  6054. * @sig ((*, String, Object) -> *) -> Object -> Object
  6055. * @param {Function} fn
  6056. * @param {Object} obj
  6057. * @return {Object}
  6058. * @see R.map
  6059. * @example
  6060. *
  6061. * var values = { x: 1, y: 2, z: 3 };
  6062. * var prependKeyAndDouble = (num, key, obj) => key + (num * 2);
  6063. *
  6064. * R.mapObjIndexed(prependKeyAndDouble, values); //=> { x: 'x2', y: 'y4', z: 'z6' }
  6065. */
  6066. var mapObjIndexed = _curry2(function mapObjIndexed(fn, obj) {
  6067. return _reduce(function (acc, key) {
  6068. acc[key] = fn(obj[key], key, obj);
  6069. return acc;
  6070. }, {}, keys(obj));
  6071. });
  6072. /**
  6073. * Creates a new object with the own properties of the two provided objects.
  6074. * If a key exists in both objects:
  6075. * - and both associated values are also objects then the values will be
  6076. * recursively merged.
  6077. * - otherwise the provided function is applied to the key and associated values
  6078. * using the resulting value as the new value associated with the key.
  6079. * If a key only exists in one object, the value will be associated with the key
  6080. * of the resulting object.
  6081. *
  6082. * @func
  6083. * @memberOf R
  6084. * @since v0.24.0
  6085. * @category Object
  6086. * @sig (String -> a -> a -> a) -> {a} -> {a} -> {a}
  6087. * @param {Function} fn
  6088. * @param {Object} lObj
  6089. * @param {Object} rObj
  6090. * @return {Object}
  6091. * @see R.mergeWithKey, R.mergeDeep, R.mergeDeepWith
  6092. * @example
  6093. *
  6094. * let concatValues = (k, l, r) => k == 'values' ? R.concat(l, r) : r
  6095. * R.mergeDeepWithKey(concatValues,
  6096. * { a: true, c: { thing: 'foo', values: [10, 20] }},
  6097. * { b: true, c: { thing: 'bar', values: [15, 35] }});
  6098. * //=> { a: true, b: true, c: { thing: 'bar', values: [10, 20, 15, 35] }}
  6099. */
  6100. var mergeDeepWithKey = _curry3(function mergeDeepWithKey(fn, lObj, rObj) {
  6101. return mergeWithKey(function (k, lVal, rVal) {
  6102. if (_isObject(lVal) && _isObject(rVal)) {
  6103. return mergeDeepWithKey(fn, lVal, rVal);
  6104. } else {
  6105. return fn(k, lVal, rVal);
  6106. }
  6107. }, lObj, rObj);
  6108. });
  6109. /**
  6110. * Creates a new object with the own properties of the two provided objects. If
  6111. * a key exists in both objects, the provided function is applied to the values
  6112. * associated with the key in each object, with the result being used as the
  6113. * value associated with the key in the returned object.
  6114. *
  6115. * @func
  6116. * @memberOf R
  6117. * @since v0.19.0
  6118. * @category Object
  6119. * @sig (a -> a -> a) -> {a} -> {a} -> {a}
  6120. * @param {Function} fn
  6121. * @param {Object} l
  6122. * @param {Object} r
  6123. * @return {Object}
  6124. * @see R.mergeDeepWith, R.merge, R.mergeWithKey
  6125. * @example
  6126. *
  6127. * R.mergeWith(R.concat,
  6128. * { a: true, values: [10, 20] },
  6129. * { b: true, values: [15, 35] });
  6130. * //=> { a: true, b: true, values: [10, 20, 15, 35] }
  6131. */
  6132. var mergeWith = _curry3(function mergeWith(fn, l, r) {
  6133. return mergeWithKey(function (_, _l, _r) {
  6134. return fn(_l, _r);
  6135. }, l, r);
  6136. });
  6137. /**
  6138. * Takes a function `f` and a list of arguments, and returns a function `g`.
  6139. * When applied, `g` returns the result of applying `f` to the arguments
  6140. * provided initially followed by the arguments provided to `g`.
  6141. *
  6142. * @func
  6143. * @memberOf R
  6144. * @since v0.10.0
  6145. * @category Function
  6146. * @sig ((a, b, c, ..., n) -> x) -> [a, b, c, ...] -> ((d, e, f, ..., n) -> x)
  6147. * @param {Function} f
  6148. * @param {Array} args
  6149. * @return {Function}
  6150. * @see R.partialRight
  6151. * @example
  6152. *
  6153. * var multiply2 = (a, b) => a * b;
  6154. * var double = R.partial(multiply2, [2]);
  6155. * double(2); //=> 4
  6156. *
  6157. * var greet = (salutation, title, firstName, lastName) =>
  6158. * salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
  6159. *
  6160. * var sayHello = R.partial(greet, ['Hello']);
  6161. * var sayHelloToMs = R.partial(sayHello, ['Ms.']);
  6162. * sayHelloToMs('Jane', 'Jones'); //=> 'Hello, Ms. Jane Jones!'
  6163. * @symb R.partial(f, [a, b])(c, d) = f(a, b, c, d)
  6164. */
  6165. var partial = _createPartialApplicator(_concat);
  6166. /**
  6167. * Takes a function `f` and a list of arguments, and returns a function `g`.
  6168. * When applied, `g` returns the result of applying `f` to the arguments
  6169. * provided to `g` followed by the arguments provided initially.
  6170. *
  6171. * @func
  6172. * @memberOf R
  6173. * @since v0.10.0
  6174. * @category Function
  6175. * @sig ((a, b, c, ..., n) -> x) -> [d, e, f, ..., n] -> ((a, b, c, ...) -> x)
  6176. * @param {Function} f
  6177. * @param {Array} args
  6178. * @return {Function}
  6179. * @see R.partial
  6180. * @example
  6181. *
  6182. * var greet = (salutation, title, firstName, lastName) =>
  6183. * salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
  6184. *
  6185. * var greetMsJaneJones = R.partialRight(greet, ['Ms.', 'Jane', 'Jones']);
  6186. *
  6187. * greetMsJaneJones('Hello'); //=> 'Hello, Ms. Jane Jones!'
  6188. * @symb R.partialRight(f, [a, b])(c, d) = f(c, d, a, b)
  6189. */
  6190. var partialRight = _createPartialApplicator(flip(_concat));
  6191. /**
  6192. * Determines whether a nested path on an object has a specific value, in
  6193. * [`R.equals`](#equals) terms. Most likely used to filter a list.
  6194. *
  6195. * @func
  6196. * @memberOf R
  6197. * @since v0.7.0
  6198. * @category Relation
  6199. * @typedefn Idx = String | Int
  6200. * @sig [Idx] -> a -> {a} -> Boolean
  6201. * @param {Array} path The path of the nested property to use
  6202. * @param {*} val The value to compare the nested property with
  6203. * @param {Object} obj The object to check the nested property in
  6204. * @return {Boolean} `true` if the value equals the nested object property,
  6205. * `false` otherwise.
  6206. * @example
  6207. *
  6208. * var user1 = { address: { zipCode: 90210 } };
  6209. * var user2 = { address: { zipCode: 55555 } };
  6210. * var user3 = { name: 'Bob' };
  6211. * var users = [ user1, user2, user3 ];
  6212. * var isFamous = R.pathEq(['address', 'zipCode'], 90210);
  6213. * R.filter(isFamous, users); //=> [ user1 ]
  6214. */
  6215. var pathEq = _curry3(function pathEq(_path, val, obj) {
  6216. return equals(path(_path, obj), val);
  6217. });
  6218. /**
  6219. * Returns a new list by plucking the same named property off all objects in
  6220. * the list supplied.
  6221. *
  6222. * `pluck` will work on
  6223. * any [functor](https://github.com/fantasyland/fantasy-land#functor) in
  6224. * addition to arrays, as it is equivalent to `R.map(R.prop(k), f)`.
  6225. *
  6226. * @func
  6227. * @memberOf R
  6228. * @since v0.1.0
  6229. * @category List
  6230. * @sig Functor f => k -> f {k: v} -> f v
  6231. * @param {Number|String} key The key name to pluck off of each object.
  6232. * @param {Array} f The array or functor to consider.
  6233. * @return {Array} The list of values for the given key.
  6234. * @see R.props
  6235. * @example
  6236. *
  6237. * R.pluck('a')([{a: 1}, {a: 2}]); //=> [1, 2]
  6238. * R.pluck(0)([[1, 2], [3, 4]]); //=> [1, 3]
  6239. * R.pluck('val', {a: {val: 3}, b: {val: 5}}); //=> {a: 3, b: 5}
  6240. * @symb R.pluck('x', [{x: 1, y: 2}, {x: 3, y: 4}, {x: 5, y: 6}]) = [1, 3, 5]
  6241. * @symb R.pluck(0, [[1, 2], [3, 4], [5, 6]]) = [1, 3, 5]
  6242. */
  6243. var pluck = _curry2(function pluck(p, list) {
  6244. return map(prop(p), list);
  6245. });
  6246. /**
  6247. * Reasonable analog to SQL `select` statement.
  6248. *
  6249. * @func
  6250. * @memberOf R
  6251. * @since v0.1.0
  6252. * @category Object
  6253. * @category Relation
  6254. * @sig [k] -> [{k: v}] -> [{k: v}]
  6255. * @param {Array} props The property names to project
  6256. * @param {Array} objs The objects to query
  6257. * @return {Array} An array of objects with just the `props` properties.
  6258. * @example
  6259. *
  6260. * var abby = {name: 'Abby', age: 7, hair: 'blond', grade: 2};
  6261. * var fred = {name: 'Fred', age: 12, hair: 'brown', grade: 7};
  6262. * var kids = [abby, fred];
  6263. * R.project(['name', 'grade'], kids); //=> [{name: 'Abby', grade: 2}, {name: 'Fred', grade: 7}]
  6264. */
  6265. // passing `identity` gives correct arity
  6266. var project = useWith(_map, [
  6267. pickAll,
  6268. identity
  6269. ]);
  6270. /**
  6271. * Returns `true` if the specified object property is equal, in
  6272. * [`R.equals`](#equals) terms, to the given value; `false` otherwise.
  6273. *
  6274. * @func
  6275. * @memberOf R
  6276. * @since v0.1.0
  6277. * @category Relation
  6278. * @sig String -> a -> Object -> Boolean
  6279. * @param {String} name
  6280. * @param {*} val
  6281. * @param {*} obj
  6282. * @return {Boolean}
  6283. * @see R.equals, R.propSatisfies
  6284. * @example
  6285. *
  6286. * var abby = {name: 'Abby', age: 7, hair: 'blond'};
  6287. * var fred = {name: 'Fred', age: 12, hair: 'brown'};
  6288. * var rusty = {name: 'Rusty', age: 10, hair: 'brown'};
  6289. * var alois = {name: 'Alois', age: 15, disposition: 'surly'};
  6290. * var kids = [abby, fred, rusty, alois];
  6291. * var hasBrownHair = R.propEq('hair', 'brown');
  6292. * R.filter(hasBrownHair, kids); //=> [fred, rusty]
  6293. */
  6294. var propEq = _curry3(function propEq(name, val, obj) {
  6295. return equals(val, obj[name]);
  6296. });
  6297. /**
  6298. * Returns a single item by iterating through the list, successively calling
  6299. * the iterator function and passing it an accumulator value and the current
  6300. * value from the array, and then passing the result to the next call.
  6301. *
  6302. * The iterator function receives two values: *(acc, value)*. It may use
  6303. * [`R.reduced`](#reduced) to shortcut the iteration.
  6304. *
  6305. * The arguments' order of [`reduceRight`](#reduceRight)'s iterator function
  6306. * is *(value, acc)*.
  6307. *
  6308. * Note: `R.reduce` does not skip deleted or unassigned indices (sparse
  6309. * arrays), unlike the native `Array.prototype.reduce` method. For more details
  6310. * on this behavior, see:
  6311. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Description
  6312. *
  6313. * Dispatches to the `reduce` method of the third argument, if present. When
  6314. * doing so, it is up to the user to handle the [`R.reduced`](#reduced)
  6315. * shortcuting, as this is not implemented by `reduce`.
  6316. *
  6317. * @func
  6318. * @memberOf R
  6319. * @since v0.1.0
  6320. * @category List
  6321. * @sig ((a, b) -> a) -> a -> [b] -> a
  6322. * @param {Function} fn The iterator function. Receives two values, the accumulator and the
  6323. * current element from the array.
  6324. * @param {*} acc The accumulator value.
  6325. * @param {Array} list The list to iterate over.
  6326. * @return {*} The final, accumulated value.
  6327. * @see R.reduced, R.addIndex, R.reduceRight
  6328. * @example
  6329. *
  6330. * R.reduce(R.subtract, 0, [1, 2, 3, 4]) // => ((((0 - 1) - 2) - 3) - 4) = -10
  6331. * - -10
  6332. * / \ / \
  6333. * - 4 -6 4
  6334. * / \ / \
  6335. * - 3 ==> -3 3
  6336. * / \ / \
  6337. * - 2 -1 2
  6338. * / \ / \
  6339. * 0 1 0 1
  6340. *
  6341. * @symb R.reduce(f, a, [b, c, d]) = f(f(f(a, b), c), d)
  6342. */
  6343. var reduce = _curry3(_reduce);
  6344. /**
  6345. * Groups the elements of the list according to the result of calling
  6346. * the String-returning function `keyFn` on each element and reduces the elements
  6347. * of each group to a single value via the reducer function `valueFn`.
  6348. *
  6349. * This function is basically a more general [`groupBy`](#groupBy) function.
  6350. *
  6351. * Acts as a transducer if a transformer is given in list position.
  6352. *
  6353. * @func
  6354. * @memberOf R
  6355. * @since v0.20.0
  6356. * @category List
  6357. * @sig ((a, b) -> a) -> a -> (b -> String) -> [b] -> {String: a}
  6358. * @param {Function} valueFn The function that reduces the elements of each group to a single
  6359. * value. Receives two values, accumulator for a particular group and the current element.
  6360. * @param {*} acc The (initial) accumulator value for each group.
  6361. * @param {Function} keyFn The function that maps the list's element into a key.
  6362. * @param {Array} list The array to group.
  6363. * @return {Object} An object with the output of `keyFn` for keys, mapped to the output of
  6364. * `valueFn` for elements which produced that key when passed to `keyFn`.
  6365. * @see R.groupBy, R.reduce
  6366. * @example
  6367. *
  6368. * var reduceToNamesBy = R.reduceBy((acc, student) => acc.concat(student.name), []);
  6369. * var namesByGrade = reduceToNamesBy(function(student) {
  6370. * var score = student.score;
  6371. * return score < 65 ? 'F' :
  6372. * score < 70 ? 'D' :
  6373. * score < 80 ? 'C' :
  6374. * score < 90 ? 'B' : 'A';
  6375. * });
  6376. * var students = [{name: 'Lucy', score: 92},
  6377. * {name: 'Drew', score: 85},
  6378. * // ...
  6379. * {name: 'Bart', score: 62}];
  6380. * namesByGrade(students);
  6381. * // {
  6382. * // 'A': ['Lucy'],
  6383. * // 'B': ['Drew']
  6384. * // // ...,
  6385. * // 'F': ['Bart']
  6386. * // }
  6387. */
  6388. var reduceBy = _curryN(4, [], _dispatchable([], _xreduceBy, function reduceBy(valueFn, valueAcc, keyFn, list) {
  6389. return _reduce(function (acc, elt) {
  6390. var key = keyFn(elt);
  6391. acc[key] = valueFn(_has(key, acc) ? acc[key] : valueAcc, elt);
  6392. return acc;
  6393. }, {}, list);
  6394. }));
  6395. /**
  6396. * Like [`reduce`](#reduce), `reduceWhile` returns a single item by iterating
  6397. * through the list, successively calling the iterator function. `reduceWhile`
  6398. * also takes a predicate that is evaluated before each step. If the predicate
  6399. * returns `false`, it "short-circuits" the iteration and returns the current
  6400. * value of the accumulator.
  6401. *
  6402. * @func
  6403. * @memberOf R
  6404. * @since v0.22.0
  6405. * @category List
  6406. * @sig ((a, b) -> Boolean) -> ((a, b) -> a) -> a -> [b] -> a
  6407. * @param {Function} pred The predicate. It is passed the accumulator and the
  6408. * current element.
  6409. * @param {Function} fn The iterator function. Receives two values, the
  6410. * accumulator and the current element.
  6411. * @param {*} a The accumulator value.
  6412. * @param {Array} list The list to iterate over.
  6413. * @return {*} The final, accumulated value.
  6414. * @see R.reduce, R.reduced
  6415. * @example
  6416. *
  6417. * var isOdd = (acc, x) => x % 2 === 1;
  6418. * var xs = [1, 3, 5, 60, 777, 800];
  6419. * R.reduceWhile(isOdd, R.add, 0, xs); //=> 9
  6420. *
  6421. * var ys = [2, 4, 6]
  6422. * R.reduceWhile(isOdd, R.add, 111, ys); //=> 111
  6423. */
  6424. var reduceWhile = _curryN(4, [], function _reduceWhile(pred, fn, a, list) {
  6425. return _reduce(function (acc, x) {
  6426. return pred(acc, x) ? fn(acc, x) : _reduced(acc);
  6427. }, a, list);
  6428. });
  6429. /**
  6430. * The complement of [`filter`](#filter).
  6431. *
  6432. * Acts as a transducer if a transformer is given in list position. Filterable
  6433. * objects include plain objects or any object that has a filter method such
  6434. * as `Array`.
  6435. *
  6436. * @func
  6437. * @memberOf R
  6438. * @since v0.1.0
  6439. * @category List
  6440. * @sig Filterable f => (a -> Boolean) -> f a -> f a
  6441. * @param {Function} pred
  6442. * @param {Array} filterable
  6443. * @return {Array}
  6444. * @see R.filter, R.transduce, R.addIndex
  6445. * @example
  6446. *
  6447. * var isOdd = (n) => n % 2 === 1;
  6448. *
  6449. * R.reject(isOdd, [1, 2, 3, 4]); //=> [2, 4]
  6450. *
  6451. * R.reject(isOdd, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
  6452. */
  6453. var reject = _curry2(function reject(pred, filterable) {
  6454. return filter(_complement(pred), filterable);
  6455. });
  6456. /**
  6457. * Returns a fixed list of size `n` containing a specified identical value.
  6458. *
  6459. * @func
  6460. * @memberOf R
  6461. * @since v0.1.1
  6462. * @category List
  6463. * @sig a -> n -> [a]
  6464. * @param {*} value The value to repeat.
  6465. * @param {Number} n The desired size of the output list.
  6466. * @return {Array} A new array containing `n` `value`s.
  6467. * @see R.times
  6468. * @example
  6469. *
  6470. * R.repeat('hi', 5); //=> ['hi', 'hi', 'hi', 'hi', 'hi']
  6471. *
  6472. * var obj = {};
  6473. * var repeatedObjs = R.repeat(obj, 5); //=> [{}, {}, {}, {}, {}]
  6474. * repeatedObjs[0] === repeatedObjs[1]; //=> true
  6475. * @symb R.repeat(a, 0) = []
  6476. * @symb R.repeat(a, 1) = [a]
  6477. * @symb R.repeat(a, 2) = [a, a]
  6478. */
  6479. var repeat = _curry2(function repeat(value, n) {
  6480. return times(always(value), n);
  6481. });
  6482. /**
  6483. * Checks if a list starts with the provided values
  6484. *
  6485. * @func
  6486. * @memberOf R
  6487. * @since v0.24.0
  6488. * @category List
  6489. * @sig [a] -> Boolean
  6490. * @sig String -> Boolean
  6491. * @param {*} prefix
  6492. * @param {*} list
  6493. * @return {Boolean}
  6494. * @example
  6495. *
  6496. * R.startsWith('a', 'abc') //=> true
  6497. * R.startsWith('b', 'abc') //=> false
  6498. * R.startsWith(['a'], ['a', 'b', 'c']) //=> true
  6499. * R.startsWith(['b'], ['a', 'b', 'c']) //=> false
  6500. */
  6501. var startsWith = _curry2(function (prefix, list) {
  6502. return equals(take(prefix.length, list), prefix);
  6503. });
  6504. /**
  6505. * Adds together all the elements of a list.
  6506. *
  6507. * @func
  6508. * @memberOf R
  6509. * @since v0.1.0
  6510. * @category Math
  6511. * @sig [Number] -> Number
  6512. * @param {Array} list An array of numbers
  6513. * @return {Number} The sum of all the numbers in the list.
  6514. * @see R.reduce
  6515. * @example
  6516. *
  6517. * R.sum([2,4,6,8,100,1]); //=> 121
  6518. */
  6519. var sum = reduce(add, 0);
  6520. /**
  6521. * Returns a new list containing the last `n` elements of the given list.
  6522. * If `n > list.length`, returns a list of `list.length` elements.
  6523. *
  6524. * @func
  6525. * @memberOf R
  6526. * @since v0.16.0
  6527. * @category List
  6528. * @sig Number -> [a] -> [a]
  6529. * @sig Number -> String -> String
  6530. * @param {Number} n The number of elements to return.
  6531. * @param {Array} xs The collection to consider.
  6532. * @return {Array}
  6533. * @see R.dropLast
  6534. * @example
  6535. *
  6536. * R.takeLast(1, ['foo', 'bar', 'baz']); //=> ['baz']
  6537. * R.takeLast(2, ['foo', 'bar', 'baz']); //=> ['bar', 'baz']
  6538. * R.takeLast(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  6539. * R.takeLast(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  6540. * R.takeLast(3, 'ramda'); //=> 'mda'
  6541. */
  6542. var takeLast = _curry2(function takeLast(n, xs) {
  6543. return drop(n >= 0 ? xs.length - n : 0, xs);
  6544. });
  6545. /**
  6546. * Initializes a transducer using supplied iterator function. Returns a single
  6547. * item by iterating through the list, successively calling the transformed
  6548. * iterator function and passing it an accumulator value and the current value
  6549. * from the array, and then passing the result to the next call.
  6550. *
  6551. * The iterator function receives two values: *(acc, value)*. It will be
  6552. * wrapped as a transformer to initialize the transducer. A transformer can be
  6553. * passed directly in place of an iterator function. In both cases, iteration
  6554. * may be stopped early with the [`R.reduced`](#reduced) function.
  6555. *
  6556. * A transducer is a function that accepts a transformer and returns a
  6557. * transformer and can be composed directly.
  6558. *
  6559. * A transformer is an an object that provides a 2-arity reducing iterator
  6560. * function, step, 0-arity initial value function, init, and 1-arity result
  6561. * extraction function, result. The step function is used as the iterator
  6562. * function in reduce. The result function is used to convert the final
  6563. * accumulator into the return type and in most cases is
  6564. * [`R.identity`](#identity). The init function can be used to provide an
  6565. * initial accumulator, but is ignored by transduce.
  6566. *
  6567. * The iteration is performed with [`R.reduce`](#reduce) after initializing the transducer.
  6568. *
  6569. * @func
  6570. * @memberOf R
  6571. * @since v0.12.0
  6572. * @category List
  6573. * @sig (c -> c) -> ((a, b) -> a) -> a -> [b] -> a
  6574. * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
  6575. * @param {Function} fn The iterator function. Receives two values, the accumulator and the
  6576. * current element from the array. Wrapped as transformer, if necessary, and used to
  6577. * initialize the transducer
  6578. * @param {*} acc The initial accumulator value.
  6579. * @param {Array} list The list to iterate over.
  6580. * @return {*} The final, accumulated value.
  6581. * @see R.reduce, R.reduced, R.into
  6582. * @example
  6583. *
  6584. * var numbers = [1, 2, 3, 4];
  6585. * var transducer = R.compose(R.map(R.add(1)), R.take(2));
  6586. * R.transduce(transducer, R.flip(R.append), [], numbers); //=> [2, 3]
  6587. *
  6588. * var isOdd = (x) => x % 2 === 1;
  6589. * var firstOddTransducer = R.compose(R.filter(isOdd), R.take(1));
  6590. * R.transduce(firstOddTransducer, R.flip(R.append), [], R.range(0, 100)); //=> [1]
  6591. */
  6592. var transduce = curryN(4, function transduce(xf, fn, acc, list) {
  6593. return _reduce(xf(typeof fn === 'function' ? _xwrap(fn) : fn), acc, list);
  6594. });
  6595. /**
  6596. * Combines two lists into a set (i.e. no duplicates) composed of the elements
  6597. * of each list. Duplication is determined according to the value returned by
  6598. * applying the supplied predicate to two list elements.
  6599. *
  6600. * @func
  6601. * @memberOf R
  6602. * @since v0.1.0
  6603. * @category Relation
  6604. * @sig (a -> a -> Boolean) -> [*] -> [*] -> [*]
  6605. * @param {Function} pred A predicate used to test whether two items are equal.
  6606. * @param {Array} list1 The first list.
  6607. * @param {Array} list2 The second list.
  6608. * @return {Array} The first and second lists concatenated, with
  6609. * duplicates removed.
  6610. * @see R.union
  6611. * @example
  6612. *
  6613. * var l1 = [{a: 1}, {a: 2}];
  6614. * var l2 = [{a: 1}, {a: 4}];
  6615. * R.unionWith(R.eqBy(R.prop('a')), l1, l2); //=> [{a: 1}, {a: 2}, {a: 4}]
  6616. */
  6617. var unionWith = _curry3(function unionWith(pred, list1, list2) {
  6618. return uniqWith(pred, _concat(list1, list2));
  6619. });
  6620. /**
  6621. * Takes a spec object and a test object; returns true if the test satisfies
  6622. * the spec, false otherwise. An object satisfies the spec if, for each of the
  6623. * spec's own properties, accessing that property of the object gives the same
  6624. * value (in [`R.equals`](#equals) terms) as accessing that property of the
  6625. * spec.
  6626. *
  6627. * `whereEq` is a specialization of [`where`](#where).
  6628. *
  6629. * @func
  6630. * @memberOf R
  6631. * @since v0.14.0
  6632. * @category Object
  6633. * @sig {String: *} -> {String: *} -> Boolean
  6634. * @param {Object} spec
  6635. * @param {Object} testObj
  6636. * @return {Boolean}
  6637. * @see R.where
  6638. * @example
  6639. *
  6640. * // pred :: Object -> Boolean
  6641. * var pred = R.whereEq({a: 1, b: 2});
  6642. *
  6643. * pred({a: 1}); //=> false
  6644. * pred({a: 1, b: 2}); //=> true
  6645. * pred({a: 1, b: 2, c: 3}); //=> true
  6646. * pred({a: 1, b: 1}); //=> false
  6647. */
  6648. var whereEq = _curry2(function whereEq(spec, testObj) {
  6649. return where(map(equals, spec), testObj);
  6650. });
  6651. var _flatCat = function () {
  6652. var preservingReduced = function (xf) {
  6653. return {
  6654. '@@transducer/init': _xfBase.init,
  6655. '@@transducer/result': function (result) {
  6656. return xf['@@transducer/result'](result);
  6657. },
  6658. '@@transducer/step': function (result, input) {
  6659. var ret = xf['@@transducer/step'](result, input);
  6660. return ret['@@transducer/reduced'] ? _forceReduced(ret) : ret;
  6661. }
  6662. };
  6663. };
  6664. return function _xcat(xf) {
  6665. var rxf = preservingReduced(xf);
  6666. return {
  6667. '@@transducer/init': _xfBase.init,
  6668. '@@transducer/result': function (result) {
  6669. return rxf['@@transducer/result'](result);
  6670. },
  6671. '@@transducer/step': function (result, input) {
  6672. return !_isArrayLike(input) ? _reduce(rxf, result, [input]) : _reduce(rxf, result, input);
  6673. }
  6674. };
  6675. };
  6676. }();
  6677. // Array.prototype.indexOf doesn't exist below IE9
  6678. // manually crawl the list to distinguish between +0 and -0
  6679. // NaN
  6680. // non-zero numbers can utilise Set
  6681. // all these types can utilise Set
  6682. // null can utilise Set
  6683. // anything else not covered above, defer to R.equals
  6684. var _indexOf = function _indexOf(list, a, idx) {
  6685. var inf, item;
  6686. // Array.prototype.indexOf doesn't exist below IE9
  6687. if (typeof list.indexOf === 'function') {
  6688. switch (typeof a) {
  6689. case 'number':
  6690. if (a === 0) {
  6691. // manually crawl the list to distinguish between +0 and -0
  6692. inf = 1 / a;
  6693. while (idx < list.length) {
  6694. item = list[idx];
  6695. if (item === 0 && 1 / item === inf) {
  6696. return idx;
  6697. }
  6698. idx += 1;
  6699. }
  6700. return -1;
  6701. } else if (a !== a) {
  6702. // NaN
  6703. while (idx < list.length) {
  6704. item = list[idx];
  6705. if (typeof item === 'number' && item !== item) {
  6706. return idx;
  6707. }
  6708. idx += 1;
  6709. }
  6710. return -1;
  6711. }
  6712. // non-zero numbers can utilise Set
  6713. return list.indexOf(a, idx);
  6714. // all these types can utilise Set
  6715. case 'string':
  6716. case 'boolean':
  6717. case 'function':
  6718. case 'undefined':
  6719. return list.indexOf(a, idx);
  6720. case 'object':
  6721. if (a === null) {
  6722. // null can utilise Set
  6723. return list.indexOf(a, idx);
  6724. }
  6725. }
  6726. }
  6727. // anything else not covered above, defer to R.equals
  6728. while (idx < list.length) {
  6729. if (equals(list[idx], a)) {
  6730. return idx;
  6731. }
  6732. idx += 1;
  6733. }
  6734. return -1;
  6735. };
  6736. var _xchain = _curry2(function _xchain(f, xf) {
  6737. return map(f, _flatCat(xf));
  6738. });
  6739. /**
  6740. * Takes a list of predicates and returns a predicate that returns true for a
  6741. * given list of arguments if every one of the provided predicates is satisfied
  6742. * by those arguments.
  6743. *
  6744. * The function returned is a curried function whose arity matches that of the
  6745. * highest-arity predicate.
  6746. *
  6747. * @func
  6748. * @memberOf R
  6749. * @since v0.9.0
  6750. * @category Logic
  6751. * @sig [(*... -> Boolean)] -> (*... -> Boolean)
  6752. * @param {Array} predicates An array of predicates to check
  6753. * @return {Function} The combined predicate
  6754. * @see R.anyPass
  6755. * @example
  6756. *
  6757. * var isQueen = R.propEq('rank', 'Q');
  6758. * var isSpade = R.propEq('suit', '♠︎');
  6759. * var isQueenOfSpades = R.allPass([isQueen, isSpade]);
  6760. *
  6761. * isQueenOfSpades({rank: 'Q', suit: '♣︎'}); //=> false
  6762. * isQueenOfSpades({rank: 'Q', suit: '♠︎'}); //=> true
  6763. */
  6764. var allPass = _curry1(function allPass(preds) {
  6765. return curryN(reduce(max, 0, pluck('length', preds)), function () {
  6766. var idx = 0;
  6767. var len = preds.length;
  6768. while (idx < len) {
  6769. if (!preds[idx].apply(this, arguments)) {
  6770. return false;
  6771. }
  6772. idx += 1;
  6773. }
  6774. return true;
  6775. });
  6776. });
  6777. /**
  6778. * Takes a list of predicates and returns a predicate that returns true for a
  6779. * given list of arguments if at least one of the provided predicates is
  6780. * satisfied by those arguments.
  6781. *
  6782. * The function returned is a curried function whose arity matches that of the
  6783. * highest-arity predicate.
  6784. *
  6785. * @func
  6786. * @memberOf R
  6787. * @since v0.9.0
  6788. * @category Logic
  6789. * @sig [(*... -> Boolean)] -> (*... -> Boolean)
  6790. * @param {Array} predicates An array of predicates to check
  6791. * @return {Function} The combined predicate
  6792. * @see R.allPass
  6793. * @example
  6794. *
  6795. * var isClub = R.propEq('suit', '♣');
  6796. * var isSpade = R.propEq('suit', '♠');
  6797. * var isBlackCard = R.anyPass([isClub, isSpade]);
  6798. *
  6799. * isBlackCard({rank: '10', suit: '♣'}); //=> true
  6800. * isBlackCard({rank: 'Q', suit: '♠'}); //=> true
  6801. * isBlackCard({rank: 'Q', suit: '♦'}); //=> false
  6802. */
  6803. var anyPass = _curry1(function anyPass(preds) {
  6804. return curryN(reduce(max, 0, pluck('length', preds)), function () {
  6805. var idx = 0;
  6806. var len = preds.length;
  6807. while (idx < len) {
  6808. if (preds[idx].apply(this, arguments)) {
  6809. return true;
  6810. }
  6811. idx += 1;
  6812. }
  6813. return false;
  6814. });
  6815. });
  6816. /**
  6817. * ap applies a list of functions to a list of values.
  6818. *
  6819. * Dispatches to the `ap` method of the second argument, if present. Also
  6820. * treats curried functions as applicatives.
  6821. *
  6822. * @func
  6823. * @memberOf R
  6824. * @since v0.3.0
  6825. * @category Function
  6826. * @sig [a -> b] -> [a] -> [b]
  6827. * @sig Apply f => f (a -> b) -> f a -> f b
  6828. * @param {*} applyF
  6829. * @param {*} applyX
  6830. * @return {*}
  6831. * @example
  6832. *
  6833. * R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6]
  6834. * R.ap([R.concat('tasty '), R.toUpper], ['pizza', 'salad']); //=> ["tasty pizza", "tasty salad", "PIZZA", "SALAD"]
  6835. * @symb R.ap([f, g], [a, b]) = [f(a), f(b), g(a), g(b)]
  6836. */
  6837. // else
  6838. var ap = _curry2(function ap(applyF, applyX) {
  6839. return typeof applyX['fantasy-land/ap'] === 'function' ? applyX['fantasy-land/ap'](applyF) : typeof applyF.ap === 'function' ? applyF.ap(applyX) : typeof applyF === 'function' ? function (x) {
  6840. return applyF(x)(applyX(x));
  6841. } : // else
  6842. _reduce(function (acc, f) {
  6843. return _concat(acc, map(f, applyX));
  6844. }, [], applyF);
  6845. });
  6846. /**
  6847. * Given a spec object recursively mapping properties to functions, creates a
  6848. * function producing an object of the same structure, by mapping each property
  6849. * to the result of calling its associated function with the supplied arguments.
  6850. *
  6851. * @func
  6852. * @memberOf R
  6853. * @since v0.20.0
  6854. * @category Function
  6855. * @sig {k: ((a, b, ..., m) -> v)} -> ((a, b, ..., m) -> {k: v})
  6856. * @param {Object} spec an object recursively mapping properties to functions for
  6857. * producing the values for these properties.
  6858. * @return {Function} A function that returns an object of the same structure
  6859. * as `spec', with each property set to the value returned by calling its
  6860. * associated function with the supplied arguments.
  6861. * @see R.converge, R.juxt
  6862. * @example
  6863. *
  6864. * var getMetrics = R.applySpec({
  6865. * sum: R.add,
  6866. * nested: { mul: R.multiply }
  6867. * });
  6868. * getMetrics(2, 4); // => { sum: 6, nested: { mul: 8 } }
  6869. * @symb R.applySpec({ x: f, y: { z: g } })(a, b) = { x: f(a, b), y: { z: g(a, b) } }
  6870. */
  6871. var applySpec = _curry1(function applySpec(spec) {
  6872. spec = map(function (v) {
  6873. return typeof v == 'function' ? v : applySpec(v);
  6874. }, spec);
  6875. return curryN(reduce(max, 0, pluck('length', values(spec))), function () {
  6876. var args = arguments;
  6877. return map(function (f) {
  6878. return apply(f, args);
  6879. }, spec);
  6880. });
  6881. });
  6882. /**
  6883. * Returns the result of calling its first argument with the remaining
  6884. * arguments. This is occasionally useful as a converging function for
  6885. * [`R.converge`](#converge): the first branch can produce a function while the
  6886. * remaining branches produce values to be passed to that function as its
  6887. * arguments.
  6888. *
  6889. * @func
  6890. * @memberOf R
  6891. * @since v0.9.0
  6892. * @category Function
  6893. * @sig (*... -> a),*... -> a
  6894. * @param {Function} fn The function to apply to the remaining arguments.
  6895. * @param {...*} args Any number of positional arguments.
  6896. * @return {*}
  6897. * @see R.apply
  6898. * @example
  6899. *
  6900. * R.call(R.add, 1, 2); //=> 3
  6901. *
  6902. * var indentN = R.pipe(R.times(R.always(' ')),
  6903. * R.join(''),
  6904. * R.replace(/^(?!$)/gm));
  6905. *
  6906. * var format = R.converge(R.call, [
  6907. * R.pipe(R.prop('indent'), indentN),
  6908. * R.prop('value')
  6909. * ]);
  6910. *
  6911. * format({indent: 2, value: 'foo\nbar\nbaz\n'}); //=> ' foo\n bar\n baz\n'
  6912. * @symb R.call(f, a, b) = f(a, b)
  6913. */
  6914. var call = curry(function call(fn) {
  6915. return fn.apply(this, Array.prototype.slice.call(arguments, 1));
  6916. });
  6917. /**
  6918. * `chain` maps a function over a list and concatenates the results. `chain`
  6919. * is also known as `flatMap` in some libraries
  6920. *
  6921. * Dispatches to the `chain` method of the second argument, if present,
  6922. * according to the [FantasyLand Chain spec](https://github.com/fantasyland/fantasy-land#chain).
  6923. *
  6924. * @func
  6925. * @memberOf R
  6926. * @since v0.3.0
  6927. * @category List
  6928. * @sig Chain m => (a -> m b) -> m a -> m b
  6929. * @param {Function} fn The function to map with
  6930. * @param {Array} list The list to map over
  6931. * @return {Array} The result of flat-mapping `list` with `fn`
  6932. * @example
  6933. *
  6934. * var duplicate = n => [n, n];
  6935. * R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3]
  6936. *
  6937. * R.chain(R.append, R.head)([1, 2, 3]); //=> [1, 2, 3, 1]
  6938. */
  6939. var chain = _curry2(_dispatchable([
  6940. 'fantasy-land/chain',
  6941. 'chain'
  6942. ], _xchain, function chain(fn, monad) {
  6943. if (typeof monad === 'function') {
  6944. return function (x) {
  6945. return fn(monad(x))(x);
  6946. };
  6947. }
  6948. return _makeFlat(false)(map(fn, monad));
  6949. }));
  6950. /**
  6951. * Returns a function, `fn`, which encapsulates `if/else, if/else, ...` logic.
  6952. * `R.cond` takes a list of [predicate, transformer] pairs. All of the arguments
  6953. * to `fn` are applied to each of the predicates in turn until one returns a
  6954. * "truthy" value, at which point `fn` returns the result of applying its
  6955. * arguments to the corresponding transformer. If none of the predicates
  6956. * matches, `fn` returns undefined.
  6957. *
  6958. * @func
  6959. * @memberOf R
  6960. * @since v0.6.0
  6961. * @category Logic
  6962. * @sig [[(*... -> Boolean),(*... -> *)]] -> (*... -> *)
  6963. * @param {Array} pairs A list of [predicate, transformer]
  6964. * @return {Function}
  6965. * @example
  6966. *
  6967. * var fn = R.cond([
  6968. * [R.equals(0), R.always('water freezes at 0°C')],
  6969. * [R.equals(100), R.always('water boils at 100°C')],
  6970. * [R.T, temp => 'nothing special happens at ' + temp + '°C']
  6971. * ]);
  6972. * fn(0); //=> 'water freezes at 0°C'
  6973. * fn(50); //=> 'nothing special happens at 50°C'
  6974. * fn(100); //=> 'water boils at 100°C'
  6975. */
  6976. var cond = _curry1(function cond(pairs) {
  6977. var arity = reduce(max, 0, map(function (pair) {
  6978. return pair[0].length;
  6979. }, pairs));
  6980. return _arity(arity, function () {
  6981. var idx = 0;
  6982. while (idx < pairs.length) {
  6983. if (pairs[idx][0].apply(this, arguments)) {
  6984. return pairs[idx][1].apply(this, arguments);
  6985. }
  6986. idx += 1;
  6987. }
  6988. });
  6989. });
  6990. /**
  6991. * Wraps a constructor function inside a curried function that can be called
  6992. * with the same arguments and returns the same type. The arity of the function
  6993. * returned is specified to allow using variadic constructor functions.
  6994. *
  6995. * @func
  6996. * @memberOf R
  6997. * @since v0.4.0
  6998. * @category Function
  6999. * @sig Number -> (* -> {*}) -> (* -> {*})
  7000. * @param {Number} n The arity of the constructor function.
  7001. * @param {Function} Fn The constructor function to wrap.
  7002. * @return {Function} A wrapped, curried constructor function.
  7003. * @example
  7004. *
  7005. * // Variadic Constructor function
  7006. * function Salad() {
  7007. * this.ingredients = arguments;
  7008. * };
  7009. * Salad.prototype.recipe = function() {
  7010. * var instructions = R.map((ingredient) => (
  7011. * 'Add a whollop of ' + ingredient, this.ingredients)
  7012. * )
  7013. * return R.join('\n', instructions)
  7014. * }
  7015. *
  7016. * var ThreeLayerSalad = R.constructN(3, Salad)
  7017. *
  7018. * // Notice we no longer need the 'new' keyword, and the constructor is curried for 3 arguments.
  7019. * var salad = ThreeLayerSalad('Mayonnaise')('Potato Chips')('Ketchup')
  7020. * console.log(salad.recipe());
  7021. * // Add a whollop of Mayonnaise
  7022. * // Add a whollop of Potato Chips
  7023. * // Add a whollop of Potato Ketchup
  7024. */
  7025. var constructN = _curry2(function constructN(n, Fn) {
  7026. if (n > 10) {
  7027. throw new Error('Constructor with greater than ten arguments');
  7028. }
  7029. if (n === 0) {
  7030. return function () {
  7031. return new Fn();
  7032. };
  7033. }
  7034. return curry(nAry(n, function ($0, $1, $2, $3, $4, $5, $6, $7, $8, $9) {
  7035. switch (arguments.length) {
  7036. case 1:
  7037. return new Fn($0);
  7038. case 2:
  7039. return new Fn($0, $1);
  7040. case 3:
  7041. return new Fn($0, $1, $2);
  7042. case 4:
  7043. return new Fn($0, $1, $2, $3);
  7044. case 5:
  7045. return new Fn($0, $1, $2, $3, $4);
  7046. case 6:
  7047. return new Fn($0, $1, $2, $3, $4, $5);
  7048. case 7:
  7049. return new Fn($0, $1, $2, $3, $4, $5, $6);
  7050. case 8:
  7051. return new Fn($0, $1, $2, $3, $4, $5, $6, $7);
  7052. case 9:
  7053. return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8);
  7054. case 10:
  7055. return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8, $9);
  7056. }
  7057. }));
  7058. });
  7059. /**
  7060. * Accepts a converging function and a list of branching functions and returns
  7061. * a new function. When invoked, this new function is applied to some
  7062. * arguments, each branching function is applied to those same arguments. The
  7063. * results of each branching function are passed as arguments to the converging
  7064. * function to produce the return value.
  7065. *
  7066. * @func
  7067. * @memberOf R
  7068. * @since v0.4.2
  7069. * @category Function
  7070. * @sig (x1 -> x2 -> ... -> z) -> [(a -> b -> ... -> x1), (a -> b -> ... -> x2), ...] -> (a -> b -> ... -> z)
  7071. * @param {Function} after A function. `after` will be invoked with the return values of
  7072. * `fn1` and `fn2` as its arguments.
  7073. * @param {Array} functions A list of functions.
  7074. * @return {Function} A new function.
  7075. * @see R.useWith
  7076. * @example
  7077. *
  7078. * var average = R.converge(R.divide, [R.sum, R.length])
  7079. * average([1, 2, 3, 4, 5, 6, 7]) //=> 4
  7080. *
  7081. * var strangeConcat = R.converge(R.concat, [R.toUpper, R.toLower])
  7082. * strangeConcat("Yodel") //=> "YODELyodel"
  7083. *
  7084. * @symb R.converge(f, [g, h])(a, b) = f(g(a, b), h(a, b))
  7085. */
  7086. var converge = _curry2(function converge(after, fns) {
  7087. return curryN(reduce(max, 0, pluck('length', fns)), function () {
  7088. var args = arguments;
  7089. var context = this;
  7090. return after.apply(context, _map(function (fn) {
  7091. return fn.apply(context, args);
  7092. }, fns));
  7093. });
  7094. });
  7095. /**
  7096. * Counts the elements of a list according to how many match each value of a
  7097. * key generated by the supplied function. Returns an object mapping the keys
  7098. * produced by `fn` to the number of occurrences in the list. Note that all
  7099. * keys are coerced to strings because of how JavaScript objects work.
  7100. *
  7101. * Acts as a transducer if a transformer is given in list position.
  7102. *
  7103. * @func
  7104. * @memberOf R
  7105. * @since v0.1.0
  7106. * @category Relation
  7107. * @sig (a -> String) -> [a] -> {*}
  7108. * @param {Function} fn The function used to map values to keys.
  7109. * @param {Array} list The list to count elements from.
  7110. * @return {Object} An object mapping keys to number of occurrences in the list.
  7111. * @example
  7112. *
  7113. * var numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
  7114. * R.countBy(Math.floor)(numbers); //=> {'1': 3, '2': 2, '3': 1}
  7115. *
  7116. * var letters = ['a', 'b', 'A', 'a', 'B', 'c'];
  7117. * R.countBy(R.toLower)(letters); //=> {'a': 3, 'b': 2, 'c': 1}
  7118. */
  7119. var countBy = reduceBy(function (acc, elem) {
  7120. return acc + 1;
  7121. }, 0);
  7122. /**
  7123. * Returns a new list without any consecutively repeating elements. Equality is
  7124. * determined by applying the supplied predicate to each pair of consecutive elements. The
  7125. * first element in a series of equal elements will be preserved.
  7126. *
  7127. * Acts as a transducer if a transformer is given in list position.
  7128. *
  7129. * @func
  7130. * @memberOf R
  7131. * @since v0.14.0
  7132. * @category List
  7133. * @sig (a, a -> Boolean) -> [a] -> [a]
  7134. * @param {Function} pred A predicate used to test whether two items are equal.
  7135. * @param {Array} list The array to consider.
  7136. * @return {Array} `list` without repeating elements.
  7137. * @see R.transduce
  7138. * @example
  7139. *
  7140. * var l = [1, -1, 1, 3, 4, -4, -4, -5, 5, 3, 3];
  7141. * R.dropRepeatsWith(R.eqBy(Math.abs), l); //=> [1, 3, 4, -5, 3]
  7142. */
  7143. var dropRepeatsWith = _curry2(_dispatchable([], _xdropRepeatsWith, function dropRepeatsWith(pred, list) {
  7144. var result = [];
  7145. var idx = 1;
  7146. var len = list.length;
  7147. if (len !== 0) {
  7148. result[0] = list[0];
  7149. while (idx < len) {
  7150. if (!pred(last(result), list[idx])) {
  7151. result[result.length] = list[idx];
  7152. }
  7153. idx += 1;
  7154. }
  7155. }
  7156. return result;
  7157. }));
  7158. /**
  7159. * Checks if a list ends with the provided values
  7160. *
  7161. * @func
  7162. * @memberOf R
  7163. * @since v0.24.0
  7164. * @category List
  7165. * @sig [a] -> Boolean
  7166. * @sig String -> Boolean
  7167. * @param {*} suffix
  7168. * @param {*} list
  7169. * @return {Boolean}
  7170. * @example
  7171. *
  7172. * R.endsWith('c', 'abc') //=> true
  7173. * R.endsWith('b', 'abc') //=> false
  7174. * R.endsWith(['c'], ['a', 'b', 'c']) //=> true
  7175. * R.endsWith(['b'], ['a', 'b', 'c']) //=> false
  7176. */
  7177. var endsWith = _curry2(function (suffix, list) {
  7178. return equals(takeLast(suffix.length, list), suffix);
  7179. });
  7180. /**
  7181. * Takes a function and two values in its domain and returns `true` if the
  7182. * values map to the same value in the codomain; `false` otherwise.
  7183. *
  7184. * @func
  7185. * @memberOf R
  7186. * @since v0.18.0
  7187. * @category Relation
  7188. * @sig (a -> b) -> a -> a -> Boolean
  7189. * @param {Function} f
  7190. * @param {*} x
  7191. * @param {*} y
  7192. * @return {Boolean}
  7193. * @example
  7194. *
  7195. * R.eqBy(Math.abs, 5, -5); //=> true
  7196. */
  7197. var eqBy = _curry3(function eqBy(f, x, y) {
  7198. return equals(f(x), f(y));
  7199. });
  7200. /**
  7201. * Reports whether two objects have the same value, in [`R.equals`](#equals)
  7202. * terms, for the specified property. Useful as a curried predicate.
  7203. *
  7204. * @func
  7205. * @memberOf R
  7206. * @since v0.1.0
  7207. * @category Object
  7208. * @sig k -> {k: v} -> {k: v} -> Boolean
  7209. * @param {String} prop The name of the property to compare
  7210. * @param {Object} obj1
  7211. * @param {Object} obj2
  7212. * @return {Boolean}
  7213. *
  7214. * @example
  7215. *
  7216. * var o1 = { a: 1, b: 2, c: 3, d: 4 };
  7217. * var o2 = { a: 10, b: 20, c: 3, d: 40 };
  7218. * R.eqProps('a', o1, o2); //=> false
  7219. * R.eqProps('c', o1, o2); //=> true
  7220. */
  7221. var eqProps = _curry3(function eqProps(prop, obj1, obj2) {
  7222. return equals(obj1[prop], obj2[prop]);
  7223. });
  7224. /**
  7225. * Splits a list into sub-lists stored in an object, based on the result of
  7226. * calling a String-returning function on each element, and grouping the
  7227. * results according to values returned.
  7228. *
  7229. * Dispatches to the `groupBy` method of the second argument, if present.
  7230. *
  7231. * Acts as a transducer if a transformer is given in list position.
  7232. *
  7233. * @func
  7234. * @memberOf R
  7235. * @since v0.1.0
  7236. * @category List
  7237. * @sig (a -> String) -> [a] -> {String: [a]}
  7238. * @param {Function} fn Function :: a -> String
  7239. * @param {Array} list The array to group
  7240. * @return {Object} An object with the output of `fn` for keys, mapped to arrays of elements
  7241. * that produced that key when passed to `fn`.
  7242. * @see R.transduce
  7243. * @example
  7244. *
  7245. * var byGrade = R.groupBy(function(student) {
  7246. * var score = student.score;
  7247. * return score < 65 ? 'F' :
  7248. * score < 70 ? 'D' :
  7249. * score < 80 ? 'C' :
  7250. * score < 90 ? 'B' : 'A';
  7251. * });
  7252. * var students = [{name: 'Abby', score: 84},
  7253. * {name: 'Eddy', score: 58},
  7254. * // ...
  7255. * {name: 'Jack', score: 69}];
  7256. * byGrade(students);
  7257. * // {
  7258. * // 'A': [{name: 'Dianne', score: 99}],
  7259. * // 'B': [{name: 'Abby', score: 84}]
  7260. * // // ...,
  7261. * // 'F': [{name: 'Eddy', score: 58}]
  7262. * // }
  7263. */
  7264. var groupBy = _curry2(_checkForMethod('groupBy', reduceBy(function (acc, item) {
  7265. if (acc == null) {
  7266. acc = [];
  7267. }
  7268. acc.push(item);
  7269. return acc;
  7270. }, null)));
  7271. /**
  7272. * Given a function that generates a key, turns a list of objects into an
  7273. * object indexing the objects by the given key. Note that if multiple
  7274. * objects generate the same value for the indexing key only the last value
  7275. * will be included in the generated object.
  7276. *
  7277. * Acts as a transducer if a transformer is given in list position.
  7278. *
  7279. * @func
  7280. * @memberOf R
  7281. * @since v0.19.0
  7282. * @category List
  7283. * @sig (a -> String) -> [{k: v}] -> {k: {k: v}}
  7284. * @param {Function} fn Function :: a -> String
  7285. * @param {Array} array The array of objects to index
  7286. * @return {Object} An object indexing each array element by the given property.
  7287. * @example
  7288. *
  7289. * var list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
  7290. * R.indexBy(R.prop('id'), list);
  7291. * //=> {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}}
  7292. */
  7293. var indexBy = reduceBy(function (acc, elem) {
  7294. return elem;
  7295. }, null);
  7296. /**
  7297. * Returns the position of the first occurrence of an item in an array, or -1
  7298. * if the item is not included in the array. [`R.equals`](#equals) is used to
  7299. * determine equality.
  7300. *
  7301. * @func
  7302. * @memberOf R
  7303. * @since v0.1.0
  7304. * @category List
  7305. * @sig a -> [a] -> Number
  7306. * @param {*} target The item to find.
  7307. * @param {Array} xs The array to search in.
  7308. * @return {Number} the index of the target, or -1 if the target is not found.
  7309. * @see R.lastIndexOf
  7310. * @example
  7311. *
  7312. * R.indexOf(3, [1,2,3,4]); //=> 2
  7313. * R.indexOf(10, [1,2,3,4]); //=> -1
  7314. */
  7315. var indexOf = _curry2(function indexOf(target, xs) {
  7316. return typeof xs.indexOf === 'function' && !_isArray(xs) ? xs.indexOf(target) : _indexOf(xs, target, 0);
  7317. });
  7318. /**
  7319. * juxt applies a list of functions to a list of values.
  7320. *
  7321. * @func
  7322. * @memberOf R
  7323. * @since v0.19.0
  7324. * @category Function
  7325. * @sig [(a, b, ..., m) -> n] -> ((a, b, ..., m) -> [n])
  7326. * @param {Array} fns An array of functions
  7327. * @return {Function} A function that returns a list of values after applying each of the original `fns` to its parameters.
  7328. * @see R.applySpec
  7329. * @example
  7330. *
  7331. * var getRange = R.juxt([Math.min, Math.max]);
  7332. * getRange(3, 4, 9, -3); //=> [-3, 9]
  7333. * @symb R.juxt([f, g, h])(a, b) = [f(a, b), g(a, b), h(a, b)]
  7334. */
  7335. var juxt = _curry1(function juxt(fns) {
  7336. return converge(function () {
  7337. return Array.prototype.slice.call(arguments, 0);
  7338. }, fns);
  7339. });
  7340. /**
  7341. * Returns a lens for the given getter and setter functions. The getter "gets"
  7342. * the value of the focus; the setter "sets" the value of the focus. The setter
  7343. * should not mutate the data structure.
  7344. *
  7345. * @func
  7346. * @memberOf R
  7347. * @since v0.8.0
  7348. * @category Object
  7349. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  7350. * @sig (s -> a) -> ((a, s) -> s) -> Lens s a
  7351. * @param {Function} getter
  7352. * @param {Function} setter
  7353. * @return {Lens}
  7354. * @see R.view, R.set, R.over, R.lensIndex, R.lensProp
  7355. * @example
  7356. *
  7357. * var xLens = R.lens(R.prop('x'), R.assoc('x'));
  7358. *
  7359. * R.view(xLens, {x: 1, y: 2}); //=> 1
  7360. * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
  7361. * R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
  7362. */
  7363. var lens = _curry2(function lens(getter, setter) {
  7364. return function (toFunctorFn) {
  7365. return function (target) {
  7366. return map(function (focus) {
  7367. return setter(focus, target);
  7368. }, toFunctorFn(getter(target)));
  7369. };
  7370. };
  7371. });
  7372. /**
  7373. * Returns a lens whose focus is the specified index.
  7374. *
  7375. * @func
  7376. * @memberOf R
  7377. * @since v0.14.0
  7378. * @category Object
  7379. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  7380. * @sig Number -> Lens s a
  7381. * @param {Number} n
  7382. * @return {Lens}
  7383. * @see R.view, R.set, R.over
  7384. * @example
  7385. *
  7386. * var headLens = R.lensIndex(0);
  7387. *
  7388. * R.view(headLens, ['a', 'b', 'c']); //=> 'a'
  7389. * R.set(headLens, 'x', ['a', 'b', 'c']); //=> ['x', 'b', 'c']
  7390. * R.over(headLens, R.toUpper, ['a', 'b', 'c']); //=> ['A', 'b', 'c']
  7391. */
  7392. var lensIndex = _curry1(function lensIndex(n) {
  7393. return lens(nth(n), update(n));
  7394. });
  7395. /**
  7396. * Returns a lens whose focus is the specified path.
  7397. *
  7398. * @func
  7399. * @memberOf R
  7400. * @since v0.19.0
  7401. * @category Object
  7402. * @typedefn Idx = String | Int
  7403. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  7404. * @sig [Idx] -> Lens s a
  7405. * @param {Array} path The path to use.
  7406. * @return {Lens}
  7407. * @see R.view, R.set, R.over
  7408. * @example
  7409. *
  7410. * var xHeadYLens = R.lensPath(['x', 0, 'y']);
  7411. *
  7412. * R.view(xHeadYLens, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  7413. * //=> 2
  7414. * R.set(xHeadYLens, 1, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  7415. * //=> {x: [{y: 1, z: 3}, {y: 4, z: 5}]}
  7416. * R.over(xHeadYLens, R.negate, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  7417. * //=> {x: [{y: -2, z: 3}, {y: 4, z: 5}]}
  7418. */
  7419. var lensPath = _curry1(function lensPath(p) {
  7420. return lens(path(p), assocPath(p));
  7421. });
  7422. /**
  7423. * Returns a lens whose focus is the specified property.
  7424. *
  7425. * @func
  7426. * @memberOf R
  7427. * @since v0.14.0
  7428. * @category Object
  7429. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  7430. * @sig String -> Lens s a
  7431. * @param {String} k
  7432. * @return {Lens}
  7433. * @see R.view, R.set, R.over
  7434. * @example
  7435. *
  7436. * var xLens = R.lensProp('x');
  7437. *
  7438. * R.view(xLens, {x: 1, y: 2}); //=> 1
  7439. * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
  7440. * R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
  7441. */
  7442. var lensProp = _curry1(function lensProp(k) {
  7443. return lens(prop(k), assoc(k));
  7444. });
  7445. /**
  7446. * "lifts" a function to be the specified arity, so that it may "map over" that
  7447. * many lists, Functions or other objects that satisfy the [FantasyLand Apply spec](https://github.com/fantasyland/fantasy-land#apply).
  7448. *
  7449. * @func
  7450. * @memberOf R
  7451. * @since v0.7.0
  7452. * @category Function
  7453. * @sig Number -> (*... -> *) -> ([*]... -> [*])
  7454. * @param {Function} fn The function to lift into higher context
  7455. * @return {Function} The lifted function.
  7456. * @see R.lift, R.ap
  7457. * @example
  7458. *
  7459. * var madd3 = R.liftN(3, (...args) => R.sum(args));
  7460. * madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
  7461. */
  7462. var liftN = _curry2(function liftN(arity, fn) {
  7463. var lifted = curryN(arity, fn);
  7464. return curryN(arity, function () {
  7465. return _reduce(ap, map(lifted, arguments[0]), Array.prototype.slice.call(arguments, 1));
  7466. });
  7467. });
  7468. /**
  7469. * Returns the mean of the given list of numbers.
  7470. *
  7471. * @func
  7472. * @memberOf R
  7473. * @since v0.14.0
  7474. * @category Math
  7475. * @sig [Number] -> Number
  7476. * @param {Array} list
  7477. * @return {Number}
  7478. * @see R.median
  7479. * @example
  7480. *
  7481. * R.mean([2, 7, 9]); //=> 6
  7482. * R.mean([]); //=> NaN
  7483. */
  7484. var mean = _curry1(function mean(list) {
  7485. return sum(list) / list.length;
  7486. });
  7487. /**
  7488. * Returns the median of the given list of numbers.
  7489. *
  7490. * @func
  7491. * @memberOf R
  7492. * @since v0.14.0
  7493. * @category Math
  7494. * @sig [Number] -> Number
  7495. * @param {Array} list
  7496. * @return {Number}
  7497. * @see R.mean
  7498. * @example
  7499. *
  7500. * R.median([2, 9, 7]); //=> 7
  7501. * R.median([7, 2, 10, 9]); //=> 8
  7502. * R.median([]); //=> NaN
  7503. */
  7504. var median = _curry1(function median(list) {
  7505. var len = list.length;
  7506. if (len === 0) {
  7507. return NaN;
  7508. }
  7509. var width = 2 - len % 2;
  7510. var idx = (len - width) / 2;
  7511. return mean(Array.prototype.slice.call(list, 0).sort(function (a, b) {
  7512. return a < b ? -1 : a > b ? 1 : 0;
  7513. }).slice(idx, idx + width));
  7514. });
  7515. /**
  7516. * Creates a new object with the own properties of the first object merged with
  7517. * the own properties of the second object. If a key exists in both objects:
  7518. * - and both values are objects, the two values will be recursively merged
  7519. * - otherwise the value from the first object will be used.
  7520. *
  7521. * @func
  7522. * @memberOf R
  7523. * @since v0.24.0
  7524. * @category Object
  7525. * @sig {a} -> {a} -> {a}
  7526. * @param {Object} lObj
  7527. * @param {Object} rObj
  7528. * @return {Object}
  7529. * @see R.merge, R.mergeDeepRight, R.mergeDeepWith, R.mergeDeepWithKey
  7530. * @example
  7531. *
  7532. * R.mergeDeepLeft({ name: 'fred', age: 10, contact: { email: 'moo@example.com' }},
  7533. * { age: 40, contact: { email: 'baa@example.com' }});
  7534. * //=> { name: 'fred', age: 10, contact: { email: 'moo@example.com' }}
  7535. */
  7536. var mergeDeepLeft = _curry2(function mergeDeepLeft(lObj, rObj) {
  7537. return mergeDeepWithKey(function (k, lVal, rVal) {
  7538. return lVal;
  7539. }, lObj, rObj);
  7540. });
  7541. /**
  7542. * Creates a new object with the own properties of the first object merged with
  7543. * the own properties of the second object. If a key exists in both objects:
  7544. * - and both values are objects, the two values will be recursively merged
  7545. * - otherwise the value from the second object will be used.
  7546. *
  7547. * @func
  7548. * @memberOf R
  7549. * @since v0.24.0
  7550. * @category Object
  7551. * @sig {a} -> {a} -> {a}
  7552. * @param {Object} lObj
  7553. * @param {Object} rObj
  7554. * @return {Object}
  7555. * @see R.merge, R.mergeDeepLeft, R.mergeDeepWith, R.mergeDeepWithKey
  7556. * @example
  7557. *
  7558. * R.mergeDeepRight({ name: 'fred', age: 10, contact: { email: 'moo@example.com' }},
  7559. * { age: 40, contact: { email: 'baa@example.com' }});
  7560. * //=> { name: 'fred', age: 40, contact: { email: 'baa@example.com' }}
  7561. */
  7562. var mergeDeepRight = _curry2(function mergeDeepRight(lObj, rObj) {
  7563. return mergeDeepWithKey(function (k, lVal, rVal) {
  7564. return rVal;
  7565. }, lObj, rObj);
  7566. });
  7567. /**
  7568. * Creates a new object with the own properties of the two provided objects.
  7569. * If a key exists in both objects:
  7570. * - and both associated values are also objects then the values will be
  7571. * recursively merged.
  7572. * - otherwise the provided function is applied to associated values using the
  7573. * resulting value as the new value associated with the key.
  7574. * If a key only exists in one object, the value will be associated with the key
  7575. * of the resulting object.
  7576. *
  7577. * @func
  7578. * @memberOf R
  7579. * @since v0.24.0
  7580. * @category Object
  7581. * @sig (a -> a -> a) -> {a} -> {a} -> {a}
  7582. * @param {Function} fn
  7583. * @param {Object} lObj
  7584. * @param {Object} rObj
  7585. * @return {Object}
  7586. * @see R.mergeWith, R.mergeDeep, R.mergeDeepWithKey
  7587. * @example
  7588. *
  7589. * R.mergeDeepWith(R.concat,
  7590. * { a: true, c: { values: [10, 20] }},
  7591. * { b: true, c: { values: [15, 35] }});
  7592. * //=> { a: true, b: true, c: { values: [10, 20, 15, 35] }}
  7593. */
  7594. var mergeDeepWith = _curry3(function mergeDeepWith(fn, lObj, rObj) {
  7595. return mergeDeepWithKey(function (k, lVal, rVal) {
  7596. return fn(lVal, rVal);
  7597. }, lObj, rObj);
  7598. });
  7599. /**
  7600. * Takes a predicate and a list or other `Filterable` object and returns the
  7601. * pair of filterable objects of the same type of elements which do and do not
  7602. * satisfy, the predicate, respectively. Filterable objects include plain objects or any object
  7603. * that has a filter method such as `Array`.
  7604. *
  7605. * @func
  7606. * @memberOf R
  7607. * @since v0.1.4
  7608. * @category List
  7609. * @sig Filterable f => (a -> Boolean) -> f a -> [f a, f a]
  7610. * @param {Function} pred A predicate to determine which side the element belongs to.
  7611. * @param {Array} filterable the list (or other filterable) to partition.
  7612. * @return {Array} An array, containing first the subset of elements that satisfy the
  7613. * predicate, and second the subset of elements that do not satisfy.
  7614. * @see R.filter, R.reject
  7615. * @example
  7616. *
  7617. * R.partition(R.contains('s'), ['sss', 'ttt', 'foo', 'bars']);
  7618. * // => [ [ 'sss', 'bars' ], [ 'ttt', 'foo' ] ]
  7619. *
  7620. * R.partition(R.contains('s'), { a: 'sss', b: 'ttt', foo: 'bars' });
  7621. * // => [ { a: 'sss', foo: 'bars' }, { b: 'ttt' } ]
  7622. */
  7623. var partition = juxt([
  7624. filter,
  7625. reject
  7626. ]);
  7627. /**
  7628. * Performs left-to-right function composition. The leftmost function may have
  7629. * any arity; the remaining functions must be unary.
  7630. *
  7631. * In some libraries this function is named `sequence`.
  7632. *
  7633. * **Note:** The result of pipe is not automatically curried.
  7634. *
  7635. * @func
  7636. * @memberOf R
  7637. * @since v0.1.0
  7638. * @category Function
  7639. * @sig (((a, b, ..., n) -> o), (o -> p), ..., (x -> y), (y -> z)) -> ((a, b, ..., n) -> z)
  7640. * @param {...Function} functions
  7641. * @return {Function}
  7642. * @see R.compose
  7643. * @example
  7644. *
  7645. * var f = R.pipe(Math.pow, R.negate, R.inc);
  7646. *
  7647. * f(3, 4); // -(3^4) + 1
  7648. * @symb R.pipe(f, g, h)(a, b) = h(g(f(a, b)))
  7649. */
  7650. var pipe = function pipe() {
  7651. if (arguments.length === 0) {
  7652. throw new Error('pipe requires at least one argument');
  7653. }
  7654. return _arity(arguments[0].length, reduce(_pipe, arguments[0], tail(arguments)));
  7655. };
  7656. /**
  7657. * Performs left-to-right composition of one or more Promise-returning
  7658. * functions. The leftmost function may have any arity; the remaining functions
  7659. * must be unary.
  7660. *
  7661. * @func
  7662. * @memberOf R
  7663. * @since v0.10.0
  7664. * @category Function
  7665. * @sig ((a -> Promise b), (b -> Promise c), ..., (y -> Promise z)) -> (a -> Promise z)
  7666. * @param {...Function} functions
  7667. * @return {Function}
  7668. * @see R.composeP
  7669. * @example
  7670. *
  7671. * // followersForUser :: String -> Promise [User]
  7672. * var followersForUser = R.pipeP(db.getUserById, db.getFollowers);
  7673. */
  7674. var pipeP = function pipeP() {
  7675. if (arguments.length === 0) {
  7676. throw new Error('pipeP requires at least one argument');
  7677. }
  7678. return _arity(arguments[0].length, reduce(_pipeP, arguments[0], tail(arguments)));
  7679. };
  7680. /**
  7681. * Multiplies together all the elements of a list.
  7682. *
  7683. * @func
  7684. * @memberOf R
  7685. * @since v0.1.0
  7686. * @category Math
  7687. * @sig [Number] -> Number
  7688. * @param {Array} list An array of numbers
  7689. * @return {Number} The product of all the numbers in the list.
  7690. * @see R.reduce
  7691. * @example
  7692. *
  7693. * R.product([2,4,6,8,100,1]); //=> 38400
  7694. */
  7695. var product = reduce(multiply, 1);
  7696. /**
  7697. * Transforms a [Traversable](https://github.com/fantasyland/fantasy-land#traversable)
  7698. * of [Applicative](https://github.com/fantasyland/fantasy-land#applicative) into an
  7699. * Applicative of Traversable.
  7700. *
  7701. * Dispatches to the `sequence` method of the second argument, if present.
  7702. *
  7703. * @func
  7704. * @memberOf R
  7705. * @since v0.19.0
  7706. * @category List
  7707. * @sig (Applicative f, Traversable t) => (a -> f a) -> t (f a) -> f (t a)
  7708. * @param {Function} of
  7709. * @param {*} traversable
  7710. * @return {*}
  7711. * @see R.traverse
  7712. * @example
  7713. *
  7714. * R.sequence(Maybe.of, [Just(1), Just(2), Just(3)]); //=> Just([1, 2, 3])
  7715. * R.sequence(Maybe.of, [Just(1), Just(2), Nothing()]); //=> Nothing()
  7716. *
  7717. * R.sequence(R.of, Just([1, 2, 3])); //=> [Just(1), Just(2), Just(3)]
  7718. * R.sequence(R.of, Nothing()); //=> [Nothing()]
  7719. */
  7720. var sequence = _curry2(function sequence(of, traversable) {
  7721. return typeof traversable.sequence === 'function' ? traversable.sequence(of) : reduceRight(function (x, acc) {
  7722. return ap(map(prepend, x), acc);
  7723. }, of([]), traversable);
  7724. });
  7725. /**
  7726. * Maps an [Applicative](https://github.com/fantasyland/fantasy-land#applicative)-returning
  7727. * function over a [Traversable](https://github.com/fantasyland/fantasy-land#traversable),
  7728. * then uses [`sequence`](#sequence) to transform the resulting Traversable of Applicative
  7729. * into an Applicative of Traversable.
  7730. *
  7731. * Dispatches to the `sequence` method of the third argument, if present.
  7732. *
  7733. * @func
  7734. * @memberOf R
  7735. * @since v0.19.0
  7736. * @category List
  7737. * @sig (Applicative f, Traversable t) => (a -> f a) -> (a -> f b) -> t a -> f (t b)
  7738. * @param {Function} of
  7739. * @param {Function} f
  7740. * @param {*} traversable
  7741. * @return {*}
  7742. * @see R.sequence
  7743. * @example
  7744. *
  7745. * // Returns `Nothing` if the given divisor is `0`
  7746. * safeDiv = n => d => d === 0 ? Nothing() : Just(n / d)
  7747. *
  7748. * R.traverse(Maybe.of, safeDiv(10), [2, 4, 5]); //=> Just([5, 2.5, 2])
  7749. * R.traverse(Maybe.of, safeDiv(10), [2, 0, 5]); //=> Nothing
  7750. */
  7751. var traverse = _curry3(function traverse(of, f, traversable) {
  7752. return typeof traversable['fantasy-land/traverse'] === 'function' ? traversable['fantasy-land/traverse'](f, of) : sequence(of, map(f, traversable));
  7753. });
  7754. /**
  7755. * Shorthand for `R.chain(R.identity)`, which removes one level of nesting from
  7756. * any [Chain](https://github.com/fantasyland/fantasy-land#chain).
  7757. *
  7758. * @func
  7759. * @memberOf R
  7760. * @since v0.3.0
  7761. * @category List
  7762. * @sig Chain c => c (c a) -> c a
  7763. * @param {*} list
  7764. * @return {*}
  7765. * @see R.flatten, R.chain
  7766. * @example
  7767. *
  7768. * R.unnest([1, [2], [[3]]]); //=> [1, 2, [3]]
  7769. * R.unnest([[1, 2], [3, 4], [5, 6]]); //=> [1, 2, 3, 4, 5, 6]
  7770. */
  7771. var unnest = chain(_identity);
  7772. var _contains = function _contains(a, list) {
  7773. return _indexOf(list, a, 0) >= 0;
  7774. };
  7775. // mapPairs :: (Object, [String]) -> [String]
  7776. var _toString = function _toString(x, seen) {
  7777. var recur = function recur(y) {
  7778. var xs = seen.concat([x]);
  7779. return _contains(y, xs) ? '<Circular>' : _toString(y, xs);
  7780. };
  7781. // mapPairs :: (Object, [String]) -> [String]
  7782. var mapPairs = function (obj, keys) {
  7783. return _map(function (k) {
  7784. return _quote(k) + ': ' + recur(obj[k]);
  7785. }, keys.slice().sort());
  7786. };
  7787. switch (Object.prototype.toString.call(x)) {
  7788. case '[object Arguments]':
  7789. return '(function() { return arguments; }(' + _map(recur, x).join(', ') + '))';
  7790. case '[object Array]':
  7791. return '[' + _map(recur, x).concat(mapPairs(x, reject(function (k) {
  7792. return /^\d+$/.test(k);
  7793. }, keys(x)))).join(', ') + ']';
  7794. case '[object Boolean]':
  7795. return typeof x === 'object' ? 'new Boolean(' + recur(x.valueOf()) + ')' : x.toString();
  7796. case '[object Date]':
  7797. return 'new Date(' + (isNaN(x.valueOf()) ? recur(NaN) : _quote(_toISOString(x))) + ')';
  7798. case '[object Null]':
  7799. return 'null';
  7800. case '[object Number]':
  7801. return typeof x === 'object' ? 'new Number(' + recur(x.valueOf()) + ')' : 1 / x === -Infinity ? '-0' : x.toString(10);
  7802. case '[object String]':
  7803. return typeof x === 'object' ? 'new String(' + recur(x.valueOf()) + ')' : _quote(x);
  7804. case '[object Undefined]':
  7805. return 'undefined';
  7806. default:
  7807. if (typeof x.toString === 'function') {
  7808. var repr = x.toString();
  7809. if (repr !== '[object Object]') {
  7810. return repr;
  7811. }
  7812. }
  7813. return '{' + mapPairs(x, keys(x)).join(', ') + '}';
  7814. }
  7815. };
  7816. /**
  7817. * Performs right-to-left function composition. The rightmost function may have
  7818. * any arity; the remaining functions must be unary.
  7819. *
  7820. * **Note:** The result of compose is not automatically curried.
  7821. *
  7822. * @func
  7823. * @memberOf R
  7824. * @since v0.1.0
  7825. * @category Function
  7826. * @sig ((y -> z), (x -> y), ..., (o -> p), ((a, b, ..., n) -> o)) -> ((a, b, ..., n) -> z)
  7827. * @param {...Function} ...functions The functions to compose
  7828. * @return {Function}
  7829. * @see R.pipe
  7830. * @example
  7831. *
  7832. * var classyGreeting = (firstName, lastName) => "The name's " + lastName + ", " + firstName + " " + lastName
  7833. * var yellGreeting = R.compose(R.toUpper, classyGreeting);
  7834. * yellGreeting('James', 'Bond'); //=> "THE NAME'S BOND, JAMES BOND"
  7835. *
  7836. * R.compose(Math.abs, R.add(1), R.multiply(2))(-4) //=> 7
  7837. *
  7838. * @symb R.compose(f, g, h)(a, b) = f(g(h(a, b)))
  7839. */
  7840. var compose = function compose() {
  7841. if (arguments.length === 0) {
  7842. throw new Error('compose requires at least one argument');
  7843. }
  7844. return pipe.apply(this, reverse(arguments));
  7845. };
  7846. /**
  7847. * Returns the right-to-left Kleisli composition of the provided functions,
  7848. * each of which must return a value of a type supported by [`chain`](#chain).
  7849. *
  7850. * `R.composeK(h, g, f)` is equivalent to `R.compose(R.chain(h), R.chain(g), R.chain(f))`.
  7851. *
  7852. * @func
  7853. * @memberOf R
  7854. * @since v0.16.0
  7855. * @category Function
  7856. * @sig Chain m => ((y -> m z), (x -> m y), ..., (a -> m b)) -> (a -> m z)
  7857. * @param {...Function} ...functions The functions to compose
  7858. * @return {Function}
  7859. * @see R.pipeK
  7860. * @example
  7861. *
  7862. * // get :: String -> Object -> Maybe *
  7863. * var get = R.curry((propName, obj) => Maybe(obj[propName]))
  7864. *
  7865. * // getStateCode :: Maybe String -> Maybe String
  7866. * var getStateCode = R.composeK(
  7867. * R.compose(Maybe.of, R.toUpper),
  7868. * get('state'),
  7869. * get('address'),
  7870. * get('user'),
  7871. * );
  7872. * getStateCode({"user":{"address":{"state":"ny"}}}); //=> Maybe.Just("NY")
  7873. * getStateCode({}); //=> Maybe.Nothing()
  7874. * @symb R.composeK(f, g, h)(a) = R.chain(f, R.chain(g, h(a)))
  7875. */
  7876. var composeK = function composeK() {
  7877. if (arguments.length === 0) {
  7878. throw new Error('composeK requires at least one argument');
  7879. }
  7880. var init = Array.prototype.slice.call(arguments);
  7881. var last = init.pop();
  7882. return compose(compose.apply(this, map(chain, init)), last);
  7883. };
  7884. /**
  7885. * Performs right-to-left composition of one or more Promise-returning
  7886. * functions. The rightmost function may have any arity; the remaining
  7887. * functions must be unary.
  7888. *
  7889. * @func
  7890. * @memberOf R
  7891. * @since v0.10.0
  7892. * @category Function
  7893. * @sig ((y -> Promise z), (x -> Promise y), ..., (a -> Promise b)) -> (a -> Promise z)
  7894. * @param {...Function} functions The functions to compose
  7895. * @return {Function}
  7896. * @see R.pipeP
  7897. * @example
  7898. *
  7899. * var db = {
  7900. * users: {
  7901. * JOE: {
  7902. * name: 'Joe',
  7903. * followers: ['STEVE', 'SUZY']
  7904. * }
  7905. * }
  7906. * }
  7907. *
  7908. * // We'll pretend to do a db lookup which returns a promise
  7909. * var lookupUser = (userId) => Promise.resolve(db.users[userId])
  7910. * var lookupFollowers = (user) => Promise.resolve(user.followers)
  7911. * lookupUser('JOE').then(lookupFollowers)
  7912. *
  7913. * // followersForUser :: String -> Promise [UserId]
  7914. * var followersForUser = R.composeP(lookupFollowers, lookupUser);
  7915. * followersForUser('JOE').then(followers => console.log('Followers:', followers))
  7916. * // Followers: ["STEVE","SUZY"]
  7917. */
  7918. var composeP = function composeP() {
  7919. if (arguments.length === 0) {
  7920. throw new Error('composeP requires at least one argument');
  7921. }
  7922. return pipeP.apply(this, reverse(arguments));
  7923. };
  7924. /**
  7925. * Wraps a constructor function inside a curried function that can be called
  7926. * with the same arguments and returns the same type.
  7927. *
  7928. * @func
  7929. * @memberOf R
  7930. * @since v0.1.0
  7931. * @category Function
  7932. * @sig (* -> {*}) -> (* -> {*})
  7933. * @param {Function} fn The constructor function to wrap.
  7934. * @return {Function} A wrapped, curried constructor function.
  7935. * @see R.invoker
  7936. * @example
  7937. *
  7938. * // Constructor function
  7939. * function Animal(kind) {
  7940. * this.kind = kind;
  7941. * };
  7942. * Animal.prototype.sighting = function() {
  7943. * return "It's a " + this.kind + "!";
  7944. * }
  7945. *
  7946. * var AnimalConstructor = R.construct(Animal)
  7947. *
  7948. * // Notice we no longer need the 'new' keyword:
  7949. * AnimalConstructor('Pig'); //=> {"kind": "Pig", "sighting": function (){...}};
  7950. *
  7951. * var animalTypes = ["Lion", "Tiger", "Bear"];
  7952. * var animalSighting = R.invoker(0, 'sighting');
  7953. * var sightNewAnimal = R.compose(animalSighting, AnimalConstructor);
  7954. * R.map(sightNewAnimal, animalTypes); //=> ["It's a Lion!", "It's a Tiger!", "It's a Bear!"]
  7955. */
  7956. var construct = _curry1(function construct(Fn) {
  7957. return constructN(Fn.length, Fn);
  7958. });
  7959. /**
  7960. * Returns `true` if the specified value is equal, in [`R.equals`](#equals)
  7961. * terms, to at least one element of the given list; `false` otherwise.
  7962. *
  7963. * @func
  7964. * @memberOf R
  7965. * @since v0.1.0
  7966. * @category List
  7967. * @sig a -> [a] -> Boolean
  7968. * @param {Object} a The item to compare against.
  7969. * @param {Array} list The array to consider.
  7970. * @return {Boolean} `true` if an equivalent item is in the list, `false` otherwise.
  7971. * @see R.any
  7972. * @example
  7973. *
  7974. * R.contains(3, [1, 2, 3]); //=> true
  7975. * R.contains(4, [1, 2, 3]); //=> false
  7976. * R.contains({ name: 'Fred' }, [{ name: 'Fred' }]); //=> true
  7977. * R.contains([42], [[42]]); //=> true
  7978. */
  7979. var contains = _curry2(_contains);
  7980. /**
  7981. * Finds the set (i.e. no duplicates) of all elements in the first list not
  7982. * contained in the second list. Objects and Arrays are compared in terms of
  7983. * value equality, not reference equality.
  7984. *
  7985. * @func
  7986. * @memberOf R
  7987. * @since v0.1.0
  7988. * @category Relation
  7989. * @sig [*] -> [*] -> [*]
  7990. * @param {Array} list1 The first list.
  7991. * @param {Array} list2 The second list.
  7992. * @return {Array} The elements in `list1` that are not in `list2`.
  7993. * @see R.differenceWith, R.symmetricDifference, R.symmetricDifferenceWith, R.without
  7994. * @example
  7995. *
  7996. * R.difference([1,2,3,4], [7,6,5,4,3]); //=> [1,2]
  7997. * R.difference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5]
  7998. * R.difference([{a: 1}, {b: 2}], [{a: 1}, {c: 3}]) //=> [{b: 2}]
  7999. */
  8000. var difference = _curry2(function difference(first, second) {
  8001. var out = [];
  8002. var idx = 0;
  8003. var firstLen = first.length;
  8004. while (idx < firstLen) {
  8005. if (!_contains(first[idx], second) && !_contains(first[idx], out)) {
  8006. out[out.length] = first[idx];
  8007. }
  8008. idx += 1;
  8009. }
  8010. return out;
  8011. });
  8012. /**
  8013. * Returns a new list without any consecutively repeating elements.
  8014. * [`R.equals`](#equals) is used to determine equality.
  8015. *
  8016. * Acts as a transducer if a transformer is given in list position.
  8017. *
  8018. * @func
  8019. * @memberOf R
  8020. * @since v0.14.0
  8021. * @category List
  8022. * @sig [a] -> [a]
  8023. * @param {Array} list The array to consider.
  8024. * @return {Array} `list` without repeating elements.
  8025. * @see R.transduce
  8026. * @example
  8027. *
  8028. * R.dropRepeats([1, 1, 1, 2, 3, 4, 4, 2, 2]); //=> [1, 2, 3, 4, 2]
  8029. */
  8030. var dropRepeats = _curry1(_dispatchable([], _xdropRepeatsWith(equals), dropRepeatsWith(equals)));
  8031. /**
  8032. * "lifts" a function of arity > 1 so that it may "map over" a list, Function or other
  8033. * object that satisfies the [FantasyLand Apply spec](https://github.com/fantasyland/fantasy-land#apply).
  8034. *
  8035. * @func
  8036. * @memberOf R
  8037. * @since v0.7.0
  8038. * @category Function
  8039. * @sig (*... -> *) -> ([*]... -> [*])
  8040. * @param {Function} fn The function to lift into higher context
  8041. * @return {Function} The lifted function.
  8042. * @see R.liftN
  8043. * @example
  8044. *
  8045. * var madd3 = R.lift((a, b, c) => a + b + c);
  8046. *
  8047. * madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
  8048. *
  8049. * var madd5 = R.lift((a, b, c, d, e) => a + b + c + d + e);
  8050. *
  8051. * madd5([1,2], [3], [4, 5], [6], [7, 8]); //=> [21, 22, 22, 23, 22, 23, 23, 24]
  8052. */
  8053. var lift = _curry1(function lift(fn) {
  8054. return liftN(fn.length, fn);
  8055. });
  8056. /**
  8057. * Returns a partial copy of an object omitting the keys specified.
  8058. *
  8059. * @func
  8060. * @memberOf R
  8061. * @since v0.1.0
  8062. * @category Object
  8063. * @sig [String] -> {String: *} -> {String: *}
  8064. * @param {Array} names an array of String property names to omit from the new object
  8065. * @param {Object} obj The object to copy from
  8066. * @return {Object} A new object with properties from `names` not on it.
  8067. * @see R.pick
  8068. * @example
  8069. *
  8070. * R.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, c: 3}
  8071. */
  8072. var omit = _curry2(function omit(names, obj) {
  8073. var result = {};
  8074. for (var prop in obj) {
  8075. if (!_contains(prop, names)) {
  8076. result[prop] = obj[prop];
  8077. }
  8078. }
  8079. return result;
  8080. });
  8081. /**
  8082. * Returns the left-to-right Kleisli composition of the provided functions,
  8083. * each of which must return a value of a type supported by [`chain`](#chain).
  8084. *
  8085. * `R.pipeK(f, g, h)` is equivalent to `R.pipe(R.chain(f), R.chain(g), R.chain(h))`.
  8086. *
  8087. * @func
  8088. * @memberOf R
  8089. * @since v0.16.0
  8090. * @category Function
  8091. * @sig Chain m => ((a -> m b), (b -> m c), ..., (y -> m z)) -> (a -> m z)
  8092. * @param {...Function}
  8093. * @return {Function}
  8094. * @see R.composeK
  8095. * @example
  8096. *
  8097. * // parseJson :: String -> Maybe *
  8098. * // get :: String -> Object -> Maybe *
  8099. *
  8100. * // getStateCode :: Maybe String -> Maybe String
  8101. * var getStateCode = R.pipeK(
  8102. * parseJson,
  8103. * get('user'),
  8104. * get('address'),
  8105. * get('state'),
  8106. * R.compose(Maybe.of, R.toUpper)
  8107. * );
  8108. *
  8109. * getStateCode('{"user":{"address":{"state":"ny"}}}');
  8110. * //=> Just('NY')
  8111. * getStateCode('[Invalid JSON]');
  8112. * //=> Nothing()
  8113. * @symb R.pipeK(f, g, h)(a) = R.chain(h, R.chain(g, f(a)))
  8114. */
  8115. var pipeK = function pipeK() {
  8116. if (arguments.length === 0) {
  8117. throw new Error('pipeK requires at least one argument');
  8118. }
  8119. return composeK.apply(this, reverse(arguments));
  8120. };
  8121. /**
  8122. * Returns the string representation of the given value. `eval`'ing the output
  8123. * should result in a value equivalent to the input value. Many of the built-in
  8124. * `toString` methods do not satisfy this requirement.
  8125. *
  8126. * If the given value is an `[object Object]` with a `toString` method other
  8127. * than `Object.prototype.toString`, this method is invoked with no arguments
  8128. * to produce the return value. This means user-defined constructor functions
  8129. * can provide a suitable `toString` method. For example:
  8130. *
  8131. * function Point(x, y) {
  8132. * this.x = x;
  8133. * this.y = y;
  8134. * }
  8135. *
  8136. * Point.prototype.toString = function() {
  8137. * return 'new Point(' + this.x + ', ' + this.y + ')';
  8138. * };
  8139. *
  8140. * R.toString(new Point(1, 2)); //=> 'new Point(1, 2)'
  8141. *
  8142. * @func
  8143. * @memberOf R
  8144. * @since v0.14.0
  8145. * @category String
  8146. * @sig * -> String
  8147. * @param {*} val
  8148. * @return {String}
  8149. * @example
  8150. *
  8151. * R.toString(42); //=> '42'
  8152. * R.toString('abc'); //=> '"abc"'
  8153. * R.toString([1, 2, 3]); //=> '[1, 2, 3]'
  8154. * R.toString({foo: 1, bar: 2, baz: 3}); //=> '{"bar": 2, "baz": 3, "foo": 1}'
  8155. * R.toString(new Date('2001-02-03T04:05:06Z')); //=> 'new Date("2001-02-03T04:05:06.000Z")'
  8156. */
  8157. var toString = _curry1(function toString(val) {
  8158. return _toString(val, []);
  8159. });
  8160. /**
  8161. * Returns a new list without values in the first argument.
  8162. * [`R.equals`](#equals) is used to determine equality.
  8163. *
  8164. * Acts as a transducer if a transformer is given in list position.
  8165. *
  8166. * @func
  8167. * @memberOf R
  8168. * @since v0.19.0
  8169. * @category List
  8170. * @sig [a] -> [a] -> [a]
  8171. * @param {Array} list1 The values to be removed from `list2`.
  8172. * @param {Array} list2 The array to remove values from.
  8173. * @return {Array} The new array without values in `list1`.
  8174. * @see R.transduce, R.difference
  8175. * @example
  8176. *
  8177. * R.without([1, 2], [1, 2, 1, 3, 4]); //=> [3, 4]
  8178. */
  8179. var without = _curry2(function (xs, list) {
  8180. return reject(flip(_contains)(xs), list);
  8181. });
  8182. // A simple Set type that honours R.equals semantics
  8183. /* globals Set */
  8184. // until we figure out why jsdoc chokes on this
  8185. // @param item The item to add to the Set
  8186. // @returns {boolean} true if the item did not exist prior, otherwise false
  8187. //
  8188. //
  8189. // @param item The item to check for existence in the Set
  8190. // @returns {boolean} true if the item exists in the Set, otherwise false
  8191. //
  8192. //
  8193. // Combines the logic for checking whether an item is a member of the set and
  8194. // for adding a new item to the set.
  8195. //
  8196. // @param item The item to check or add to the Set instance.
  8197. // @param shouldAdd If true, the item will be added to the set if it doesn't
  8198. // already exist.
  8199. // @param set The set instance to check or add to.
  8200. // @return {boolean} true if the item already existed, otherwise false.
  8201. //
  8202. // distinguish between +0 and -0
  8203. // these types can all utilise the native Set
  8204. // set._items['boolean'] holds a two element array
  8205. // representing [ falseExists, trueExists ]
  8206. // compare functions for reference equality
  8207. /* falls through */
  8208. // reduce the search size of heterogeneous sets by creating buckets
  8209. // for each type.
  8210. // scan through all previously applied items
  8211. var _Set = function () {
  8212. function _Set() {
  8213. /* globals Set */
  8214. this._nativeSet = typeof Set === 'function' ? new Set() : null;
  8215. this._items = {};
  8216. }
  8217. // until we figure out why jsdoc chokes on this
  8218. // @param item The item to add to the Set
  8219. // @returns {boolean} true if the item did not exist prior, otherwise false
  8220. //
  8221. _Set.prototype.add = function (item) {
  8222. return !hasOrAdd(item, true, this);
  8223. };
  8224. //
  8225. // @param item The item to check for existence in the Set
  8226. // @returns {boolean} true if the item exists in the Set, otherwise false
  8227. //
  8228. _Set.prototype.has = function (item) {
  8229. return hasOrAdd(item, false, this);
  8230. };
  8231. //
  8232. // Combines the logic for checking whether an item is a member of the set and
  8233. // for adding a new item to the set.
  8234. //
  8235. // @param item The item to check or add to the Set instance.
  8236. // @param shouldAdd If true, the item will be added to the set if it doesn't
  8237. // already exist.
  8238. // @param set The set instance to check or add to.
  8239. // @return {boolean} true if the item already existed, otherwise false.
  8240. //
  8241. function hasOrAdd(item, shouldAdd, set) {
  8242. var type = typeof item;
  8243. var prevSize, newSize;
  8244. switch (type) {
  8245. case 'string':
  8246. case 'number':
  8247. // distinguish between +0 and -0
  8248. if (item === 0 && 1 / item === -Infinity) {
  8249. if (set._items['-0']) {
  8250. return true;
  8251. } else {
  8252. if (shouldAdd) {
  8253. set._items['-0'] = true;
  8254. }
  8255. return false;
  8256. }
  8257. }
  8258. // these types can all utilise the native Set
  8259. if (set._nativeSet !== null) {
  8260. if (shouldAdd) {
  8261. prevSize = set._nativeSet.size;
  8262. set._nativeSet.add(item);
  8263. newSize = set._nativeSet.size;
  8264. return newSize === prevSize;
  8265. } else {
  8266. return set._nativeSet.has(item);
  8267. }
  8268. } else {
  8269. if (!(type in set._items)) {
  8270. if (shouldAdd) {
  8271. set._items[type] = {};
  8272. set._items[type][item] = true;
  8273. }
  8274. return false;
  8275. } else if (item in set._items[type]) {
  8276. return true;
  8277. } else {
  8278. if (shouldAdd) {
  8279. set._items[type][item] = true;
  8280. }
  8281. return false;
  8282. }
  8283. }
  8284. case 'boolean':
  8285. // set._items['boolean'] holds a two element array
  8286. // representing [ falseExists, trueExists ]
  8287. if (type in set._items) {
  8288. var bIdx = item ? 1 : 0;
  8289. if (set._items[type][bIdx]) {
  8290. return true;
  8291. } else {
  8292. if (shouldAdd) {
  8293. set._items[type][bIdx] = true;
  8294. }
  8295. return false;
  8296. }
  8297. } else {
  8298. if (shouldAdd) {
  8299. set._items[type] = item ? [
  8300. false,
  8301. true
  8302. ] : [
  8303. true,
  8304. false
  8305. ];
  8306. }
  8307. return false;
  8308. }
  8309. case 'function':
  8310. // compare functions for reference equality
  8311. if (set._nativeSet !== null) {
  8312. if (shouldAdd) {
  8313. prevSize = set._nativeSet.size;
  8314. set._nativeSet.add(item);
  8315. newSize = set._nativeSet.size;
  8316. return newSize === prevSize;
  8317. } else {
  8318. return set._nativeSet.has(item);
  8319. }
  8320. } else {
  8321. if (!(type in set._items)) {
  8322. if (shouldAdd) {
  8323. set._items[type] = [item];
  8324. }
  8325. return false;
  8326. }
  8327. if (!_contains(item, set._items[type])) {
  8328. if (shouldAdd) {
  8329. set._items[type].push(item);
  8330. }
  8331. return false;
  8332. }
  8333. return true;
  8334. }
  8335. case 'undefined':
  8336. if (set._items[type]) {
  8337. return true;
  8338. } else {
  8339. if (shouldAdd) {
  8340. set._items[type] = true;
  8341. }
  8342. return false;
  8343. }
  8344. case 'object':
  8345. if (item === null) {
  8346. if (!set._items['null']) {
  8347. if (shouldAdd) {
  8348. set._items['null'] = true;
  8349. }
  8350. return false;
  8351. }
  8352. return true;
  8353. }
  8354. /* falls through */
  8355. default:
  8356. // reduce the search size of heterogeneous sets by creating buckets
  8357. // for each type.
  8358. type = Object.prototype.toString.call(item);
  8359. if (!(type in set._items)) {
  8360. if (shouldAdd) {
  8361. set._items[type] = [item];
  8362. }
  8363. return false;
  8364. }
  8365. // scan through all previously applied items
  8366. if (!_contains(item, set._items[type])) {
  8367. if (shouldAdd) {
  8368. set._items[type].push(item);
  8369. }
  8370. return false;
  8371. }
  8372. return true;
  8373. }
  8374. }
  8375. return _Set;
  8376. }();
  8377. /**
  8378. * A function which calls the two provided functions and returns the `&&`
  8379. * of the results.
  8380. * It returns the result of the first function if it is false-y and the result
  8381. * of the second function otherwise. Note that this is short-circuited,
  8382. * meaning that the second function will not be invoked if the first returns a
  8383. * false-y value.
  8384. *
  8385. * In addition to functions, `R.both` also accepts any fantasy-land compatible
  8386. * applicative functor.
  8387. *
  8388. * @func
  8389. * @memberOf R
  8390. * @since v0.12.0
  8391. * @category Logic
  8392. * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
  8393. * @param {Function} f A predicate
  8394. * @param {Function} g Another predicate
  8395. * @return {Function} a function that applies its arguments to `f` and `g` and `&&`s their outputs together.
  8396. * @see R.and
  8397. * @example
  8398. *
  8399. * var gt10 = R.gt(R.__, 10)
  8400. * var lt20 = R.lt(R.__, 20)
  8401. * var f = R.both(gt10, lt20);
  8402. * f(15); //=> true
  8403. * f(30); //=> false
  8404. */
  8405. var both = _curry2(function both(f, g) {
  8406. return _isFunction(f) ? function _both() {
  8407. return f.apply(this, arguments) && g.apply(this, arguments);
  8408. } : lift(and)(f, g);
  8409. });
  8410. /**
  8411. * Takes a function `f` and returns a function `g` such that if called with the same arguments
  8412. * when `f` returns a "truthy" value, `g` returns `false` and when `f` returns a "falsy" value `g` returns `true`.
  8413. *
  8414. * `R.complement` may be applied to any functor
  8415. *
  8416. * @func
  8417. * @memberOf R
  8418. * @since v0.12.0
  8419. * @category Logic
  8420. * @sig (*... -> *) -> (*... -> Boolean)
  8421. * @param {Function} f
  8422. * @return {Function}
  8423. * @see R.not
  8424. * @example
  8425. *
  8426. * var isNotNil = R.complement(R.isNil);
  8427. * isNil(null); //=> true
  8428. * isNotNil(null); //=> false
  8429. * isNil(7); //=> false
  8430. * isNotNil(7); //=> true
  8431. */
  8432. var complement = lift(not);
  8433. /**
  8434. * Returns the result of concatenating the given lists or strings.
  8435. *
  8436. * Note: `R.concat` expects both arguments to be of the same type,
  8437. * unlike the native `Array.prototype.concat` method. It will throw
  8438. * an error if you `concat` an Array with a non-Array value.
  8439. *
  8440. * Dispatches to the `concat` method of the first argument, if present.
  8441. *
  8442. * @func
  8443. * @memberOf R
  8444. * @since v0.1.0
  8445. * @category List
  8446. * @sig [a] -> [a] -> [a]
  8447. * @sig String -> String -> String
  8448. * @param {Array|String} firstList The first list
  8449. * @param {Array|String} secondList The second list
  8450. * @return {Array|String} A list consisting of the elements of `firstList` followed by the elements of
  8451. * `secondList`.
  8452. *
  8453. * @example
  8454. *
  8455. * R.concat('ABC', 'DEF'); // 'ABCDEF'
  8456. * R.concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
  8457. * R.concat([], []); //=> []
  8458. */
  8459. var concat = _curry2(function concat(a, b) {
  8460. if (_isArray(a)) {
  8461. if (_isArray(b)) {
  8462. return a.concat(b);
  8463. }
  8464. throw new TypeError(toString(b) + ' is not an array');
  8465. }
  8466. if (_isString(a)) {
  8467. if (_isString(b)) {
  8468. return a + b;
  8469. }
  8470. throw new TypeError(toString(b) + ' is not a string');
  8471. }
  8472. if (a != null && _isFunction(a['fantasy-land/concat'])) {
  8473. return a['fantasy-land/concat'](b);
  8474. }
  8475. if (a != null && _isFunction(a.concat)) {
  8476. return a.concat(b);
  8477. }
  8478. throw new TypeError(toString(a) + ' does not have a method named "concat" or "fantasy-land/concat"');
  8479. });
  8480. /**
  8481. * A function wrapping calls to the two functions in an `||` operation,
  8482. * returning the result of the first function if it is truth-y and the result
  8483. * of the second function otherwise. Note that this is short-circuited,
  8484. * meaning that the second function will not be invoked if the first returns a
  8485. * truth-y value.
  8486. *
  8487. * In addition to functions, `R.either` also accepts any fantasy-land compatible
  8488. * applicative functor.
  8489. *
  8490. * @func
  8491. * @memberOf R
  8492. * @since v0.12.0
  8493. * @category Logic
  8494. * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
  8495. * @param {Function} f a predicate
  8496. * @param {Function} g another predicate
  8497. * @return {Function} a function that applies its arguments to `f` and `g` and `||`s their outputs together.
  8498. * @see R.or
  8499. * @example
  8500. *
  8501. * var gt10 = x => x > 10;
  8502. * var even = x => x % 2 === 0;
  8503. * var f = R.either(gt10, even);
  8504. * f(101); //=> true
  8505. * f(8); //=> true
  8506. */
  8507. var either = _curry2(function either(f, g) {
  8508. return _isFunction(f) ? function _either() {
  8509. return f.apply(this, arguments) || g.apply(this, arguments);
  8510. } : lift(or)(f, g);
  8511. });
  8512. /**
  8513. * Turns a named method with a specified arity into a function that can be
  8514. * called directly supplied with arguments and a target object.
  8515. *
  8516. * The returned function is curried and accepts `arity + 1` parameters where
  8517. * the final parameter is the target object.
  8518. *
  8519. * @func
  8520. * @memberOf R
  8521. * @since v0.1.0
  8522. * @category Function
  8523. * @sig Number -> String -> (a -> b -> ... -> n -> Object -> *)
  8524. * @param {Number} arity Number of arguments the returned function should take
  8525. * before the target object.
  8526. * @param {String} method Name of the method to call.
  8527. * @return {Function} A new curried function.
  8528. * @see R.construct
  8529. * @example
  8530. *
  8531. * var sliceFrom = R.invoker(1, 'slice');
  8532. * sliceFrom(6, 'abcdefghijklm'); //=> 'ghijklm'
  8533. * var sliceFrom6 = R.invoker(2, 'slice')(6);
  8534. * sliceFrom6(8, 'abcdefghijklm'); //=> 'gh'
  8535. * @symb R.invoker(0, 'method')(o) = o['method']()
  8536. * @symb R.invoker(1, 'method')(a, o) = o['method'](a)
  8537. * @symb R.invoker(2, 'method')(a, b, o) = o['method'](a, b)
  8538. */
  8539. var invoker = _curry2(function invoker(arity, method) {
  8540. return curryN(arity + 1, function () {
  8541. var target = arguments[arity];
  8542. if (target != null && _isFunction(target[method])) {
  8543. return target[method].apply(target, Array.prototype.slice.call(arguments, 0, arity));
  8544. }
  8545. throw new TypeError(toString(target) + ' does not have a method named "' + method + '"');
  8546. });
  8547. });
  8548. /**
  8549. * Returns a string made by inserting the `separator` between each element and
  8550. * concatenating all the elements into a single string.
  8551. *
  8552. * @func
  8553. * @memberOf R
  8554. * @since v0.1.0
  8555. * @category List
  8556. * @sig String -> [a] -> String
  8557. * @param {Number|String} separator The string used to separate the elements.
  8558. * @param {Array} xs The elements to join into a string.
  8559. * @return {String} str The string made by concatenating `xs` with `separator`.
  8560. * @see R.split
  8561. * @example
  8562. *
  8563. * var spacer = R.join(' ');
  8564. * spacer(['a', 2, 3.4]); //=> 'a 2 3.4'
  8565. * R.join('|', [1, 2, 3]); //=> '1|2|3'
  8566. */
  8567. var join = invoker(1, 'join');
  8568. /**
  8569. * Creates a new function that, when invoked, caches the result of calling `fn`
  8570. * for a given argument set and returns the result. Subsequent calls to the
  8571. * memoized `fn` with the same argument set will not result in an additional
  8572. * call to `fn`; instead, the cached result for that set of arguments will be
  8573. * returned.
  8574. *
  8575. * @func
  8576. * @memberOf R
  8577. * @since v0.1.0
  8578. * @category Function
  8579. * @sig (*... -> a) -> (*... -> a)
  8580. * @param {Function} fn The function to memoize.
  8581. * @return {Function} Memoized version of `fn`.
  8582. * @see R.memoizeWith
  8583. * @example
  8584. *
  8585. * let count = 0;
  8586. * const factorial = R.memoize(n => {
  8587. * count += 1;
  8588. * return R.product(R.range(1, n + 1));
  8589. * });
  8590. * factorial(5); //=> 120
  8591. * factorial(5); //=> 120
  8592. * factorial(5); //=> 120
  8593. * count; //=> 1
  8594. */
  8595. var memoize = memoizeWith(function () {
  8596. return toString(arguments);
  8597. });
  8598. /**
  8599. * Splits a string into an array of strings based on the given
  8600. * separator.
  8601. *
  8602. * @func
  8603. * @memberOf R
  8604. * @since v0.1.0
  8605. * @category String
  8606. * @sig (String | RegExp) -> String -> [String]
  8607. * @param {String|RegExp} sep The pattern.
  8608. * @param {String} str The string to separate into an array.
  8609. * @return {Array} The array of strings from `str` separated by `str`.
  8610. * @see R.join
  8611. * @example
  8612. *
  8613. * var pathComponents = R.split('/');
  8614. * R.tail(pathComponents('/usr/local/bin/node')); //=> ['usr', 'local', 'bin', 'node']
  8615. *
  8616. * R.split('.', 'a.b.c.xyz.d'); //=> ['a', 'b', 'c', 'xyz', 'd']
  8617. */
  8618. var split = invoker(1, 'split');
  8619. /**
  8620. * Finds the set (i.e. no duplicates) of all elements contained in the first or
  8621. * second list, but not both.
  8622. *
  8623. * @func
  8624. * @memberOf R
  8625. * @since v0.19.0
  8626. * @category Relation
  8627. * @sig [*] -> [*] -> [*]
  8628. * @param {Array} list1 The first list.
  8629. * @param {Array} list2 The second list.
  8630. * @return {Array} The elements in `list1` or `list2`, but not both.
  8631. * @see R.symmetricDifferenceWith, R.difference, R.differenceWith
  8632. * @example
  8633. *
  8634. * R.symmetricDifference([1,2,3,4], [7,6,5,4,3]); //=> [1,2,7,6,5]
  8635. * R.symmetricDifference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5,1,2]
  8636. */
  8637. var symmetricDifference = _curry2(function symmetricDifference(list1, list2) {
  8638. return concat(difference(list1, list2), difference(list2, list1));
  8639. });
  8640. /**
  8641. * Finds the set (i.e. no duplicates) of all elements contained in the first or
  8642. * second list, but not both. Duplication is determined according to the value
  8643. * returned by applying the supplied predicate to two list elements.
  8644. *
  8645. * @func
  8646. * @memberOf R
  8647. * @since v0.19.0
  8648. * @category Relation
  8649. * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
  8650. * @param {Function} pred A predicate used to test whether two items are equal.
  8651. * @param {Array} list1 The first list.
  8652. * @param {Array} list2 The second list.
  8653. * @return {Array} The elements in `list1` or `list2`, but not both.
  8654. * @see R.symmetricDifference, R.difference, R.differenceWith
  8655. * @example
  8656. *
  8657. * var eqA = R.eqBy(R.prop('a'));
  8658. * var l1 = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
  8659. * var l2 = [{a: 3}, {a: 4}, {a: 5}, {a: 6}];
  8660. * R.symmetricDifferenceWith(eqA, l1, l2); //=> [{a: 1}, {a: 2}, {a: 5}, {a: 6}]
  8661. */
  8662. var symmetricDifferenceWith = _curry3(function symmetricDifferenceWith(pred, list1, list2) {
  8663. return concat(differenceWith(pred, list1, list2), differenceWith(pred, list2, list1));
  8664. });
  8665. /**
  8666. * Determines whether a given string matches a given regular expression.
  8667. *
  8668. * @func
  8669. * @memberOf R
  8670. * @since v0.12.0
  8671. * @category String
  8672. * @sig RegExp -> String -> Boolean
  8673. * @param {RegExp} pattern
  8674. * @param {String} str
  8675. * @return {Boolean}
  8676. * @see R.match
  8677. * @example
  8678. *
  8679. * R.test(/^x/, 'xyz'); //=> true
  8680. * R.test(/^y/, 'xyz'); //=> false
  8681. */
  8682. var test = _curry2(function test(pattern, str) {
  8683. if (!_isRegExp(pattern)) {
  8684. throw new TypeError('\u2018test\u2019 requires a value of type RegExp as its first argument; received ' + toString(pattern));
  8685. }
  8686. return _cloneRegExp(pattern).test(str);
  8687. });
  8688. /**
  8689. * The lower case version of a string.
  8690. *
  8691. * @func
  8692. * @memberOf R
  8693. * @since v0.9.0
  8694. * @category String
  8695. * @sig String -> String
  8696. * @param {String} str The string to lower case.
  8697. * @return {String} The lower case version of `str`.
  8698. * @see R.toUpper
  8699. * @example
  8700. *
  8701. * R.toLower('XYZ'); //=> 'xyz'
  8702. */
  8703. var toLower = invoker(0, 'toLowerCase');
  8704. /**
  8705. * The upper case version of a string.
  8706. *
  8707. * @func
  8708. * @memberOf R
  8709. * @since v0.9.0
  8710. * @category String
  8711. * @sig String -> String
  8712. * @param {String} str The string to upper case.
  8713. * @return {String} The upper case version of `str`.
  8714. * @see R.toLower
  8715. * @example
  8716. *
  8717. * R.toUpper('abc'); //=> 'ABC'
  8718. */
  8719. var toUpper = invoker(0, 'toUpperCase');
  8720. /**
  8721. * Returns a new list containing only one copy of each element in the original
  8722. * list, based upon the value returned by applying the supplied function to
  8723. * each list element. Prefers the first item if the supplied function produces
  8724. * the same value on two items. [`R.equals`](#equals) is used for comparison.
  8725. *
  8726. * @func
  8727. * @memberOf R
  8728. * @since v0.16.0
  8729. * @category List
  8730. * @sig (a -> b) -> [a] -> [a]
  8731. * @param {Function} fn A function used to produce a value to use during comparisons.
  8732. * @param {Array} list The array to consider.
  8733. * @return {Array} The list of unique items.
  8734. * @example
  8735. *
  8736. * R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]); //=> [-1, -5, 2, 10]
  8737. */
  8738. var uniqBy = _curry2(function uniqBy(fn, list) {
  8739. var set = new _Set();
  8740. var result = [];
  8741. var idx = 0;
  8742. var appliedItem, item;
  8743. while (idx < list.length) {
  8744. item = list[idx];
  8745. appliedItem = fn(item);
  8746. if (set.add(appliedItem)) {
  8747. result.push(item);
  8748. }
  8749. idx += 1;
  8750. }
  8751. return result;
  8752. });
  8753. /**
  8754. * Returns a new list containing only one copy of each element in the original
  8755. * list. [`R.equals`](#equals) is used to determine equality.
  8756. *
  8757. * @func
  8758. * @memberOf R
  8759. * @since v0.1.0
  8760. * @category List
  8761. * @sig [a] -> [a]
  8762. * @param {Array} list The array to consider.
  8763. * @return {Array} The list of unique items.
  8764. * @example
  8765. *
  8766. * R.uniq([1, 1, 2, 1]); //=> [1, 2]
  8767. * R.uniq([1, '1']); //=> [1, '1']
  8768. * R.uniq([[42], [42]]); //=> [[42]]
  8769. */
  8770. var uniq = uniqBy(identity);
  8771. /**
  8772. * Combines two lists into a set (i.e. no duplicates) composed of those
  8773. * elements common to both lists.
  8774. *
  8775. * @func
  8776. * @memberOf R
  8777. * @since v0.1.0
  8778. * @category Relation
  8779. * @sig [*] -> [*] -> [*]
  8780. * @param {Array} list1 The first list.
  8781. * @param {Array} list2 The second list.
  8782. * @return {Array} The list of elements found in both `list1` and `list2`.
  8783. * @see R.innerJoin
  8784. * @example
  8785. *
  8786. * R.intersection([1,2,3,4], [7,6,5,4,3]); //=> [4, 3]
  8787. */
  8788. var intersection = _curry2(function intersection(list1, list2) {
  8789. var lookupList, filteredList;
  8790. if (list1.length > list2.length) {
  8791. lookupList = list1;
  8792. filteredList = list2;
  8793. } else {
  8794. lookupList = list2;
  8795. filteredList = list1;
  8796. }
  8797. return uniq(_filter(flip(_contains)(lookupList), filteredList));
  8798. });
  8799. /**
  8800. * Combines two lists into a set (i.e. no duplicates) composed of the elements
  8801. * of each list.
  8802. *
  8803. * @func
  8804. * @memberOf R
  8805. * @since v0.1.0
  8806. * @category Relation
  8807. * @sig [*] -> [*] -> [*]
  8808. * @param {Array} as The first list.
  8809. * @param {Array} bs The second list.
  8810. * @return {Array} The first and second lists concatenated, with
  8811. * duplicates removed.
  8812. * @example
  8813. *
  8814. * R.union([1, 2, 3], [2, 3, 4]); //=> [1, 2, 3, 4]
  8815. */
  8816. var union = _curry2(compose(uniq, _concat));
  8817. var R = {
  8818. F: F,
  8819. T: T,
  8820. __: __,
  8821. add: add,
  8822. addIndex: addIndex,
  8823. adjust: adjust,
  8824. all: all,
  8825. allPass: allPass,
  8826. always: always,
  8827. and: and,
  8828. any: any,
  8829. anyPass: anyPass,
  8830. ap: ap,
  8831. aperture: aperture,
  8832. append: append,
  8833. apply: apply,
  8834. applySpec: applySpec,
  8835. ascend: ascend,
  8836. assoc: assoc,
  8837. assocPath: assocPath,
  8838. binary: binary,
  8839. bind: bind,
  8840. both: both,
  8841. call: call,
  8842. chain: chain,
  8843. clamp: clamp,
  8844. clone: clone,
  8845. comparator: comparator,
  8846. complement: complement,
  8847. compose: compose,
  8848. composeK: composeK,
  8849. composeP: composeP,
  8850. concat: concat,
  8851. cond: cond,
  8852. construct: construct,
  8853. constructN: constructN,
  8854. contains: contains,
  8855. converge: converge,
  8856. countBy: countBy,
  8857. curry: curry,
  8858. curryN: curryN,
  8859. dec: dec,
  8860. defaultTo: defaultTo,
  8861. descend: descend,
  8862. difference: difference,
  8863. differenceWith: differenceWith,
  8864. dissoc: dissoc,
  8865. dissocPath: dissocPath,
  8866. divide: divide,
  8867. drop: drop,
  8868. dropLast: dropLast,
  8869. dropLastWhile: dropLastWhile,
  8870. dropRepeats: dropRepeats,
  8871. dropRepeatsWith: dropRepeatsWith,
  8872. dropWhile: dropWhile,
  8873. either: either,
  8874. empty: empty,
  8875. endsWith: endsWith,
  8876. eqBy: eqBy,
  8877. eqProps: eqProps,
  8878. equals: equals,
  8879. evolve: evolve,
  8880. filter: filter,
  8881. find: find,
  8882. findIndex: findIndex,
  8883. findLast: findLast,
  8884. findLastIndex: findLastIndex,
  8885. flatten: flatten,
  8886. flip: flip,
  8887. forEach: forEach,
  8888. forEachObjIndexed: forEachObjIndexed,
  8889. fromPairs: fromPairs,
  8890. groupBy: groupBy,
  8891. groupWith: groupWith,
  8892. gt: gt,
  8893. gte: gte,
  8894. has: has,
  8895. hasIn: hasIn,
  8896. head: head,
  8897. identical: identical,
  8898. identity: identity,
  8899. ifElse: ifElse,
  8900. inc: inc,
  8901. indexBy: indexBy,
  8902. indexOf: indexOf,
  8903. init: init,
  8904. innerJoin: innerJoin,
  8905. insert: insert,
  8906. insertAll: insertAll,
  8907. intersection: intersection,
  8908. intersectionWith: intersectionWith,
  8909. intersperse: intersperse,
  8910. into: into,
  8911. invert: invert,
  8912. invertObj: invertObj,
  8913. invoker: invoker,
  8914. is: is,
  8915. isEmpty: isEmpty,
  8916. isNil: isNil,
  8917. join: join,
  8918. juxt: juxt,
  8919. keys: keys,
  8920. keysIn: keysIn,
  8921. last: last,
  8922. lastIndexOf: lastIndexOf,
  8923. length: length,
  8924. lens: lens,
  8925. lensIndex: lensIndex,
  8926. lensPath: lensPath,
  8927. lensProp: lensProp,
  8928. lift: lift,
  8929. liftN: liftN,
  8930. lt: lt,
  8931. lte: lte,
  8932. map: map,
  8933. mapAccum: mapAccum,
  8934. mapAccumRight: mapAccumRight,
  8935. mapObjIndexed: mapObjIndexed,
  8936. match: match,
  8937. mathMod: mathMod,
  8938. max: max,
  8939. maxBy: maxBy,
  8940. mean: mean,
  8941. median: median,
  8942. memoize: memoize,
  8943. memoizeWith: memoizeWith,
  8944. merge: merge,
  8945. mergeAll: mergeAll,
  8946. mergeDeepLeft: mergeDeepLeft,
  8947. mergeDeepRight: mergeDeepRight,
  8948. mergeDeepWith: mergeDeepWith,
  8949. mergeDeepWithKey: mergeDeepWithKey,
  8950. mergeWith: mergeWith,
  8951. mergeWithKey: mergeWithKey,
  8952. min: min,
  8953. minBy: minBy,
  8954. modulo: modulo,
  8955. multiply: multiply,
  8956. nAry: nAry,
  8957. negate: negate,
  8958. none: none,
  8959. not: not,
  8960. nth: nth,
  8961. nthArg: nthArg,
  8962. o: o,
  8963. objOf: objOf,
  8964. of: of,
  8965. omit: omit,
  8966. once: once,
  8967. or: or,
  8968. over: over,
  8969. pair: pair,
  8970. partial: partial,
  8971. partialRight: partialRight,
  8972. partition: partition,
  8973. path: path,
  8974. pathEq: pathEq,
  8975. pathOr: pathOr,
  8976. pathSatisfies: pathSatisfies,
  8977. pick: pick,
  8978. pickAll: pickAll,
  8979. pickBy: pickBy,
  8980. pipe: pipe,
  8981. pipeK: pipeK,
  8982. pipeP: pipeP,
  8983. pluck: pluck,
  8984. prepend: prepend,
  8985. product: product,
  8986. project: project,
  8987. prop: prop,
  8988. propEq: propEq,
  8989. propIs: propIs,
  8990. propOr: propOr,
  8991. propSatisfies: propSatisfies,
  8992. props: props,
  8993. range: range,
  8994. reduce: reduce,
  8995. reduceBy: reduceBy,
  8996. reduceRight: reduceRight,
  8997. reduceWhile: reduceWhile,
  8998. reduced: reduced,
  8999. reject: reject,
  9000. remove: remove,
  9001. repeat: repeat,
  9002. replace: replace,
  9003. reverse: reverse,
  9004. scan: scan,
  9005. sequence: sequence,
  9006. set: set,
  9007. slice: slice,
  9008. sort: sort,
  9009. sortBy: sortBy,
  9010. sortWith: sortWith,
  9011. split: split,
  9012. splitAt: splitAt,
  9013. splitEvery: splitEvery,
  9014. splitWhen: splitWhen,
  9015. startsWith: startsWith,
  9016. subtract: subtract,
  9017. sum: sum,
  9018. symmetricDifference: symmetricDifference,
  9019. symmetricDifferenceWith: symmetricDifferenceWith,
  9020. tail: tail,
  9021. take: take,
  9022. takeLast: takeLast,
  9023. takeLastWhile: takeLastWhile,
  9024. takeWhile: takeWhile,
  9025. tap: tap,
  9026. test: test,
  9027. times: times,
  9028. toLower: toLower,
  9029. toPairs: toPairs,
  9030. toPairsIn: toPairsIn,
  9031. toString: toString,
  9032. toUpper: toUpper,
  9033. transduce: transduce,
  9034. transpose: transpose,
  9035. traverse: traverse,
  9036. trim: trim,
  9037. tryCatch: tryCatch,
  9038. type: type,
  9039. unapply: unapply,
  9040. unary: unary,
  9041. uncurryN: uncurryN,
  9042. unfold: unfold,
  9043. union: union,
  9044. unionWith: unionWith,
  9045. uniq: uniq,
  9046. uniqBy: uniqBy,
  9047. uniqWith: uniqWith,
  9048. unless: unless,
  9049. unnest: unnest,
  9050. until: until,
  9051. update: update,
  9052. useWith: useWith,
  9053. values: values,
  9054. valuesIn: valuesIn,
  9055. view: view,
  9056. when: when,
  9057. where: where,
  9058. whereEq: whereEq,
  9059. without: without,
  9060. xprod: xprod,
  9061. zip: zip,
  9062. zipObj: zipObj,
  9063. zipWith: zipWith
  9064. };
  9065. /* eslint-env amd */
  9066. /* TEST_ENTRY_POINT */
  9067. if (typeof exports === 'object') {
  9068. module.exports = R;
  9069. } else if (typeof define === 'function' && define.amd) {
  9070. define(function() { return R; });
  9071. } else {
  9072. this.R = R;
  9073. }
  9074. }.call(this));