ramda.js 332 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439
  1. // Ramda v0.29.0
  2. // https://github.com/ramda/ramda
  3. // (c) 2013-2023 Scott Sauyet, Michael Hurley, and David Chambers
  4. // Ramda may be freely distributed under the MIT license.
  5. (function (global, factory) {
  6. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  7. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  8. (global = global || self, factory(global.R = {}));
  9. }(this, (function (exports) { 'use strict';
  10. /**
  11. * A function that always returns `false`. Any passed in parameters are ignored.
  12. *
  13. * @func
  14. * @memberOf R
  15. * @since v0.9.0
  16. * @category Function
  17. * @sig * -> Boolean
  18. * @param {*}
  19. * @return {Boolean}
  20. * @see R.T
  21. * @example
  22. *
  23. * R.F(); //=> false
  24. */
  25. var F = function F() {
  26. return false;
  27. };
  28. /**
  29. * A function that always returns `true`. Any passed in parameters are ignored.
  30. *
  31. * @func
  32. * @memberOf R
  33. * @since v0.9.0
  34. * @category Function
  35. * @sig * -> Boolean
  36. * @param {*}
  37. * @return {Boolean}
  38. * @see R.F
  39. * @example
  40. *
  41. * R.T(); //=> true
  42. */
  43. var T = function T() {
  44. return true;
  45. };
  46. /**
  47. * A special placeholder value used to specify "gaps" within curried functions,
  48. * allowing partial application of any combination of arguments, regardless of
  49. * their positions.
  50. *
  51. * If `g` is a curried ternary function and `_` is `R.__`, the following are
  52. * equivalent:
  53. *
  54. * - `g(1, 2, 3)`
  55. * - `g(_, 2, 3)(1)`
  56. * - `g(_, _, 3)(1)(2)`
  57. * - `g(_, _, 3)(1, 2)`
  58. * - `g(_, 2, _)(1, 3)`
  59. * - `g(_, 2)(1)(3)`
  60. * - `g(_, 2)(1, 3)`
  61. * - `g(_, 2)(_, 3)(1)`
  62. *
  63. * @name __
  64. * @constant
  65. * @memberOf R
  66. * @since v0.6.0
  67. * @category Function
  68. * @example
  69. *
  70. * const greet = R.replace('{name}', R.__, 'Hello, {name}!');
  71. * greet('Alice'); //=> 'Hello, Alice!'
  72. */
  73. var __ = {
  74. '@@functional/placeholder': true
  75. };
  76. function _typeof(obj) {
  77. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  78. _typeof = function (obj) {
  79. return typeof obj;
  80. };
  81. } else {
  82. _typeof = function (obj) {
  83. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  84. };
  85. }
  86. return _typeof(obj);
  87. }
  88. function _isPlaceholder(a) {
  89. return a != null && _typeof(a) === 'object' && a['@@functional/placeholder'] === true;
  90. }
  91. /**
  92. * Optimized internal one-arity curry function.
  93. *
  94. * @private
  95. * @category Function
  96. * @param {Function} fn The function to curry.
  97. * @return {Function} The curried function.
  98. */
  99. function _curry1(fn) {
  100. return function f1(a) {
  101. if (arguments.length === 0 || _isPlaceholder(a)) {
  102. return f1;
  103. } else {
  104. return fn.apply(this, arguments);
  105. }
  106. };
  107. }
  108. /**
  109. * Optimized internal two-arity curry function.
  110. *
  111. * @private
  112. * @category Function
  113. * @param {Function} fn The function to curry.
  114. * @return {Function} The curried function.
  115. */
  116. function _curry2(fn) {
  117. return function f2(a, b) {
  118. switch (arguments.length) {
  119. case 0:
  120. return f2;
  121. case 1:
  122. return _isPlaceholder(a) ? f2 : _curry1(function (_b) {
  123. return fn(a, _b);
  124. });
  125. default:
  126. return _isPlaceholder(a) && _isPlaceholder(b) ? f2 : _isPlaceholder(a) ? _curry1(function (_a) {
  127. return fn(_a, b);
  128. }) : _isPlaceholder(b) ? _curry1(function (_b) {
  129. return fn(a, _b);
  130. }) : fn(a, b);
  131. }
  132. };
  133. }
  134. /**
  135. * Adds two values.
  136. *
  137. * @func
  138. * @memberOf R
  139. * @since v0.1.0
  140. * @category Math
  141. * @sig Number -> Number -> Number
  142. * @param {Number} a
  143. * @param {Number} b
  144. * @return {Number}
  145. * @see R.subtract
  146. * @example
  147. *
  148. * R.add(2, 3); //=> 5
  149. * R.add(7)(10); //=> 17
  150. */
  151. var add = _curry2(function add(a, b) {
  152. return Number(a) + Number(b);
  153. });
  154. /**
  155. * Private `concat` function to merge two array-like objects.
  156. *
  157. * @private
  158. * @param {Array|Arguments} [set1=[]] An array-like object.
  159. * @param {Array|Arguments} [set2=[]] An array-like object.
  160. * @return {Array} A new, merged array.
  161. * @example
  162. *
  163. * _concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
  164. */
  165. function _concat(set1, set2) {
  166. set1 = set1 || [];
  167. set2 = set2 || [];
  168. var idx;
  169. var len1 = set1.length;
  170. var len2 = set2.length;
  171. var result = [];
  172. idx = 0;
  173. while (idx < len1) {
  174. result[result.length] = set1[idx];
  175. idx += 1;
  176. }
  177. idx = 0;
  178. while (idx < len2) {
  179. result[result.length] = set2[idx];
  180. idx += 1;
  181. }
  182. return result;
  183. }
  184. function _arity(n, fn) {
  185. /* eslint-disable no-unused-vars */
  186. switch (n) {
  187. case 0:
  188. return function () {
  189. return fn.apply(this, arguments);
  190. };
  191. case 1:
  192. return function (a0) {
  193. return fn.apply(this, arguments);
  194. };
  195. case 2:
  196. return function (a0, a1) {
  197. return fn.apply(this, arguments);
  198. };
  199. case 3:
  200. return function (a0, a1, a2) {
  201. return fn.apply(this, arguments);
  202. };
  203. case 4:
  204. return function (a0, a1, a2, a3) {
  205. return fn.apply(this, arguments);
  206. };
  207. case 5:
  208. return function (a0, a1, a2, a3, a4) {
  209. return fn.apply(this, arguments);
  210. };
  211. case 6:
  212. return function (a0, a1, a2, a3, a4, a5) {
  213. return fn.apply(this, arguments);
  214. };
  215. case 7:
  216. return function (a0, a1, a2, a3, a4, a5, a6) {
  217. return fn.apply(this, arguments);
  218. };
  219. case 8:
  220. return function (a0, a1, a2, a3, a4, a5, a6, a7) {
  221. return fn.apply(this, arguments);
  222. };
  223. case 9:
  224. return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
  225. return fn.apply(this, arguments);
  226. };
  227. case 10:
  228. return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
  229. return fn.apply(this, arguments);
  230. };
  231. default:
  232. throw new Error('First argument to _arity must be a non-negative integer no greater than ten');
  233. }
  234. }
  235. /**
  236. * Internal curryN function.
  237. *
  238. * @private
  239. * @category Function
  240. * @param {Number} length The arity of the curried function.
  241. * @param {Array} received An array of arguments received thus far.
  242. * @param {Function} fn The function to curry.
  243. * @return {Function} The curried function.
  244. */
  245. function _curryN(length, received, fn) {
  246. return function () {
  247. var combined = [];
  248. var argsIdx = 0;
  249. var left = length;
  250. var combinedIdx = 0;
  251. while (combinedIdx < received.length || argsIdx < arguments.length) {
  252. var result;
  253. if (combinedIdx < received.length && (!_isPlaceholder(received[combinedIdx]) || argsIdx >= arguments.length)) {
  254. result = received[combinedIdx];
  255. } else {
  256. result = arguments[argsIdx];
  257. argsIdx += 1;
  258. }
  259. combined[combinedIdx] = result;
  260. if (!_isPlaceholder(result)) {
  261. left -= 1;
  262. }
  263. combinedIdx += 1;
  264. }
  265. return left <= 0 ? fn.apply(this, combined) : _arity(left, _curryN(length, combined, fn));
  266. };
  267. }
  268. /**
  269. * Returns a curried equivalent of the provided function, with the specified
  270. * arity. The curried function has two unusual capabilities. First, its
  271. * arguments needn't be provided one at a time. If `g` is `R.curryN(3, f)`, the
  272. * following are equivalent:
  273. *
  274. * - `g(1)(2)(3)`
  275. * - `g(1)(2, 3)`
  276. * - `g(1, 2)(3)`
  277. * - `g(1, 2, 3)`
  278. *
  279. * Secondly, the special placeholder value [`R.__`](#__) may be used to specify
  280. * "gaps", allowing partial application of any combination of arguments,
  281. * regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
  282. * the following are equivalent:
  283. *
  284. * - `g(1, 2, 3)`
  285. * - `g(_, 2, 3)(1)`
  286. * - `g(_, _, 3)(1)(2)`
  287. * - `g(_, _, 3)(1, 2)`
  288. * - `g(_, 2)(1)(3)`
  289. * - `g(_, 2)(1, 3)`
  290. * - `g(_, 2)(_, 3)(1)`
  291. *
  292. * @func
  293. * @memberOf R
  294. * @since v0.5.0
  295. * @category Function
  296. * @sig Number -> (* -> a) -> (* -> a)
  297. * @param {Number} length The arity for the returned function.
  298. * @param {Function} fn The function to curry.
  299. * @return {Function} A new, curried function.
  300. * @see R.curry
  301. * @example
  302. *
  303. * const sumArgs = (...args) => R.sum(args);
  304. *
  305. * const curriedAddFourNumbers = R.curryN(4, sumArgs);
  306. * const f = curriedAddFourNumbers(1, 2);
  307. * const g = f(3);
  308. * g(4); //=> 10
  309. */
  310. var curryN = _curry2(function curryN(length, fn) {
  311. if (length === 1) {
  312. return _curry1(fn);
  313. }
  314. return _arity(length, _curryN(length, [], fn));
  315. });
  316. /**
  317. * Creates a new list iteration function from an existing one by adding two new
  318. * parameters to its callback function: the current index, and the entire list.
  319. *
  320. * This would turn, for instance, [`R.map`](#map) function into one that
  321. * more closely resembles `Array.prototype.map`. Note that this will only work
  322. * for functions in which the iteration callback function is the first
  323. * parameter, and where the list is the last parameter. (This latter might be
  324. * unimportant if the list parameter is not used.)
  325. *
  326. * @func
  327. * @memberOf R
  328. * @since v0.15.0
  329. * @category Function
  330. * @category List
  331. * @sig (((a ...) -> b) ... -> [a] -> *) -> (((a ..., Int, [a]) -> b) ... -> [a] -> *)
  332. * @param {Function} fn A list iteration function that does not pass index or list to its callback
  333. * @return {Function} An altered list iteration function that passes (item, index, list) to its callback
  334. * @example
  335. *
  336. * const mapIndexed = R.addIndex(R.map);
  337. * mapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']);
  338. * //=> ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']
  339. */
  340. var addIndex = _curry1(function addIndex(fn) {
  341. return curryN(fn.length, function () {
  342. var idx = 0;
  343. var origFn = arguments[0];
  344. var list = arguments[arguments.length - 1];
  345. var args = Array.prototype.slice.call(arguments, 0);
  346. args[0] = function () {
  347. var result = origFn.apply(this, _concat(arguments, [idx, list]));
  348. idx += 1;
  349. return result;
  350. };
  351. return fn.apply(this, args);
  352. });
  353. });
  354. /**
  355. * As with `addIndex`, `addIndexRight` creates a new list iteration function
  356. * from an existing one by adding two new parameters to its callback function:
  357. * the current index, and the entire list.
  358. *
  359. * Unlike `addIndex`, `addIndexRight` iterates from the right to the left.
  360. *
  361. * @func
  362. * @memberOf R
  363. * @since v0.29.0
  364. * @category Function
  365. * @category List
  366. * @sig ((a ... -> b) ... -> [a] -> *) -> (a ..., Int, [a] -> b) ... -> [a] -> *)
  367. * @param {Function} fn A list iteration function that does not pass index or list to its callback
  368. * @return {Function} An altered list iteration function that passes (item, index, list) to its callback
  369. * @example
  370. *
  371. * const revmap = (fn, ary) => R.map(fn, R.reverse(ary));
  372. * const revmapIndexed = R.addIndexRight(revmap);
  373. * revmapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']);
  374. * //=> [ '5-r', '4-a', '3-b', '2-o', '1-o', '0-f' ]
  375. */
  376. var addIndexRight = _curry1(function addIndex(fn) {
  377. return curryN(fn.length, function () {
  378. var origFn = arguments[0];
  379. var list = arguments[arguments.length - 1];
  380. var idx = list.length - 1;
  381. var args = Array.prototype.slice.call(arguments, 0);
  382. args[0] = function () {
  383. var result = origFn.apply(this, _concat(arguments, [idx, list]));
  384. idx -= 1;
  385. return result;
  386. };
  387. return fn.apply(this, args);
  388. });
  389. });
  390. /**
  391. * Optimized internal three-arity curry function.
  392. *
  393. * @private
  394. * @category Function
  395. * @param {Function} fn The function to curry.
  396. * @return {Function} The curried function.
  397. */
  398. function _curry3(fn) {
  399. return function f3(a, b, c) {
  400. switch (arguments.length) {
  401. case 0:
  402. return f3;
  403. case 1:
  404. return _isPlaceholder(a) ? f3 : _curry2(function (_b, _c) {
  405. return fn(a, _b, _c);
  406. });
  407. case 2:
  408. return _isPlaceholder(a) && _isPlaceholder(b) ? f3 : _isPlaceholder(a) ? _curry2(function (_a, _c) {
  409. return fn(_a, b, _c);
  410. }) : _isPlaceholder(b) ? _curry2(function (_b, _c) {
  411. return fn(a, _b, _c);
  412. }) : _curry1(function (_c) {
  413. return fn(a, b, _c);
  414. });
  415. default:
  416. return _isPlaceholder(a) && _isPlaceholder(b) && _isPlaceholder(c) ? f3 : _isPlaceholder(a) && _isPlaceholder(b) ? _curry2(function (_a, _b) {
  417. return fn(_a, _b, c);
  418. }) : _isPlaceholder(a) && _isPlaceholder(c) ? _curry2(function (_a, _c) {
  419. return fn(_a, b, _c);
  420. }) : _isPlaceholder(b) && _isPlaceholder(c) ? _curry2(function (_b, _c) {
  421. return fn(a, _b, _c);
  422. }) : _isPlaceholder(a) ? _curry1(function (_a) {
  423. return fn(_a, b, c);
  424. }) : _isPlaceholder(b) ? _curry1(function (_b) {
  425. return fn(a, _b, c);
  426. }) : _isPlaceholder(c) ? _curry1(function (_c) {
  427. return fn(a, b, _c);
  428. }) : fn(a, b, c);
  429. }
  430. };
  431. }
  432. /**
  433. * Applies a function to the value at the given index of an array, returning a
  434. * new copy of the array with the element at the given index replaced with the
  435. * result of the function application.
  436. *
  437. * @func
  438. * @memberOf R
  439. * @since v0.14.0
  440. * @category List
  441. * @sig Number -> (a -> a) -> [a] -> [a]
  442. * @param {Number} idx The index.
  443. * @param {Function} fn The function to apply.
  444. * @param {Array|Arguments} list An array-like object whose value
  445. * at the supplied index will be replaced.
  446. * @return {Array} A copy of the supplied array-like object with
  447. * the element at index `idx` replaced with the value
  448. * returned by applying `fn` to the existing element.
  449. * @see R.update
  450. * @example
  451. *
  452. * R.adjust(1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'B', 'c', 'd']
  453. * R.adjust(-1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c', 'D']
  454. * @symb R.adjust(-1, f, [a, b]) = [a, f(b)]
  455. * @symb R.adjust(0, f, [a, b]) = [f(a), b]
  456. */
  457. var adjust = _curry3(function adjust(idx, fn, list) {
  458. var len = list.length;
  459. if (idx >= len || idx < -len) {
  460. return list;
  461. }
  462. var _idx = (len + idx) % len;
  463. var _list = _concat(list);
  464. _list[_idx] = fn(list[_idx]);
  465. return _list;
  466. });
  467. /**
  468. * Tests whether or not an object is an array.
  469. *
  470. * @private
  471. * @param {*} val The object to test.
  472. * @return {Boolean} `true` if `val` is an array, `false` otherwise.
  473. * @example
  474. *
  475. * _isArray([]); //=> true
  476. * _isArray(null); //=> false
  477. * _isArray({}); //=> false
  478. */
  479. var _isArray = Array.isArray || function _isArray(val) {
  480. return val != null && val.length >= 0 && Object.prototype.toString.call(val) === '[object Array]';
  481. };
  482. function _isTransformer(obj) {
  483. return obj != null && typeof obj['@@transducer/step'] === 'function';
  484. }
  485. /**
  486. * Returns a function that dispatches with different strategies based on the
  487. * object in list position (last argument). If it is an array, executes [fn].
  488. * Otherwise, if it has a function with one of the given method names, it will
  489. * execute that function (functor case). Otherwise, if it is a transformer,
  490. * uses transducer created by [transducerCreator] to return a new transformer
  491. * (transducer case).
  492. * Otherwise, it will default to executing [fn].
  493. *
  494. * @private
  495. * @param {Array} methodNames properties to check for a custom implementation
  496. * @param {Function} transducerCreator transducer factory if object is transformer
  497. * @param {Function} fn default ramda implementation
  498. * @return {Function} A function that dispatches on object in list position
  499. */
  500. function _dispatchable(methodNames, transducerCreator, fn) {
  501. return function () {
  502. if (arguments.length === 0) {
  503. return fn();
  504. }
  505. var obj = arguments[arguments.length - 1];
  506. if (!_isArray(obj)) {
  507. var idx = 0;
  508. while (idx < methodNames.length) {
  509. if (typeof obj[methodNames[idx]] === 'function') {
  510. return obj[methodNames[idx]].apply(obj, Array.prototype.slice.call(arguments, 0, -1));
  511. }
  512. idx += 1;
  513. }
  514. if (_isTransformer(obj)) {
  515. var transducer = transducerCreator.apply(null, Array.prototype.slice.call(arguments, 0, -1));
  516. return transducer(obj);
  517. }
  518. }
  519. return fn.apply(this, arguments);
  520. };
  521. }
  522. function _reduced(x) {
  523. return x && x['@@transducer/reduced'] ? x : {
  524. '@@transducer/value': x,
  525. '@@transducer/reduced': true
  526. };
  527. }
  528. var _xfBase = {
  529. init: function init() {
  530. return this.xf['@@transducer/init']();
  531. },
  532. result: function result(_result) {
  533. return this.xf['@@transducer/result'](_result);
  534. }
  535. };
  536. function XAll(f, xf) {
  537. this.xf = xf;
  538. this.f = f;
  539. this.all = true;
  540. }
  541. XAll.prototype['@@transducer/init'] = _xfBase.init;
  542. XAll.prototype['@@transducer/result'] = function (result) {
  543. if (this.all) {
  544. result = this.xf['@@transducer/step'](result, true);
  545. }
  546. return this.xf['@@transducer/result'](result);
  547. };
  548. XAll.prototype['@@transducer/step'] = function (result, input) {
  549. if (!this.f(input)) {
  550. this.all = false;
  551. result = _reduced(this.xf['@@transducer/step'](result, false));
  552. }
  553. return result;
  554. };
  555. function _xall(f) {
  556. return function (xf) {
  557. return new XAll(f, xf);
  558. };
  559. }
  560. /**
  561. * Returns `true` if all elements of the list match the predicate, `false` if
  562. * there are any that don't.
  563. *
  564. * Dispatches to the `all` method of the second argument, if present.
  565. *
  566. * Acts as a transducer if a transformer is given in list position.
  567. *
  568. * @func
  569. * @memberOf R
  570. * @since v0.1.0
  571. * @category List
  572. * @sig (a -> Boolean) -> [a] -> Boolean
  573. * @param {Function} fn The predicate function.
  574. * @param {Array} list The array to consider.
  575. * @return {Boolean} `true` if the predicate is satisfied by every element, `false`
  576. * otherwise.
  577. * @see R.any, R.none, R.transduce
  578. * @example
  579. *
  580. * const equals3 = R.equals(3);
  581. * R.all(equals3)([3, 3, 3, 3]); //=> true
  582. * R.all(equals3)([3, 3, 1, 3]); //=> false
  583. */
  584. var all = _curry2(_dispatchable(['all'], _xall, function all(fn, list) {
  585. var idx = 0;
  586. while (idx < list.length) {
  587. if (!fn(list[idx])) {
  588. return false;
  589. }
  590. idx += 1;
  591. }
  592. return true;
  593. }));
  594. function _arrayFromIterator(iter) {
  595. var list = [];
  596. var next;
  597. while (!(next = iter.next()).done) {
  598. list.push(next.value);
  599. }
  600. return list;
  601. }
  602. function _includesWith(pred, x, list) {
  603. var idx = 0;
  604. var len = list.length;
  605. while (idx < len) {
  606. if (pred(x, list[idx])) {
  607. return true;
  608. }
  609. idx += 1;
  610. }
  611. return false;
  612. }
  613. function _functionName(f) {
  614. // String(x => x) evaluates to "x => x", so the pattern may not match.
  615. var match = String(f).match(/^function (\w*)/);
  616. return match == null ? '' : match[1];
  617. }
  618. function _has(prop, obj) {
  619. return Object.prototype.hasOwnProperty.call(obj, prop);
  620. }
  621. // Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
  622. function _objectIs(a, b) {
  623. // SameValue algorithm
  624. if (a === b) {
  625. // Steps 1-5, 7-10
  626. // Steps 6.b-6.e: +0 != -0
  627. return a !== 0 || 1 / a === 1 / b;
  628. } else {
  629. // Step 6.a: NaN == NaN
  630. return a !== a && b !== b;
  631. }
  632. }
  633. var _objectIs$1 = typeof Object.is === 'function' ? Object.is : _objectIs;
  634. var toString = Object.prototype.toString;
  635. var _isArguments = function () {
  636. return toString.call(arguments) === '[object Arguments]' ? function _isArguments(x) {
  637. return toString.call(x) === '[object Arguments]';
  638. } : function _isArguments(x) {
  639. return _has('callee', x);
  640. };
  641. }();
  642. var hasEnumBug = !{
  643. toString: null
  644. }.propertyIsEnumerable('toString');
  645. var nonEnumerableProps = ['constructor', 'valueOf', 'isPrototypeOf', 'toString', 'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString']; // Safari bug
  646. var hasArgsEnumBug = function () {
  647. return arguments.propertyIsEnumerable('length');
  648. }();
  649. var contains = function contains(list, item) {
  650. var idx = 0;
  651. while (idx < list.length) {
  652. if (list[idx] === item) {
  653. return true;
  654. }
  655. idx += 1;
  656. }
  657. return false;
  658. };
  659. /**
  660. * Returns a list containing the names of all the enumerable own properties of
  661. * the supplied object.
  662. * Note that the order of the output array is not guaranteed to be consistent
  663. * across different JS platforms.
  664. *
  665. * @func
  666. * @memberOf R
  667. * @since v0.1.0
  668. * @category Object
  669. * @sig {k: v} -> [k]
  670. * @param {Object} obj The object to extract properties from
  671. * @return {Array} An array of the object's own properties.
  672. * @see R.keysIn, R.values, R.toPairs
  673. * @example
  674. *
  675. * R.keys({a: 1, b: 2, c: 3}); //=> ['a', 'b', 'c']
  676. */
  677. var keys = typeof Object.keys === 'function' && !hasArgsEnumBug ? _curry1(function keys(obj) {
  678. return Object(obj) !== obj ? [] : Object.keys(obj);
  679. }) : _curry1(function keys(obj) {
  680. if (Object(obj) !== obj) {
  681. return [];
  682. }
  683. var prop, nIdx;
  684. var ks = [];
  685. var checkArgsLength = hasArgsEnumBug && _isArguments(obj);
  686. for (prop in obj) {
  687. if (_has(prop, obj) && (!checkArgsLength || prop !== 'length')) {
  688. ks[ks.length] = prop;
  689. }
  690. }
  691. if (hasEnumBug) {
  692. nIdx = nonEnumerableProps.length - 1;
  693. while (nIdx >= 0) {
  694. prop = nonEnumerableProps[nIdx];
  695. if (_has(prop, obj) && !contains(ks, prop)) {
  696. ks[ks.length] = prop;
  697. }
  698. nIdx -= 1;
  699. }
  700. }
  701. return ks;
  702. });
  703. /**
  704. * Gives a single-word string description of the (native) type of a value,
  705. * returning such answers as 'Object', 'Number', 'Array', or 'Null'. Does not
  706. * attempt to distinguish user Object types any further, reporting them all as
  707. * 'Object'.
  708. *
  709. * @func
  710. * @memberOf R
  711. * @since v0.8.0
  712. * @category Type
  713. * @sig * -> String
  714. * @param {*} val The value to test
  715. * @return {String}
  716. * @example
  717. *
  718. * R.type({}); //=> "Object"
  719. * R.type(1); //=> "Number"
  720. * R.type(false); //=> "Boolean"
  721. * R.type('s'); //=> "String"
  722. * R.type(null); //=> "Null"
  723. * R.type([]); //=> "Array"
  724. * R.type(/[A-z]/); //=> "RegExp"
  725. * R.type(() => {}); //=> "Function"
  726. * R.type(undefined); //=> "Undefined"
  727. */
  728. var type = _curry1(function type(val) {
  729. return val === null ? 'Null' : val === undefined ? 'Undefined' : Object.prototype.toString.call(val).slice(8, -1);
  730. });
  731. /**
  732. * private _uniqContentEquals function.
  733. * That function is checking equality of 2 iterator contents with 2 assumptions
  734. * - iterators lengths are the same
  735. * - iterators values are unique
  736. *
  737. * false-positive result will be returned for comparison of, e.g.
  738. * - [1,2,3] and [1,2,3,4]
  739. * - [1,1,1] and [1,2,3]
  740. * */
  741. function _uniqContentEquals(aIterator, bIterator, stackA, stackB) {
  742. var a = _arrayFromIterator(aIterator);
  743. var b = _arrayFromIterator(bIterator);
  744. function eq(_a, _b) {
  745. return _equals(_a, _b, stackA.slice(), stackB.slice());
  746. } // if *a* array contains any element that is not included in *b*
  747. return !_includesWith(function (b, aItem) {
  748. return !_includesWith(eq, aItem, b);
  749. }, b, a);
  750. }
  751. function _equals(a, b, stackA, stackB) {
  752. if (_objectIs$1(a, b)) {
  753. return true;
  754. }
  755. var typeA = type(a);
  756. if (typeA !== type(b)) {
  757. return false;
  758. }
  759. if (typeof a['fantasy-land/equals'] === 'function' || typeof b['fantasy-land/equals'] === 'function') {
  760. return typeof a['fantasy-land/equals'] === 'function' && a['fantasy-land/equals'](b) && typeof b['fantasy-land/equals'] === 'function' && b['fantasy-land/equals'](a);
  761. }
  762. if (typeof a.equals === 'function' || typeof b.equals === 'function') {
  763. return typeof a.equals === 'function' && a.equals(b) && typeof b.equals === 'function' && b.equals(a);
  764. }
  765. switch (typeA) {
  766. case 'Arguments':
  767. case 'Array':
  768. case 'Object':
  769. if (typeof a.constructor === 'function' && _functionName(a.constructor) === 'Promise') {
  770. return a === b;
  771. }
  772. break;
  773. case 'Boolean':
  774. case 'Number':
  775. case 'String':
  776. if (!(_typeof(a) === _typeof(b) && _objectIs$1(a.valueOf(), b.valueOf()))) {
  777. return false;
  778. }
  779. break;
  780. case 'Date':
  781. if (!_objectIs$1(a.valueOf(), b.valueOf())) {
  782. return false;
  783. }
  784. break;
  785. case 'Error':
  786. return a.name === b.name && a.message === b.message;
  787. case 'RegExp':
  788. 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)) {
  789. return false;
  790. }
  791. break;
  792. }
  793. var idx = stackA.length - 1;
  794. while (idx >= 0) {
  795. if (stackA[idx] === a) {
  796. return stackB[idx] === b;
  797. }
  798. idx -= 1;
  799. }
  800. switch (typeA) {
  801. case 'Map':
  802. if (a.size !== b.size) {
  803. return false;
  804. }
  805. return _uniqContentEquals(a.entries(), b.entries(), stackA.concat([a]), stackB.concat([b]));
  806. case 'Set':
  807. if (a.size !== b.size) {
  808. return false;
  809. }
  810. return _uniqContentEquals(a.values(), b.values(), stackA.concat([a]), stackB.concat([b]));
  811. case 'Arguments':
  812. case 'Array':
  813. case 'Object':
  814. case 'Boolean':
  815. case 'Number':
  816. case 'String':
  817. case 'Date':
  818. case 'Error':
  819. case 'RegExp':
  820. case 'Int8Array':
  821. case 'Uint8Array':
  822. case 'Uint8ClampedArray':
  823. case 'Int16Array':
  824. case 'Uint16Array':
  825. case 'Int32Array':
  826. case 'Uint32Array':
  827. case 'Float32Array':
  828. case 'Float64Array':
  829. case 'ArrayBuffer':
  830. break;
  831. default:
  832. // Values of other types are only equal if identical.
  833. return false;
  834. }
  835. var keysA = keys(a);
  836. if (keysA.length !== keys(b).length) {
  837. return false;
  838. }
  839. var extendedStackA = stackA.concat([a]);
  840. var extendedStackB = stackB.concat([b]);
  841. idx = keysA.length - 1;
  842. while (idx >= 0) {
  843. var key = keysA[idx];
  844. if (!(_has(key, b) && _equals(b[key], a[key], extendedStackA, extendedStackB))) {
  845. return false;
  846. }
  847. idx -= 1;
  848. }
  849. return true;
  850. }
  851. /**
  852. * Returns `true` if its arguments are equivalent, `false` otherwise. Handles
  853. * cyclical data structures.
  854. *
  855. * Dispatches symmetrically to the `equals` methods of both arguments, if
  856. * present.
  857. *
  858. * @func
  859. * @memberOf R
  860. * @since v0.15.0
  861. * @category Relation
  862. * @sig a -> b -> Boolean
  863. * @param {*} a
  864. * @param {*} b
  865. * @return {Boolean}
  866. * @example
  867. *
  868. * R.equals(1, 1); //=> true
  869. * R.equals(1, '1'); //=> false
  870. * R.equals([1, 2, 3], [1, 2, 3]); //=> true
  871. *
  872. * const a = {}; a.v = a;
  873. * const b = {}; b.v = b;
  874. * R.equals(a, b); //=> true
  875. */
  876. var equals = _curry2(function equals(a, b) {
  877. return _equals(a, b, [], []);
  878. });
  879. function _indexOf(list, a, idx) {
  880. var inf, item; // Array.prototype.indexOf doesn't exist below IE9
  881. if (typeof list.indexOf === 'function') {
  882. switch (_typeof(a)) {
  883. case 'number':
  884. if (a === 0) {
  885. // manually crawl the list to distinguish between +0 and -0
  886. inf = 1 / a;
  887. while (idx < list.length) {
  888. item = list[idx];
  889. if (item === 0 && 1 / item === inf) {
  890. return idx;
  891. }
  892. idx += 1;
  893. }
  894. return -1;
  895. } else if (a !== a) {
  896. // NaN
  897. while (idx < list.length) {
  898. item = list[idx];
  899. if (typeof item === 'number' && item !== item) {
  900. return idx;
  901. }
  902. idx += 1;
  903. }
  904. return -1;
  905. } // non-zero numbers can utilise Set
  906. return list.indexOf(a, idx);
  907. // all these types can utilise Set
  908. case 'string':
  909. case 'boolean':
  910. case 'function':
  911. case 'undefined':
  912. return list.indexOf(a, idx);
  913. case 'object':
  914. if (a === null) {
  915. // null can utilise Set
  916. return list.indexOf(a, idx);
  917. }
  918. }
  919. } // anything else not covered above, defer to R.equals
  920. while (idx < list.length) {
  921. if (equals(list[idx], a)) {
  922. return idx;
  923. }
  924. idx += 1;
  925. }
  926. return -1;
  927. }
  928. function _includes(a, list) {
  929. return _indexOf(list, a, 0) >= 0;
  930. }
  931. function _map(fn, functor) {
  932. var idx = 0;
  933. var len = functor.length;
  934. var result = Array(len);
  935. while (idx < len) {
  936. result[idx] = fn(functor[idx]);
  937. idx += 1;
  938. }
  939. return result;
  940. }
  941. function _quote(s) {
  942. var escaped = s.replace(/\\/g, '\\\\').replace(/[\b]/g, '\\b') // \b matches word boundary; [\b] matches backspace
  943. .replace(/\f/g, '\\f').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t').replace(/\v/g, '\\v').replace(/\0/g, '\\0');
  944. return '"' + escaped.replace(/"/g, '\\"') + '"';
  945. }
  946. /**
  947. * Polyfill from <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString>.
  948. */
  949. var pad = function pad(n) {
  950. return (n < 10 ? '0' : '') + n;
  951. };
  952. var _toISOString = typeof Date.prototype.toISOString === 'function' ? function _toISOString(d) {
  953. return d.toISOString();
  954. } : function _toISOString(d) {
  955. 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';
  956. };
  957. function _complement(f) {
  958. return function () {
  959. return !f.apply(this, arguments);
  960. };
  961. }
  962. function _arrayReduce(reducer, acc, list) {
  963. var index = 0;
  964. var length = list.length;
  965. while (index < length) {
  966. acc = reducer(acc, list[index]);
  967. index += 1;
  968. }
  969. return acc;
  970. }
  971. function _filter(fn, list) {
  972. var idx = 0;
  973. var len = list.length;
  974. var result = [];
  975. while (idx < len) {
  976. if (fn(list[idx])) {
  977. result[result.length] = list[idx];
  978. }
  979. idx += 1;
  980. }
  981. return result;
  982. }
  983. function _isObject(x) {
  984. return Object.prototype.toString.call(x) === '[object Object]';
  985. }
  986. function XFilter(f, xf) {
  987. this.xf = xf;
  988. this.f = f;
  989. }
  990. XFilter.prototype['@@transducer/init'] = _xfBase.init;
  991. XFilter.prototype['@@transducer/result'] = _xfBase.result;
  992. XFilter.prototype['@@transducer/step'] = function (result, input) {
  993. return this.f(input) ? this.xf['@@transducer/step'](result, input) : result;
  994. };
  995. function _xfilter(f) {
  996. return function (xf) {
  997. return new XFilter(f, xf);
  998. };
  999. }
  1000. /**
  1001. * Takes a predicate and a `Filterable`, and returns a new filterable of the
  1002. * same type containing the members of the given filterable which satisfy the
  1003. * given predicate. Filterable objects include plain objects or any object
  1004. * that has a filter method such as `Array`.
  1005. *
  1006. * Dispatches to the `filter` method of the second argument, if present.
  1007. *
  1008. * Acts as a transducer if a transformer is given in list position.
  1009. *
  1010. * @func
  1011. * @memberOf R
  1012. * @since v0.1.0
  1013. * @category List
  1014. * @category Object
  1015. * @sig Filterable f => (a -> Boolean) -> f a -> f a
  1016. * @param {Function} pred
  1017. * @param {Array} filterable
  1018. * @return {Array} Filterable
  1019. * @see R.reject, R.transduce, R.addIndex
  1020. * @example
  1021. *
  1022. * const isEven = n => n % 2 === 0;
  1023. *
  1024. * R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
  1025. *
  1026. * R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
  1027. */
  1028. var filter = _curry2(_dispatchable(['fantasy-land/filter', 'filter'], _xfilter, function (pred, filterable) {
  1029. return _isObject(filterable) ? _arrayReduce(function (acc, key) {
  1030. if (pred(filterable[key])) {
  1031. acc[key] = filterable[key];
  1032. }
  1033. return acc;
  1034. }, {}, keys(filterable)) : // else
  1035. _filter(pred, filterable);
  1036. }));
  1037. /**
  1038. * The complement of [`filter`](#filter).
  1039. *
  1040. * Acts as a transducer if a transformer is given in list position. Filterable
  1041. * objects include plain objects or any object that has a filter method such
  1042. * as `Array`.
  1043. *
  1044. * @func
  1045. * @memberOf R
  1046. * @since v0.1.0
  1047. * @category List
  1048. * @sig Filterable f => (a -> Boolean) -> f a -> f a
  1049. * @param {Function} pred
  1050. * @param {Array} filterable
  1051. * @return {Array}
  1052. * @see R.filter, R.transduce, R.addIndex
  1053. * @example
  1054. *
  1055. * const isOdd = (n) => n % 2 !== 0;
  1056. *
  1057. * R.reject(isOdd, [1, 2, 3, 4]); //=> [2, 4]
  1058. *
  1059. * R.reject(isOdd, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
  1060. */
  1061. var reject = _curry2(function reject(pred, filterable) {
  1062. return filter(_complement(pred), filterable);
  1063. });
  1064. function _toString(x, seen) {
  1065. var recur = function recur(y) {
  1066. var xs = seen.concat([x]);
  1067. return _includes(y, xs) ? '<Circular>' : _toString(y, xs);
  1068. }; // mapPairs :: (Object, [String]) -> [String]
  1069. var mapPairs = function mapPairs(obj, keys) {
  1070. return _map(function (k) {
  1071. return _quote(k) + ': ' + recur(obj[k]);
  1072. }, keys.slice().sort());
  1073. };
  1074. switch (Object.prototype.toString.call(x)) {
  1075. case '[object Arguments]':
  1076. return '(function() { return arguments; }(' + _map(recur, x).join(', ') + '))';
  1077. case '[object Array]':
  1078. return '[' + _map(recur, x).concat(mapPairs(x, reject(function (k) {
  1079. return /^\d+$/.test(k);
  1080. }, keys(x)))).join(', ') + ']';
  1081. case '[object Boolean]':
  1082. return _typeof(x) === 'object' ? 'new Boolean(' + recur(x.valueOf()) + ')' : x.toString();
  1083. case '[object Date]':
  1084. return 'new Date(' + (isNaN(x.valueOf()) ? recur(NaN) : _quote(_toISOString(x))) + ')';
  1085. case '[object Map]':
  1086. return 'new Map(' + recur(Array.from(x)) + ')';
  1087. case '[object Null]':
  1088. return 'null';
  1089. case '[object Number]':
  1090. return _typeof(x) === 'object' ? 'new Number(' + recur(x.valueOf()) + ')' : 1 / x === -Infinity ? '-0' : x.toString(10);
  1091. case '[object Set]':
  1092. return 'new Set(' + recur(Array.from(x).sort()) + ')';
  1093. case '[object String]':
  1094. return _typeof(x) === 'object' ? 'new String(' + recur(x.valueOf()) + ')' : _quote(x);
  1095. case '[object Undefined]':
  1096. return 'undefined';
  1097. default:
  1098. if (typeof x.toString === 'function') {
  1099. var repr = x.toString();
  1100. if (repr !== '[object Object]') {
  1101. return repr;
  1102. }
  1103. }
  1104. return '{' + mapPairs(x, keys(x)).join(', ') + '}';
  1105. }
  1106. }
  1107. /**
  1108. * Returns the string representation of the given value. `eval`'ing the output
  1109. * should result in a value equivalent to the input value. Many of the built-in
  1110. * `toString` methods do not satisfy this requirement.
  1111. *
  1112. * If the given value is an `[object Object]` with a `toString` method other
  1113. * than `Object.prototype.toString`, this method is invoked with no arguments
  1114. * to produce the return value. This means user-defined constructor functions
  1115. * can provide a suitable `toString` method. For example:
  1116. *
  1117. * function Point(x, y) {
  1118. * this.x = x;
  1119. * this.y = y;
  1120. * }
  1121. *
  1122. * Point.prototype.toString = function() {
  1123. * return 'new Point(' + this.x + ', ' + this.y + ')';
  1124. * };
  1125. *
  1126. * R.toString(new Point(1, 2)); //=> 'new Point(1, 2)'
  1127. *
  1128. * @func
  1129. * @memberOf R
  1130. * @since v0.14.0
  1131. * @category String
  1132. * @sig * -> String
  1133. * @param {*} val
  1134. * @return {String}
  1135. * @example
  1136. *
  1137. * R.toString(42); //=> '42'
  1138. * R.toString('abc'); //=> '"abc"'
  1139. * R.toString([1, 2, 3]); //=> '[1, 2, 3]'
  1140. * R.toString({foo: 1, bar: 2, baz: 3}); //=> '{"bar": 2, "baz": 3, "foo": 1}'
  1141. * R.toString(new Date('2001-02-03T04:05:06Z')); //=> 'new Date("2001-02-03T04:05:06.000Z")'
  1142. */
  1143. var toString$1 = _curry1(function toString(val) {
  1144. return _toString(val, []);
  1145. });
  1146. /**
  1147. * Returns the larger of its two arguments.
  1148. *
  1149. * @func
  1150. * @memberOf R
  1151. * @since v0.1.0
  1152. * @category Relation
  1153. * @sig Ord a => a -> a -> a
  1154. * @param {*} a
  1155. * @param {*} b
  1156. * @return {*}
  1157. * @see R.maxBy, R.min
  1158. * @example
  1159. *
  1160. * R.max(789, 123); //=> 789
  1161. * R.max('a', 'b'); //=> 'b'
  1162. */
  1163. var max = _curry2(function max(a, b) {
  1164. if (a === b) {
  1165. return b;
  1166. }
  1167. function safeMax(x, y) {
  1168. if (x > y !== y > x) {
  1169. return y > x ? y : x;
  1170. }
  1171. return undefined;
  1172. }
  1173. var maxByValue = safeMax(a, b);
  1174. if (maxByValue !== undefined) {
  1175. return maxByValue;
  1176. }
  1177. var maxByType = safeMax(_typeof(a), _typeof(b));
  1178. if (maxByType !== undefined) {
  1179. return maxByType === _typeof(a) ? a : b;
  1180. }
  1181. var stringA = toString$1(a);
  1182. var maxByStringValue = safeMax(stringA, toString$1(b));
  1183. if (maxByStringValue !== undefined) {
  1184. return maxByStringValue === stringA ? a : b;
  1185. }
  1186. return b;
  1187. });
  1188. function XMap(f, xf) {
  1189. this.xf = xf;
  1190. this.f = f;
  1191. }
  1192. XMap.prototype['@@transducer/init'] = _xfBase.init;
  1193. XMap.prototype['@@transducer/result'] = _xfBase.result;
  1194. XMap.prototype['@@transducer/step'] = function (result, input) {
  1195. return this.xf['@@transducer/step'](result, this.f(input));
  1196. };
  1197. var _xmap = function _xmap(f) {
  1198. return function (xf) {
  1199. return new XMap(f, xf);
  1200. };
  1201. };
  1202. /**
  1203. * Takes a function and
  1204. * a [functor](https://github.com/fantasyland/fantasy-land#functor),
  1205. * applies the function to each of the functor's values, and returns
  1206. * a functor of the same shape.
  1207. *
  1208. * Ramda provides suitable `map` implementations for `Array` and `Object`,
  1209. * so this function may be applied to `[1, 2, 3]` or `{x: 1, y: 2, z: 3}`.
  1210. *
  1211. * Dispatches to the `map` method of the second argument, if present.
  1212. *
  1213. * Acts as a transducer if a transformer is given in list position.
  1214. *
  1215. * Also treats functions as functors and will compose them together.
  1216. *
  1217. * @func
  1218. * @memberOf R
  1219. * @since v0.1.0
  1220. * @category List
  1221. * @sig Functor f => (a -> b) -> f a -> f b
  1222. * @param {Function} fn The function to be called on every element of the input `list`.
  1223. * @param {Array} list The list to be iterated over.
  1224. * @return {Array} The new list.
  1225. * @see R.transduce, R.addIndex, R.pluck, R.project
  1226. * @example
  1227. *
  1228. * const double = x => x * 2;
  1229. *
  1230. * R.map(double, [1, 2, 3]); //=> [2, 4, 6]
  1231. *
  1232. * R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
  1233. * @symb R.map(f, [a, b]) = [f(a), f(b)]
  1234. * @symb R.map(f, { x: a, y: b }) = { x: f(a), y: f(b) }
  1235. * @symb R.map(f, functor_o) = functor_o.map(f)
  1236. */
  1237. var map = _curry2(_dispatchable(['fantasy-land/map', 'map'], _xmap, function map(fn, functor) {
  1238. switch (Object.prototype.toString.call(functor)) {
  1239. case '[object Function]':
  1240. return curryN(functor.length, function () {
  1241. return fn.call(this, functor.apply(this, arguments));
  1242. });
  1243. case '[object Object]':
  1244. return _arrayReduce(function (acc, key) {
  1245. acc[key] = fn(functor[key]);
  1246. return acc;
  1247. }, {}, keys(functor));
  1248. default:
  1249. return _map(fn, functor);
  1250. }
  1251. }));
  1252. /**
  1253. * Determine if the passed argument is an integer.
  1254. *
  1255. * @private
  1256. * @param {*} n
  1257. * @category Type
  1258. * @return {Boolean}
  1259. */
  1260. var _isInteger = Number.isInteger || function _isInteger(n) {
  1261. return n << 0 === n;
  1262. };
  1263. function _isString(x) {
  1264. return Object.prototype.toString.call(x) === '[object String]';
  1265. }
  1266. /**
  1267. * Returns the nth element of the given list or string. If n is negative the
  1268. * element at index length + n is returned.
  1269. *
  1270. * @func
  1271. * @memberOf R
  1272. * @since v0.1.0
  1273. * @category List
  1274. * @sig Number -> [a] -> a | Undefined
  1275. * @sig Number -> String -> String
  1276. * @param {Number} offset
  1277. * @param {*} list
  1278. * @return {*}
  1279. * @example
  1280. *
  1281. * const list = ['foo', 'bar', 'baz', 'quux'];
  1282. * R.nth(1, list); //=> 'bar'
  1283. * R.nth(-1, list); //=> 'quux'
  1284. * R.nth(-99, list); //=> undefined
  1285. *
  1286. * R.nth(2, 'abc'); //=> 'c'
  1287. * R.nth(3, 'abc'); //=> ''
  1288. * @symb R.nth(-1, [a, b, c]) = c
  1289. * @symb R.nth(0, [a, b, c]) = a
  1290. * @symb R.nth(1, [a, b, c]) = b
  1291. */
  1292. var nth = _curry2(function nth(offset, list) {
  1293. var idx = offset < 0 ? list.length + offset : offset;
  1294. return _isString(list) ? list.charAt(idx) : list[idx];
  1295. });
  1296. /**
  1297. * Returns a function that when supplied an object returns the indicated
  1298. * property of that object, if it exists.
  1299. *
  1300. * @func
  1301. * @memberOf R
  1302. * @since v0.1.0
  1303. * @category Object
  1304. * @typedefn Idx = String | Int | Symbol
  1305. * @sig Idx -> {s: a} -> a | Undefined
  1306. * @param {String|Number} p The property name or array index
  1307. * @param {Object} obj The object to query
  1308. * @return {*} The value at `obj.p`.
  1309. * @see R.path, R.props, R.pluck, R.project, R.nth
  1310. * @example
  1311. *
  1312. * R.prop('x', {x: 100}); //=> 100
  1313. * R.prop('x', {}); //=> undefined
  1314. * R.prop(0, [100]); //=> 100
  1315. * R.compose(R.inc, R.prop('x'))({ x: 3 }) //=> 4
  1316. */
  1317. var prop = _curry2(function prop(p, obj) {
  1318. if (obj == null) {
  1319. return;
  1320. }
  1321. return _isInteger(p) ? nth(p, obj) : obj[p];
  1322. });
  1323. /**
  1324. * Returns a new list by plucking the same named property off all objects in
  1325. * the list supplied.
  1326. *
  1327. * `pluck` will work on
  1328. * any [functor](https://github.com/fantasyland/fantasy-land#functor) in
  1329. * addition to arrays, as it is equivalent to `R.map(R.prop(k), f)`.
  1330. *
  1331. * @func
  1332. * @memberOf R
  1333. * @since v0.1.0
  1334. * @category List
  1335. * @sig Functor f => k -> f {k: v} -> f v
  1336. * @param {Number|String} key The key name to pluck off of each object.
  1337. * @param {Array} f The array or functor to consider.
  1338. * @return {Array} The list of values for the given key.
  1339. * @see R.project, R.prop, R.props
  1340. * @example
  1341. *
  1342. * var getAges = R.pluck('age');
  1343. * getAges([{name: 'fred', age: 29}, {name: 'wilma', age: 27}]); //=> [29, 27]
  1344. *
  1345. * R.pluck(0, [[1, 2], [3, 4]]); //=> [1, 3]
  1346. * R.pluck('val', {a: {val: 3}, b: {val: 5}}); //=> {a: 3, b: 5}
  1347. * @symb R.pluck('x', [{x: 1, y: 2}, {x: 3, y: 4}, {x: 5, y: 6}]) = [1, 3, 5]
  1348. * @symb R.pluck(0, [[1, 2], [3, 4], [5, 6]]) = [1, 3, 5]
  1349. */
  1350. var pluck = _curry2(function pluck(p, list) {
  1351. return map(prop(p), list);
  1352. });
  1353. /**
  1354. * Tests whether or not an object is similar to an array.
  1355. *
  1356. * @private
  1357. * @category Type
  1358. * @category List
  1359. * @sig * -> Boolean
  1360. * @param {*} x The object to test.
  1361. * @return {Boolean} `true` if `x` has a numeric length property and extreme indices defined; `false` otherwise.
  1362. * @example
  1363. *
  1364. * _isArrayLike([]); //=> true
  1365. * _isArrayLike(true); //=> false
  1366. * _isArrayLike({}); //=> false
  1367. * _isArrayLike({length: 10}); //=> false
  1368. * _isArrayLike({0: 'zero', 9: 'nine', length: 10}); //=> true
  1369. * _isArrayLike({nodeType: 1, length: 1}) // => false
  1370. */
  1371. var _isArrayLike = _curry1(function isArrayLike(x) {
  1372. if (_isArray(x)) {
  1373. return true;
  1374. }
  1375. if (!x) {
  1376. return false;
  1377. }
  1378. if (_typeof(x) !== 'object') {
  1379. return false;
  1380. }
  1381. if (_isString(x)) {
  1382. return false;
  1383. }
  1384. if (x.length === 0) {
  1385. return true;
  1386. }
  1387. if (x.length > 0) {
  1388. return x.hasOwnProperty(0) && x.hasOwnProperty(x.length - 1);
  1389. }
  1390. return false;
  1391. });
  1392. var symIterator = typeof Symbol !== 'undefined' ? Symbol.iterator : '@@iterator';
  1393. function _createReduce(arrayReduce, methodReduce, iterableReduce) {
  1394. return function _reduce(xf, acc, list) {
  1395. if (_isArrayLike(list)) {
  1396. return arrayReduce(xf, acc, list);
  1397. }
  1398. if (list == null) {
  1399. return acc;
  1400. }
  1401. if (typeof list['fantasy-land/reduce'] === 'function') {
  1402. return methodReduce(xf, acc, list, 'fantasy-land/reduce');
  1403. }
  1404. if (list[symIterator] != null) {
  1405. return iterableReduce(xf, acc, list[symIterator]());
  1406. }
  1407. if (typeof list.next === 'function') {
  1408. return iterableReduce(xf, acc, list);
  1409. }
  1410. if (typeof list.reduce === 'function') {
  1411. return methodReduce(xf, acc, list, 'reduce');
  1412. }
  1413. throw new TypeError('reduce: list must be array or iterable');
  1414. };
  1415. }
  1416. function _xArrayReduce(xf, acc, list) {
  1417. var idx = 0;
  1418. var len = list.length;
  1419. while (idx < len) {
  1420. acc = xf['@@transducer/step'](acc, list[idx]);
  1421. if (acc && acc['@@transducer/reduced']) {
  1422. acc = acc['@@transducer/value'];
  1423. break;
  1424. }
  1425. idx += 1;
  1426. }
  1427. return xf['@@transducer/result'](acc);
  1428. }
  1429. /**
  1430. * Creates a function that is bound to a context.
  1431. * Note: `R.bind` does not provide the additional argument-binding capabilities of
  1432. * [Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).
  1433. *
  1434. * @func
  1435. * @memberOf R
  1436. * @since v0.6.0
  1437. * @category Function
  1438. * @category Object
  1439. * @sig (* -> *) -> {*} -> (* -> *)
  1440. * @param {Function} fn The function to bind to context
  1441. * @param {Object} thisObj The context to bind `fn` to
  1442. * @return {Function} A function that will execute in the context of `thisObj`.
  1443. * @see R.partial
  1444. * @example
  1445. *
  1446. * const log = R.bind(console.log, console);
  1447. * R.pipe(R.assoc('a', 2), R.tap(log), R.assoc('a', 3))({a: 1}); //=> {a: 3}
  1448. * // logs {a: 2}
  1449. * @symb R.bind(f, o)(a, b) = f.call(o, a, b)
  1450. */
  1451. var bind = _curry2(function bind(fn, thisObj) {
  1452. return _arity(fn.length, function () {
  1453. return fn.apply(thisObj, arguments);
  1454. });
  1455. });
  1456. function _xIterableReduce(xf, acc, iter) {
  1457. var step = iter.next();
  1458. while (!step.done) {
  1459. acc = xf['@@transducer/step'](acc, step.value);
  1460. if (acc && acc['@@transducer/reduced']) {
  1461. acc = acc['@@transducer/value'];
  1462. break;
  1463. }
  1464. step = iter.next();
  1465. }
  1466. return xf['@@transducer/result'](acc);
  1467. }
  1468. function _xMethodReduce(xf, acc, obj, methodName) {
  1469. return xf['@@transducer/result'](obj[methodName](bind(xf['@@transducer/step'], xf), acc));
  1470. }
  1471. var _xReduce = _createReduce(_xArrayReduce, _xMethodReduce, _xIterableReduce);
  1472. function XWrap(fn) {
  1473. this.f = fn;
  1474. }
  1475. XWrap.prototype['@@transducer/init'] = function () {
  1476. throw new Error('init not implemented on XWrap');
  1477. };
  1478. XWrap.prototype['@@transducer/result'] = function (acc) {
  1479. return acc;
  1480. };
  1481. XWrap.prototype['@@transducer/step'] = function (acc, x) {
  1482. return this.f(acc, x);
  1483. };
  1484. function _xwrap(fn) {
  1485. return new XWrap(fn);
  1486. }
  1487. /**
  1488. * Returns a single item by iterating through the list, successively calling
  1489. * the iterator function and passing it an accumulator value and the current
  1490. * value from the array, and then passing the result to the next call.
  1491. *
  1492. * The iterator function receives two values: *(acc, value)*. It may use
  1493. * [`R.reduced`](#reduced) to shortcut the iteration.
  1494. *
  1495. * The arguments' order of [`reduceRight`](#reduceRight)'s iterator function
  1496. * is *(value, acc)*.
  1497. *
  1498. * Note: `R.reduce` does not skip deleted or unassigned indices (sparse
  1499. * arrays), unlike the native `Array.prototype.reduce` method. For more details
  1500. * on this behavior, see:
  1501. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Description
  1502. *
  1503. * Be cautious of mutating and returning the accumulator. If you reuse it across
  1504. * invocations, it will continue to accumulate onto the same value. The general
  1505. * recommendation is to always return a new value. If you can't do so for
  1506. * performance reasons, then be sure to reinitialize the accumulator on each
  1507. * invocation.
  1508. *
  1509. * Dispatches to the `reduce` method of the third argument, if present. When
  1510. * doing so, it is up to the user to handle the [`R.reduced`](#reduced)
  1511. * shortcuting, as this is not implemented by `reduce`.
  1512. *
  1513. * @func
  1514. * @memberOf R
  1515. * @since v0.1.0
  1516. * @category List
  1517. * @sig ((a, b) -> a) -> a -> [b] -> a
  1518. * @param {Function} fn The iterator function. Receives two values, the accumulator and the
  1519. * current element from the array.
  1520. * @param {*} acc The accumulator value.
  1521. * @param {Array} list The list to iterate over.
  1522. * @return {*} The final, accumulated value.
  1523. * @see R.reduced, R.addIndex, R.reduceRight
  1524. * @example
  1525. *
  1526. * R.reduce(R.subtract, 0, [1, 2, 3, 4]) // => ((((0 - 1) - 2) - 3) - 4) = -10
  1527. * // - -10
  1528. * // / \ / \
  1529. * // - 4 -6 4
  1530. * // / \ / \
  1531. * // - 3 ==> -3 3
  1532. * // / \ / \
  1533. * // - 2 -1 2
  1534. * // / \ / \
  1535. * // 0 1 0 1
  1536. *
  1537. * @symb R.reduce(f, a, [b, c, d]) = f(f(f(a, b), c), d)
  1538. */
  1539. var reduce = _curry3(function (xf, acc, list) {
  1540. return _xReduce(typeof xf === 'function' ? _xwrap(xf) : xf, acc, list);
  1541. });
  1542. /**
  1543. * Takes a list of predicates and returns a predicate that returns true for a
  1544. * given list of arguments if every one of the provided predicates is satisfied
  1545. * by those arguments.
  1546. *
  1547. * The function returned is a curried function whose arity matches that of the
  1548. * highest-arity predicate.
  1549. *
  1550. * @func
  1551. * @memberOf R
  1552. * @since v0.9.0
  1553. * @category Logic
  1554. * @sig [(*... -> Boolean)] -> (*... -> Boolean)
  1555. * @param {Array} predicates An array of predicates to check
  1556. * @return {Function} The combined predicate
  1557. * @see R.anyPass, R.both
  1558. * @example
  1559. *
  1560. * const isQueen = R.propEq('rank', 'Q');
  1561. * const isSpade = R.propEq('suit', '♠︎');
  1562. * const isQueenOfSpades = R.allPass([isQueen, isSpade]);
  1563. *
  1564. * isQueenOfSpades({rank: 'Q', suit: '♣︎'}); //=> false
  1565. * isQueenOfSpades({rank: 'Q', suit: '♠︎'}); //=> true
  1566. */
  1567. var allPass = _curry1(function allPass(preds) {
  1568. return curryN(reduce(max, 0, pluck('length', preds)), function () {
  1569. var idx = 0;
  1570. var len = preds.length;
  1571. while (idx < len) {
  1572. if (!preds[idx].apply(this, arguments)) {
  1573. return false;
  1574. }
  1575. idx += 1;
  1576. }
  1577. return true;
  1578. });
  1579. });
  1580. /**
  1581. * Returns a function that always returns the given value. Note that for
  1582. * non-primitives the value returned is a reference to the original value.
  1583. *
  1584. * This function is known as `const`, `constant`, or `K` (for K combinator) in
  1585. * other languages and libraries.
  1586. *
  1587. * @func
  1588. * @memberOf R
  1589. * @since v0.1.0
  1590. * @category Function
  1591. * @sig a -> (* -> a)
  1592. * @param {*} val The value to wrap in a function
  1593. * @return {Function} A Function :: * -> val.
  1594. * @example
  1595. *
  1596. * const t = R.always('Tee');
  1597. * t(); //=> 'Tee'
  1598. */
  1599. var always = _curry1(function always(val) {
  1600. return function () {
  1601. return val;
  1602. };
  1603. });
  1604. /**
  1605. * Returns the first argument if it is falsy, otherwise the second argument.
  1606. * Acts as the boolean `and` statement if both inputs are `Boolean`s.
  1607. *
  1608. * @func
  1609. * @memberOf R
  1610. * @since v0.1.0
  1611. * @category Logic
  1612. * @sig a -> b -> a | b
  1613. * @param {Any} a
  1614. * @param {Any} b
  1615. * @return {Any}
  1616. * @see R.both, R.or
  1617. * @example
  1618. *
  1619. * R.and(true, true); //=> true
  1620. * R.and(true, false); //=> false
  1621. * R.and(false, true); //=> false
  1622. * R.and(false, false); //=> false
  1623. */
  1624. var and = _curry2(function and(a, b) {
  1625. return a && b;
  1626. });
  1627. function XAny(f, xf) {
  1628. this.xf = xf;
  1629. this.f = f;
  1630. this.any = false;
  1631. }
  1632. XAny.prototype['@@transducer/init'] = _xfBase.init;
  1633. XAny.prototype['@@transducer/result'] = function (result) {
  1634. if (!this.any) {
  1635. result = this.xf['@@transducer/step'](result, false);
  1636. }
  1637. return this.xf['@@transducer/result'](result);
  1638. };
  1639. XAny.prototype['@@transducer/step'] = function (result, input) {
  1640. if (this.f(input)) {
  1641. this.any = true;
  1642. result = _reduced(this.xf['@@transducer/step'](result, true));
  1643. }
  1644. return result;
  1645. };
  1646. function _xany(f) {
  1647. return function (xf) {
  1648. return new XAny(f, xf);
  1649. };
  1650. }
  1651. /**
  1652. * Returns `true` if at least one of the elements of the list match the predicate,
  1653. * `false` otherwise.
  1654. *
  1655. * Dispatches to the `any` method of the second argument, if present.
  1656. *
  1657. * Acts as a transducer if a transformer is given in list position.
  1658. *
  1659. * @func
  1660. * @memberOf R
  1661. * @since v0.1.0
  1662. * @category List
  1663. * @sig (a -> Boolean) -> [a] -> Boolean
  1664. * @param {Function} fn The predicate function.
  1665. * @param {Array} list The array to consider.
  1666. * @return {Boolean} `true` if the predicate is satisfied by at least one element, `false`
  1667. * otherwise.
  1668. * @see R.all, R.none, R.transduce
  1669. * @example
  1670. *
  1671. * const lessThan0 = R.flip(R.lt)(0);
  1672. * const lessThan2 = R.flip(R.lt)(2);
  1673. * R.any(lessThan0)([1, 2]); //=> false
  1674. * R.any(lessThan2)([1, 2]); //=> true
  1675. */
  1676. var any = _curry2(_dispatchable(['any'], _xany, function any(fn, list) {
  1677. var idx = 0;
  1678. while (idx < list.length) {
  1679. if (fn(list[idx])) {
  1680. return true;
  1681. }
  1682. idx += 1;
  1683. }
  1684. return false;
  1685. }));
  1686. /**
  1687. * Takes a list of predicates and returns a predicate that returns true for a
  1688. * given list of arguments if at least one of the provided predicates is
  1689. * satisfied by those arguments.
  1690. *
  1691. * The function returned is a curried function whose arity matches that of the
  1692. * highest-arity predicate.
  1693. *
  1694. * @func
  1695. * @memberOf R
  1696. * @since v0.9.0
  1697. * @category Logic
  1698. * @sig [(*... -> Boolean)] -> (*... -> Boolean)
  1699. * @param {Array} predicates An array of predicates to check
  1700. * @return {Function} The combined predicate
  1701. * @see R.allPass, R.either
  1702. * @example
  1703. *
  1704. * const isClub = R.propEq('suit', '♣');
  1705. * const isSpade = R.propEq('suit', '♠');
  1706. * const isBlackCard = R.anyPass([isClub, isSpade]);
  1707. *
  1708. * isBlackCard({rank: '10', suit: '♣'}); //=> true
  1709. * isBlackCard({rank: 'Q', suit: '♠'}); //=> true
  1710. * isBlackCard({rank: 'Q', suit: '♦'}); //=> false
  1711. */
  1712. var anyPass = _curry1(function anyPass(preds) {
  1713. return curryN(reduce(max, 0, pluck('length', preds)), function () {
  1714. var idx = 0;
  1715. var len = preds.length;
  1716. while (idx < len) {
  1717. if (preds[idx].apply(this, arguments)) {
  1718. return true;
  1719. }
  1720. idx += 1;
  1721. }
  1722. return false;
  1723. });
  1724. });
  1725. function _iterableReduce(reducer, acc, iter) {
  1726. var step = iter.next();
  1727. while (!step.done) {
  1728. acc = reducer(acc, step.value);
  1729. step = iter.next();
  1730. }
  1731. return acc;
  1732. }
  1733. function _methodReduce(reducer, acc, obj, methodName) {
  1734. return obj[methodName](reducer, acc);
  1735. }
  1736. var _reduce = _createReduce(_arrayReduce, _methodReduce, _iterableReduce);
  1737. /**
  1738. * ap applies a list of functions to a list of values.
  1739. *
  1740. * Dispatches to the `ap` method of the first argument, if present. Also
  1741. * treats curried functions as applicatives.
  1742. *
  1743. * @func
  1744. * @memberOf R
  1745. * @since v0.3.0
  1746. * @category Function
  1747. * @sig [a -> b] -> [a] -> [b]
  1748. * @sig Apply f => f (a -> b) -> f a -> f b
  1749. * @sig (r -> a -> b) -> (r -> a) -> (r -> b)
  1750. * @param {*} applyF
  1751. * @param {*} applyX
  1752. * @return {*}
  1753. * @example
  1754. *
  1755. * R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6]
  1756. * R.ap([R.concat('tasty '), R.toUpper], ['pizza', 'salad']); //=> ["tasty pizza", "tasty salad", "PIZZA", "SALAD"]
  1757. *
  1758. * // R.ap can also be used as S combinator
  1759. * // when only two functions are passed
  1760. * R.ap(R.concat, R.toUpper)('Ramda') //=> 'RamdaRAMDA'
  1761. * @symb R.ap([f, g], [a, b]) = [f(a), f(b), g(a), g(b)]
  1762. */
  1763. var ap = _curry2(function ap(applyF, applyX) {
  1764. return typeof applyX['fantasy-land/ap'] === 'function' ? applyX['fantasy-land/ap'](applyF) : typeof applyF.ap === 'function' ? applyF.ap(applyX) : typeof applyF === 'function' ? function (x) {
  1765. return applyF(x)(applyX(x));
  1766. } : _reduce(function (acc, f) {
  1767. return _concat(acc, map(f, applyX));
  1768. }, [], applyF);
  1769. });
  1770. function _aperture(n, list) {
  1771. var idx = 0;
  1772. var limit = list.length - (n - 1);
  1773. var acc = new Array(limit >= 0 ? limit : 0);
  1774. while (idx < limit) {
  1775. acc[idx] = Array.prototype.slice.call(list, idx, idx + n);
  1776. idx += 1;
  1777. }
  1778. return acc;
  1779. }
  1780. function XAperture(n, xf) {
  1781. this.xf = xf;
  1782. this.pos = 0;
  1783. this.full = false;
  1784. this.acc = new Array(n);
  1785. }
  1786. XAperture.prototype['@@transducer/init'] = _xfBase.init;
  1787. XAperture.prototype['@@transducer/result'] = function (result) {
  1788. this.acc = null;
  1789. return this.xf['@@transducer/result'](result);
  1790. };
  1791. XAperture.prototype['@@transducer/step'] = function (result, input) {
  1792. this.store(input);
  1793. return this.full ? this.xf['@@transducer/step'](result, this.getCopy()) : result;
  1794. };
  1795. XAperture.prototype.store = function (input) {
  1796. this.acc[this.pos] = input;
  1797. this.pos += 1;
  1798. if (this.pos === this.acc.length) {
  1799. this.pos = 0;
  1800. this.full = true;
  1801. }
  1802. };
  1803. XAperture.prototype.getCopy = function () {
  1804. return _concat(Array.prototype.slice.call(this.acc, this.pos), Array.prototype.slice.call(this.acc, 0, this.pos));
  1805. };
  1806. function _xaperture(n) {
  1807. return function (xf) {
  1808. return new XAperture(n, xf);
  1809. };
  1810. }
  1811. /**
  1812. * Returns a new list, composed of n-tuples of consecutive elements. If `n` is
  1813. * greater than the length of the list, an empty list is returned.
  1814. *
  1815. * Acts as a transducer if a transformer is given in list position.
  1816. *
  1817. * @func
  1818. * @memberOf R
  1819. * @since v0.12.0
  1820. * @category List
  1821. * @sig Number -> [a] -> [[a]]
  1822. * @param {Number} n The size of the tuples to create
  1823. * @param {Array} list The list to split into `n`-length tuples
  1824. * @return {Array} The resulting list of `n`-length tuples
  1825. * @see R.transduce
  1826. * @example
  1827. *
  1828. * R.aperture(2, [1, 2, 3, 4, 5]); //=> [[1, 2], [2, 3], [3, 4], [4, 5]]
  1829. * R.aperture(3, [1, 2, 3, 4, 5]); //=> [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
  1830. * R.aperture(7, [1, 2, 3, 4, 5]); //=> []
  1831. */
  1832. var aperture = _curry2(_dispatchable([], _xaperture, _aperture));
  1833. /**
  1834. * Returns a new list containing the contents of the given list, followed by
  1835. * the given element.
  1836. *
  1837. * @func
  1838. * @memberOf R
  1839. * @since v0.1.0
  1840. * @category List
  1841. * @sig a -> [a] -> [a]
  1842. * @param {*} el The element to add to the end of the new list.
  1843. * @param {Array} list The list of elements to add a new item to.
  1844. * list.
  1845. * @return {Array} A new list containing the elements of the old list followed by `el`.
  1846. * @see R.prepend
  1847. * @example
  1848. *
  1849. * R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
  1850. * R.append('tests', []); //=> ['tests']
  1851. * R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]
  1852. */
  1853. var append = _curry2(function append(el, list) {
  1854. return _concat(list, [el]);
  1855. });
  1856. /**
  1857. * Applies function `fn` to the argument list `args`. This is useful for
  1858. * creating a fixed-arity function from a variadic function. `fn` should be a
  1859. * bound function if context is significant.
  1860. *
  1861. * @func
  1862. * @memberOf R
  1863. * @since v0.7.0
  1864. * @category Function
  1865. * @sig (*... -> a) -> [*] -> a
  1866. * @param {Function} fn The function which will be called with `args`
  1867. * @param {Array} args The arguments to call `fn` with
  1868. * @return {*} result The result, equivalent to `fn(...args)`
  1869. * @see R.call, R.unapply
  1870. * @example
  1871. *
  1872. * const nums = [1, 2, 3, -99, 42, 6, 7];
  1873. * R.apply(Math.max, nums); //=> 42
  1874. * @symb R.apply(f, [a, b, c]) = f(a, b, c)
  1875. */
  1876. var apply = _curry2(function apply(fn, args) {
  1877. return fn.apply(this, args);
  1878. });
  1879. /**
  1880. * Returns a list of all the enumerable own properties of the supplied object.
  1881. * Note that the order of the output array is not guaranteed across different
  1882. * JS platforms.
  1883. *
  1884. * @func
  1885. * @memberOf R
  1886. * @since v0.1.0
  1887. * @category Object
  1888. * @sig {k: v} -> [v]
  1889. * @param {Object} obj The object to extract values from
  1890. * @return {Array} An array of the values of the object's own properties.
  1891. * @see R.valuesIn, R.keys, R.toPairs
  1892. * @example
  1893. *
  1894. * R.values({a: 1, b: 2, c: 3}); //=> [1, 2, 3]
  1895. */
  1896. var values = _curry1(function values(obj) {
  1897. var props = keys(obj);
  1898. var len = props.length;
  1899. var vals = [];
  1900. var idx = 0;
  1901. while (idx < len) {
  1902. vals[idx] = obj[props[idx]];
  1903. idx += 1;
  1904. }
  1905. return vals;
  1906. });
  1907. // delegating calls to .map
  1908. function mapValues(fn, obj) {
  1909. return _isArray(obj) ? obj.map(fn) : keys(obj).reduce(function (acc, key) {
  1910. acc[key] = fn(obj[key]);
  1911. return acc;
  1912. }, {});
  1913. }
  1914. /**
  1915. * Given a spec object recursively mapping properties to functions, creates a
  1916. * function producing an object of the same structure, by mapping each property
  1917. * to the result of calling its associated function with the supplied arguments.
  1918. *
  1919. * @func
  1920. * @memberOf R
  1921. * @since v0.20.0
  1922. * @category Function
  1923. * @sig {k: ((a, b, ..., m) -> v)} -> ((a, b, ..., m) -> {k: v})
  1924. * @param {Object} spec an object recursively mapping properties to functions for
  1925. * producing the values for these properties.
  1926. * @return {Function} A function that returns an object of the same structure
  1927. * as `spec', with each property set to the value returned by calling its
  1928. * associated function with the supplied arguments.
  1929. * @see R.converge, R.juxt
  1930. * @example
  1931. *
  1932. * const getMetrics = R.applySpec({
  1933. * sum: R.add,
  1934. * nested: { mul: R.multiply }
  1935. * });
  1936. * getMetrics(2, 4); // => { sum: 6, nested: { mul: 8 } }
  1937. * @symb R.applySpec({ x: f, y: { z: g } })(a, b) = { x: f(a, b), y: { z: g(a, b) } }
  1938. */
  1939. var applySpec = _curry1(function applySpec(spec) {
  1940. spec = mapValues(function (v) {
  1941. return typeof v == 'function' ? v : applySpec(v);
  1942. }, spec);
  1943. return curryN(reduce(max, 0, pluck('length', values(spec))), function () {
  1944. var args = arguments;
  1945. return mapValues(function (f) {
  1946. return apply(f, args);
  1947. }, spec);
  1948. });
  1949. });
  1950. /**
  1951. * Takes a value and applies a function to it.
  1952. *
  1953. * This function is also known as the `thrush` combinator.
  1954. *
  1955. * @func
  1956. * @memberOf R
  1957. * @since v0.25.0
  1958. * @category Function
  1959. * @sig a -> (a -> b) -> b
  1960. * @param {*} x The value
  1961. * @param {Function} f The function to apply
  1962. * @return {*} The result of applying `f` to `x`
  1963. * @example
  1964. *
  1965. * const t42 = R.applyTo(42);
  1966. * t42(R.identity); //=> 42
  1967. * t42(R.add(1)); //=> 43
  1968. */
  1969. var applyTo = _curry2(function applyTo(x, f) {
  1970. return f(x);
  1971. });
  1972. /**
  1973. * Makes an ascending comparator function out of a function that returns a value
  1974. * that can be compared with `<` and `>`.
  1975. *
  1976. * @func
  1977. * @memberOf R
  1978. * @since v0.23.0
  1979. * @category Function
  1980. * @sig Ord b => (a -> b) -> a -> a -> Number
  1981. * @param {Function} fn A function of arity one that returns a value that can be compared
  1982. * @param {*} a The first item to be compared.
  1983. * @param {*} b The second item to be compared.
  1984. * @return {Number} `-1` if fn(a) < fn(b), `1` if fn(b) < fn(a), otherwise `0`
  1985. * @see R.descend
  1986. * @example
  1987. *
  1988. * const byAge = R.ascend(R.prop('age'));
  1989. * const people = [
  1990. * { name: 'Emma', age: 70 },
  1991. * { name: 'Peter', age: 78 },
  1992. * { name: 'Mikhail', age: 62 },
  1993. * ];
  1994. * const peopleByYoungestFirst = R.sort(byAge, people);
  1995. * //=> [{ name: 'Mikhail', age: 62 },{ name: 'Emma', age: 70 }, { name: 'Peter', age: 78 }]
  1996. */
  1997. var ascend = _curry3(function ascend(fn, a, b) {
  1998. var aa = fn(a);
  1999. var bb = fn(b);
  2000. return aa < bb ? -1 : aa > bb ? 1 : 0;
  2001. });
  2002. /**
  2003. * Makes a shallow clone of an object, setting or overriding the specified
  2004. * property with the given value. Note that this copies and flattens prototype
  2005. * properties onto the new object as well. All non-primitive properties are
  2006. * copied by reference.
  2007. *
  2008. * @private
  2009. * @param {String|Number} prop The property name to set
  2010. * @param {*} val The new value
  2011. * @param {Object|Array} obj The object to clone
  2012. * @return {Object|Array} A new object equivalent to the original except for the changed property.
  2013. */
  2014. function _assoc(prop, val, obj) {
  2015. if (_isInteger(prop) && _isArray(obj)) {
  2016. var arr = [].concat(obj);
  2017. arr[prop] = val;
  2018. return arr;
  2019. }
  2020. var result = {};
  2021. for (var p in obj) {
  2022. result[p] = obj[p];
  2023. }
  2024. result[prop] = val;
  2025. return result;
  2026. }
  2027. /**
  2028. * Checks if the input value is `null` or `undefined`.
  2029. *
  2030. * @func
  2031. * @memberOf R
  2032. * @since v0.9.0
  2033. * @category Type
  2034. * @sig * -> Boolean
  2035. * @param {*} x The value to test.
  2036. * @return {Boolean} `true` if `x` is `undefined` or `null`, otherwise `false`.
  2037. * @example
  2038. *
  2039. * R.isNil(null); //=> true
  2040. * R.isNil(undefined); //=> true
  2041. * R.isNil(0); //=> false
  2042. * R.isNil([]); //=> false
  2043. */
  2044. var isNil = _curry1(function isNil(x) {
  2045. return x == null;
  2046. });
  2047. /**
  2048. * Makes a shallow clone of an object, setting or overriding the nodes required
  2049. * to create the given path, and placing the specific value at the tail end of
  2050. * that path. Note that this copies and flattens prototype properties onto the
  2051. * new object as well. All non-primitive properties are copied by reference.
  2052. *
  2053. * @func
  2054. * @memberOf R
  2055. * @since v0.8.0
  2056. * @category Object
  2057. * @typedefn Idx = String | Int | Symbol
  2058. * @sig [Idx] -> a -> {a} -> {a}
  2059. * @param {Array} path the path to set
  2060. * @param {*} val The new value
  2061. * @param {Object} obj The object to clone
  2062. * @return {Object} A new object equivalent to the original except along the specified path.
  2063. * @see R.dissocPath
  2064. * @example
  2065. *
  2066. * R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}); //=> {a: {b: {c: 42}}}
  2067. *
  2068. * // Any missing or non-object keys in path will be overridden
  2069. * R.assocPath(['a', 'b', 'c'], 42, {a: 5}); //=> {a: {b: {c: 42}}}
  2070. */
  2071. var assocPath = _curry3(function assocPath(path, val, obj) {
  2072. if (path.length === 0) {
  2073. return val;
  2074. }
  2075. var idx = path[0];
  2076. if (path.length > 1) {
  2077. var nextObj = !isNil(obj) && _has(idx, obj) && _typeof(obj[idx]) === 'object' ? obj[idx] : _isInteger(path[1]) ? [] : {};
  2078. val = assocPath(Array.prototype.slice.call(path, 1), val, nextObj);
  2079. }
  2080. return _assoc(idx, val, obj);
  2081. });
  2082. /**
  2083. * Makes a shallow clone of an object, setting or overriding the specified
  2084. * property with the given value. Note that this copies and flattens prototype
  2085. * properties onto the new object as well. All non-primitive properties are
  2086. * copied by reference.
  2087. *
  2088. * @func
  2089. * @memberOf R
  2090. * @since v0.8.0
  2091. * @category Object
  2092. * @typedefn Idx = String | Int
  2093. * @sig Idx -> a -> {k: v} -> {k: v}
  2094. * @param {String|Number} prop The property name to set
  2095. * @param {*} val The new value
  2096. * @param {Object} obj The object to clone
  2097. * @return {Object} A new object equivalent to the original except for the changed property.
  2098. * @see R.dissoc, R.pick
  2099. * @example
  2100. *
  2101. * R.assoc('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3}
  2102. */
  2103. var assoc = _curry3(function assoc(prop, val, obj) {
  2104. return assocPath([prop], val, obj);
  2105. });
  2106. /**
  2107. * Wraps a function of any arity (including nullary) in a function that accepts
  2108. * exactly `n` parameters. Any extraneous parameters will not be passed to the
  2109. * supplied function.
  2110. *
  2111. * @func
  2112. * @memberOf R
  2113. * @since v0.1.0
  2114. * @category Function
  2115. * @sig Number -> (* -> a) -> (* -> a)
  2116. * @param {Number} n The desired arity of the new function.
  2117. * @param {Function} fn The function to wrap.
  2118. * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
  2119. * arity `n`.
  2120. * @see R.binary, R.unary
  2121. * @example
  2122. *
  2123. * const takesTwoArgs = (a, b) => [a, b];
  2124. *
  2125. * takesTwoArgs.length; //=> 2
  2126. * takesTwoArgs(1, 2); //=> [1, 2]
  2127. *
  2128. * const takesOneArg = R.nAry(1, takesTwoArgs);
  2129. * takesOneArg.length; //=> 1
  2130. * // Only `n` arguments are passed to the wrapped function
  2131. * takesOneArg(1, 2); //=> [1, undefined]
  2132. * @symb R.nAry(0, f)(a, b) = f()
  2133. * @symb R.nAry(1, f)(a, b) = f(a)
  2134. * @symb R.nAry(2, f)(a, b) = f(a, b)
  2135. */
  2136. var nAry = _curry2(function nAry(n, fn) {
  2137. switch (n) {
  2138. case 0:
  2139. return function () {
  2140. return fn.call(this);
  2141. };
  2142. case 1:
  2143. return function (a0) {
  2144. return fn.call(this, a0);
  2145. };
  2146. case 2:
  2147. return function (a0, a1) {
  2148. return fn.call(this, a0, a1);
  2149. };
  2150. case 3:
  2151. return function (a0, a1, a2) {
  2152. return fn.call(this, a0, a1, a2);
  2153. };
  2154. case 4:
  2155. return function (a0, a1, a2, a3) {
  2156. return fn.call(this, a0, a1, a2, a3);
  2157. };
  2158. case 5:
  2159. return function (a0, a1, a2, a3, a4) {
  2160. return fn.call(this, a0, a1, a2, a3, a4);
  2161. };
  2162. case 6:
  2163. return function (a0, a1, a2, a3, a4, a5) {
  2164. return fn.call(this, a0, a1, a2, a3, a4, a5);
  2165. };
  2166. case 7:
  2167. return function (a0, a1, a2, a3, a4, a5, a6) {
  2168. return fn.call(this, a0, a1, a2, a3, a4, a5, a6);
  2169. };
  2170. case 8:
  2171. return function (a0, a1, a2, a3, a4, a5, a6, a7) {
  2172. return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7);
  2173. };
  2174. case 9:
  2175. return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
  2176. return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8);
  2177. };
  2178. case 10:
  2179. return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
  2180. return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
  2181. };
  2182. default:
  2183. throw new Error('First argument to nAry must be a non-negative integer no greater than ten');
  2184. }
  2185. });
  2186. /**
  2187. * Wraps a function of any arity (including nullary) in a function that accepts
  2188. * exactly 2 parameters. Any extraneous parameters will not be passed to the
  2189. * supplied function.
  2190. *
  2191. * @func
  2192. * @memberOf R
  2193. * @since v0.2.0
  2194. * @category Function
  2195. * @sig (a -> b -> c -> ... -> z) -> ((a, b) -> z)
  2196. * @param {Function} fn The function to wrap.
  2197. * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
  2198. * arity 2.
  2199. * @see R.nAry, R.unary
  2200. * @example
  2201. *
  2202. * const takesThreeArgs = function(a, b, c) {
  2203. * return [a, b, c];
  2204. * };
  2205. * takesThreeArgs.length; //=> 3
  2206. * takesThreeArgs(1, 2, 3); //=> [1, 2, 3]
  2207. *
  2208. * const takesTwoArgs = R.binary(takesThreeArgs);
  2209. * takesTwoArgs.length; //=> 2
  2210. * // Only 2 arguments are passed to the wrapped function
  2211. * takesTwoArgs(1, 2, 3); //=> [1, 2, undefined]
  2212. * @symb R.binary(f)(a, b, c) = f(a, b)
  2213. */
  2214. var binary = _curry1(function binary(fn) {
  2215. return nAry(2, fn);
  2216. });
  2217. function _isFunction(x) {
  2218. var type = Object.prototype.toString.call(x);
  2219. return type === '[object Function]' || type === '[object AsyncFunction]' || type === '[object GeneratorFunction]' || type === '[object AsyncGeneratorFunction]';
  2220. }
  2221. /**
  2222. * "lifts" a function to be the specified arity, so that it may "map over" that
  2223. * many lists, Functions or other objects that satisfy the [FantasyLand Apply spec](https://github.com/fantasyland/fantasy-land#apply).
  2224. *
  2225. * @func
  2226. * @memberOf R
  2227. * @since v0.7.0
  2228. * @category Function
  2229. * @sig Number -> (*... -> *) -> ([*]... -> [*])
  2230. * @param {Function} fn The function to lift into higher context
  2231. * @return {Function} The lifted function.
  2232. * @see R.lift, R.ap
  2233. * @example
  2234. *
  2235. * const madd3 = R.liftN(3, (...args) => R.sum(args));
  2236. * madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
  2237. */
  2238. var liftN = _curry2(function liftN(arity, fn) {
  2239. var lifted = curryN(arity, fn);
  2240. return curryN(arity, function () {
  2241. return _arrayReduce(ap, map(lifted, arguments[0]), Array.prototype.slice.call(arguments, 1));
  2242. });
  2243. });
  2244. /**
  2245. * "lifts" a function of arity >= 1 so that it may "map over" a list, Function or other
  2246. * object that satisfies the [FantasyLand Apply spec](https://github.com/fantasyland/fantasy-land#apply).
  2247. *
  2248. * @func
  2249. * @memberOf R
  2250. * @since v0.7.0
  2251. * @category Function
  2252. * @sig (*... -> *) -> ([*]... -> [*])
  2253. * @param {Function} fn The function to lift into higher context
  2254. * @return {Function} The lifted function.
  2255. * @see R.liftN
  2256. * @example
  2257. *
  2258. * const madd3 = R.lift((a, b, c) => a + b + c);
  2259. *
  2260. * madd3([100, 200], [30, 40], [5, 6, 7]); //=> [135, 136, 137, 145, 146, 147, 235, 236, 237, 245, 246, 247]
  2261. *
  2262. * const madd5 = R.lift((a, b, c, d, e) => a + b + c + d + e);
  2263. *
  2264. * madd5([10, 20], [1], [2, 3], [4], [100, 200]); //=> [117, 217, 118, 218, 127, 227, 128, 228]
  2265. */
  2266. var lift = _curry1(function lift(fn) {
  2267. return liftN(fn.length, fn);
  2268. });
  2269. /**
  2270. * A function which calls the two provided functions and returns the `&&`
  2271. * of the results.
  2272. * It returns the result of the first function if it is false-y and the result
  2273. * of the second function otherwise. Note that this is short-circuited,
  2274. * meaning that the second function will not be invoked if the first returns a
  2275. * false-y value.
  2276. *
  2277. * In addition to functions, `R.both` also accepts any fantasy-land compatible
  2278. * applicative functor.
  2279. *
  2280. * @func
  2281. * @memberOf R
  2282. * @since v0.12.0
  2283. * @category Logic
  2284. * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
  2285. * @param {Function} f A predicate
  2286. * @param {Function} g Another predicate
  2287. * @return {Function} a function that applies its arguments to `f` and `g` and `&&`s their outputs together.
  2288. * @see R.either, R.allPass, R.and
  2289. * @example
  2290. *
  2291. * const gt10 = R.gt(R.__, 10)
  2292. * const lt20 = R.lt(R.__, 20)
  2293. * const f = R.both(gt10, lt20);
  2294. * f(15); //=> true
  2295. * f(30); //=> false
  2296. *
  2297. * R.both(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(false)
  2298. * R.both([false, false, 'a'], [11]); //=> [false, false, 11]
  2299. */
  2300. var both = _curry2(function both(f, g) {
  2301. return _isFunction(f) ? function _both() {
  2302. return f.apply(this, arguments) && g.apply(this, arguments);
  2303. } : lift(and)(f, g);
  2304. });
  2305. /**
  2306. * Returns the result of calling its first argument with the remaining
  2307. * arguments. This is occasionally useful as a converging function for
  2308. * [`R.converge`](#converge): the first branch can produce a function while the
  2309. * remaining branches produce values to be passed to that function as its
  2310. * arguments.
  2311. *
  2312. * @func
  2313. * @memberOf R
  2314. * @since v0.9.0
  2315. * @category Function
  2316. * @sig ((*... -> a), *...) -> a
  2317. * @param {Function} fn The function to apply to the remaining arguments.
  2318. * @param {...*} args Any number of positional arguments.
  2319. * @return {*}
  2320. * @see R.apply
  2321. * @example
  2322. *
  2323. * R.call(R.add, 1, 2); //=> 3
  2324. *
  2325. * const indentN = R.pipe(
  2326. * R.repeat(' '),
  2327. * R.join(''),
  2328. * R.replace(/^(?!$)/gm)
  2329. * );
  2330. *
  2331. * const format = R.converge(
  2332. * R.call,
  2333. * [
  2334. * R.pipe(R.prop('indent'), indentN),
  2335. * R.prop('value')
  2336. * ]
  2337. * );
  2338. *
  2339. * format({indent: 2, value: 'foo\nbar\nbaz\n'}); //=> ' foo\n bar\n baz\n'
  2340. * @symb R.call(f, a, b) = f(a, b)
  2341. */
  2342. var call = _curry1(function call(fn) {
  2343. return fn.apply(this, Array.prototype.slice.call(arguments, 1));
  2344. });
  2345. /**
  2346. * `_makeFlat` is a helper function that returns a one-level or fully recursive
  2347. * function based on the flag passed in.
  2348. *
  2349. * @private
  2350. */
  2351. function _makeFlat(recursive) {
  2352. return function flatt(list) {
  2353. var value, jlen, j;
  2354. var result = [];
  2355. var idx = 0;
  2356. var ilen = list.length;
  2357. while (idx < ilen) {
  2358. if (_isArrayLike(list[idx])) {
  2359. value = recursive ? flatt(list[idx]) : list[idx];
  2360. j = 0;
  2361. jlen = value.length;
  2362. while (j < jlen) {
  2363. result[result.length] = value[j];
  2364. j += 1;
  2365. }
  2366. } else {
  2367. result[result.length] = list[idx];
  2368. }
  2369. idx += 1;
  2370. }
  2371. return result;
  2372. };
  2373. }
  2374. function _forceReduced(x) {
  2375. return {
  2376. '@@transducer/value': x,
  2377. '@@transducer/reduced': true
  2378. };
  2379. }
  2380. var tInit = '@@transducer/init';
  2381. var tStep = '@@transducer/step';
  2382. var tResult = '@@transducer/result';
  2383. function XPreservingReduced(xf) {
  2384. this.xf = xf;
  2385. }
  2386. XPreservingReduced.prototype[tInit] = _xfBase.init;
  2387. XPreservingReduced.prototype[tResult] = _xfBase.result;
  2388. XPreservingReduced.prototype[tStep] = function (result, input) {
  2389. var ret = this.xf[tStep](result, input);
  2390. return ret['@@transducer/reduced'] ? _forceReduced(ret) : ret;
  2391. };
  2392. function XFlatCat(xf) {
  2393. this.xf = new XPreservingReduced(xf);
  2394. }
  2395. XFlatCat.prototype[tInit] = _xfBase.init;
  2396. XFlatCat.prototype[tResult] = _xfBase.result;
  2397. XFlatCat.prototype[tStep] = function (result, input) {
  2398. return !_isArrayLike(input) ? _xArrayReduce(this.xf, result, [input]) : _xReduce(this.xf, result, input);
  2399. };
  2400. var _flatCat = function _xcat(xf) {
  2401. return new XFlatCat(xf);
  2402. };
  2403. function _xchain(f) {
  2404. return function (xf) {
  2405. return _xmap(f)(_flatCat(xf));
  2406. };
  2407. }
  2408. /**
  2409. * `chain` maps a function over a list and concatenates the results. `chain`
  2410. * is also known as `flatMap` in some libraries.
  2411. *
  2412. * Dispatches to the `chain` method of the second argument, if present,
  2413. * according to the [FantasyLand Chain spec](https://github.com/fantasyland/fantasy-land#chain).
  2414. *
  2415. * If second argument is a function, `chain(f, g)(x)` is equivalent to `f(g(x), x)`.
  2416. *
  2417. * Acts as a transducer if a transformer is given in list position.
  2418. *
  2419. * @func
  2420. * @memberOf R
  2421. * @since v0.3.0
  2422. * @category List
  2423. * @sig Chain m => (a -> m b) -> m a -> m b
  2424. * @param {Function} fn The function to map with
  2425. * @param {Array} list The list to map over
  2426. * @return {Array} The result of flat-mapping `list` with `fn`
  2427. * @example
  2428. *
  2429. * const duplicate = n => [n, n];
  2430. * R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3]
  2431. *
  2432. * R.chain(R.append, R.head)([1, 2, 3]); //=> [1, 2, 3, 1]
  2433. */
  2434. var chain = _curry2(_dispatchable(['fantasy-land/chain', 'chain'], _xchain, function chain(fn, monad) {
  2435. if (typeof monad === 'function') {
  2436. return function (x) {
  2437. return fn(monad(x))(x);
  2438. };
  2439. }
  2440. return _makeFlat(false)(map(fn, monad));
  2441. }));
  2442. /**
  2443. * Restricts a number to be within a range.
  2444. *
  2445. * Also works for other ordered types such as Strings and Dates.
  2446. *
  2447. * @func
  2448. * @memberOf R
  2449. * @since v0.20.0
  2450. * @category Relation
  2451. * @sig Ord a => a -> a -> a -> a
  2452. * @param {Number} minimum The lower limit of the clamp (inclusive)
  2453. * @param {Number} maximum The upper limit of the clamp (inclusive)
  2454. * @param {Number} value Value to be clamped
  2455. * @return {Number} Returns `minimum` when `val < minimum`, `maximum` when `val > maximum`, returns `val` otherwise
  2456. * @example
  2457. *
  2458. * R.clamp(1, 10, -5) // => 1
  2459. * R.clamp(1, 10, 15) // => 10
  2460. * R.clamp(1, 10, 4) // => 4
  2461. */
  2462. var clamp = _curry3(function clamp(min, max, value) {
  2463. if (min > max) {
  2464. throw new Error('min must not be greater than max in clamp(min, max, value)');
  2465. }
  2466. return value < min ? min : value > max ? max : value;
  2467. });
  2468. function _cloneRegExp(pattern) {
  2469. return new RegExp(pattern.source, pattern.flags ? pattern.flags : (pattern.global ? 'g' : '') + (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : '') + (pattern.sticky ? 'y' : '') + (pattern.unicode ? 'u' : '') + (pattern.dotAll ? 's' : ''));
  2470. }
  2471. /**
  2472. * Copies an object.
  2473. *
  2474. * @private
  2475. * @param {*} value The value to be copied
  2476. * @param {Boolean} deep Whether or not to perform deep cloning.
  2477. * @return {*} The copied value.
  2478. */
  2479. function _clone(value, deep, map) {
  2480. map || (map = new _ObjectMap()); // this avoids the slower switch with a quick if decision removing some milliseconds in each run.
  2481. if (_isPrimitive(value)) {
  2482. return value;
  2483. }
  2484. var copy = function copy(copiedValue) {
  2485. // Check for circular and same references on the object graph and return its corresponding clone.
  2486. var cachedCopy = map.get(value);
  2487. if (cachedCopy) {
  2488. return cachedCopy;
  2489. }
  2490. map.set(value, copiedValue);
  2491. for (var key in value) {
  2492. if (Object.prototype.hasOwnProperty.call(value, key)) {
  2493. copiedValue[key] = deep ? _clone(value[key], true, map) : value[key];
  2494. }
  2495. }
  2496. return copiedValue;
  2497. };
  2498. switch (type(value)) {
  2499. case 'Object':
  2500. return copy(Object.create(Object.getPrototypeOf(value)));
  2501. case 'Array':
  2502. return copy([]);
  2503. case 'Date':
  2504. return new Date(value.valueOf());
  2505. case 'RegExp':
  2506. return _cloneRegExp(value);
  2507. case 'Int8Array':
  2508. case 'Uint8Array':
  2509. case 'Uint8ClampedArray':
  2510. case 'Int16Array':
  2511. case 'Uint16Array':
  2512. case 'Int32Array':
  2513. case 'Uint32Array':
  2514. case 'Float32Array':
  2515. case 'Float64Array':
  2516. case 'BigInt64Array':
  2517. case 'BigUint64Array':
  2518. return value.slice();
  2519. default:
  2520. return value;
  2521. }
  2522. }
  2523. function _isPrimitive(param) {
  2524. var type = _typeof(param);
  2525. return param == null || type != 'object' && type != 'function';
  2526. }
  2527. function _ObjectMap() {
  2528. this.map = {};
  2529. this.length = 0;
  2530. }
  2531. _ObjectMap.prototype.set = function (key, value) {
  2532. var hashedKey = this.hash(key);
  2533. var bucket = this.map[hashedKey];
  2534. if (!bucket) {
  2535. this.map[hashedKey] = bucket = [];
  2536. }
  2537. bucket.push([key, value]);
  2538. this.length += 1;
  2539. };
  2540. _ObjectMap.prototype.hash = function (key) {
  2541. var hashedKey = [];
  2542. for (var value in key) {
  2543. hashedKey.push(Object.prototype.toString.call(key[value]));
  2544. }
  2545. return hashedKey.join();
  2546. };
  2547. _ObjectMap.prototype.get = function (key) {
  2548. /**
  2549. * depending on the number of objects to be cloned is faster to just iterate over the items in the map just because the hash function is so costly,
  2550. * on my tests this number is 180, anything above that using the hash function is faster.
  2551. */
  2552. if (this.length <= 180) {
  2553. for (var p in this.map) {
  2554. var _bucket = this.map[p];
  2555. for (var i = 0; i < _bucket.length; i += 1) {
  2556. var element = _bucket[i];
  2557. if (element[0] === key) {
  2558. return element[1];
  2559. }
  2560. }
  2561. }
  2562. return;
  2563. }
  2564. var hashedKey = this.hash(key);
  2565. var bucket = this.map[hashedKey];
  2566. if (!bucket) {
  2567. return;
  2568. }
  2569. for (var _i = 0; _i < bucket.length; _i += 1) {
  2570. var _element = bucket[_i];
  2571. if (_element[0] === key) {
  2572. return _element[1];
  2573. }
  2574. }
  2575. };
  2576. /**
  2577. * Creates a deep copy of the source that can be used in place of the source
  2578. * object without retaining any references to it.
  2579. * The source object may contain (nested) `Array`s and `Object`s,
  2580. * `Number`s, `String`s, `Boolean`s and `Date`s.
  2581. * `Function`s are assigned by reference rather than copied.
  2582. *
  2583. * Dispatches to a `clone` method if present.
  2584. *
  2585. * Note that if the source object has multiple nodes that share a reference,
  2586. * the returned object will have the same structure, but the references will
  2587. * be pointed to the location within the cloned value.
  2588. *
  2589. * @func
  2590. * @memberOf R
  2591. * @since v0.1.0
  2592. * @category Object
  2593. * @sig {*} -> {*}
  2594. * @param {*} value The object or array to clone
  2595. * @return {*} A deeply cloned copy of `val`
  2596. * @example
  2597. *
  2598. * const objects = [{}, {}, {}];
  2599. * const objectsClone = R.clone(objects);
  2600. * objects === objectsClone; //=> false
  2601. * objects[0] === objectsClone[0]; //=> false
  2602. */
  2603. var clone = _curry1(function clone(value) {
  2604. return value != null && typeof value.clone === 'function' ? value.clone() : _clone(value, true);
  2605. });
  2606. /**
  2607. * Splits a list into sub-lists, based on the result of calling a key-returning function on each element,
  2608. * and grouping the results according to values returned.
  2609. *
  2610. * @func
  2611. * @memberOf R
  2612. * @since v0.28.0
  2613. * @category List
  2614. * @typedefn Idx = String | Int | Symbol
  2615. * @sig Idx a => (b -> a) -> [b] -> [[b]]
  2616. * @param {Function} fn Function :: a -> Idx
  2617. * @param {Array} list The array to group
  2618. * @return {Array}
  2619. * An array of arrays where each sub-array contains items for which
  2620. * the String-returning function has returned the same value.
  2621. * @see R.groupBy, R.partition
  2622. * @example
  2623. * R.collectBy(R.prop('type'), [
  2624. * {type: 'breakfast', item: '☕️'},
  2625. * {type: 'lunch', item: '🌯'},
  2626. * {type: 'dinner', item: '🍝'},
  2627. * {type: 'breakfast', item: '🥐'},
  2628. * {type: 'lunch', item: '🍕'}
  2629. * ]);
  2630. *
  2631. * // [ [ {type: 'breakfast', item: '☕️'},
  2632. * // {type: 'breakfast', item: '🥐'} ],
  2633. * // [ {type: 'lunch', item: '🌯'},
  2634. * // {type: 'lunch', item: '🍕'} ],
  2635. * // [ {type: 'dinner', item: '🍝'} ] ]
  2636. */
  2637. var collectBy = _curry2(function collectBy(fn, list) {
  2638. var group = _reduce(function (o, x) {
  2639. var tag = fn(x);
  2640. if (o[tag] === undefined) {
  2641. o[tag] = [];
  2642. }
  2643. o[tag].push(x);
  2644. return o;
  2645. }, {}, list);
  2646. var newList = [];
  2647. for (var tag in group) {
  2648. newList.push(group[tag]);
  2649. }
  2650. return newList;
  2651. });
  2652. /**
  2653. * Makes a comparator function out of a function that reports whether the first
  2654. * element is less than the second.
  2655. *
  2656. * @func
  2657. * @memberOf R
  2658. * @since v0.1.0
  2659. * @category Function
  2660. * @sig ((a, b) -> Boolean) -> ((a, b) -> Number)
  2661. * @param {Function} pred A predicate function of arity two which will return `true` if the first argument
  2662. * is less than the second, `false` otherwise
  2663. * @return {Function} A Function :: a -> b -> Int that returns `-1` if a < b, `1` if b < a, otherwise `0`
  2664. * @example
  2665. *
  2666. * const byAge = R.comparator((a, b) => a.age < b.age);
  2667. * const people = [
  2668. * { name: 'Emma', age: 70 },
  2669. * { name: 'Peter', age: 78 },
  2670. * { name: 'Mikhail', age: 62 },
  2671. * ];
  2672. * const peopleByIncreasingAge = R.sort(byAge, people);
  2673. * //=> [{ name: 'Mikhail', age: 62 },{ name: 'Emma', age: 70 }, { name: 'Peter', age: 78 }]
  2674. */
  2675. var comparator = _curry1(function comparator(pred) {
  2676. return function (a, b) {
  2677. return pred(a, b) ? -1 : pred(b, a) ? 1 : 0;
  2678. };
  2679. });
  2680. /**
  2681. * A function that returns the `!` of its argument. It will return `true` when
  2682. * passed false-y value, and `false` when passed a truth-y one.
  2683. *
  2684. * @func
  2685. * @memberOf R
  2686. * @since v0.1.0
  2687. * @category Logic
  2688. * @sig * -> Boolean
  2689. * @param {*} a any value
  2690. * @return {Boolean} the logical inverse of passed argument.
  2691. * @see R.complement
  2692. * @example
  2693. *
  2694. * R.not(true); //=> false
  2695. * R.not(false); //=> true
  2696. * R.not(0); //=> true
  2697. * R.not(1); //=> false
  2698. */
  2699. var not = _curry1(function not(a) {
  2700. return !a;
  2701. });
  2702. /**
  2703. * Takes a function `f` and returns a function `g` such that if called with the same arguments
  2704. * when `f` returns a "truthy" value, `g` returns `false` and when `f` returns a "falsy" value `g` returns `true`.
  2705. *
  2706. * `R.complement` may be applied to any functor
  2707. *
  2708. * @func
  2709. * @memberOf R
  2710. * @since v0.12.0
  2711. * @category Logic
  2712. * @sig (*... -> *) -> (*... -> Boolean)
  2713. * @param {Function} f
  2714. * @return {Function}
  2715. * @see R.not
  2716. * @example
  2717. *
  2718. * const isNotNil = R.complement(R.isNil);
  2719. * R.isNil(null); //=> true
  2720. * isNotNil(null); //=> false
  2721. * R.isNil(7); //=> false
  2722. * isNotNil(7); //=> true
  2723. */
  2724. var complement = lift(not);
  2725. function _pipe(f, g) {
  2726. return function () {
  2727. return g.call(this, f.apply(this, arguments));
  2728. };
  2729. }
  2730. /**
  2731. * This checks whether a function has a [methodname] function. If it isn't an
  2732. * array it will execute that function otherwise it will default to the ramda
  2733. * implementation.
  2734. *
  2735. * @private
  2736. * @param {Function} fn ramda implementation
  2737. * @param {String} methodname property to check for a custom implementation
  2738. * @return {Object} Whatever the return value of the method is.
  2739. */
  2740. function _checkForMethod(methodname, fn) {
  2741. return function () {
  2742. var length = arguments.length;
  2743. if (length === 0) {
  2744. return fn();
  2745. }
  2746. var obj = arguments[length - 1];
  2747. return _isArray(obj) || typeof obj[methodname] !== 'function' ? fn.apply(this, arguments) : obj[methodname].apply(obj, Array.prototype.slice.call(arguments, 0, length - 1));
  2748. };
  2749. }
  2750. /**
  2751. * Returns the elements of the given list or string (or object with a `slice`
  2752. * method) from `fromIndex` (inclusive) to `toIndex` (exclusive).
  2753. *
  2754. * Dispatches to the `slice` method of the third argument, if present.
  2755. *
  2756. * @func
  2757. * @memberOf R
  2758. * @since v0.1.4
  2759. * @category List
  2760. * @sig Number -> Number -> [a] -> [a]
  2761. * @sig Number -> Number -> String -> String
  2762. * @param {Number} fromIndex The start index (inclusive).
  2763. * @param {Number} toIndex The end index (exclusive).
  2764. * @param {*} list
  2765. * @return {*}
  2766. * @example
  2767. *
  2768. * R.slice(1, 3, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
  2769. * R.slice(1, Infinity, ['a', 'b', 'c', 'd']); //=> ['b', 'c', 'd']
  2770. * R.slice(0, -1, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c']
  2771. * R.slice(-3, -1, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
  2772. * R.slice(0, 3, 'ramda'); //=> 'ram'
  2773. */
  2774. var slice = _curry3(_checkForMethod('slice', function slice(fromIndex, toIndex, list) {
  2775. return Array.prototype.slice.call(list, fromIndex, toIndex);
  2776. }));
  2777. /**
  2778. * Returns all but the first element of the given list or string (or object
  2779. * with a `tail` method).
  2780. *
  2781. * Dispatches to the `slice` method of the first argument, if present.
  2782. *
  2783. * @func
  2784. * @memberOf R
  2785. * @since v0.1.0
  2786. * @category List
  2787. * @sig [a] -> [a]
  2788. * @sig String -> String
  2789. * @param {*} list
  2790. * @return {*}
  2791. * @see R.head, R.init, R.last
  2792. * @example
  2793. *
  2794. * R.tail([1, 2, 3]); //=> [2, 3]
  2795. * R.tail([1, 2]); //=> [2]
  2796. * R.tail([1]); //=> []
  2797. * R.tail([]); //=> []
  2798. *
  2799. * R.tail('abc'); //=> 'bc'
  2800. * R.tail('ab'); //=> 'b'
  2801. * R.tail('a'); //=> ''
  2802. * R.tail(''); //=> ''
  2803. */
  2804. var tail = _curry1(_checkForMethod('tail', slice(1, Infinity)));
  2805. /**
  2806. * Performs left-to-right function composition. The first argument may have
  2807. * any arity; the remaining arguments must be unary.
  2808. *
  2809. * In some libraries this function is named `sequence`.
  2810. *
  2811. * **Note:** The result of pipe is not automatically curried.
  2812. *
  2813. * @func
  2814. * @memberOf R
  2815. * @since v0.1.0
  2816. * @category Function
  2817. * @sig (((a, b, ..., n) -> o), (o -> p), ..., (x -> y), (y -> z)) -> ((a, b, ..., n) -> z)
  2818. * @param {...Function} functions
  2819. * @return {Function}
  2820. * @see R.compose
  2821. * @example
  2822. *
  2823. * const f = R.pipe(Math.pow, R.negate, R.inc);
  2824. *
  2825. * f(3, 4); // -(3^4) + 1
  2826. * @symb R.pipe(f, g, h)(a, b) = h(g(f(a, b)))
  2827. * @symb R.pipe(f, g, h)(a)(b) = h(g(f(a)))(b)
  2828. */
  2829. function pipe() {
  2830. if (arguments.length === 0) {
  2831. throw new Error('pipe requires at least one argument');
  2832. }
  2833. return _arity(arguments[0].length, reduce(_pipe, arguments[0], tail(arguments)));
  2834. }
  2835. /**
  2836. * Returns a new list or string with the elements or characters in reverse
  2837. * order.
  2838. *
  2839. * @func
  2840. * @memberOf R
  2841. * @since v0.1.0
  2842. * @category List
  2843. * @sig [a] -> [a]
  2844. * @sig String -> String
  2845. * @param {Array|String} list
  2846. * @return {Array|String}
  2847. * @example
  2848. *
  2849. * R.reverse([1, 2, 3]); //=> [3, 2, 1]
  2850. * R.reverse([1, 2]); //=> [2, 1]
  2851. * R.reverse([1]); //=> [1]
  2852. * R.reverse([]); //=> []
  2853. *
  2854. * R.reverse('abc'); //=> 'cba'
  2855. * R.reverse('ab'); //=> 'ba'
  2856. * R.reverse('a'); //=> 'a'
  2857. * R.reverse(''); //=> ''
  2858. */
  2859. var reverse = _curry1(function reverse(list) {
  2860. return _isString(list) ? list.split('').reverse().join('') : Array.prototype.slice.call(list, 0).reverse();
  2861. });
  2862. /**
  2863. * Performs right-to-left function composition. The last argument may have
  2864. * any arity; the remaining arguments must be unary.
  2865. *
  2866. * **Note:** The result of compose is not automatically curried.
  2867. *
  2868. * @func
  2869. * @memberOf R
  2870. * @since v0.1.0
  2871. * @category Function
  2872. * @sig ((y -> z), (x -> y), ..., (o -> p), ((a, b, ..., n) -> o)) -> ((a, b, ..., n) -> z)
  2873. * @param {...Function} ...functions The functions to compose
  2874. * @return {Function}
  2875. * @see R.pipe
  2876. * @example
  2877. *
  2878. * const classyGreeting = (firstName, lastName) => "The name's " + lastName + ", " + firstName + " " + lastName
  2879. * const yellGreeting = R.compose(R.toUpper, classyGreeting);
  2880. * yellGreeting('James', 'Bond'); //=> "THE NAME'S BOND, JAMES BOND"
  2881. *
  2882. * R.compose(Math.abs, R.add(1), R.multiply(2))(-4) //=> 7
  2883. *
  2884. * @symb R.compose(f, g, h)(a, b) = f(g(h(a, b)))
  2885. * @symb R.compose(f, g, h)(a)(b) = f(g(h(a)))(b)
  2886. */
  2887. function compose() {
  2888. if (arguments.length === 0) {
  2889. throw new Error('compose requires at least one argument');
  2890. }
  2891. return pipe.apply(this, reverse(arguments));
  2892. }
  2893. /**
  2894. * Returns the first element of the given list or string. In some libraries
  2895. * this function is named `first`.
  2896. *
  2897. * @func
  2898. * @memberOf R
  2899. * @since v0.1.0
  2900. * @category List
  2901. * @sig [a] -> a | Undefined
  2902. * @sig String -> String
  2903. * @param {Array|String} list
  2904. * @return {*}
  2905. * @see R.tail, R.init, R.last
  2906. * @example
  2907. *
  2908. * R.head(['fi', 'fo', 'fum']); //=> 'fi'
  2909. * R.head([]); //=> undefined
  2910. *
  2911. * R.head('abc'); //=> 'a'
  2912. * R.head(''); //=> ''
  2913. */
  2914. var head = nth(0);
  2915. function _identity(x) {
  2916. return x;
  2917. }
  2918. /**
  2919. * A function that does nothing but return the parameter supplied to it. Good
  2920. * as a default or placeholder function.
  2921. *
  2922. * @func
  2923. * @memberOf R
  2924. * @since v0.1.0
  2925. * @category Function
  2926. * @sig a -> a
  2927. * @param {*} x The value to return.
  2928. * @return {*} The input value, `x`.
  2929. * @example
  2930. *
  2931. * R.identity(1); //=> 1
  2932. *
  2933. * const obj = {};
  2934. * R.identity(obj) === obj; //=> true
  2935. * @symb R.identity(a) = a
  2936. */
  2937. var identity = _curry1(_identity);
  2938. /**
  2939. * Performs left-to-right function composition using transforming function. The first function may have
  2940. * any arity; the remaining functions must be unary.
  2941. *
  2942. * **Note:** The result of pipeWith is not automatically curried. Transforming function is not used on the
  2943. * first argument.
  2944. *
  2945. * @func
  2946. * @memberOf R
  2947. * @since v0.26.0
  2948. * @category Function
  2949. * @sig ((* -> *), [((a, b, ..., n) -> o), (o -> p), ..., (x -> y), (y -> z)]) -> ((a, b, ..., n) -> z)
  2950. * @param {Function} transformer The transforming function
  2951. * @param {Array} functions The functions to pipe
  2952. * @return {Function}
  2953. * @see R.composeWith, R.pipe
  2954. * @example
  2955. *
  2956. * const pipeWhileNotNil = R.pipeWith((f, res) => R.isNil(res) ? res : f(res));
  2957. * const f = pipeWhileNotNil([Math.pow, R.negate, R.inc])
  2958. *
  2959. * f(3, 4); // -(3^4) + 1
  2960. * @symb R.pipeWith(f)([g, h, i])(...args) = f(i, f(h, g(...args)))
  2961. */
  2962. var pipeWith = _curry2(function pipeWith(xf, list) {
  2963. if (list.length <= 0) {
  2964. return identity;
  2965. }
  2966. var headList = head(list);
  2967. var tailList = tail(list);
  2968. return _arity(headList.length, function () {
  2969. return _reduce(function (result, f) {
  2970. return xf.call(this, f, result);
  2971. }, headList.apply(this, arguments), tailList);
  2972. });
  2973. });
  2974. /**
  2975. * Performs right-to-left function composition using transforming function. The last function may have
  2976. * any arity; the remaining functions must be unary. Unlike `compose`, functions are passed in an array.
  2977. *
  2978. * **Note:** The result of composeWith is not automatically curried. Transforming function is not used
  2979. * on the last argument.
  2980. *
  2981. * @func
  2982. * @memberOf R
  2983. * @since v0.26.0
  2984. * @category Function
  2985. * @sig ((* -> *), [(y -> z), (x -> y), ..., (o -> p), ((a, b, ..., n) -> o)]) -> ((a, b, ..., n) -> z)
  2986. * @param {Function} transformer The transforming function
  2987. * @param {Array} functions The functions to compose
  2988. * @return {Function}
  2989. * @see R.compose, R.pipeWith
  2990. * @example
  2991. *
  2992. * const composeWhileNotNil = R.composeWith((f, res) => R.isNil(res) ? res : f(res));
  2993. *
  2994. * composeWhileNotNil([R.inc, R.prop('age')])({age: 1}) //=> 2
  2995. * composeWhileNotNil([R.inc, R.prop('age')])({}) //=> undefined
  2996. *
  2997. * @symb R.composeWith(f)([g, h, i])(...args) = f(g, f(h, i(...args)))
  2998. */
  2999. var composeWith = _curry2(function composeWith(xf, list) {
  3000. return pipeWith.apply(this, [xf, reverse(list)]);
  3001. });
  3002. /**
  3003. * Returns the result of concatenating the given lists or strings.
  3004. *
  3005. * Note: `R.concat` expects both arguments to be of the same type,
  3006. * unlike the native `Array.prototype.concat` method. It will throw
  3007. * an error if you `concat` an Array with a non-Array value.
  3008. *
  3009. * Dispatches to the `concat` method of the first argument, if present.
  3010. * Can also concatenate two members of a [fantasy-land
  3011. * compatible semigroup](https://github.com/fantasyland/fantasy-land#semigroup).
  3012. *
  3013. * @func
  3014. * @memberOf R
  3015. * @since v0.1.0
  3016. * @category List
  3017. * @sig [a] -> [a] -> [a]
  3018. * @sig String -> String -> String
  3019. * @param {Array|String} firstList The first list
  3020. * @param {Array|String} secondList The second list
  3021. * @return {Array|String} A list consisting of the elements of `firstList` followed by the elements of
  3022. * `secondList`.
  3023. *
  3024. * @example
  3025. *
  3026. * R.concat('ABC', 'DEF'); // 'ABCDEF'
  3027. * R.concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
  3028. * R.concat([], []); //=> []
  3029. */
  3030. var concat = _curry2(function concat(a, b) {
  3031. if (_isArray(a)) {
  3032. if (_isArray(b)) {
  3033. return a.concat(b);
  3034. }
  3035. throw new TypeError(toString$1(b) + ' is not an array');
  3036. }
  3037. if (_isString(a)) {
  3038. if (_isString(b)) {
  3039. return a + b;
  3040. }
  3041. throw new TypeError(toString$1(b) + ' is not a string');
  3042. }
  3043. if (a != null && _isFunction(a['fantasy-land/concat'])) {
  3044. return a['fantasy-land/concat'](b);
  3045. }
  3046. if (a != null && _isFunction(a.concat)) {
  3047. return a.concat(b);
  3048. }
  3049. throw new TypeError(toString$1(a) + ' does not have a method named "concat" or "fantasy-land/concat"');
  3050. });
  3051. /**
  3052. * Returns a function, `fn`, which encapsulates `if/else, if/else, ...` logic.
  3053. * `R.cond` takes a list of [predicate, transformer] pairs. All of the arguments
  3054. * to `fn` are applied to each of the predicates in turn until one returns a
  3055. * "truthy" value, at which point `fn` returns the result of applying its
  3056. * arguments to the corresponding transformer. If none of the predicates
  3057. * matches, `fn` returns undefined.
  3058. *
  3059. * **Please note**: This is not a direct substitute for a `switch` statement.
  3060. * Remember that both elements of every pair passed to `cond` are *functions*,
  3061. * and `cond` returns a function.
  3062. *
  3063. * @func
  3064. * @memberOf R
  3065. * @since v0.6.0
  3066. * @category Logic
  3067. * @sig [[(*... -> Boolean),(*... -> *)]] -> (*... -> *)
  3068. * @param {Array} pairs A list of [predicate, transformer]
  3069. * @return {Function}
  3070. * @see R.ifElse, R.unless, R.when
  3071. * @example
  3072. *
  3073. * const fn = R.cond([
  3074. * [R.equals(0), R.always('water freezes at 0°C')],
  3075. * [R.equals(100), R.always('water boils at 100°C')],
  3076. * [R.T, temp => 'nothing special happens at ' + temp + '°C']
  3077. * ]);
  3078. * fn(0); //=> 'water freezes at 0°C'
  3079. * fn(50); //=> 'nothing special happens at 50°C'
  3080. * fn(100); //=> 'water boils at 100°C'
  3081. */
  3082. var cond = _curry1(function cond(pairs) {
  3083. var arity = reduce(max, 0, map(function (pair) {
  3084. return pair[0].length;
  3085. }, pairs));
  3086. return _arity(arity, function () {
  3087. var idx = 0;
  3088. while (idx < pairs.length) {
  3089. if (pairs[idx][0].apply(this, arguments)) {
  3090. return pairs[idx][1].apply(this, arguments);
  3091. }
  3092. idx += 1;
  3093. }
  3094. });
  3095. });
  3096. /**
  3097. * Returns a curried equivalent of the provided function. The curried function
  3098. * has two unusual capabilities. First, its arguments needn't be provided one
  3099. * at a time. If `f` is a ternary function and `g` is `R.curry(f)`, the
  3100. * following are equivalent:
  3101. *
  3102. * - `g(1)(2)(3)`
  3103. * - `g(1)(2, 3)`
  3104. * - `g(1, 2)(3)`
  3105. * - `g(1, 2, 3)`
  3106. *
  3107. * Secondly, the special placeholder value [`R.__`](#__) may be used to specify
  3108. * "gaps", allowing partial application of any combination of arguments,
  3109. * regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
  3110. * the following are equivalent:
  3111. *
  3112. * - `g(1, 2, 3)`
  3113. * - `g(_, 2, 3)(1)`
  3114. * - `g(_, _, 3)(1)(2)`
  3115. * - `g(_, _, 3)(1, 2)`
  3116. * - `g(_, 2)(1)(3)`
  3117. * - `g(_, 2)(1, 3)`
  3118. * - `g(_, 2)(_, 3)(1)`
  3119. *
  3120. * Please note that default parameters don't count towards a [function arity](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length)
  3121. * and therefore `curry` won't work well with those:
  3122. *
  3123. * ```
  3124. * const h = R.curry((a, b, c = 2) => a + b + c);
  3125. *
  3126. * h(40);
  3127. * //=> function (waits for `b`)
  3128. *
  3129. * h(39)(1);
  3130. * //=> 42
  3131. *
  3132. * h(1)(2, 3);
  3133. * //=> 6
  3134. *
  3135. * h(1)(2)(7);
  3136. * //=> Error! (`3` is not a function!)
  3137. * ```
  3138. *
  3139. * @func
  3140. * @memberOf R
  3141. * @since v0.1.0
  3142. * @category Function
  3143. * @sig (* -> a) -> (* -> a)
  3144. * @param {Function} fn The function to curry.
  3145. * @return {Function} A new, curried function.
  3146. * @see R.curryN, R.partial
  3147. * @example
  3148. *
  3149. * const addFourNumbers = (a, b, c, d) => a + b + c + d;
  3150. *
  3151. * const curriedAddFourNumbers = R.curry(addFourNumbers);
  3152. * const f = curriedAddFourNumbers(1, 2);
  3153. * const g = f(3);
  3154. * g(4); //=> 10
  3155. */
  3156. var curry = _curry1(function curry(fn) {
  3157. return curryN(fn.length, fn);
  3158. });
  3159. /**
  3160. * Wraps a constructor function inside a curried function that can be called
  3161. * with the same arguments and returns the same type. The arity of the function
  3162. * returned is specified to allow using variadic constructor functions.
  3163. *
  3164. * @func
  3165. * @memberOf R
  3166. * @since v0.4.0
  3167. * @category Function
  3168. * @sig Number -> (* -> {*}) -> (* -> {*})
  3169. * @param {Number} n The arity of the constructor function.
  3170. * @param {Function} Fn The constructor function to wrap.
  3171. * @return {Function} A wrapped, curried constructor function.
  3172. * @example
  3173. *
  3174. * // Variadic Constructor function
  3175. * function Salad() {
  3176. * this.ingredients = arguments;
  3177. * }
  3178. *
  3179. * Salad.prototype.recipe = function() {
  3180. * const instructions = R.map(ingredient => 'Add a dollop of ' + ingredient, this.ingredients);
  3181. * return R.join('\n', instructions);
  3182. * };
  3183. *
  3184. * const ThreeLayerSalad = R.constructN(3, Salad);
  3185. *
  3186. * // Notice we no longer need the 'new' keyword, and the constructor is curried for 3 arguments.
  3187. * const salad = ThreeLayerSalad('Mayonnaise')('Potato Chips')('Ketchup');
  3188. *
  3189. * console.log(salad.recipe());
  3190. * // Add a dollop of Mayonnaise
  3191. * // Add a dollop of Potato Chips
  3192. * // Add a dollop of Ketchup
  3193. */
  3194. var constructN = _curry2(function constructN(n, Fn) {
  3195. if (n > 10) {
  3196. throw new Error('Constructor with greater than ten arguments');
  3197. }
  3198. if (n === 0) {
  3199. return function () {
  3200. return new Fn();
  3201. };
  3202. }
  3203. return curry(nAry(n, function ($0, $1, $2, $3, $4, $5, $6, $7, $8, $9) {
  3204. switch (n) {
  3205. case 1:
  3206. return new Fn($0);
  3207. case 2:
  3208. return new Fn($0, $1);
  3209. case 3:
  3210. return new Fn($0, $1, $2);
  3211. case 4:
  3212. return new Fn($0, $1, $2, $3);
  3213. case 5:
  3214. return new Fn($0, $1, $2, $3, $4);
  3215. case 6:
  3216. return new Fn($0, $1, $2, $3, $4, $5);
  3217. case 7:
  3218. return new Fn($0, $1, $2, $3, $4, $5, $6);
  3219. case 8:
  3220. return new Fn($0, $1, $2, $3, $4, $5, $6, $7);
  3221. case 9:
  3222. return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8);
  3223. case 10:
  3224. return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8, $9);
  3225. }
  3226. }));
  3227. });
  3228. /**
  3229. * Wraps a constructor function inside a curried function that can be called
  3230. * with the same arguments and returns the same type.
  3231. *
  3232. * @func
  3233. * @memberOf R
  3234. * @since v0.1.0
  3235. * @category Function
  3236. * @sig (* -> {*}) -> (* -> {*})
  3237. * @param {Function} fn The constructor function to wrap.
  3238. * @return {Function} A wrapped, curried constructor function.
  3239. * @see R.invoker
  3240. * @example
  3241. *
  3242. * // Constructor function
  3243. * function Animal(kind) {
  3244. * this.kind = kind;
  3245. * };
  3246. * Animal.prototype.sighting = function() {
  3247. * return "It's a " + this.kind + "!";
  3248. * }
  3249. *
  3250. * const AnimalConstructor = R.construct(Animal)
  3251. *
  3252. * // Notice we no longer need the 'new' keyword:
  3253. * AnimalConstructor('Pig'); //=> {"kind": "Pig", "sighting": function (){...}};
  3254. *
  3255. * const animalTypes = ["Lion", "Tiger", "Bear"];
  3256. * const animalSighting = R.invoker(0, 'sighting');
  3257. * const sightNewAnimal = R.compose(animalSighting, AnimalConstructor);
  3258. * R.map(sightNewAnimal, animalTypes); //=> ["It's a Lion!", "It's a Tiger!", "It's a Bear!"]
  3259. */
  3260. var construct = _curry1(function construct(Fn) {
  3261. return constructN(Fn.length, Fn);
  3262. });
  3263. /**
  3264. * Accepts a converging function and a list of branching functions and returns
  3265. * a new function. The arity of the new function is the same as the arity of
  3266. * the longest branching function. When invoked, this new function is applied
  3267. * to some arguments, and each branching function is applied to those same
  3268. * arguments. The results of each branching function are passed as arguments
  3269. * to the converging function to produce the return value.
  3270. *
  3271. * @func
  3272. * @memberOf R
  3273. * @since v0.4.2
  3274. * @category Function
  3275. * @sig ((x1, x2, ...) -> z) -> [((a, b, ...) -> x1), ((a, b, ...) -> x2), ...] -> (a -> b -> ... -> z)
  3276. * @param {Function} after A function. `after` will be invoked with the return values of
  3277. * `fn1` and `fn2` as its arguments.
  3278. * @param {Array} functions A list of functions.
  3279. * @return {Function} A new function.
  3280. * @see R.useWith
  3281. * @example
  3282. *
  3283. * const average = R.converge(R.divide, [R.sum, R.length])
  3284. * average([1, 2, 3, 4, 5, 6, 7]) //=> 4
  3285. *
  3286. * const strangeConcat = R.converge(R.concat, [R.toUpper, R.toLower])
  3287. * strangeConcat("Yodel") //=> "YODELyodel"
  3288. *
  3289. * @symb R.converge(f, [g, h])(a, b) = f(g(a, b), h(a, b))
  3290. */
  3291. var converge = _curry2(function converge(after, fns) {
  3292. return curryN(reduce(max, 0, pluck('length', fns)), function () {
  3293. var args = arguments;
  3294. var context = this;
  3295. return after.apply(context, _map(function (fn) {
  3296. return fn.apply(context, args);
  3297. }, fns));
  3298. });
  3299. });
  3300. /**
  3301. * Returns the number of items in a given `list` matching the predicate `f`
  3302. *
  3303. * @func
  3304. * @memberOf R
  3305. * @since v0.28.0
  3306. * @category List
  3307. * @sig (a -> Boolean) -> [a] -> Number
  3308. * @param {Function} predicate to match items against
  3309. * @return {Array} list of items to count in
  3310. * @example
  3311. *
  3312. * const even = x => x % 2 == 0;
  3313. *
  3314. * R.count(even, [1, 2, 3, 4, 5]); // => 2
  3315. * R.map(R.count(even), [[1, 1, 1], [2, 3, 4, 5], [6]]); // => [0, 2, 1]
  3316. */
  3317. var count = curry(function (pred, list) {
  3318. return _reduce(function (a, e) {
  3319. return pred(e) ? a + 1 : a;
  3320. }, 0, list);
  3321. });
  3322. function XReduceBy(valueFn, valueAcc, keyFn, xf) {
  3323. this.valueFn = valueFn;
  3324. this.valueAcc = valueAcc;
  3325. this.keyFn = keyFn;
  3326. this.xf = xf;
  3327. this.inputs = {};
  3328. }
  3329. XReduceBy.prototype['@@transducer/init'] = _xfBase.init;
  3330. XReduceBy.prototype['@@transducer/result'] = function (result) {
  3331. var key;
  3332. for (key in this.inputs) {
  3333. if (_has(key, this.inputs)) {
  3334. result = this.xf['@@transducer/step'](result, this.inputs[key]);
  3335. if (result['@@transducer/reduced']) {
  3336. result = result['@@transducer/value'];
  3337. break;
  3338. }
  3339. }
  3340. }
  3341. this.inputs = null;
  3342. return this.xf['@@transducer/result'](result);
  3343. };
  3344. XReduceBy.prototype['@@transducer/step'] = function (result, input) {
  3345. var key = this.keyFn(input);
  3346. this.inputs[key] = this.inputs[key] || [key, _clone(this.valueAcc, false)];
  3347. this.inputs[key][1] = this.valueFn(this.inputs[key][1], input);
  3348. return result;
  3349. };
  3350. function _xreduceBy(valueFn, valueAcc, keyFn) {
  3351. return function (xf) {
  3352. return new XReduceBy(valueFn, valueAcc, keyFn, xf);
  3353. };
  3354. }
  3355. /**
  3356. * Groups the elements of the list according to the result of calling
  3357. * the String-returning function `keyFn` on each element and reduces the elements
  3358. * of each group to a single value via the reducer function `valueFn`.
  3359. *
  3360. * The value function receives two values: *(acc, value)*. It may use
  3361. * [`R.reduced`](#reduced) to short circuit the iteration.
  3362. *
  3363. * This function is basically a more general [`groupBy`](#groupBy) function.
  3364. *
  3365. * Acts as a transducer if a transformer is given in list position.
  3366. *
  3367. * @func
  3368. * @memberOf R
  3369. * @since v0.20.0
  3370. * @category List
  3371. * @sig ((a, b) -> a) -> a -> (b -> String) -> [b] -> {String: a}
  3372. * @param {Function} valueFn The function that reduces the elements of each group to a single
  3373. * value. Receives two values, accumulator for a particular group and the current element.
  3374. * @param {*} acc The (initial) accumulator value for each group.
  3375. * @param {Function} keyFn The function that maps the list's element into a key.
  3376. * @param {Array} list The array to group.
  3377. * @return {Object} An object with the output of `keyFn` for keys, mapped to the output of
  3378. * `valueFn` for elements which produced that key when passed to `keyFn`.
  3379. * @see R.groupBy, R.reduce, R.reduced
  3380. * @example
  3381. *
  3382. * const groupNames = (acc, {name}) => acc.concat(name)
  3383. * const toGrade = ({score}) =>
  3384. * score < 65 ? 'F' :
  3385. * score < 70 ? 'D' :
  3386. * score < 80 ? 'C' :
  3387. * score < 90 ? 'B' : 'A'
  3388. *
  3389. * var students = [
  3390. * {name: 'Abby', score: 83},
  3391. * {name: 'Bart', score: 62},
  3392. * {name: 'Curt', score: 88},
  3393. * {name: 'Dora', score: 92},
  3394. * ]
  3395. *
  3396. * reduceBy(groupNames, [], toGrade, students)
  3397. * //=> {"A": ["Dora"], "B": ["Abby", "Curt"], "F": ["Bart"]}
  3398. */
  3399. var reduceBy = _curryN(4, [], _dispatchable([], _xreduceBy, function reduceBy(valueFn, valueAcc, keyFn, list) {
  3400. var xf = _xwrap(function (acc, elt) {
  3401. var key = keyFn(elt);
  3402. var value = valueFn(_has(key, acc) ? acc[key] : _clone(valueAcc, false), elt);
  3403. if (value && value['@@transducer/reduced']) {
  3404. return _reduced(acc);
  3405. }
  3406. acc[key] = value;
  3407. return acc;
  3408. });
  3409. return _xReduce(xf, {}, list);
  3410. }));
  3411. /**
  3412. * Counts the elements of a list according to how many match each value of a
  3413. * key generated by the supplied function. Returns an object mapping the keys
  3414. * produced by `fn` to the number of occurrences in the list. Note that all
  3415. * keys are coerced to strings because of how JavaScript objects work.
  3416. *
  3417. * Acts as a transducer if a transformer is given in list position.
  3418. *
  3419. * @func
  3420. * @memberOf R
  3421. * @since v0.1.0
  3422. * @category Relation
  3423. * @sig (a -> String) -> [a] -> {*}
  3424. * @param {Function} fn The function used to map values to keys.
  3425. * @param {Array} list The list to count elements from.
  3426. * @return {Object} An object mapping keys to number of occurrences in the list.
  3427. * @example
  3428. *
  3429. * const numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
  3430. * R.countBy(Math.floor)(numbers); //=> {'1': 3, '2': 2, '3': 1}
  3431. *
  3432. * const letters = ['a', 'b', 'A', 'a', 'B', 'c'];
  3433. * R.countBy(R.toLower)(letters); //=> {'a': 3, 'b': 2, 'c': 1}
  3434. */
  3435. var countBy = reduceBy(function (acc, elem) {
  3436. return acc + 1;
  3437. }, 0);
  3438. /**
  3439. * Decrements its argument.
  3440. *
  3441. * @func
  3442. * @memberOf R
  3443. * @since v0.9.0
  3444. * @category Math
  3445. * @sig Number -> Number
  3446. * @param {Number} n
  3447. * @return {Number} n - 1
  3448. * @see R.inc
  3449. * @example
  3450. *
  3451. * R.dec(42); //=> 41
  3452. */
  3453. var dec = add(-1);
  3454. /**
  3455. * Returns the second argument if it is not `null`, `undefined` or `NaN`;
  3456. * otherwise the first argument is returned.
  3457. *
  3458. * @func
  3459. * @memberOf R
  3460. * @since v0.10.0
  3461. * @category Logic
  3462. * @sig a -> b -> a | b
  3463. * @param {a} default The default value.
  3464. * @param {b} val `val` will be returned instead of `default` unless `val` is `null`, `undefined` or `NaN`.
  3465. * @return {*} The second value if it is not `null`, `undefined` or `NaN`, otherwise the default value
  3466. * @example
  3467. *
  3468. * const defaultTo42 = R.defaultTo(42);
  3469. *
  3470. * defaultTo42(null); //=> 42
  3471. * defaultTo42(undefined); //=> 42
  3472. * defaultTo42(false); //=> false
  3473. * defaultTo42('Ramda'); //=> 'Ramda'
  3474. * // parseInt('string') results in NaN
  3475. * defaultTo42(parseInt('string')); //=> 42
  3476. */
  3477. var defaultTo = _curry2(function defaultTo(d, v) {
  3478. return v == null || v !== v ? d : v;
  3479. });
  3480. /**
  3481. * Makes a descending comparator function out of a function that returns a value
  3482. * that can be compared with `<` and `>`.
  3483. *
  3484. * @func
  3485. * @memberOf R
  3486. * @since v0.23.0
  3487. * @category Function
  3488. * @sig Ord b => (a -> b) -> a -> a -> Number
  3489. * @param {Function} fn A function of arity one that returns a value that can be compared
  3490. * @param {*} a The first item to be compared.
  3491. * @param {*} b The second item to be compared.
  3492. * @return {Number} `-1` if fn(a) > fn(b), `1` if fn(b) > fn(a), otherwise `0`
  3493. * @see R.ascend
  3494. * @example
  3495. *
  3496. * const byAge = R.descend(R.prop('age'));
  3497. * const people = [
  3498. * { name: 'Emma', age: 70 },
  3499. * { name: 'Peter', age: 78 },
  3500. * { name: 'Mikhail', age: 62 },
  3501. * ];
  3502. * const peopleByOldestFirst = R.sort(byAge, people);
  3503. * //=> [{ name: 'Peter', age: 78 }, { name: 'Emma', age: 70 }, { name: 'Mikhail', age: 62 }]
  3504. */
  3505. var descend = _curry3(function descend(fn, a, b) {
  3506. var aa = fn(a);
  3507. var bb = fn(b);
  3508. return aa > bb ? -1 : aa < bb ? 1 : 0;
  3509. });
  3510. function _Set() {
  3511. /* globals Set */
  3512. this._nativeSet = typeof Set === 'function' ? new Set() : null;
  3513. this._items = {};
  3514. } // until we figure out why jsdoc chokes on this
  3515. // @param item The item to add to the Set
  3516. // @returns {boolean} true if the item did not exist prior, otherwise false
  3517. //
  3518. _Set.prototype.add = function (item) {
  3519. return !hasOrAdd(item, true, this);
  3520. }; //
  3521. // @param item The item to check for existence in the Set
  3522. // @returns {boolean} true if the item exists in the Set, otherwise false
  3523. //
  3524. _Set.prototype.has = function (item) {
  3525. return hasOrAdd(item, false, this);
  3526. }; //
  3527. // Combines the logic for checking whether an item is a member of the set and
  3528. // for adding a new item to the set.
  3529. //
  3530. // @param item The item to check or add to the Set instance.
  3531. // @param shouldAdd If true, the item will be added to the set if it doesn't
  3532. // already exist.
  3533. // @param set The set instance to check or add to.
  3534. // @return {boolean} true if the item already existed, otherwise false.
  3535. //
  3536. function hasOrAdd(item, shouldAdd, set) {
  3537. var type = _typeof(item);
  3538. var prevSize, newSize;
  3539. switch (type) {
  3540. case 'string':
  3541. case 'number':
  3542. // distinguish between +0 and -0
  3543. if (item === 0 && 1 / item === -Infinity) {
  3544. if (set._items['-0']) {
  3545. return true;
  3546. } else {
  3547. if (shouldAdd) {
  3548. set._items['-0'] = true;
  3549. }
  3550. return false;
  3551. }
  3552. } // these types can all utilise the native Set
  3553. if (set._nativeSet !== null) {
  3554. if (shouldAdd) {
  3555. prevSize = set._nativeSet.size;
  3556. set._nativeSet.add(item);
  3557. newSize = set._nativeSet.size;
  3558. return newSize === prevSize;
  3559. } else {
  3560. return set._nativeSet.has(item);
  3561. }
  3562. } else {
  3563. if (!(type in set._items)) {
  3564. if (shouldAdd) {
  3565. set._items[type] = {};
  3566. set._items[type][item] = true;
  3567. }
  3568. return false;
  3569. } else if (item in set._items[type]) {
  3570. return true;
  3571. } else {
  3572. if (shouldAdd) {
  3573. set._items[type][item] = true;
  3574. }
  3575. return false;
  3576. }
  3577. }
  3578. case 'boolean':
  3579. // set._items['boolean'] holds a two element array
  3580. // representing [ falseExists, trueExists ]
  3581. if (type in set._items) {
  3582. var bIdx = item ? 1 : 0;
  3583. if (set._items[type][bIdx]) {
  3584. return true;
  3585. } else {
  3586. if (shouldAdd) {
  3587. set._items[type][bIdx] = true;
  3588. }
  3589. return false;
  3590. }
  3591. } else {
  3592. if (shouldAdd) {
  3593. set._items[type] = item ? [false, true] : [true, false];
  3594. }
  3595. return false;
  3596. }
  3597. case 'function':
  3598. // compare functions for reference equality
  3599. if (set._nativeSet !== null) {
  3600. if (shouldAdd) {
  3601. prevSize = set._nativeSet.size;
  3602. set._nativeSet.add(item);
  3603. newSize = set._nativeSet.size;
  3604. return newSize === prevSize;
  3605. } else {
  3606. return set._nativeSet.has(item);
  3607. }
  3608. } else {
  3609. if (!(type in set._items)) {
  3610. if (shouldAdd) {
  3611. set._items[type] = [item];
  3612. }
  3613. return false;
  3614. }
  3615. if (!_includes(item, set._items[type])) {
  3616. if (shouldAdd) {
  3617. set._items[type].push(item);
  3618. }
  3619. return false;
  3620. }
  3621. return true;
  3622. }
  3623. case 'undefined':
  3624. if (set._items[type]) {
  3625. return true;
  3626. } else {
  3627. if (shouldAdd) {
  3628. set._items[type] = true;
  3629. }
  3630. return false;
  3631. }
  3632. case 'object':
  3633. if (item === null) {
  3634. if (!set._items['null']) {
  3635. if (shouldAdd) {
  3636. set._items['null'] = true;
  3637. }
  3638. return false;
  3639. }
  3640. return true;
  3641. }
  3642. /* falls through */
  3643. default:
  3644. // reduce the search size of heterogeneous sets by creating buckets
  3645. // for each type.
  3646. type = Object.prototype.toString.call(item);
  3647. if (!(type in set._items)) {
  3648. if (shouldAdd) {
  3649. set._items[type] = [item];
  3650. }
  3651. return false;
  3652. } // scan through all previously applied items
  3653. if (!_includes(item, set._items[type])) {
  3654. if (shouldAdd) {
  3655. set._items[type].push(item);
  3656. }
  3657. return false;
  3658. }
  3659. return true;
  3660. }
  3661. } // A simple Set type that honours R.equals semantics
  3662. /**
  3663. * Finds the set (i.e. no duplicates) of all elements in the first list not
  3664. * contained in the second list. Objects and Arrays are compared in terms of
  3665. * value equality, not reference equality.
  3666. *
  3667. * @func
  3668. * @memberOf R
  3669. * @since v0.1.0
  3670. * @category Relation
  3671. * @sig [*] -> [*] -> [*]
  3672. * @param {Array} list1 The first list.
  3673. * @param {Array} list2 The second list.
  3674. * @return {Array} The elements in `list1` that are not in `list2`.
  3675. * @see R.differenceWith, R.symmetricDifference, R.symmetricDifferenceWith, R.without
  3676. * @example
  3677. *
  3678. * R.difference([1,2,3,4], [7,6,5,4,3]); //=> [1,2]
  3679. * R.difference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5]
  3680. * R.difference([{a: 1}, {b: 2}], [{a: 1}, {c: 3}]) //=> [{b: 2}]
  3681. */
  3682. var difference = _curry2(function difference(first, second) {
  3683. var out = [];
  3684. var idx = 0;
  3685. var firstLen = first.length;
  3686. var secondLen = second.length;
  3687. var toFilterOut = new _Set();
  3688. for (var i = 0; i < secondLen; i += 1) {
  3689. toFilterOut.add(second[i]);
  3690. }
  3691. while (idx < firstLen) {
  3692. if (toFilterOut.add(first[idx])) {
  3693. out[out.length] = first[idx];
  3694. }
  3695. idx += 1;
  3696. }
  3697. return out;
  3698. });
  3699. /**
  3700. * Finds the set (i.e. no duplicates) of all elements in the first list not
  3701. * contained in the second list. Duplication is determined according to the
  3702. * value returned by applying the supplied predicate to two list elements.
  3703. *
  3704. * @func
  3705. * @memberOf R
  3706. * @since v0.1.0
  3707. * @category Relation
  3708. * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
  3709. * @param {Function} pred A predicate used to test whether two items are equal.
  3710. * @param {Array} list1 The first list.
  3711. * @param {Array} list2 The second list.
  3712. * @return {Array} The elements in `list1` that are not in `list2`.
  3713. * @see R.difference, R.symmetricDifference, R.symmetricDifferenceWith
  3714. * @example
  3715. *
  3716. * const cmp = (x, y) => x.a === y.a;
  3717. * const l1 = [{a: 1}, {a: 2}, {a: 3}];
  3718. * const l2 = [{a: 3}, {a: 4}];
  3719. * R.differenceWith(cmp, l1, l2); //=> [{a: 1}, {a: 2}]
  3720. *
  3721. * R.differenceWith(R.equals, [1, 2, 3, 3, 3], []); //=> [1, 2, 3]
  3722. * R.differenceWith(R.equals, [1, 2, 3, 3, 3], [1]); //=> [2, 3]
  3723. */
  3724. var differenceWith = _curry3(function differenceWith(pred, first, second) {
  3725. var out = [];
  3726. var idx = 0;
  3727. var firstLen = first.length;
  3728. while (idx < firstLen) {
  3729. if (!_includesWith(pred, first[idx], second) && !_includesWith(pred, first[idx], out)) {
  3730. out.push(first[idx]);
  3731. }
  3732. idx += 1;
  3733. }
  3734. return out;
  3735. });
  3736. /**
  3737. * Removes the sub-list of `list` starting at index `start` and containing
  3738. * `count` elements. _Note that this is not destructive_: it returns a copy of
  3739. * the list with the changes.
  3740. * <small>No lists have been harmed in the application of this function.</small>
  3741. *
  3742. * @func
  3743. * @memberOf R
  3744. * @since v0.2.2
  3745. * @category List
  3746. * @sig Number -> Number -> [a] -> [a]
  3747. * @param {Number} start The position to start removing elements
  3748. * @param {Number} count The number of elements to remove
  3749. * @param {Array} list The list to remove from
  3750. * @return {Array} A new Array with `count` elements from `start` removed.
  3751. * @see R.without
  3752. * @example
  3753. *
  3754. * R.remove(2, 3, [1,2,3,4,5,6,7,8]); //=> [1,2,6,7,8]
  3755. */
  3756. var remove = _curry3(function remove(start, count, list) {
  3757. var result = Array.prototype.slice.call(list, 0);
  3758. result.splice(start, count);
  3759. return result;
  3760. });
  3761. /**
  3762. * Returns a new object that does not contain a `prop` property.
  3763. *
  3764. * @private
  3765. * @param {String|Number} prop The name of the property to dissociate
  3766. * @param {Object|Array} obj The object to clone
  3767. * @return {Object} A new object equivalent to the original but without the specified property
  3768. */
  3769. function _dissoc(prop, obj) {
  3770. if (obj == null) {
  3771. return obj;
  3772. }
  3773. if (_isInteger(prop) && _isArray(obj)) {
  3774. return remove(prop, 1, obj);
  3775. }
  3776. var result = {};
  3777. for (var p in obj) {
  3778. result[p] = obj[p];
  3779. }
  3780. delete result[prop];
  3781. return result;
  3782. }
  3783. /**
  3784. * Makes a shallow clone of an object. Note that this copies and flattens
  3785. * prototype properties onto the new object as well. All non-primitive
  3786. * properties are copied by reference.
  3787. *
  3788. * @private
  3789. * @param {String|Integer} prop The prop operating
  3790. * @param {Object|Array} obj The object to clone
  3791. * @return {Object|Array} A new object equivalent to the original.
  3792. */
  3793. function _shallowCloneObject(prop, obj) {
  3794. if (_isInteger(prop) && _isArray(obj)) {
  3795. return [].concat(obj);
  3796. }
  3797. var result = {};
  3798. for (var p in obj) {
  3799. result[p] = obj[p];
  3800. }
  3801. return result;
  3802. }
  3803. /**
  3804. * Makes a shallow clone of an object, omitting the property at the given path.
  3805. * Note that this copies and flattens prototype properties onto the new object
  3806. * as well. All non-primitive properties are copied by reference.
  3807. *
  3808. * @func
  3809. * @memberOf R
  3810. * @since v0.11.0
  3811. * @category Object
  3812. * @typedefn Idx = String | Int | Symbol
  3813. * @sig [Idx] -> {k: v} -> {k: v}
  3814. * @param {Array} path The path to the value to omit
  3815. * @param {Object} obj The object to clone
  3816. * @return {Object} A new object without the property at path
  3817. * @see R.assocPath
  3818. * @example
  3819. *
  3820. * R.dissocPath(['a', 'b', 'c'], {a: {b: {c: 42}}}); //=> {a: {b: {}}}
  3821. */
  3822. var dissocPath = _curry2(function dissocPath(path, obj) {
  3823. if (obj == null) {
  3824. return obj;
  3825. }
  3826. switch (path.length) {
  3827. case 0:
  3828. return obj;
  3829. case 1:
  3830. return _dissoc(path[0], obj);
  3831. default:
  3832. var head = path[0];
  3833. var tail = Array.prototype.slice.call(path, 1);
  3834. if (obj[head] == null) {
  3835. return _shallowCloneObject(head, obj);
  3836. } else {
  3837. return assoc(head, dissocPath(tail, obj[head]), obj);
  3838. }
  3839. }
  3840. });
  3841. /**
  3842. * Returns a new object that does not contain a `prop` property.
  3843. *
  3844. * @func
  3845. * @memberOf R
  3846. * @since v0.10.0
  3847. * @category Object
  3848. * @sig String -> {k: v} -> {k: v}
  3849. * @param {String} prop The name of the property to dissociate
  3850. * @param {Object} obj The object to clone
  3851. * @return {Object} A new object equivalent to the original but without the specified property
  3852. * @see R.assoc, R.omit
  3853. * @example
  3854. *
  3855. * R.dissoc('b', {a: 1, b: 2, c: 3}); //=> {a: 1, c: 3}
  3856. */
  3857. var dissoc = _curry2(function dissoc(prop, obj) {
  3858. return dissocPath([prop], obj);
  3859. });
  3860. /**
  3861. * Divides two numbers. Equivalent to `a / b`.
  3862. *
  3863. * @func
  3864. * @memberOf R
  3865. * @since v0.1.0
  3866. * @category Math
  3867. * @sig Number -> Number -> Number
  3868. * @param {Number} a The first value.
  3869. * @param {Number} b The second value.
  3870. * @return {Number} The result of `a / b`.
  3871. * @see R.multiply
  3872. * @example
  3873. *
  3874. * R.divide(71, 100); //=> 0.71
  3875. *
  3876. * const half = R.divide(R.__, 2);
  3877. * half(42); //=> 21
  3878. *
  3879. * const reciprocal = R.divide(1);
  3880. * reciprocal(4); //=> 0.25
  3881. */
  3882. var divide = _curry2(function divide(a, b) {
  3883. return a / b;
  3884. });
  3885. function XDrop(n, xf) {
  3886. this.xf = xf;
  3887. this.n = n;
  3888. }
  3889. XDrop.prototype['@@transducer/init'] = _xfBase.init;
  3890. XDrop.prototype['@@transducer/result'] = _xfBase.result;
  3891. XDrop.prototype['@@transducer/step'] = function (result, input) {
  3892. if (this.n > 0) {
  3893. this.n -= 1;
  3894. return result;
  3895. }
  3896. return this.xf['@@transducer/step'](result, input);
  3897. };
  3898. function _xdrop(n) {
  3899. return function (xf) {
  3900. return new XDrop(n, xf);
  3901. };
  3902. }
  3903. /**
  3904. * Returns all but the first `n` elements of the given list, string, or
  3905. * transducer/transformer (or object with a `drop` method).
  3906. *
  3907. * Dispatches to the `drop` method of the second argument, if present.
  3908. *
  3909. * @func
  3910. * @memberOf R
  3911. * @since v0.1.0
  3912. * @category List
  3913. * @sig Number -> [a] -> [a]
  3914. * @sig Number -> String -> String
  3915. * @param {Number} n
  3916. * @param {*} list
  3917. * @return {*} A copy of list without the first `n` elements
  3918. * @see R.take, R.transduce, R.dropLast, R.dropWhile
  3919. * @example
  3920. *
  3921. * R.drop(1, ['foo', 'bar', 'baz']); //=> ['bar', 'baz']
  3922. * R.drop(2, ['foo', 'bar', 'baz']); //=> ['baz']
  3923. * R.drop(3, ['foo', 'bar', 'baz']); //=> []
  3924. * R.drop(4, ['foo', 'bar', 'baz']); //=> []
  3925. * R.drop(3, 'ramda'); //=> 'da'
  3926. */
  3927. var drop = _curry2(_dispatchable(['drop'], _xdrop, function drop(n, xs) {
  3928. return slice(Math.max(0, n), Infinity, xs);
  3929. }));
  3930. function XTake(n, xf) {
  3931. this.xf = xf;
  3932. this.n = n;
  3933. this.i = 0;
  3934. }
  3935. XTake.prototype['@@transducer/init'] = _xfBase.init;
  3936. XTake.prototype['@@transducer/result'] = _xfBase.result;
  3937. XTake.prototype['@@transducer/step'] = function (result, input) {
  3938. this.i += 1;
  3939. var ret = this.n === 0 ? result : this.xf['@@transducer/step'](result, input);
  3940. return this.n >= 0 && this.i >= this.n ? _reduced(ret) : ret;
  3941. };
  3942. function _xtake(n) {
  3943. return function (xf) {
  3944. return new XTake(n, xf);
  3945. };
  3946. }
  3947. /**
  3948. * Returns the first `n` elements of the given list, string, or
  3949. * transducer/transformer (or object with a `take` method).
  3950. *
  3951. * Dispatches to the `take` method of the second argument, if present.
  3952. *
  3953. * @func
  3954. * @memberOf R
  3955. * @since v0.1.0
  3956. * @category List
  3957. * @sig Number -> [a] -> [a]
  3958. * @sig Number -> String -> String
  3959. * @param {Number} n
  3960. * @param {*} list
  3961. * @return {*}
  3962. * @see R.drop
  3963. * @example
  3964. *
  3965. * R.take(1, ['foo', 'bar', 'baz']); //=> ['foo']
  3966. * R.take(2, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
  3967. * R.take(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  3968. * R.take(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  3969. * R.take(3, 'ramda'); //=> 'ram'
  3970. *
  3971. * const personnel = [
  3972. * 'Dave Brubeck',
  3973. * 'Paul Desmond',
  3974. * 'Eugene Wright',
  3975. * 'Joe Morello',
  3976. * 'Gerry Mulligan',
  3977. * 'Bob Bates',
  3978. * 'Joe Dodge',
  3979. * 'Ron Crotty'
  3980. * ];
  3981. *
  3982. * const takeFive = R.take(5);
  3983. * takeFive(personnel);
  3984. * //=> ['Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan']
  3985. * @symb R.take(-1, [a, b]) = [a, b]
  3986. * @symb R.take(0, [a, b]) = []
  3987. * @symb R.take(1, [a, b]) = [a]
  3988. * @symb R.take(2, [a, b]) = [a, b]
  3989. */
  3990. var take = _curry2(_dispatchable(['take'], _xtake, function take(n, xs) {
  3991. return slice(0, n < 0 ? Infinity : n, xs);
  3992. }));
  3993. function dropLast(n, xs) {
  3994. return take(n < xs.length ? xs.length - n : 0, xs);
  3995. }
  3996. function XDropLast(n, xf) {
  3997. if (n <= 0) {
  3998. return xf;
  3999. }
  4000. this.xf = xf;
  4001. this.pos = 0;
  4002. this.full = false;
  4003. this.acc = new Array(n);
  4004. }
  4005. XDropLast.prototype['@@transducer/init'] = _xfBase.init;
  4006. XDropLast.prototype['@@transducer/result'] = function (result) {
  4007. this.acc = null;
  4008. return this.xf['@@transducer/result'](result);
  4009. };
  4010. XDropLast.prototype['@@transducer/step'] = function (result, input) {
  4011. if (this.full) {
  4012. result = this.xf['@@transducer/step'](result, this.acc[this.pos]);
  4013. }
  4014. this.store(input);
  4015. return result;
  4016. };
  4017. XDropLast.prototype.store = function (input) {
  4018. this.acc[this.pos] = input;
  4019. this.pos += 1;
  4020. if (this.pos === this.acc.length) {
  4021. this.pos = 0;
  4022. this.full = true;
  4023. }
  4024. };
  4025. function _xdropLast(n) {
  4026. return function (xf) {
  4027. return new XDropLast(n, xf);
  4028. };
  4029. }
  4030. /**
  4031. * Returns a list containing all but the last `n` elements of the given `list`.
  4032. *
  4033. * Acts as a transducer if a transformer is given in list position.
  4034. *
  4035. * @func
  4036. * @memberOf R
  4037. * @since v0.16.0
  4038. * @category List
  4039. * @sig Number -> [a] -> [a]
  4040. * @sig Number -> String -> String
  4041. * @param {Number} n The number of elements of `list` to skip.
  4042. * @param {Array} list The list of elements to consider.
  4043. * @return {Array} A copy of the list with only the first `list.length - n` elements
  4044. * @see R.takeLast, R.drop, R.dropWhile, R.dropLastWhile
  4045. * @example
  4046. *
  4047. * R.dropLast(1, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
  4048. * R.dropLast(2, ['foo', 'bar', 'baz']); //=> ['foo']
  4049. * R.dropLast(3, ['foo', 'bar', 'baz']); //=> []
  4050. * R.dropLast(4, ['foo', 'bar', 'baz']); //=> []
  4051. * R.dropLast(3, 'ramda'); //=> 'ra'
  4052. */
  4053. var dropLast$1 = _curry2(_dispatchable([], _xdropLast, dropLast));
  4054. function dropLastWhile(pred, xs) {
  4055. var idx = xs.length - 1;
  4056. while (idx >= 0 && pred(xs[idx])) {
  4057. idx -= 1;
  4058. }
  4059. return slice(0, idx + 1, xs);
  4060. }
  4061. function XDropLastWhile(fn, xf) {
  4062. this.f = fn;
  4063. this.retained = [];
  4064. this.xf = xf;
  4065. }
  4066. XDropLastWhile.prototype['@@transducer/init'] = _xfBase.init;
  4067. XDropLastWhile.prototype['@@transducer/result'] = function (result) {
  4068. this.retained = null;
  4069. return this.xf['@@transducer/result'](result);
  4070. };
  4071. XDropLastWhile.prototype['@@transducer/step'] = function (result, input) {
  4072. return this.f(input) ? this.retain(result, input) : this.flush(result, input);
  4073. };
  4074. XDropLastWhile.prototype.flush = function (result, input) {
  4075. result = _xReduce(this.xf, result, this.retained);
  4076. this.retained = [];
  4077. return this.xf['@@transducer/step'](result, input);
  4078. };
  4079. XDropLastWhile.prototype.retain = function (result, input) {
  4080. this.retained.push(input);
  4081. return result;
  4082. };
  4083. function _xdropLastWhile(fn) {
  4084. return function (xf) {
  4085. return new XDropLastWhile(fn, xf);
  4086. };
  4087. }
  4088. /**
  4089. * Returns a new list excluding all the tailing elements of a given list which
  4090. * satisfy the supplied predicate function. It passes each value from the right
  4091. * to the supplied predicate function, skipping elements until the predicate
  4092. * function returns a `falsy` value. The predicate function is applied to one argument:
  4093. * *(value)*.
  4094. *
  4095. * Acts as a transducer if a transformer is given in list position.
  4096. *
  4097. * @func
  4098. * @memberOf R
  4099. * @since v0.16.0
  4100. * @category List
  4101. * @sig (a -> Boolean) -> [a] -> [a]
  4102. * @sig (a -> Boolean) -> String -> String
  4103. * @param {Function} predicate The function to be called on each element
  4104. * @param {Array} xs The collection to iterate over.
  4105. * @return {Array} A new array without any trailing elements that return `falsy` values from the `predicate`.
  4106. * @see R.takeLastWhile, R.addIndex, R.drop, R.dropWhile
  4107. * @example
  4108. *
  4109. * const lteThree = x => x <= 3;
  4110. *
  4111. * R.dropLastWhile(lteThree, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3, 4]
  4112. *
  4113. * R.dropLastWhile(x => x !== 'd' , 'Ramda'); //=> 'Ramd'
  4114. */
  4115. var dropLastWhile$1 = _curry2(_dispatchable([], _xdropLastWhile, dropLastWhile));
  4116. function XDropRepeatsWith(pred, xf) {
  4117. this.xf = xf;
  4118. this.pred = pred;
  4119. this.lastValue = undefined;
  4120. this.seenFirstValue = false;
  4121. }
  4122. XDropRepeatsWith.prototype['@@transducer/init'] = _xfBase.init;
  4123. XDropRepeatsWith.prototype['@@transducer/result'] = _xfBase.result;
  4124. XDropRepeatsWith.prototype['@@transducer/step'] = function (result, input) {
  4125. var sameAsLast = false;
  4126. if (!this.seenFirstValue) {
  4127. this.seenFirstValue = true;
  4128. } else if (this.pred(this.lastValue, input)) {
  4129. sameAsLast = true;
  4130. }
  4131. this.lastValue = input;
  4132. return sameAsLast ? result : this.xf['@@transducer/step'](result, input);
  4133. };
  4134. function _xdropRepeatsWith(pred) {
  4135. return function (xf) {
  4136. return new XDropRepeatsWith(pred, xf);
  4137. };
  4138. }
  4139. /**
  4140. * Returns the last element of the given list or string.
  4141. *
  4142. * @func
  4143. * @memberOf R
  4144. * @since v0.1.4
  4145. * @category List
  4146. * @sig [a] -> a | Undefined
  4147. * @sig String -> String
  4148. * @param {*} list
  4149. * @return {*}
  4150. * @see R.init, R.head, R.tail
  4151. * @example
  4152. *
  4153. * R.last(['fi', 'fo', 'fum']); //=> 'fum'
  4154. * R.last([]); //=> undefined
  4155. *
  4156. * R.last('abc'); //=> 'c'
  4157. * R.last(''); //=> ''
  4158. */
  4159. var last = nth(-1);
  4160. /**
  4161. * Returns a new list without any consecutively repeating elements. Equality is
  4162. * determined by applying the supplied predicate to each pair of consecutive elements. The
  4163. * first element in a series of equal elements will be preserved.
  4164. *
  4165. * Acts as a transducer if a transformer is given in list position.
  4166. *
  4167. * @func
  4168. * @memberOf R
  4169. * @since v0.14.0
  4170. * @category List
  4171. * @sig ((a, a) -> Boolean) -> [a] -> [a]
  4172. * @param {Function} pred A predicate used to test whether two items are equal.
  4173. * @param {Array} list The array to consider.
  4174. * @return {Array} `list` without repeating elements.
  4175. * @see R.transduce
  4176. * @example
  4177. *
  4178. * const l = [1, -1, 1, 3, 4, -4, -4, -5, 5, 3, 3];
  4179. * R.dropRepeatsWith(R.eqBy(Math.abs), l); //=> [1, 3, 4, -5, 3]
  4180. */
  4181. var dropRepeatsWith = _curry2(_dispatchable([], _xdropRepeatsWith, function dropRepeatsWith(pred, list) {
  4182. var result = [];
  4183. var idx = 1;
  4184. var len = list.length;
  4185. if (len !== 0) {
  4186. result[0] = list[0];
  4187. while (idx < len) {
  4188. if (!pred(last(result), list[idx])) {
  4189. result[result.length] = list[idx];
  4190. }
  4191. idx += 1;
  4192. }
  4193. }
  4194. return result;
  4195. }));
  4196. /**
  4197. * Returns a new list without any consecutively repeating elements.
  4198. * [`R.equals`](#equals) is used to determine equality.
  4199. *
  4200. * Acts as a transducer if a transformer is given in list position.
  4201. *
  4202. * @func
  4203. * @memberOf R
  4204. * @since v0.14.0
  4205. * @category List
  4206. * @sig [a] -> [a]
  4207. * @param {Array} list The array to consider.
  4208. * @return {Array} `list` without repeating elements.
  4209. * @see R.transduce
  4210. * @example
  4211. *
  4212. * R.dropRepeats([1, 1, 1, 2, 3, 4, 4, 2, 2]); //=> [1, 2, 3, 4, 2]
  4213. */
  4214. var dropRepeats = _curry1(_dispatchable([], function () {
  4215. return _xdropRepeatsWith(equals);
  4216. }, dropRepeatsWith(equals)));
  4217. /**
  4218. * Takes a function and two values in its domain and returns `true` if the
  4219. * values map to the same value in the codomain; `false` otherwise.
  4220. *
  4221. * @func
  4222. * @memberOf R
  4223. * @since v0.18.0
  4224. * @category Relation
  4225. * @sig (a -> b) -> a -> a -> Boolean
  4226. * @param {Function} f
  4227. * @param {*} x
  4228. * @param {*} y
  4229. * @return {Boolean}
  4230. * @example
  4231. *
  4232. * R.eqBy(Math.abs, 5, -5); //=> true
  4233. */
  4234. var eqBy = _curry3(function eqBy(f, x, y) {
  4235. return equals(f(x), f(y));
  4236. });
  4237. /**
  4238. * Returns a new list without any consecutively repeating elements,
  4239. * based upon the value returned by applying the supplied function to
  4240. * each list element. [`R.equals`](#equals) is used to determine equality.
  4241. *
  4242. * Acts as a transducer if a transformer is given in list position.
  4243. *
  4244. * @func
  4245. * @memberOf R
  4246. * @since v0.29.0
  4247. * @category List
  4248. * @sig (a -> b) -> [a] -> [a]
  4249. * @param {Function} fn A function used to produce a value to use during comparisons.
  4250. * @param {Array} list The array to consider.
  4251. * @return {Array} `list` without repeating elements.
  4252. * @see R.transduce
  4253. * @example
  4254. *
  4255. * R.dropRepeatsBy(Math.abs, [1, -1, -1, 2, 3, -4, 4, 2, 2]); //=> [1, 2, 3, -4, 2]
  4256. */
  4257. var dropRepeatsBy = _curry2(function (fn, list) {
  4258. return _dispatchable([], function () {
  4259. return _xdropRepeatsWith(eqBy(fn));
  4260. }, dropRepeatsWith(eqBy(fn)))(list);
  4261. });
  4262. function XDropWhile(f, xf) {
  4263. this.xf = xf;
  4264. this.f = f;
  4265. }
  4266. XDropWhile.prototype['@@transducer/init'] = _xfBase.init;
  4267. XDropWhile.prototype['@@transducer/result'] = _xfBase.result;
  4268. XDropWhile.prototype['@@transducer/step'] = function (result, input) {
  4269. if (this.f) {
  4270. if (this.f(input)) {
  4271. return result;
  4272. }
  4273. this.f = null;
  4274. }
  4275. return this.xf['@@transducer/step'](result, input);
  4276. };
  4277. function _xdropWhile(f) {
  4278. return function (xf) {
  4279. return new XDropWhile(f, xf);
  4280. };
  4281. }
  4282. /**
  4283. * Returns a new list excluding the leading elements of a given list which
  4284. * satisfy the supplied predicate function. It passes each value to the supplied
  4285. * predicate function, skipping elements while the predicate function returns
  4286. * `true`. The predicate function is applied to one argument: *(value)*.
  4287. *
  4288. * Dispatches to the `dropWhile` method of the second argument, if present.
  4289. *
  4290. * Acts as a transducer if a transformer is given in list position.
  4291. *
  4292. * @func
  4293. * @memberOf R
  4294. * @since v0.9.0
  4295. * @category List
  4296. * @sig (a -> Boolean) -> [a] -> [a]
  4297. * @sig (a -> Boolean) -> String -> String
  4298. * @param {Function} fn The function called per iteration.
  4299. * @param {Array} xs The collection to iterate over.
  4300. * @return {Array} A new array.
  4301. * @see R.takeWhile, R.transduce, R.addIndex
  4302. * @example
  4303. *
  4304. * const lteTwo = x => x <= 2;
  4305. *
  4306. * R.dropWhile(lteTwo, [1, 2, 3, 4, 3, 2, 1]); //=> [3, 4, 3, 2, 1]
  4307. *
  4308. * R.dropWhile(x => x !== 'd' , 'Ramda'); //=> 'da'
  4309. */
  4310. var dropWhile = _curry2(_dispatchable(['dropWhile'], _xdropWhile, function dropWhile(pred, xs) {
  4311. var idx = 0;
  4312. var len = xs.length;
  4313. while (idx < len && pred(xs[idx])) {
  4314. idx += 1;
  4315. }
  4316. return slice(idx, Infinity, xs);
  4317. }));
  4318. /**
  4319. * Returns the first argument if it is truthy, otherwise the second argument.
  4320. * Acts as the boolean `or` statement if both inputs are `Boolean`s.
  4321. *
  4322. * @func
  4323. * @memberOf R
  4324. * @since v0.1.0
  4325. * @category Logic
  4326. * @sig a -> b -> a | b
  4327. * @param {Any} a
  4328. * @param {Any} b
  4329. * @return {Any}
  4330. * @see R.either, R.and
  4331. * @example
  4332. *
  4333. * R.or(true, true); //=> true
  4334. * R.or(true, false); //=> true
  4335. * R.or(false, true); //=> true
  4336. * R.or(false, false); //=> false
  4337. */
  4338. var or = _curry2(function or(a, b) {
  4339. return a || b;
  4340. });
  4341. /**
  4342. * A function wrapping calls to the two functions in an `||` operation,
  4343. * returning the result of the first function if it is truth-y and the result
  4344. * of the second function otherwise. Note that this is short-circuited,
  4345. * meaning that the second function will not be invoked if the first returns a
  4346. * truth-y value.
  4347. *
  4348. * In addition to functions, `R.either` also accepts any fantasy-land compatible
  4349. * applicative functor.
  4350. *
  4351. * @func
  4352. * @memberOf R
  4353. * @since v0.12.0
  4354. * @category Logic
  4355. * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
  4356. * @param {Function} f a predicate
  4357. * @param {Function} g another predicate
  4358. * @return {Function} a function that applies its arguments to `f` and `g` and `||`s their outputs together.
  4359. * @see R.both, R.anyPass, R.or
  4360. * @example
  4361. *
  4362. * const gt10 = x => x > 10;
  4363. * const even = x => x % 2 === 0;
  4364. * const f = R.either(gt10, even);
  4365. * f(101); //=> true
  4366. * f(8); //=> true
  4367. *
  4368. * R.either(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(55)
  4369. * R.either([false, false, 'a'], [11]) // => [11, 11, "a"]
  4370. */
  4371. var either = _curry2(function either(f, g) {
  4372. return _isFunction(f) ? function _either() {
  4373. return f.apply(this, arguments) || g.apply(this, arguments);
  4374. } : lift(or)(f, g);
  4375. });
  4376. /**
  4377. * Tests whether or not an object is a typed array.
  4378. *
  4379. * @private
  4380. * @param {*} val The object to test.
  4381. * @return {Boolean} `true` if `val` is a typed array, `false` otherwise.
  4382. * @example
  4383. *
  4384. * _isTypedArray(new Uint8Array([])); //=> true
  4385. * _isTypedArray(new Float32Array([])); //=> true
  4386. * _isTypedArray([]); //=> false
  4387. * _isTypedArray(null); //=> false
  4388. * _isTypedArray({}); //=> false
  4389. */
  4390. function _isTypedArray(val) {
  4391. var type = Object.prototype.toString.call(val);
  4392. return type === '[object Uint8ClampedArray]' || type === '[object Int8Array]' || type === '[object Uint8Array]' || type === '[object Int16Array]' || type === '[object Uint16Array]' || type === '[object Int32Array]' || type === '[object Uint32Array]' || type === '[object Float32Array]' || type === '[object Float64Array]' || type === '[object BigInt64Array]' || type === '[object BigUint64Array]';
  4393. }
  4394. /**
  4395. * Returns the empty value of its argument's type. Ramda defines the empty
  4396. * value of Array (`[]`), Object (`{}`), String (`''`),
  4397. * TypedArray (`Uint8Array []`, `Float32Array []`, etc), and Arguments. Other
  4398. * types are supported if they define `<Type>.empty`,
  4399. * `<Type>.prototype.empty` or implement the
  4400. * [FantasyLand Monoid spec](https://github.com/fantasyland/fantasy-land#monoid).
  4401. *
  4402. * Dispatches to the `empty` method of the first argument, if present.
  4403. *
  4404. * @func
  4405. * @memberOf R
  4406. * @since v0.3.0
  4407. * @category Function
  4408. * @sig a -> a
  4409. * @param {*} x
  4410. * @return {*}
  4411. * @example
  4412. *
  4413. * R.empty(Just(42)); //=> Nothing()
  4414. * R.empty([1, 2, 3]); //=> []
  4415. * R.empty('unicorns'); //=> ''
  4416. * R.empty({x: 1, y: 2}); //=> {}
  4417. * R.empty(Uint8Array.from('123')); //=> Uint8Array []
  4418. */
  4419. var empty = _curry1(function empty(x) {
  4420. 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 () {
  4421. return arguments;
  4422. }() : _isTypedArray(x) ? x.constructor.from('') : void 0 // else
  4423. ;
  4424. });
  4425. /**
  4426. * Returns a new list containing the last `n` elements of the given list.
  4427. * If `n > list.length`, returns a list of `list.length` elements.
  4428. *
  4429. * @func
  4430. * @memberOf R
  4431. * @since v0.16.0
  4432. * @category List
  4433. * @sig Number -> [a] -> [a]
  4434. * @sig Number -> String -> String
  4435. * @param {Number} n The number of elements to return.
  4436. * @param {Array} xs The collection to consider.
  4437. * @return {Array}
  4438. * @see R.dropLast
  4439. * @example
  4440. *
  4441. * R.takeLast(1, ['foo', 'bar', 'baz']); //=> ['baz']
  4442. * R.takeLast(2, ['foo', 'bar', 'baz']); //=> ['bar', 'baz']
  4443. * R.takeLast(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  4444. * R.takeLast(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  4445. * R.takeLast(3, 'ramda'); //=> 'mda'
  4446. */
  4447. var takeLast = _curry2(function takeLast(n, xs) {
  4448. return drop(n >= 0 ? xs.length - n : 0, xs);
  4449. });
  4450. /**
  4451. * Checks if a list ends with the provided sublist.
  4452. *
  4453. * Similarly, checks if a string ends with the provided substring.
  4454. *
  4455. * @func
  4456. * @memberOf R
  4457. * @since v0.24.0
  4458. * @category List
  4459. * @sig [a] -> [a] -> Boolean
  4460. * @sig String -> String -> Boolean
  4461. * @param {*} suffix
  4462. * @param {*} list
  4463. * @return {Boolean}
  4464. * @see R.startsWith
  4465. * @example
  4466. *
  4467. * R.endsWith('c', 'abc') //=> true
  4468. * R.endsWith('b', 'abc') //=> false
  4469. * R.endsWith(['c'], ['a', 'b', 'c']) //=> true
  4470. * R.endsWith(['b'], ['a', 'b', 'c']) //=> false
  4471. */
  4472. var endsWith = _curry2(function (suffix, list) {
  4473. return equals(takeLast(suffix.length, list), suffix);
  4474. });
  4475. /**
  4476. * Reports whether two objects have the same value, in [`R.equals`](#equals)
  4477. * terms, for the specified property. Useful as a curried predicate.
  4478. *
  4479. * @func
  4480. * @memberOf R
  4481. * @since v0.1.0
  4482. * @category Object
  4483. * @sig k -> {k: v} -> {k: v} -> Boolean
  4484. * @param {String} prop The name of the property to compare
  4485. * @param {Object} obj1
  4486. * @param {Object} obj2
  4487. * @return {Boolean}
  4488. *
  4489. * @example
  4490. *
  4491. * const o1 = { a: 1, b: 2, c: 3, d: 4 };
  4492. * const o2 = { a: 10, b: 20, c: 3, d: 40 };
  4493. * R.eqProps('a', o1, o2); //=> false
  4494. * R.eqProps('c', o1, o2); //=> true
  4495. */
  4496. var eqProps = _curry3(function eqProps(prop, obj1, obj2) {
  4497. return equals(obj1[prop], obj2[prop]);
  4498. });
  4499. /**
  4500. * Creates a new object by recursively evolving a shallow copy of `object`,
  4501. * according to the `transformation` functions. All non-primitive properties
  4502. * are copied by reference.
  4503. *
  4504. * A `transformation` function will not be invoked if its corresponding key
  4505. * does not exist in the evolved object.
  4506. *
  4507. * @func
  4508. * @memberOf R
  4509. * @since v0.9.0
  4510. * @category Object
  4511. * @sig {k: (v -> v)} -> {k: v} -> {k: v}
  4512. * @param {Object} transformations The object specifying transformation functions to apply
  4513. * to the object.
  4514. * @param {Object} object The object to be transformed.
  4515. * @return {Object} The transformed object.
  4516. * @example
  4517. *
  4518. * const tomato = {firstName: ' Tomato ', data: {elapsed: 100, remaining: 1400}, id:123};
  4519. * const transformations = {
  4520. * firstName: R.trim,
  4521. * lastName: R.trim, // Will not get invoked.
  4522. * data: {elapsed: R.add(1), remaining: R.add(-1)}
  4523. * };
  4524. * R.evolve(transformations, tomato); //=> {firstName: 'Tomato', data: {elapsed: 101, remaining: 1399}, id:123}
  4525. */
  4526. var evolve = _curry2(function evolve(transformations, object) {
  4527. if (!_isObject(object) && !_isArray(object)) {
  4528. return object;
  4529. }
  4530. var result = object instanceof Array ? [] : {};
  4531. var transformation, key, type;
  4532. for (key in object) {
  4533. transformation = transformations[key];
  4534. type = _typeof(transformation);
  4535. result[key] = type === 'function' ? transformation(object[key]) : transformation && type === 'object' ? evolve(transformation, object[key]) : object[key];
  4536. }
  4537. return result;
  4538. });
  4539. function XFind(f, xf) {
  4540. this.xf = xf;
  4541. this.f = f;
  4542. this.found = false;
  4543. }
  4544. XFind.prototype['@@transducer/init'] = _xfBase.init;
  4545. XFind.prototype['@@transducer/result'] = function (result) {
  4546. if (!this.found) {
  4547. result = this.xf['@@transducer/step'](result, void 0);
  4548. }
  4549. return this.xf['@@transducer/result'](result);
  4550. };
  4551. XFind.prototype['@@transducer/step'] = function (result, input) {
  4552. if (this.f(input)) {
  4553. this.found = true;
  4554. result = _reduced(this.xf['@@transducer/step'](result, input));
  4555. }
  4556. return result;
  4557. };
  4558. function _xfind(f) {
  4559. return function (xf) {
  4560. return new XFind(f, xf);
  4561. };
  4562. }
  4563. /**
  4564. * Returns the first element of the list which matches the predicate, or
  4565. * `undefined` if no element matches.
  4566. *
  4567. * Dispatches to the `find` method of the second argument, if present.
  4568. *
  4569. * Acts as a transducer if a transformer is given in list position.
  4570. *
  4571. * @func
  4572. * @memberOf R
  4573. * @since v0.1.0
  4574. * @category List
  4575. * @sig (a -> Boolean) -> [a] -> a | undefined
  4576. * @param {Function} fn The predicate function used to determine if the element is the
  4577. * desired one.
  4578. * @param {Array} list The array to consider.
  4579. * @return {Object} The element found, or `undefined`.
  4580. * @see R.transduce
  4581. * @example
  4582. *
  4583. * const xs = [{a: 1}, {a: 2}, {a: 3}];
  4584. * R.find(R.propEq('a', 2))(xs); //=> {a: 2}
  4585. * R.find(R.propEq('a', 4))(xs); //=> undefined
  4586. */
  4587. var find = _curry2(_dispatchable(['find'], _xfind, function find(fn, list) {
  4588. var idx = 0;
  4589. var len = list.length;
  4590. while (idx < len) {
  4591. if (fn(list[idx])) {
  4592. return list[idx];
  4593. }
  4594. idx += 1;
  4595. }
  4596. }));
  4597. function XFindIndex(f, xf) {
  4598. this.xf = xf;
  4599. this.f = f;
  4600. this.idx = -1;
  4601. this.found = false;
  4602. }
  4603. XFindIndex.prototype['@@transducer/init'] = _xfBase.init;
  4604. XFindIndex.prototype['@@transducer/result'] = function (result) {
  4605. if (!this.found) {
  4606. result = this.xf['@@transducer/step'](result, -1);
  4607. }
  4608. return this.xf['@@transducer/result'](result);
  4609. };
  4610. XFindIndex.prototype['@@transducer/step'] = function (result, input) {
  4611. this.idx += 1;
  4612. if (this.f(input)) {
  4613. this.found = true;
  4614. result = _reduced(this.xf['@@transducer/step'](result, this.idx));
  4615. }
  4616. return result;
  4617. };
  4618. function _xfindIndex(f) {
  4619. return function (xf) {
  4620. return new XFindIndex(f, xf);
  4621. };
  4622. }
  4623. /**
  4624. * Returns the index of the first element of the list which matches the
  4625. * predicate, or `-1` if no element matches.
  4626. *
  4627. * Acts as a transducer if a transformer is given in list position.
  4628. *
  4629. * @func
  4630. * @memberOf R
  4631. * @since v0.1.1
  4632. * @category List
  4633. * @sig (a -> Boolean) -> [a] -> Number
  4634. * @param {Function} fn The predicate function used to determine if the element is the
  4635. * desired one.
  4636. * @param {Array} list The array to consider.
  4637. * @return {Number} The index of the element found, or `-1`.
  4638. * @see R.transduce, R.indexOf
  4639. * @example
  4640. *
  4641. * const xs = [{a: 1}, {a: 2}, {a: 3}];
  4642. * R.findIndex(R.propEq('a', 2))(xs); //=> 1
  4643. * R.findIndex(R.propEq('a', 4))(xs); //=> -1
  4644. */
  4645. var findIndex = _curry2(_dispatchable([], _xfindIndex, function findIndex(fn, list) {
  4646. var idx = 0;
  4647. var len = list.length;
  4648. while (idx < len) {
  4649. if (fn(list[idx])) {
  4650. return idx;
  4651. }
  4652. idx += 1;
  4653. }
  4654. return -1;
  4655. }));
  4656. function XFindLast(f, xf) {
  4657. this.xf = xf;
  4658. this.f = f;
  4659. }
  4660. XFindLast.prototype['@@transducer/init'] = _xfBase.init;
  4661. XFindLast.prototype['@@transducer/result'] = function (result) {
  4662. return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.last));
  4663. };
  4664. XFindLast.prototype['@@transducer/step'] = function (result, input) {
  4665. if (this.f(input)) {
  4666. this.last = input;
  4667. }
  4668. return result;
  4669. };
  4670. function _xfindLast(f) {
  4671. return function (xf) {
  4672. return new XFindLast(f, xf);
  4673. };
  4674. }
  4675. /**
  4676. * Returns the last element of the list which matches the predicate, or
  4677. * `undefined` if no element matches.
  4678. *
  4679. * Acts as a transducer if a transformer is given in list position.
  4680. *
  4681. * @func
  4682. * @memberOf R
  4683. * @since v0.1.1
  4684. * @category List
  4685. * @sig (a -> Boolean) -> [a] -> a | undefined
  4686. * @param {Function} fn The predicate function used to determine if the element is the
  4687. * desired one.
  4688. * @param {Array} list The array to consider.
  4689. * @return {Object} The element found, or `undefined`.
  4690. * @see R.transduce
  4691. * @example
  4692. *
  4693. * const xs = [{a: 1, b: 0}, {a:1, b: 1}];
  4694. * R.findLast(R.propEq('a', 1))(xs); //=> {a: 1, b: 1}
  4695. * R.findLast(R.propEq('a', 4))(xs); //=> undefined
  4696. */
  4697. var findLast = _curry2(_dispatchable([], _xfindLast, function findLast(fn, list) {
  4698. var idx = list.length - 1;
  4699. while (idx >= 0) {
  4700. if (fn(list[idx])) {
  4701. return list[idx];
  4702. }
  4703. idx -= 1;
  4704. }
  4705. }));
  4706. function XFindLastIndex(f, xf) {
  4707. this.xf = xf;
  4708. this.f = f;
  4709. this.idx = -1;
  4710. this.lastIdx = -1;
  4711. }
  4712. XFindLastIndex.prototype['@@transducer/init'] = _xfBase.init;
  4713. XFindLastIndex.prototype['@@transducer/result'] = function (result) {
  4714. return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.lastIdx));
  4715. };
  4716. XFindLastIndex.prototype['@@transducer/step'] = function (result, input) {
  4717. this.idx += 1;
  4718. if (this.f(input)) {
  4719. this.lastIdx = this.idx;
  4720. }
  4721. return result;
  4722. };
  4723. function _xfindLastIndex(f) {
  4724. return function (xf) {
  4725. return new XFindLastIndex(f, xf);
  4726. };
  4727. }
  4728. /**
  4729. * Returns the index of the last element of the list which matches the
  4730. * predicate, or `-1` if no element matches.
  4731. *
  4732. * Acts as a transducer if a transformer is given in list position.
  4733. *
  4734. * @func
  4735. * @memberOf R
  4736. * @since v0.1.1
  4737. * @category List
  4738. * @sig (a -> Boolean) -> [a] -> Number
  4739. * @param {Function} fn The predicate function used to determine if the element is the
  4740. * desired one.
  4741. * @param {Array} list The array to consider.
  4742. * @return {Number} The index of the element found, or `-1`.
  4743. * @see R.transduce, R.lastIndexOf
  4744. * @example
  4745. *
  4746. * const xs = [{a: 1, b: 0}, {a:1, b: 1}];
  4747. * R.findLastIndex(R.propEq('a', 1))(xs); //=> 1
  4748. * R.findLastIndex(R.propEq('a', 4))(xs); //=> -1
  4749. */
  4750. var findLastIndex = _curry2(_dispatchable([], _xfindLastIndex, function findLastIndex(fn, list) {
  4751. var idx = list.length - 1;
  4752. while (idx >= 0) {
  4753. if (fn(list[idx])) {
  4754. return idx;
  4755. }
  4756. idx -= 1;
  4757. }
  4758. return -1;
  4759. }));
  4760. /**
  4761. * Returns a new list by pulling every item out of it (and all its sub-arrays)
  4762. * and putting them in a new array, depth-first.
  4763. *
  4764. * @func
  4765. * @memberOf R
  4766. * @since v0.1.0
  4767. * @category List
  4768. * @sig [a] -> [b]
  4769. * @param {Array} list The array to consider.
  4770. * @return {Array} The flattened list.
  4771. * @see R.unnest
  4772. * @example
  4773. *
  4774. * R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
  4775. * //=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  4776. */
  4777. var flatten = _curry1(_makeFlat(true));
  4778. /**
  4779. * Returns a new function much like the supplied one, except that the first two
  4780. * arguments' order is reversed.
  4781. *
  4782. * @func
  4783. * @memberOf R
  4784. * @since v0.1.0
  4785. * @category Function
  4786. * @sig ((a, b, c, ...) -> z) -> (b -> a -> c -> ... -> z)
  4787. * @param {Function} fn The function to invoke with its first two parameters reversed.
  4788. * @return {*} The result of invoking `fn` with its first two parameters' order reversed.
  4789. * @example
  4790. *
  4791. * const mergeThree = (a, b, c) => [].concat(a, b, c);
  4792. *
  4793. * mergeThree(1, 2, 3); //=> [1, 2, 3]
  4794. *
  4795. * R.flip(mergeThree)(1, 2, 3); //=> [2, 1, 3]
  4796. * @symb R.flip(f)(a, b, c) = f(b, a, c)
  4797. */
  4798. var flip = _curry1(function flip(fn) {
  4799. return curryN(fn.length, function (a, b) {
  4800. var args = Array.prototype.slice.call(arguments, 0);
  4801. args[0] = b;
  4802. args[1] = a;
  4803. return fn.apply(this, args);
  4804. });
  4805. });
  4806. /**
  4807. * Iterate over an input `list`, calling a provided function `fn` for each
  4808. * element in the list.
  4809. *
  4810. * `fn` receives one argument: *(value)*.
  4811. *
  4812. * Note: `R.forEach` does not skip deleted or unassigned indices (sparse
  4813. * arrays), unlike the native `Array.prototype.forEach` method. For more
  4814. * details on this behavior, see:
  4815. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description
  4816. *
  4817. * Also note that, unlike `Array.prototype.forEach`, Ramda's `forEach` returns
  4818. * the original array. In some libraries this function is named `each`.
  4819. *
  4820. * Dispatches to the `forEach` method of the second argument, if present.
  4821. *
  4822. * @func
  4823. * @memberOf R
  4824. * @since v0.1.1
  4825. * @category List
  4826. * @sig (a -> *) -> [a] -> [a]
  4827. * @param {Function} fn The function to invoke. Receives one argument, `value`.
  4828. * @param {Array} list The list to iterate over.
  4829. * @return {Array} The original list.
  4830. * @see R.addIndex
  4831. * @example
  4832. *
  4833. * const printXPlusFive = x => console.log(x + 5);
  4834. * R.forEach(printXPlusFive, [1, 2, 3]); //=> [1, 2, 3]
  4835. * // logs 6
  4836. * // logs 7
  4837. * // logs 8
  4838. * @symb R.forEach(f, [a, b, c]) = [a, b, c]
  4839. */
  4840. var forEach = _curry2(_checkForMethod('forEach', function forEach(fn, list) {
  4841. var len = list.length;
  4842. var idx = 0;
  4843. while (idx < len) {
  4844. fn(list[idx]);
  4845. idx += 1;
  4846. }
  4847. return list;
  4848. }));
  4849. /**
  4850. * Iterate over an input `object`, calling a provided function `fn` for each
  4851. * key and value in the object.
  4852. *
  4853. * `fn` receives three argument: *(value, key, obj)*.
  4854. *
  4855. * @func
  4856. * @memberOf R
  4857. * @since v0.23.0
  4858. * @category Object
  4859. * @sig ((a, String, StrMap a) -> Any) -> StrMap a -> StrMap a
  4860. * @param {Function} fn The function to invoke. Receives three argument, `value`, `key`, `obj`.
  4861. * @param {Object} obj The object to iterate over.
  4862. * @return {Object} The original object.
  4863. * @example
  4864. *
  4865. * const printKeyConcatValue = (value, key) => console.log(key + ':' + value);
  4866. * R.forEachObjIndexed(printKeyConcatValue, {x: 1, y: 2}); //=> {x: 1, y: 2}
  4867. * // logs x:1
  4868. * // logs y:2
  4869. * @symb R.forEachObjIndexed(f, {x: a, y: b}) = {x: a, y: b}
  4870. */
  4871. var forEachObjIndexed = _curry2(function forEachObjIndexed(fn, obj) {
  4872. var keyList = keys(obj);
  4873. var idx = 0;
  4874. while (idx < keyList.length) {
  4875. var key = keyList[idx];
  4876. fn(obj[key], key, obj);
  4877. idx += 1;
  4878. }
  4879. return obj;
  4880. });
  4881. /**
  4882. * Creates a new object from a list key-value pairs. If a key appears in
  4883. * multiple pairs, the rightmost pair is included in the object.
  4884. *
  4885. * @func
  4886. * @memberOf R
  4887. * @since v0.3.0
  4888. * @category List
  4889. * @sig [[k,v]] -> {k: v}
  4890. * @param {Array} pairs An array of two-element arrays that will be the keys and values of the output object.
  4891. * @return {Object} The object made by pairing up `keys` and `values`.
  4892. * @see R.toPairs, R.pair
  4893. * @example
  4894. *
  4895. * R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3}
  4896. */
  4897. var fromPairs = _curry1(function fromPairs(pairs) {
  4898. var result = {};
  4899. var idx = 0;
  4900. while (idx < pairs.length) {
  4901. result[pairs[idx][0]] = pairs[idx][1];
  4902. idx += 1;
  4903. }
  4904. return result;
  4905. });
  4906. /**
  4907. * Splits a list into sub-lists stored in an object, based on the result of
  4908. * calling a key-returning function on each element, and grouping the
  4909. * results according to values returned.
  4910. *
  4911. * Dispatches to the `groupBy` method of the second argument, if present.
  4912. *
  4913. * Acts as a transducer if a transformer is given in list position.
  4914. *
  4915. * @func
  4916. * @memberOf R
  4917. * @since v0.1.0
  4918. * @category List
  4919. * @typedefn Idx = String | Int | Symbol
  4920. * @sig Idx a => (b -> a) -> [b] -> {a: [b]}
  4921. * @param {Function} fn Function :: a -> Idx
  4922. * @param {Array} list The array to group
  4923. * @return {Object} An object with the output of `fn` for keys, mapped to arrays of elements
  4924. * that produced that key when passed to `fn`.
  4925. * @see R.reduceBy, R.transduce, R.indexBy, R.collectBy
  4926. * @example
  4927. *
  4928. * const byGrade = R.groupBy(function(student) {
  4929. * const score = student.score;
  4930. * return score < 65 ? 'F' :
  4931. * score < 70 ? 'D' :
  4932. * score < 80 ? 'C' :
  4933. * score < 90 ? 'B' : 'A';
  4934. * });
  4935. * const students = [{name: 'Abby', score: 84},
  4936. * {name: 'Eddy', score: 58},
  4937. * // ...
  4938. * {name: 'Jack', score: 69}];
  4939. * byGrade(students);
  4940. * // {
  4941. * // 'A': [{name: 'Dianne', score: 99}],
  4942. * // 'B': [{name: 'Abby', score: 84}]
  4943. * // // ...,
  4944. * // 'F': [{name: 'Eddy', score: 58}]
  4945. * // }
  4946. */
  4947. var groupBy = _curry2(_checkForMethod('groupBy', reduceBy(function (acc, item) {
  4948. acc.push(item);
  4949. return acc;
  4950. }, [])));
  4951. /**
  4952. * Takes a list and returns a list of lists where each sublist's elements are
  4953. * all satisfied pairwise comparison according to the provided function.
  4954. * Only adjacent elements are passed to the comparison function.
  4955. *
  4956. * @func
  4957. * @memberOf R
  4958. * @since v0.21.0
  4959. * @category List
  4960. * @sig ((a, a) → Boolean) → [a] → [[a]]
  4961. * @param {Function} fn Function for determining whether two given (adjacent)
  4962. * elements should be in the same group
  4963. * @param {Array} list The array to group. Also accepts a string, which will be
  4964. * treated as a list of characters.
  4965. * @return {List} A list that contains sublists of elements,
  4966. * whose concatenations are equal to the original list.
  4967. * @example
  4968. *
  4969. * R.groupWith(R.equals, [0, 1, 1, 2, 3, 5, 8, 13, 21])
  4970. * //=> [[0], [1, 1], [2], [3], [5], [8], [13], [21]]
  4971. *
  4972. * R.groupWith((a, b) => a + 1 === b, [0, 1, 1, 2, 3, 5, 8, 13, 21])
  4973. * //=> [[0, 1], [1, 2, 3], [5], [8], [13], [21]]
  4974. *
  4975. * R.groupWith((a, b) => a % 2 === b % 2, [0, 1, 1, 2, 3, 5, 8, 13, 21])
  4976. * //=> [[0], [1, 1], [2], [3, 5], [8], [13, 21]]
  4977. *
  4978. * const isVowel = R.test(/^[aeiou]$/i);
  4979. * R.groupWith(R.eqBy(isVowel), 'aestiou')
  4980. * //=> ['ae', 'st', 'iou']
  4981. */
  4982. var groupWith = _curry2(function (fn, list) {
  4983. var res = [];
  4984. var idx = 0;
  4985. var len = list.length;
  4986. while (idx < len) {
  4987. var nextidx = idx + 1;
  4988. while (nextidx < len && fn(list[nextidx - 1], list[nextidx])) {
  4989. nextidx += 1;
  4990. }
  4991. res.push(list.slice(idx, nextidx));
  4992. idx = nextidx;
  4993. }
  4994. return res;
  4995. });
  4996. /**
  4997. * Returns `true` if the first argument is greater than the second; `false`
  4998. * otherwise.
  4999. *
  5000. * @func
  5001. * @memberOf R
  5002. * @since v0.1.0
  5003. * @category Relation
  5004. * @sig Ord a => a -> a -> Boolean
  5005. * @param {*} a
  5006. * @param {*} b
  5007. * @return {Boolean}
  5008. * @see R.lt
  5009. * @example
  5010. *
  5011. * R.gt(2, 1); //=> true
  5012. * R.gt(2, 2); //=> false
  5013. * R.gt(2, 3); //=> false
  5014. * R.gt('a', 'z'); //=> false
  5015. * R.gt('z', 'a'); //=> true
  5016. */
  5017. var gt = _curry2(function gt(a, b) {
  5018. return a > b;
  5019. });
  5020. /**
  5021. * Returns `true` if the first argument is greater than or equal to the second;
  5022. * `false` otherwise.
  5023. *
  5024. * @func
  5025. * @memberOf R
  5026. * @since v0.1.0
  5027. * @category Relation
  5028. * @sig Ord a => a -> a -> Boolean
  5029. * @param {Number} a
  5030. * @param {Number} b
  5031. * @return {Boolean}
  5032. * @see R.lte
  5033. * @example
  5034. *
  5035. * R.gte(2, 1); //=> true
  5036. * R.gte(2, 2); //=> true
  5037. * R.gte(2, 3); //=> false
  5038. * R.gte('a', 'z'); //=> false
  5039. * R.gte('z', 'a'); //=> true
  5040. */
  5041. var gte = _curry2(function gte(a, b) {
  5042. return a >= b;
  5043. });
  5044. /**
  5045. * Returns whether or not a path exists in an object. Only the object's
  5046. * own properties are checked.
  5047. *
  5048. * @func
  5049. * @memberOf R
  5050. * @since v0.26.0
  5051. * @category Object
  5052. * @typedefn Idx = String | Int | Symbol
  5053. * @sig [Idx] -> {a} -> Boolean
  5054. * @param {Array} path The path to use.
  5055. * @param {Object} obj The object to check the path in.
  5056. * @return {Boolean} Whether the path exists.
  5057. * @see R.has
  5058. * @example
  5059. *
  5060. * R.hasPath(['a', 'b'], {a: {b: 2}}); // => true
  5061. * R.hasPath(['a', 'b'], {a: {b: undefined}}); // => true
  5062. * R.hasPath(['a', 'b'], {a: {c: 2}}); // => false
  5063. * R.hasPath(['a', 'b'], {}); // => false
  5064. */
  5065. var hasPath = _curry2(function hasPath(_path, obj) {
  5066. if (_path.length === 0 || isNil(obj)) {
  5067. return false;
  5068. }
  5069. var val = obj;
  5070. var idx = 0;
  5071. while (idx < _path.length) {
  5072. if (!isNil(val) && _has(_path[idx], val)) {
  5073. val = val[_path[idx]];
  5074. idx += 1;
  5075. } else {
  5076. return false;
  5077. }
  5078. }
  5079. return true;
  5080. });
  5081. /**
  5082. * Returns whether or not an object has an own property with the specified name
  5083. *
  5084. * @func
  5085. * @memberOf R
  5086. * @since v0.7.0
  5087. * @category Object
  5088. * @sig s -> {s: x} -> Boolean
  5089. * @param {String} prop The name of the property to check for.
  5090. * @param {Object} obj The object to query.
  5091. * @return {Boolean} Whether the property exists.
  5092. * @example
  5093. *
  5094. * const hasName = R.has('name');
  5095. * hasName({name: 'alice'}); //=> true
  5096. * hasName({name: 'bob'}); //=> true
  5097. * hasName({}); //=> false
  5098. *
  5099. * const point = {x: 0, y: 0};
  5100. * const pointHas = R.has(R.__, point);
  5101. * pointHas('x'); //=> true
  5102. * pointHas('y'); //=> true
  5103. * pointHas('z'); //=> false
  5104. */
  5105. var has = _curry2(function has(prop, obj) {
  5106. return hasPath([prop], obj);
  5107. });
  5108. /**
  5109. * Returns whether or not an object or its prototype chain has a property with
  5110. * the specified name
  5111. *
  5112. * @func
  5113. * @memberOf R
  5114. * @since v0.7.0
  5115. * @category Object
  5116. * @sig s -> {s: x} -> Boolean
  5117. * @param {String} prop The name of the property to check for.
  5118. * @param {Object} obj The object to query.
  5119. * @return {Boolean} Whether the property exists.
  5120. * @example
  5121. *
  5122. * function Rectangle(width, height) {
  5123. * this.width = width;
  5124. * this.height = height;
  5125. * }
  5126. * Rectangle.prototype.area = function() {
  5127. * return this.width * this.height;
  5128. * };
  5129. *
  5130. * const square = new Rectangle(2, 2);
  5131. * R.hasIn('width', square); //=> true
  5132. * R.hasIn('area', square); //=> true
  5133. */
  5134. var hasIn = _curry2(function hasIn(prop, obj) {
  5135. if (isNil(obj)) {
  5136. return false;
  5137. }
  5138. return prop in obj;
  5139. });
  5140. /**
  5141. * Returns true if its arguments are identical, false otherwise. Values are
  5142. * identical if they reference the same memory. `NaN` is identical to `NaN`;
  5143. * `0` and `-0` are not identical.
  5144. *
  5145. * Note this is merely a curried version of ES6 `Object.is`.
  5146. *
  5147. * `identical` does not support the `__` placeholder.
  5148. *
  5149. * @func
  5150. * @memberOf R
  5151. * @since v0.15.0
  5152. * @category Relation
  5153. * @sig a -> a -> Boolean
  5154. * @param {*} a
  5155. * @param {*} b
  5156. * @return {Boolean}
  5157. * @example
  5158. *
  5159. * const o = {};
  5160. * R.identical(o, o); //=> true
  5161. * R.identical(1, 1); //=> true
  5162. * R.identical(1, '1'); //=> false
  5163. * R.identical([], []); //=> false
  5164. * R.identical(0, -0); //=> false
  5165. * R.identical(NaN, NaN); //=> true
  5166. */
  5167. var identical = function identical(a, b) {
  5168. switch (arguments.length) {
  5169. case 0:
  5170. return identical;
  5171. case 1:
  5172. return function () {
  5173. return function unaryIdentical(_b) {
  5174. switch (arguments.length) {
  5175. case 0:
  5176. return unaryIdentical;
  5177. default:
  5178. return _objectIs$1(a, _b);
  5179. }
  5180. };
  5181. }();
  5182. default:
  5183. return _objectIs$1(a, b);
  5184. }
  5185. }; // In order to support Cross-origin Window objects as arguments to identical,
  5186. /**
  5187. * Creates a function that will process either the `onTrue` or the `onFalse`
  5188. * function depending upon the result of the `condition` predicate.
  5189. *
  5190. * Note that `ifElse` takes its arity from the longest of the three functions passed to it.
  5191. *
  5192. * @func
  5193. * @memberOf R
  5194. * @since v0.8.0
  5195. * @category Logic
  5196. * @sig (*... -> Boolean) -> (*... -> *) -> (*... -> *) -> (*... -> *)
  5197. * @param {Function} condition A predicate function
  5198. * @param {Function} onTrue A function to invoke when the `condition` evaluates to a truthy value.
  5199. * @param {Function} onFalse A function to invoke when the `condition` evaluates to a falsy value.
  5200. * @return {Function} A new function that will process either the `onTrue` or the `onFalse`
  5201. * function depending upon the result of the `condition` predicate.
  5202. * @see R.unless, R.when, R.cond
  5203. * @example
  5204. *
  5205. * const incCount = R.ifElse(
  5206. * R.has('count'),
  5207. * R.over(R.lensProp('count'), R.inc),
  5208. * R.assoc('count', 1)
  5209. * );
  5210. * incCount({ count: 1 }); //=> { count: 2 }
  5211. * incCount({}); //=> { count: 1 }
  5212. */
  5213. var ifElse = _curry3(function ifElse(condition, onTrue, onFalse) {
  5214. return curryN(Math.max(condition.length, onTrue.length, onFalse.length), function _ifElse() {
  5215. return condition.apply(this, arguments) ? onTrue.apply(this, arguments) : onFalse.apply(this, arguments);
  5216. });
  5217. });
  5218. /**
  5219. * Increments its argument.
  5220. *
  5221. * @func
  5222. * @memberOf R
  5223. * @since v0.9.0
  5224. * @category Math
  5225. * @sig Number -> Number
  5226. * @param {Number} n
  5227. * @return {Number} n + 1
  5228. * @see R.dec
  5229. * @example
  5230. *
  5231. * R.inc(42); //=> 43
  5232. */
  5233. var inc = add(1);
  5234. /**
  5235. * Returns `true` if the specified value is equal, in [`R.equals`](#equals)
  5236. * terms, to at least one element of the given list; `false` otherwise.
  5237. * Also works with strings.
  5238. *
  5239. * @func
  5240. * @memberOf R
  5241. * @since v0.26.0
  5242. * @category List
  5243. * @sig a -> [a] -> Boolean
  5244. * @param {Object} a The item to compare against.
  5245. * @param {Array} list The array to consider.
  5246. * @return {Boolean} `true` if an equivalent item is in the list, `false` otherwise.
  5247. * @see R.any
  5248. * @example
  5249. *
  5250. * R.includes(3, [1, 2, 3]); //=> true
  5251. * R.includes(4, [1, 2, 3]); //=> false
  5252. * R.includes({ name: 'Fred' }, [{ name: 'Fred' }]); //=> true
  5253. * R.includes([42], [[42]]); //=> true
  5254. * R.includes('ba', 'banana'); //=>true
  5255. */
  5256. var includes = _curry2(_includes);
  5257. /**
  5258. * Given a function that generates a key, turns a list of objects into an
  5259. * object indexing the objects by the given key. Note that if multiple
  5260. * objects generate the same value for the indexing key only the last value
  5261. * will be included in the generated object.
  5262. *
  5263. * Acts as a transducer if a transformer is given in list position.
  5264. *
  5265. * @func
  5266. * @memberOf R
  5267. * @since v0.19.0
  5268. * @category List
  5269. * @typedefn Idx = String | Int | Symbol
  5270. * @sig Idx a => (b -> a) -> [b] -> {a: b}
  5271. * @param {Function} fn Function :: a -> Idx
  5272. * @param {Array} array The array of objects to index
  5273. * @return {Object} An object indexing each array element by the given property.
  5274. * @see R.groupBy
  5275. * @example
  5276. *
  5277. * const list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
  5278. * R.indexBy(R.prop('id'), list);
  5279. * //=> {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}}
  5280. */
  5281. var indexBy = reduceBy(function (acc, elem) {
  5282. return elem;
  5283. }, null);
  5284. /**
  5285. * Returns the position of the first occurrence of an item in an array, or -1
  5286. * if the item is not included in the array. [`R.equals`](#equals) is used to
  5287. * determine equality.
  5288. *
  5289. * @func
  5290. * @memberOf R
  5291. * @since v0.1.0
  5292. * @category List
  5293. * @sig a -> [a] -> Number
  5294. * @param {*} target The item to find.
  5295. * @param {Array} xs The array to search in.
  5296. * @return {Number} the index of the target, or -1 if the target is not found.
  5297. * @see R.lastIndexOf, R.findIndex
  5298. * @example
  5299. *
  5300. * R.indexOf(3, [1,2,3,4]); //=> 2
  5301. * R.indexOf(10, [1,2,3,4]); //=> -1
  5302. */
  5303. var indexOf = _curry2(function indexOf(target, xs) {
  5304. return typeof xs.indexOf === 'function' && !_isArray(xs) ? xs.indexOf(target) : _indexOf(xs, target, 0);
  5305. });
  5306. /**
  5307. * Returns all but the last element of the given list or string.
  5308. *
  5309. * @func
  5310. * @memberOf R
  5311. * @since v0.9.0
  5312. * @category List
  5313. * @sig [a] -> [a]
  5314. * @sig String -> String
  5315. * @param {*} list
  5316. * @return {*}
  5317. * @see R.last, R.head, R.tail
  5318. * @example
  5319. *
  5320. * R.init([1, 2, 3]); //=> [1, 2]
  5321. * R.init([1, 2]); //=> [1]
  5322. * R.init([1]); //=> []
  5323. * R.init([]); //=> []
  5324. *
  5325. * R.init('abc'); //=> 'ab'
  5326. * R.init('ab'); //=> 'a'
  5327. * R.init('a'); //=> ''
  5328. * R.init(''); //=> ''
  5329. */
  5330. var init = slice(0, -1);
  5331. /**
  5332. * Takes a predicate `pred`, a list `xs`, and a list `ys`, and returns a list
  5333. * `xs'` comprising each of the elements of `xs` which is equal to one or more
  5334. * elements of `ys` according to `pred`.
  5335. *
  5336. * `pred` must be a binary function expecting an element from each list.
  5337. *
  5338. * `xs`, `ys`, and `xs'` are treated as sets, semantically, so ordering should
  5339. * not be significant, but since `xs'` is ordered the implementation guarantees
  5340. * that its values are in the same order as they appear in `xs`. Duplicates are
  5341. * not removed, so `xs'` may contain duplicates if `xs` contains duplicates.
  5342. *
  5343. * @func
  5344. * @memberOf R
  5345. * @since v0.24.0
  5346. * @category Relation
  5347. * @sig ((a, b) -> Boolean) -> [a] -> [b] -> [a]
  5348. * @param {Function} pred
  5349. * @param {Array} xs
  5350. * @param {Array} ys
  5351. * @return {Array}
  5352. * @see R.intersection
  5353. * @example
  5354. *
  5355. * R.innerJoin(
  5356. * (record, id) => record.id === id,
  5357. * [{id: 824, name: 'Richie Furay'},
  5358. * {id: 956, name: 'Dewey Martin'},
  5359. * {id: 313, name: 'Bruce Palmer'},
  5360. * {id: 456, name: 'Stephen Stills'},
  5361. * {id: 177, name: 'Neil Young'}],
  5362. * [177, 456, 999]
  5363. * );
  5364. * //=> [{id: 456, name: 'Stephen Stills'}, {id: 177, name: 'Neil Young'}]
  5365. */
  5366. var innerJoin = _curry3(function innerJoin(pred, xs, ys) {
  5367. return _filter(function (x) {
  5368. return _includesWith(pred, x, ys);
  5369. }, xs);
  5370. });
  5371. /**
  5372. * Inserts the supplied element into the list, at the specified `index`. _Note that
  5373. * this is not destructive_: it returns a copy of the list with the changes.
  5374. * <small>No lists have been harmed in the application of this function.</small>
  5375. *
  5376. * @func
  5377. * @memberOf R
  5378. * @since v0.2.2
  5379. * @category List
  5380. * @sig Number -> a -> [a] -> [a]
  5381. * @param {Number} index The position to insert the element
  5382. * @param {*} elt The element to insert into the Array
  5383. * @param {Array} list The list to insert into
  5384. * @return {Array} A new Array with `elt` inserted at `index`.
  5385. * @example
  5386. *
  5387. * R.insert(2, 'x', [1,2,3,4]); //=> [1,2,'x',3,4]
  5388. */
  5389. var insert = _curry3(function insert(idx, elt, list) {
  5390. idx = idx < list.length && idx >= 0 ? idx : list.length;
  5391. var result = Array.prototype.slice.call(list, 0);
  5392. result.splice(idx, 0, elt);
  5393. return result;
  5394. });
  5395. /**
  5396. * Inserts the sub-list into the list, at the specified `index`. _Note that this is not
  5397. * destructive_: it returns a copy of the list with the changes.
  5398. * <small>No lists have been harmed in the application of this function.</small>
  5399. *
  5400. * @func
  5401. * @memberOf R
  5402. * @since v0.9.0
  5403. * @category List
  5404. * @sig Number -> [a] -> [a] -> [a]
  5405. * @param {Number} index The position to insert the sub-list
  5406. * @param {Array} elts The sub-list to insert into the Array
  5407. * @param {Array} list The list to insert the sub-list into
  5408. * @return {Array} A new Array with `elts` inserted starting at `index`.
  5409. * @example
  5410. *
  5411. * R.insertAll(2, ['x','y','z'], [1,2,3,4]); //=> [1,2,'x','y','z',3,4]
  5412. */
  5413. var insertAll = _curry3(function insertAll(idx, elts, list) {
  5414. idx = idx < list.length && idx >= 0 ? idx : list.length;
  5415. return [].concat(Array.prototype.slice.call(list, 0, idx), elts, Array.prototype.slice.call(list, idx));
  5416. });
  5417. function XUniqBy(f, xf) {
  5418. this.xf = xf;
  5419. this.f = f;
  5420. this.set = new _Set();
  5421. }
  5422. XUniqBy.prototype['@@transducer/init'] = _xfBase.init;
  5423. XUniqBy.prototype['@@transducer/result'] = _xfBase.result;
  5424. XUniqBy.prototype['@@transducer/step'] = function (result, input) {
  5425. return this.set.add(this.f(input)) ? this.xf['@@transducer/step'](result, input) : result;
  5426. };
  5427. function _xuniqBy(f) {
  5428. return function (xf) {
  5429. return new XUniqBy(f, xf);
  5430. };
  5431. }
  5432. /**
  5433. * Returns a new list containing only one copy of each element in the original
  5434. * list, based upon the value returned by applying the supplied function to
  5435. * each list element. Prefers the first item if the supplied function produces
  5436. * the same value on two items. [`R.equals`](#equals) is used for comparison.
  5437. *
  5438. * Acts as a transducer if a transformer is given in list position.
  5439. *
  5440. * @func
  5441. * @memberOf R
  5442. * @since v0.16.0
  5443. * @category List
  5444. * @sig (a -> b) -> [a] -> [a]
  5445. * @param {Function} fn A function used to produce a value to use during comparisons.
  5446. * @param {Array} list The array to consider.
  5447. * @return {Array} The list of unique items.
  5448. * @example
  5449. *
  5450. * R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]); //=> [-1, -5, 2, 10]
  5451. */
  5452. var uniqBy = _curry2(_dispatchable([], _xuniqBy, function (fn, list) {
  5453. var set = new _Set();
  5454. var result = [];
  5455. var idx = 0;
  5456. var appliedItem, item;
  5457. while (idx < list.length) {
  5458. item = list[idx];
  5459. appliedItem = fn(item);
  5460. if (set.add(appliedItem)) {
  5461. result.push(item);
  5462. }
  5463. idx += 1;
  5464. }
  5465. return result;
  5466. }));
  5467. /**
  5468. * Returns a new list containing only one copy of each element in the original
  5469. * list. [`R.equals`](#equals) is used to determine equality.
  5470. *
  5471. * @func
  5472. * @memberOf R
  5473. * @since v0.1.0
  5474. * @category List
  5475. * @sig [a] -> [a]
  5476. * @param {Array} list The array to consider.
  5477. * @return {Array} The list of unique items.
  5478. * @example
  5479. *
  5480. * R.uniq([1, 1, 2, 1]); //=> [1, 2]
  5481. * R.uniq([1, '1']); //=> [1, '1']
  5482. * R.uniq([[42], [42]]); //=> [[42]]
  5483. */
  5484. var uniq = uniqBy(identity);
  5485. /**
  5486. * Combines two lists into a set (i.e. no duplicates) composed of those
  5487. * elements common to both lists.
  5488. *
  5489. * @func
  5490. * @memberOf R
  5491. * @since v0.1.0
  5492. * @category Relation
  5493. * @sig [*] -> [*] -> [*]
  5494. * @param {Array} list1 The first list.
  5495. * @param {Array} list2 The second list.
  5496. * @return {Array} The list of elements found in both `list1` and `list2`.
  5497. * @see R.innerJoin
  5498. * @example
  5499. *
  5500. * R.intersection([1,2,3,4], [7,6,5,4,3]); //=> [4, 3]
  5501. */
  5502. var intersection = _curry2(function intersection(list1, list2) {
  5503. var toKeep = new _Set();
  5504. for (var i = 0; i < list1.length; i += 1) {
  5505. toKeep.add(list1[i]);
  5506. }
  5507. return uniq(_filter(toKeep.has.bind(toKeep), list2));
  5508. });
  5509. /**
  5510. * Creates a new list with the separator interposed between elements.
  5511. *
  5512. * Dispatches to the `intersperse` method of the second argument, if present.
  5513. *
  5514. * @func
  5515. * @memberOf R
  5516. * @since v0.14.0
  5517. * @category List
  5518. * @sig a -> [a] -> [a]
  5519. * @param {*} separator The element to add to the list.
  5520. * @param {Array} list The list to be interposed.
  5521. * @return {Array} The new list.
  5522. * @example
  5523. *
  5524. * R.intersperse('a', ['b', 'n', 'n', 's']); //=> ['b', 'a', 'n', 'a', 'n', 'a', 's']
  5525. */
  5526. var intersperse = _curry2(_checkForMethod('intersperse', function intersperse(separator, list) {
  5527. var out = [];
  5528. var idx = 0;
  5529. var length = list.length;
  5530. while (idx < length) {
  5531. if (idx === length - 1) {
  5532. out.push(list[idx]);
  5533. } else {
  5534. out.push(list[idx], separator);
  5535. }
  5536. idx += 1;
  5537. }
  5538. return out;
  5539. }));
  5540. function _objectAssign(target) {
  5541. if (target == null) {
  5542. throw new TypeError('Cannot convert undefined or null to object');
  5543. }
  5544. var output = Object(target);
  5545. var idx = 1;
  5546. var length = arguments.length;
  5547. while (idx < length) {
  5548. var source = arguments[idx];
  5549. if (source != null) {
  5550. for (var nextKey in source) {
  5551. if (_has(nextKey, source)) {
  5552. output[nextKey] = source[nextKey];
  5553. }
  5554. }
  5555. }
  5556. idx += 1;
  5557. }
  5558. return output;
  5559. }
  5560. var _objectAssign$1 = typeof Object.assign === 'function' ? Object.assign : _objectAssign;
  5561. /**
  5562. * Creates an object containing a single key:value pair.
  5563. *
  5564. * @func
  5565. * @memberOf R
  5566. * @since v0.18.0
  5567. * @category Object
  5568. * @sig String -> a -> {String:a}
  5569. * @param {String} key
  5570. * @param {*} val
  5571. * @return {Object}
  5572. * @see R.pair
  5573. * @example
  5574. *
  5575. * const matchPhrases = R.compose(
  5576. * R.objOf('must'),
  5577. * R.map(R.objOf('match_phrase'))
  5578. * );
  5579. * matchPhrases(['foo', 'bar', 'baz']); //=> {must: [{match_phrase: 'foo'}, {match_phrase: 'bar'}, {match_phrase: 'baz'}]}
  5580. */
  5581. var objOf = _curry2(function objOf(key, val) {
  5582. var obj = {};
  5583. obj[key] = val;
  5584. return obj;
  5585. });
  5586. var _stepCatArray = {
  5587. '@@transducer/init': Array,
  5588. '@@transducer/step': function transducerStep(xs, x) {
  5589. xs.push(x);
  5590. return xs;
  5591. },
  5592. '@@transducer/result': _identity
  5593. };
  5594. var _stepCatString = {
  5595. '@@transducer/init': String,
  5596. '@@transducer/step': function transducerStep(a, b) {
  5597. return a + b;
  5598. },
  5599. '@@transducer/result': _identity
  5600. };
  5601. var _stepCatObject = {
  5602. '@@transducer/init': Object,
  5603. '@@transducer/step': function transducerStep(result, input) {
  5604. return _objectAssign$1(result, _isArrayLike(input) ? objOf(input[0], input[1]) : input);
  5605. },
  5606. '@@transducer/result': _identity
  5607. };
  5608. function _stepCat(obj) {
  5609. if (_isTransformer(obj)) {
  5610. return obj;
  5611. }
  5612. if (_isArrayLike(obj)) {
  5613. return _stepCatArray;
  5614. }
  5615. if (typeof obj === 'string') {
  5616. return _stepCatString;
  5617. }
  5618. if (_typeof(obj) === 'object') {
  5619. return _stepCatObject;
  5620. }
  5621. throw new Error('Cannot create transformer for ' + obj);
  5622. }
  5623. /**
  5624. * Transforms the items of the list with the transducer and appends the
  5625. * transformed items to the accumulator using an appropriate iterator function
  5626. * based on the accumulator type.
  5627. *
  5628. * The accumulator can be an array, string, object or a transformer. Iterated
  5629. * items will be appended to arrays and concatenated to strings. Objects will
  5630. * be merged directly or 2-item arrays will be merged as key, value pairs.
  5631. *
  5632. * The accumulator can also be a transformer object that provides a 2-arity
  5633. * reducing iterator function, step, 0-arity initial value function, init, and
  5634. * 1-arity result extraction function result. The step function is used as the
  5635. * iterator function in reduce. The result function is used to convert the
  5636. * final accumulator into the return type and in most cases is R.identity. The
  5637. * init function is used to provide the initial accumulator.
  5638. *
  5639. * The iteration is performed with [`R.reduce`](#reduce) after initializing the
  5640. * transducer.
  5641. *
  5642. * @func
  5643. * @memberOf R
  5644. * @since v0.12.0
  5645. * @category List
  5646. * @sig a -> (b -> b) -> [c] -> a
  5647. * @param {*} acc The initial accumulator value.
  5648. * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
  5649. * @param {Array} list The list to iterate over.
  5650. * @return {*} The final, accumulated value.
  5651. * @see R.transduce
  5652. * @example
  5653. *
  5654. * const numbers = [1, 2, 3, 4];
  5655. * const transducer = R.compose(R.map(R.add(1)), R.take(2));
  5656. *
  5657. * R.into([], transducer, numbers); //=> [2, 3]
  5658. *
  5659. * const intoArray = R.into([]);
  5660. * intoArray(transducer, numbers); //=> [2, 3]
  5661. */
  5662. var into = _curry3(function into(acc, transducer, list) {
  5663. var xf = transducer(_isTransformer(acc) ? acc : _stepCat(acc));
  5664. return _xReduce(xf, xf['@@transducer/init'](), list);
  5665. });
  5666. /**
  5667. * Same as [`R.invertObj`](#invertObj), however this accounts for objects with
  5668. * duplicate values by putting the values into an array.
  5669. *
  5670. * @func
  5671. * @memberOf R
  5672. * @since v0.9.0
  5673. * @category Object
  5674. * @sig {s: x} -> {x: [ s, ... ]}
  5675. * @param {Object} obj The object or array to invert
  5676. * @return {Object} out A new object with keys in an array.
  5677. * @see R.invertObj
  5678. * @example
  5679. *
  5680. * const raceResultsByFirstName = {
  5681. * first: 'alice',
  5682. * second: 'jake',
  5683. * third: 'alice',
  5684. * };
  5685. * R.invert(raceResultsByFirstName);
  5686. * //=> { 'alice': ['first', 'third'], 'jake':['second'] }
  5687. */
  5688. var invert = _curry1(function invert(obj) {
  5689. var props = keys(obj);
  5690. var len = props.length;
  5691. var idx = 0;
  5692. var out = {};
  5693. while (idx < len) {
  5694. var key = props[idx];
  5695. var val = obj[key];
  5696. var list = _has(val, out) ? out[val] : out[val] = [];
  5697. list[list.length] = key;
  5698. idx += 1;
  5699. }
  5700. return out;
  5701. });
  5702. /**
  5703. * Returns a new object with the keys of the given object as values, and the
  5704. * values of the given object, which are coerced to strings, as keys. Note
  5705. * that the last key found is preferred when handling the same value.
  5706. *
  5707. * @func
  5708. * @memberOf R
  5709. * @since v0.9.0
  5710. * @category Object
  5711. * @sig {s: x} -> {x: s}
  5712. * @param {Object} obj The object or array to invert
  5713. * @return {Object} out A new object
  5714. * @see R.invert
  5715. * @example
  5716. *
  5717. * const raceResults = {
  5718. * first: 'alice',
  5719. * second: 'jake'
  5720. * };
  5721. * R.invertObj(raceResults);
  5722. * //=> { 'alice': 'first', 'jake':'second' }
  5723. *
  5724. * // Alternatively:
  5725. * const raceResults = ['alice', 'jake'];
  5726. * R.invertObj(raceResults);
  5727. * //=> { 'alice': '0', 'jake':'1' }
  5728. */
  5729. var invertObj = _curry1(function invertObj(obj) {
  5730. var props = keys(obj);
  5731. var len = props.length;
  5732. var idx = 0;
  5733. var out = {};
  5734. while (idx < len) {
  5735. var key = props[idx];
  5736. out[obj[key]] = key;
  5737. idx += 1;
  5738. }
  5739. return out;
  5740. });
  5741. /**
  5742. * Given an `arity` (Number) and a `name` (String) the `invoker` function
  5743. * returns a curried function that takes `arity` arguments and a `context`
  5744. * object. It will "invoke" the `name`'d function (a method) on the `context`
  5745. * object.
  5746. *
  5747. * @func
  5748. * @memberOf R
  5749. * @since v0.1.0
  5750. * @category Function
  5751. * @sig Number -> String -> (a -> b -> ... -> n -> Object -> *)
  5752. * @param {Number} arity Number of arguments the returned function should take
  5753. * before the target object.
  5754. * @param {String} method Name of any of the target object's methods to call.
  5755. * @return {Function} A new curried function.
  5756. * @see R.construct
  5757. * @example
  5758. * // A function with no arguments
  5759. * const asJson = invoker(0, "json")
  5760. * // Just like calling .then((response) => response.json())
  5761. * fetch("http://example.com/index.json").then(asJson)
  5762. *
  5763. * // A function with one argument
  5764. * const sliceFrom = invoker(1, 'slice');
  5765. * sliceFrom(6, 'abcdefghijklm'); //=> 'ghijklm'
  5766. *
  5767. * // A function with two arguments
  5768. * const sliceFrom6 = invoker(2, 'slice')(6);
  5769. * sliceFrom6(8, 'abcdefghijklm'); //=> 'gh'
  5770. *
  5771. * // NOTE: You can't simply pass some of the arguments to the initial invoker function.
  5772. * const firstCreditCardSection = invoker(2, "slice", 0, 4)
  5773. * firstCreditCardSection("4242 4242 4242 4242") // => Function<...>
  5774. *
  5775. * // Since invoker returns a curried function, you may partially apply it to create the function you need.
  5776. * const firstCreditCardSection = invoker(2, "slice")(0, 4)
  5777. * firstCreditCardSection("4242 4242 4242 4242") // => "4242"
  5778. *
  5779. * @symb R.invoker(0, 'method')(o) = o['method']()
  5780. * @symb R.invoker(1, 'method')(a, o) = o['method'](a)
  5781. * @symb R.invoker(2, 'method')(a, b, o) = o['method'](a, b)
  5782. */
  5783. var invoker = _curry2(function invoker(arity, method) {
  5784. return curryN(arity + 1, function () {
  5785. var target = arguments[arity];
  5786. if (target != null && _isFunction(target[method])) {
  5787. return target[method].apply(target, Array.prototype.slice.call(arguments, 0, arity));
  5788. }
  5789. throw new TypeError(toString$1(target) + ' does not have a method named "' + method + '"');
  5790. });
  5791. });
  5792. /**
  5793. * See if an object (i.e. `val`) is an instance of the supplied constructor. This
  5794. * function will check up the inheritance chain, if any.
  5795. * If `val` was created using `Object.create`, `R.is(Object, val) === true`.
  5796. *
  5797. * @func
  5798. * @memberOf R
  5799. * @since v0.3.0
  5800. * @category Type
  5801. * @sig (* -> {*}) -> a -> Boolean
  5802. * @param {Object} ctor A constructor
  5803. * @param {*} val The value to test
  5804. * @return {Boolean}
  5805. * @example
  5806. *
  5807. * R.is(Object, {}); //=> true
  5808. * R.is(Number, 1); //=> true
  5809. * R.is(Object, 1); //=> false
  5810. * R.is(String, 's'); //=> true
  5811. * R.is(String, new String('')); //=> true
  5812. * R.is(Object, new String('')); //=> true
  5813. * R.is(Object, 's'); //=> false
  5814. * R.is(Number, {}); //=> false
  5815. */
  5816. var is = _curry2(function is(Ctor, val) {
  5817. return val instanceof Ctor || val != null && (val.constructor === Ctor || Ctor.name === 'Object' && _typeof(val) === 'object');
  5818. });
  5819. /**
  5820. * Returns `true` if the given value is its type's empty value; `false`
  5821. * otherwise.
  5822. *
  5823. * @func
  5824. * @memberOf R
  5825. * @since v0.1.0
  5826. * @category Logic
  5827. * @sig a -> Boolean
  5828. * @param {*} x
  5829. * @return {Boolean}
  5830. * @see R.empty
  5831. * @example
  5832. *
  5833. * R.isEmpty([1, 2, 3]); //=> false
  5834. * R.isEmpty([]); //=> true
  5835. * R.isEmpty(''); //=> true
  5836. * R.isEmpty(null); //=> false
  5837. * R.isEmpty({}); //=> true
  5838. * R.isEmpty({length: 0}); //=> false
  5839. * R.isEmpty(Uint8Array.from('')); //=> true
  5840. */
  5841. var isEmpty = _curry1(function isEmpty(x) {
  5842. return x != null && equals(x, empty(x));
  5843. });
  5844. /**
  5845. * Checks if the input value is not `null` and not `undefined`.
  5846. *
  5847. * @func
  5848. * @memberOf R
  5849. * @since v0.29.0
  5850. * @category Type
  5851. * @sig * -> Boolean
  5852. * @param {*} x The value to test.
  5853. * @return {Boolean} `true` if `x` is not `undefined` or not `null`, otherwise `false`.
  5854. * @example
  5855. *
  5856. * R.isNotNil(null); //=> false
  5857. * R.isNotNil(undefined); //=> false
  5858. * R.isNotNil(0); //=> true
  5859. * R.isNotNil([]); //=> true
  5860. */
  5861. var isNotNil = _curry1(function isNotNil(x) {
  5862. return !isNil(x);
  5863. });
  5864. /**
  5865. * Returns a string made by inserting the `separator` between each element and
  5866. * concatenating all the elements into a single string.
  5867. *
  5868. * @func
  5869. * @memberOf R
  5870. * @since v0.1.0
  5871. * @category List
  5872. * @sig String -> [a] -> String
  5873. * @param {Number|String} separator The string used to separate the elements.
  5874. * @param {Array} xs The elements to join into a string.
  5875. * @return {String} str The string made by concatenating `xs` with `separator`.
  5876. * @see R.split
  5877. * @example
  5878. *
  5879. * const spacer = R.join(' ');
  5880. * spacer(['a', 2, 3.4]); //=> 'a 2 3.4'
  5881. * R.join('|', [1, 2, 3]); //=> '1|2|3'
  5882. */
  5883. var join = invoker(1, 'join');
  5884. /**
  5885. * juxt applies a list of functions to a list of values.
  5886. *
  5887. * @func
  5888. * @memberOf R
  5889. * @since v0.19.0
  5890. * @category Function
  5891. * @sig [(a, b, ..., m) -> n] -> ((a, b, ..., m) -> [n])
  5892. * @param {Array} fns An array of functions
  5893. * @return {Function} A function that returns a list of values after applying each of the original `fns` to its parameters.
  5894. * @see R.applySpec
  5895. * @example
  5896. *
  5897. * const getRange = R.juxt([Math.min, Math.max]);
  5898. * getRange(3, 4, 9, -3); //=> [-3, 9]
  5899. * @symb R.juxt([f, g, h])(a, b) = [f(a, b), g(a, b), h(a, b)]
  5900. */
  5901. var juxt = _curry1(function juxt(fns) {
  5902. return converge(function () {
  5903. return Array.prototype.slice.call(arguments, 0);
  5904. }, fns);
  5905. });
  5906. /**
  5907. * Returns a list containing the names of all the properties of the supplied
  5908. * object, including prototype properties.
  5909. * Note that the order of the output array is not guaranteed to be consistent
  5910. * across different JS platforms.
  5911. *
  5912. * @func
  5913. * @memberOf R
  5914. * @since v0.2.0
  5915. * @category Object
  5916. * @sig {k: v} -> [k]
  5917. * @param {Object} obj The object to extract properties from
  5918. * @return {Array} An array of the object's own and prototype properties.
  5919. * @see R.keys, R.valuesIn
  5920. * @example
  5921. *
  5922. * const F = function() { this.x = 'X'; };
  5923. * F.prototype.y = 'Y';
  5924. * const f = new F();
  5925. * R.keysIn(f); //=> ['x', 'y']
  5926. */
  5927. var keysIn = _curry1(function keysIn(obj) {
  5928. var prop;
  5929. var ks = [];
  5930. for (prop in obj) {
  5931. ks[ks.length] = prop;
  5932. }
  5933. return ks;
  5934. });
  5935. /**
  5936. * Returns the position of the last occurrence of an item in an array, or -1 if
  5937. * the item is not included in the array. [`R.equals`](#equals) is used to
  5938. * determine equality.
  5939. *
  5940. * @func
  5941. * @memberOf R
  5942. * @since v0.1.0
  5943. * @category List
  5944. * @sig a -> [a] -> Number
  5945. * @param {*} target The item to find.
  5946. * @param {Array} xs The array to search in.
  5947. * @return {Number} the index of the target, or -1 if the target is not found.
  5948. * @see R.indexOf, R.findLastIndex
  5949. * @example
  5950. *
  5951. * R.lastIndexOf(3, [-1,3,3,0,1,2,3,4]); //=> 6
  5952. * R.lastIndexOf(10, [1,2,3,4]); //=> -1
  5953. */
  5954. var lastIndexOf = _curry2(function lastIndexOf(target, xs) {
  5955. if (typeof xs.lastIndexOf === 'function' && !_isArray(xs)) {
  5956. return xs.lastIndexOf(target);
  5957. } else {
  5958. var idx = xs.length - 1;
  5959. while (idx >= 0) {
  5960. if (equals(xs[idx], target)) {
  5961. return idx;
  5962. }
  5963. idx -= 1;
  5964. }
  5965. return -1;
  5966. }
  5967. });
  5968. function _isNumber(x) {
  5969. return Object.prototype.toString.call(x) === '[object Number]';
  5970. }
  5971. /**
  5972. * Returns the number of elements in the array by returning `list.length`.
  5973. *
  5974. * @func
  5975. * @memberOf R
  5976. * @since v0.3.0
  5977. * @category List
  5978. * @sig [a] -> Number
  5979. * @param {Array} list The array to inspect.
  5980. * @return {Number} The length of the array.
  5981. * @example
  5982. *
  5983. * R.length([]); //=> 0
  5984. * R.length([1, 2, 3]); //=> 3
  5985. */
  5986. var length = _curry1(function length(list) {
  5987. return list != null && _isNumber(list.length) ? list.length : NaN;
  5988. });
  5989. /**
  5990. * Returns a lens for the given getter and setter functions. The getter "gets"
  5991. * the value of the focus; the setter "sets" the value of the focus. The setter
  5992. * should not mutate the data structure.
  5993. *
  5994. * @func
  5995. * @memberOf R
  5996. * @since v0.8.0
  5997. * @category Object
  5998. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  5999. * @sig (s -> a) -> ((a, s) -> s) -> Lens s a
  6000. * @param {Function} getter
  6001. * @param {Function} setter
  6002. * @return {Lens}
  6003. * @see R.view, R.set, R.over, R.lensIndex, R.lensProp
  6004. * @example
  6005. *
  6006. * const xLens = R.lens(R.prop('x'), R.assoc('x'));
  6007. *
  6008. * R.view(xLens, {x: 1, y: 2}); //=> 1
  6009. * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
  6010. * R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
  6011. */
  6012. var lens = _curry2(function lens(getter, setter) {
  6013. return function (toFunctorFn) {
  6014. return function (target) {
  6015. return map(function (focus) {
  6016. return setter(focus, target);
  6017. }, toFunctorFn(getter(target)));
  6018. };
  6019. };
  6020. });
  6021. /**
  6022. * Returns a new copy of the array with the element at the provided index
  6023. * replaced with the given value.
  6024. *
  6025. * @func
  6026. * @memberOf R
  6027. * @since v0.14.0
  6028. * @category List
  6029. * @sig Number -> a -> [a] -> [a]
  6030. * @param {Number} idx The index to update.
  6031. * @param {*} x The value to exist at the given index of the returned array.
  6032. * @param {Array|Arguments} list The source array-like object to be updated.
  6033. * @return {Array} A copy of `list` with the value at index `idx` replaced with `x`.
  6034. * @see R.adjust
  6035. * @example
  6036. *
  6037. * R.update(1, '_', ['a', 'b', 'c']); //=> ['a', '_', 'c']
  6038. * R.update(-1, '_', ['a', 'b', 'c']); //=> ['a', 'b', '_']
  6039. * @symb R.update(-1, a, [b, c]) = [b, a]
  6040. * @symb R.update(0, a, [b, c]) = [a, c]
  6041. * @symb R.update(1, a, [b, c]) = [b, a]
  6042. */
  6043. var update = _curry3(function update(idx, x, list) {
  6044. return adjust(idx, always(x), list);
  6045. });
  6046. /**
  6047. * Returns a lens whose focus is the specified index.
  6048. *
  6049. * @func
  6050. * @memberOf R
  6051. * @since v0.14.0
  6052. * @category Object
  6053. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  6054. * @sig Number -> Lens s a
  6055. * @param {Number} n
  6056. * @return {Lens}
  6057. * @see R.view, R.set, R.over, R.nth
  6058. * @example
  6059. *
  6060. * const headLens = R.lensIndex(0);
  6061. *
  6062. * R.view(headLens, ['a', 'b', 'c']); //=> 'a'
  6063. * R.set(headLens, 'x', ['a', 'b', 'c']); //=> ['x', 'b', 'c']
  6064. * R.over(headLens, R.toUpper, ['a', 'b', 'c']); //=> ['A', 'b', 'c']
  6065. */
  6066. var lensIndex = _curry1(function lensIndex(n) {
  6067. return lens(nth(n), update(n));
  6068. });
  6069. /**
  6070. * Retrieves the values at given paths of an object.
  6071. *
  6072. * @func
  6073. * @memberOf R
  6074. * @since v0.27.1
  6075. * @category Object
  6076. * @typedefn Idx = [String | Int | Symbol]
  6077. * @sig [Idx] -> {a} -> [a | Undefined]
  6078. * @param {Array} pathsArray The array of paths to be fetched.
  6079. * @param {Object} obj The object to retrieve the nested properties from.
  6080. * @return {Array} A list consisting of values at paths specified by "pathsArray".
  6081. * @see R.path
  6082. * @example
  6083. *
  6084. * R.paths([['a', 'b'], ['p', 0, 'q']], {a: {b: 2}, p: [{q: 3}]}); //=> [2, 3]
  6085. * R.paths([['a', 'b'], ['p', 'r']], {a: {b: 2}, p: [{q: 3}]}); //=> [2, undefined]
  6086. */
  6087. var paths = _curry2(function paths(pathsArray, obj) {
  6088. return pathsArray.map(function (paths) {
  6089. var val = obj;
  6090. var idx = 0;
  6091. var p;
  6092. while (idx < paths.length) {
  6093. if (val == null) {
  6094. return;
  6095. }
  6096. p = paths[idx];
  6097. val = _isInteger(p) ? nth(p, val) : val[p];
  6098. idx += 1;
  6099. }
  6100. return val;
  6101. });
  6102. });
  6103. /**
  6104. * Retrieves the value at a given path. The nodes of the path can be arbitrary strings or non-negative integers.
  6105. * For anything else, the value is unspecified. Integer paths are meant to index arrays, strings are meant for objects.
  6106. *
  6107. * @func
  6108. * @memberOf R
  6109. * @since v0.2.0
  6110. * @category Object
  6111. * @typedefn Idx = String | Int | Symbol
  6112. * @sig [Idx] -> {a} -> a | Undefined
  6113. * @sig Idx = String | NonNegativeInt
  6114. * @param {Array} path The path to use.
  6115. * @param {Object} obj The object or array to retrieve the nested property from.
  6116. * @return {*} The data at `path`.
  6117. * @see R.prop, R.nth, R.assocPath, R.dissocPath
  6118. * @example
  6119. *
  6120. * R.path(['a', 'b'], {a: {b: 2}}); //=> 2
  6121. * R.path(['a', 'b'], {c: {b: 2}}); //=> undefined
  6122. * R.path(['a', 'b', 0], {a: {b: [1, 2, 3]}}); //=> 1
  6123. * R.path(['a', 'b', -2], {a: {b: [1, 2, 3]}}); //=> 2
  6124. * R.path([2], {'2': 2}); //=> 2
  6125. * R.path([-2], {'-2': 'a'}); //=> undefined
  6126. */
  6127. var path = _curry2(function path(pathAr, obj) {
  6128. return paths([pathAr], obj)[0];
  6129. });
  6130. /**
  6131. * Returns a lens whose focus is the specified path.
  6132. *
  6133. * @func
  6134. * @memberOf R
  6135. * @since v0.19.0
  6136. * @category Object
  6137. * @typedefn Idx = String | Int | Symbol
  6138. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  6139. * @sig [Idx] -> Lens s a
  6140. * @param {Array} path The path to use.
  6141. * @return {Lens}
  6142. * @see R.view, R.set, R.over
  6143. * @example
  6144. *
  6145. * const xHeadYLens = R.lensPath(['x', 0, 'y']);
  6146. *
  6147. * R.view(xHeadYLens, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  6148. * //=> 2
  6149. * R.set(xHeadYLens, 1, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  6150. * //=> {x: [{y: 1, z: 3}, {y: 4, z: 5}]}
  6151. * R.over(xHeadYLens, R.negate, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  6152. * //=> {x: [{y: -2, z: 3}, {y: 4, z: 5}]}
  6153. */
  6154. var lensPath = _curry1(function lensPath(p) {
  6155. return lens(path(p), assocPath(p));
  6156. });
  6157. /**
  6158. * Returns a lens whose focus is the specified property.
  6159. *
  6160. * @func
  6161. * @memberOf R
  6162. * @since v0.14.0
  6163. * @category Object
  6164. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  6165. * @sig String -> Lens s a
  6166. * @param {String} k
  6167. * @return {Lens}
  6168. * @see R.view, R.set, R.over
  6169. * @example
  6170. *
  6171. * const xLens = R.lensProp('x');
  6172. *
  6173. * R.view(xLens, {x: 1, y: 2}); //=> 1
  6174. * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
  6175. * R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
  6176. */
  6177. var lensProp = _curry1(function lensProp(k) {
  6178. return lens(prop(k), assoc(k));
  6179. });
  6180. /**
  6181. * Returns `true` if the first argument is less than the second; `false`
  6182. * otherwise.
  6183. *
  6184. * @func
  6185. * @memberOf R
  6186. * @since v0.1.0
  6187. * @category Relation
  6188. * @sig Ord a => a -> a -> Boolean
  6189. * @param {*} a
  6190. * @param {*} b
  6191. * @return {Boolean}
  6192. * @see R.gt
  6193. * @example
  6194. *
  6195. * R.lt(2, 1); //=> false
  6196. * R.lt(2, 2); //=> false
  6197. * R.lt(2, 3); //=> true
  6198. * R.lt('a', 'z'); //=> true
  6199. * R.lt('z', 'a'); //=> false
  6200. */
  6201. var lt = _curry2(function lt(a, b) {
  6202. return a < b;
  6203. });
  6204. /**
  6205. * Returns `true` if the first argument is less than or equal to the second;
  6206. * `false` otherwise.
  6207. *
  6208. * @func
  6209. * @memberOf R
  6210. * @since v0.1.0
  6211. * @category Relation
  6212. * @sig Ord a => a -> a -> Boolean
  6213. * @param {Number} a
  6214. * @param {Number} b
  6215. * @return {Boolean}
  6216. * @see R.gte
  6217. * @example
  6218. *
  6219. * R.lte(2, 1); //=> false
  6220. * R.lte(2, 2); //=> true
  6221. * R.lte(2, 3); //=> true
  6222. * R.lte('a', 'z'); //=> true
  6223. * R.lte('z', 'a'); //=> false
  6224. */
  6225. var lte = _curry2(function lte(a, b) {
  6226. return a <= b;
  6227. });
  6228. /**
  6229. * The `mapAccum` function behaves like a combination of map and reduce; it
  6230. * applies a function to each element of a list, passing an accumulating
  6231. * parameter from left to right, and returning a final value of this
  6232. * accumulator together with the new list.
  6233. *
  6234. * The iterator function receives two arguments, *acc* and *value*, and should
  6235. * return a tuple *[acc, value]*.
  6236. *
  6237. * @func
  6238. * @memberOf R
  6239. * @since v0.10.0
  6240. * @category List
  6241. * @sig ((acc, x) -> (acc, y)) -> acc -> [x] -> (acc, [y])
  6242. * @param {Function} fn The function to be called on every element of the input `list`.
  6243. * @param {*} acc The accumulator value.
  6244. * @param {Array} list The list to iterate over.
  6245. * @return {*} The final, accumulated value.
  6246. * @see R.scan, R.addIndex, R.mapAccumRight
  6247. * @example
  6248. *
  6249. * const digits = ['1', '2', '3', '4'];
  6250. * const appender = (a, b) => [a + b, a + b];
  6251. *
  6252. * R.mapAccum(appender, 0, digits); //=> ['01234', ['01', '012', '0123', '01234']]
  6253. * @symb R.mapAccum(f, a, [b, c, d]) = [
  6254. * f(f(f(a, b)[0], c)[0], d)[0],
  6255. * [
  6256. * f(a, b)[1],
  6257. * f(f(a, b)[0], c)[1],
  6258. * f(f(f(a, b)[0], c)[0], d)[1]
  6259. * ]
  6260. * ]
  6261. */
  6262. var mapAccum = _curry3(function mapAccum(fn, acc, list) {
  6263. var idx = 0;
  6264. var len = list.length;
  6265. var result = [];
  6266. var tuple = [acc];
  6267. while (idx < len) {
  6268. tuple = fn(tuple[0], list[idx]);
  6269. result[idx] = tuple[1];
  6270. idx += 1;
  6271. }
  6272. return [tuple[0], result];
  6273. });
  6274. /**
  6275. * The `mapAccumRight` function behaves like a combination of map and reduce; it
  6276. * applies a function to each element of a list, passing an accumulating
  6277. * parameter from right to left, and returning a final value of this
  6278. * accumulator together with the new list.
  6279. *
  6280. * Similar to [`mapAccum`](#mapAccum), except moves through the input list from
  6281. * the right to the left.
  6282. *
  6283. * The iterator function receives two arguments, *acc* and *value*, and should
  6284. * return a tuple *[acc, value]*.
  6285. *
  6286. * @func
  6287. * @memberOf R
  6288. * @since v0.10.0
  6289. * @category List
  6290. * @sig ((acc, x) -> (acc, y)) -> acc -> [x] -> (acc, [y])
  6291. * @param {Function} fn The function to be called on every element of the input `list`.
  6292. * @param {*} acc The accumulator value.
  6293. * @param {Array} list The list to iterate over.
  6294. * @return {*} The final, accumulated value.
  6295. * @see R.addIndex, R.mapAccum
  6296. * @example
  6297. *
  6298. * const digits = ['1', '2', '3', '4'];
  6299. * const appender = (a, b) => [b + a, b + a];
  6300. *
  6301. * R.mapAccumRight(appender, 5, digits); //=> ['12345', ['12345', '2345', '345', '45']]
  6302. * @symb R.mapAccumRight(f, a, [b, c, d]) = [
  6303. * f(f(f(a, d)[0], c)[0], b)[0],
  6304. * [
  6305. * f(a, d)[1],
  6306. * f(f(a, d)[0], c)[1],
  6307. * f(f(f(a, d)[0], c)[0], b)[1]
  6308. * ]
  6309. * ]
  6310. */
  6311. var mapAccumRight = _curry3(function mapAccumRight(fn, acc, list) {
  6312. var idx = list.length - 1;
  6313. var result = [];
  6314. var tuple = [acc];
  6315. while (idx >= 0) {
  6316. tuple = fn(tuple[0], list[idx]);
  6317. result[idx] = tuple[1];
  6318. idx -= 1;
  6319. }
  6320. return [tuple[0], result];
  6321. });
  6322. /**
  6323. * An Object-specific version of [`map`](#map). The function is applied to three
  6324. * arguments: *(value, key, obj)*. If only the value is significant, use
  6325. * [`map`](#map) instead.
  6326. *
  6327. * @func
  6328. * @memberOf R
  6329. * @since v0.9.0
  6330. * @category Object
  6331. * @sig ((*, String, Object) -> *) -> Object -> Object
  6332. * @param {Function} fn
  6333. * @param {Object} obj
  6334. * @return {Object}
  6335. * @see R.map
  6336. * @example
  6337. *
  6338. * const xyz = { x: 1, y: 2, z: 3 };
  6339. * const prependKeyAndDouble = (num, key, obj) => key + (num * 2);
  6340. *
  6341. * R.mapObjIndexed(prependKeyAndDouble, xyz); //=> { x: 'x2', y: 'y4', z: 'z6' }
  6342. */
  6343. var mapObjIndexed = _curry2(function mapObjIndexed(fn, obj) {
  6344. return _arrayReduce(function (acc, key) {
  6345. acc[key] = fn(obj[key], key, obj);
  6346. return acc;
  6347. }, {}, keys(obj));
  6348. });
  6349. /**
  6350. * Tests a regular expression against a String. Note that this function will
  6351. * return an empty array when there are no matches. This differs from
  6352. * [`String.prototype.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)
  6353. * which returns `null` when there are no matches.
  6354. *
  6355. * @func
  6356. * @memberOf R
  6357. * @since v0.1.0
  6358. * @category String
  6359. * @sig RegExp -> String -> [String | Undefined]
  6360. * @param {RegExp} rx A regular expression.
  6361. * @param {String} str The string to match against
  6362. * @return {Array} The list of matches or empty array.
  6363. * @see R.test
  6364. * @example
  6365. *
  6366. * R.match(/([a-z]a)/g, 'bananas'); //=> ['ba', 'na', 'na']
  6367. * R.match(/a/, 'b'); //=> []
  6368. * R.match(/a/, null); //=> TypeError: null does not have a method named "match"
  6369. */
  6370. var match = _curry2(function match(rx, str) {
  6371. return str.match(rx) || [];
  6372. });
  6373. /**
  6374. * `mathMod` behaves like the modulo operator should mathematically, unlike the
  6375. * `%` operator (and by extension, [`R.modulo`](#modulo)). So while
  6376. * `-17 % 5` is `-2`, `mathMod(-17, 5)` is `3`. `mathMod` requires Integer
  6377. * arguments, and returns NaN when the modulus is zero or negative.
  6378. *
  6379. * @func
  6380. * @memberOf R
  6381. * @since v0.3.0
  6382. * @category Math
  6383. * @sig Number -> Number -> Number
  6384. * @param {Number} m The dividend.
  6385. * @param {Number} p the modulus.
  6386. * @return {Number} The result of `b mod a`.
  6387. * @see R.modulo
  6388. * @example
  6389. *
  6390. * R.mathMod(-17, 5); //=> 3
  6391. * R.mathMod(17, 5); //=> 2
  6392. * R.mathMod(17, -5); //=> NaN
  6393. * R.mathMod(17, 0); //=> NaN
  6394. * R.mathMod(17.2, 5); //=> NaN
  6395. * R.mathMod(17, 5.3); //=> NaN
  6396. *
  6397. * const clock = R.mathMod(R.__, 12);
  6398. * clock(15); //=> 3
  6399. * clock(24); //=> 0
  6400. *
  6401. * const seventeenMod = R.mathMod(17);
  6402. * seventeenMod(3); //=> 2
  6403. * seventeenMod(4); //=> 1
  6404. * seventeenMod(10); //=> 7
  6405. */
  6406. var mathMod = _curry2(function mathMod(m, p) {
  6407. if (!_isInteger(m)) {
  6408. return NaN;
  6409. }
  6410. if (!_isInteger(p) || p < 1) {
  6411. return NaN;
  6412. }
  6413. return (m % p + p) % p;
  6414. });
  6415. /**
  6416. * Takes a function and two values, and returns whichever value produces the
  6417. * larger result when passed to the provided function.
  6418. *
  6419. * @func
  6420. * @memberOf R
  6421. * @since v0.8.0
  6422. * @category Relation
  6423. * @sig Ord b => (a -> b) -> a -> a -> a
  6424. * @param {Function} f
  6425. * @param {*} a
  6426. * @param {*} b
  6427. * @return {*}
  6428. * @see R.max, R.minBy
  6429. * @example
  6430. *
  6431. * // square :: Number -> Number
  6432. * const square = n => n * n;
  6433. *
  6434. * R.maxBy(square, -3, 2); //=> -3
  6435. *
  6436. * R.reduce(R.maxBy(square), 0, [3, -5, 4, 1, -2]); //=> -5
  6437. * R.reduce(R.maxBy(square), 0, []); //=> 0
  6438. */
  6439. var maxBy = _curry3(function maxBy(f, a, b) {
  6440. var resultB = f(b);
  6441. return max(f(a), resultB) === resultB ? b : a;
  6442. });
  6443. /**
  6444. * Adds together all the elements of a list.
  6445. *
  6446. * @func
  6447. * @memberOf R
  6448. * @since v0.1.0
  6449. * @category Math
  6450. * @sig [Number] -> Number
  6451. * @param {Array} list An array of numbers
  6452. * @return {Number} The sum of all the numbers in the list.
  6453. * @see R.reduce
  6454. * @example
  6455. *
  6456. * R.sum([2,4,6,8,100,1]); //=> 121
  6457. */
  6458. var sum = reduce(add, 0);
  6459. /**
  6460. * Returns the mean of the given list of numbers.
  6461. *
  6462. * @func
  6463. * @memberOf R
  6464. * @since v0.14.0
  6465. * @category Math
  6466. * @sig [Number] -> Number
  6467. * @param {Array} list
  6468. * @return {Number}
  6469. * @see R.median
  6470. * @example
  6471. *
  6472. * R.mean([2, 7, 9]); //=> 6
  6473. * R.mean([]); //=> NaN
  6474. */
  6475. var mean = _curry1(function mean(list) {
  6476. return sum(list) / list.length;
  6477. });
  6478. /**
  6479. * Returns the median of the given list of numbers.
  6480. *
  6481. * @func
  6482. * @memberOf R
  6483. * @since v0.14.0
  6484. * @category Math
  6485. * @sig [Number] -> Number
  6486. * @param {Array} list
  6487. * @return {Number}
  6488. * @see R.mean
  6489. * @example
  6490. *
  6491. * R.median([2, 9, 7]); //=> 7
  6492. * R.median([7, 2, 10, 9]); //=> 8
  6493. * R.median([]); //=> NaN
  6494. */
  6495. var median = _curry1(function median(list) {
  6496. var len = list.length;
  6497. if (len === 0) {
  6498. return NaN;
  6499. }
  6500. var width = 2 - len % 2;
  6501. var idx = (len - width) / 2;
  6502. return mean(Array.prototype.slice.call(list, 0).sort(function (a, b) {
  6503. return a < b ? -1 : a > b ? 1 : 0;
  6504. }).slice(idx, idx + width));
  6505. });
  6506. /**
  6507. * Takes a string-returning function `keyGen` and a function `fn` and returns
  6508. * a new function that returns cached results for subsequent
  6509. * calls with the same arguments.
  6510. *
  6511. * When the function is invoked, `keyGen` is applied to the same arguments
  6512. * and its result becomes the cache key. If the cache contains something
  6513. * under that key, the function simply returns it and does not invoke `fn` at all.
  6514. *
  6515. * Otherwise `fn` is applied to the same arguments and its return value
  6516. * is cached under that key and returned by the function.
  6517. *
  6518. * Care must be taken when implementing `keyGen` to avoid key collision,
  6519. * or if tracking references, memory leaks and mutating arguments.
  6520. *
  6521. * @func
  6522. * @memberOf R
  6523. * @since v0.24.0
  6524. * @category Function
  6525. * @sig (*... -> String) -> (*... -> a) -> (*... -> a)
  6526. * @param {Function} keyGen The function to generate the cache key.
  6527. * @param {Function} fn The function to memoize.
  6528. * @return {Function} Memoized version of `fn`.
  6529. * @example
  6530. * const withAge = memoizeWith(o => `${o.birth}/${o.death}`, ({birth, death}) => {
  6531. * // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^
  6532. * // keyGen fn
  6533. * console.log(`computing age for ${birth}/${death}`);
  6534. * return ({birth, death, age: death - birth});
  6535. * });
  6536. *
  6537. * withAge({birth: 1921, death: 1999});
  6538. * //=> LOG: computing age for 1921/1999
  6539. * //=> {birth: 1921, death: 1999, age: 78} (returned from fn)
  6540. *
  6541. * withAge({birth: 1921, death: 1999});
  6542. * //=> {birth: 1921, death: 1999, age: 78} (returned from cache)
  6543. */
  6544. var memoizeWith = _curry2(function memoizeWith(keyGen, fn) {
  6545. var cache = {};
  6546. return _arity(fn.length, function () {
  6547. var key = keyGen.apply(this, arguments);
  6548. if (!_has(key, cache)) {
  6549. cache[key] = fn.apply(this, arguments);
  6550. }
  6551. return cache[key];
  6552. });
  6553. });
  6554. /**
  6555. * Creates one new object with the own properties from a list of objects.
  6556. * If a key exists in more than one object, the value from the last
  6557. * object it exists in will be used.
  6558. *
  6559. * @func
  6560. * @memberOf R
  6561. * @since v0.10.0
  6562. * @category List
  6563. * @sig [{k: v}] -> {k: v}
  6564. * @param {Array} list An array of objects
  6565. * @return {Object} A merged object.
  6566. * @see R.reduce
  6567. * @example
  6568. *
  6569. * R.mergeAll([{foo:1},{bar:2},{baz:3}]); //=> {foo:1,bar:2,baz:3}
  6570. * R.mergeAll([{foo:1},{foo:2},{bar:2}]); //=> {foo:2,bar:2}
  6571. * @symb R.mergeAll([{ x: 1 }, { y: 2 }, { z: 3 }]) = { x: 1, y: 2, z: 3 }
  6572. */
  6573. var mergeAll = _curry1(function mergeAll(list) {
  6574. return _objectAssign$1.apply(null, [{}].concat(list));
  6575. });
  6576. /**
  6577. * Creates a new object with the own properties of the two provided objects. If
  6578. * a key exists in both objects, the provided function is applied to the key
  6579. * and the values associated with the key in each object, with the result being
  6580. * used as the value associated with the key in the returned object.
  6581. *
  6582. * @func
  6583. * @memberOf R
  6584. * @since v0.19.0
  6585. * @category Object
  6586. * @sig ((String, a, a) -> a) -> {a} -> {a} -> {a}
  6587. * @param {Function} fn
  6588. * @param {Object} l
  6589. * @param {Object} r
  6590. * @return {Object}
  6591. * @see R.mergeDeepWithKey, R.merge, R.mergeWith
  6592. * @example
  6593. *
  6594. * let concatValues = (k, l, r) => k == 'values' ? R.concat(l, r) : r
  6595. * R.mergeWithKey(concatValues,
  6596. * { a: true, thing: 'foo', values: [10, 20] },
  6597. * { b: true, thing: 'bar', values: [15, 35] });
  6598. * //=> { a: true, b: true, thing: 'bar', values: [10, 20, 15, 35] }
  6599. * @symb R.mergeWithKey(f, { x: 1, y: 2 }, { y: 5, z: 3 }) = { x: 1, y: f('y', 2, 5), z: 3 }
  6600. */
  6601. var mergeWithKey = _curry3(function mergeWithKey(fn, l, r) {
  6602. var result = {};
  6603. var k;
  6604. l = l || {};
  6605. r = r || {};
  6606. for (k in l) {
  6607. if (_has(k, l)) {
  6608. result[k] = _has(k, r) ? fn(k, l[k], r[k]) : l[k];
  6609. }
  6610. }
  6611. for (k in r) {
  6612. if (_has(k, r) && !_has(k, result)) {
  6613. result[k] = r[k];
  6614. }
  6615. }
  6616. return result;
  6617. });
  6618. /**
  6619. * Creates a new object with the own properties of the two provided objects.
  6620. * If a key exists in both objects:
  6621. * - and both associated values are also objects then the values will be
  6622. * recursively merged.
  6623. * - otherwise the provided function is applied to the key and associated values
  6624. * using the resulting value as the new value associated with the key.
  6625. * If a key only exists in one object, the value will be associated with the key
  6626. * of the resulting object.
  6627. *
  6628. * @func
  6629. * @memberOf R
  6630. * @since v0.24.0
  6631. * @category Object
  6632. * @sig ((String, a, a) -> a) -> {a} -> {a} -> {a}
  6633. * @param {Function} fn
  6634. * @param {Object} lObj
  6635. * @param {Object} rObj
  6636. * @return {Object}
  6637. * @see R.mergeWithKey, R.mergeDeepWith
  6638. * @example
  6639. *
  6640. * let concatValues = (k, l, r) => k == 'values' ? R.concat(l, r) : r
  6641. * R.mergeDeepWithKey(concatValues,
  6642. * { a: true, c: { thing: 'foo', values: [10, 20] }},
  6643. * { b: true, c: { thing: 'bar', values: [15, 35] }});
  6644. * //=> { a: true, b: true, c: { thing: 'bar', values: [10, 20, 15, 35] }}
  6645. */
  6646. var mergeDeepWithKey = _curry3(function mergeDeepWithKey(fn, lObj, rObj) {
  6647. return mergeWithKey(function (k, lVal, rVal) {
  6648. if (_isObject(lVal) && _isObject(rVal)) {
  6649. return mergeDeepWithKey(fn, lVal, rVal);
  6650. } else {
  6651. return fn(k, lVal, rVal);
  6652. }
  6653. }, lObj, rObj);
  6654. });
  6655. /**
  6656. * Creates a new object with the own properties of the first object merged with
  6657. * the own properties of the second object. If a key exists in both objects:
  6658. * - and both values are objects, the two values will be recursively merged
  6659. * - otherwise the value from the first object will be used.
  6660. *
  6661. * @func
  6662. * @memberOf R
  6663. * @since v0.24.0
  6664. * @category Object
  6665. * @sig {a} -> {a} -> {a}
  6666. * @param {Object} lObj
  6667. * @param {Object} rObj
  6668. * @return {Object}
  6669. * @see R.merge, R.mergeDeepRight, R.mergeDeepWith, R.mergeDeepWithKey
  6670. * @example
  6671. *
  6672. * R.mergeDeepLeft({ name: 'fred', age: 10, contact: { email: 'moo@example.com' }},
  6673. * { age: 40, contact: { email: 'baa@example.com' }});
  6674. * //=> { name: 'fred', age: 10, contact: { email: 'moo@example.com' }}
  6675. */
  6676. var mergeDeepLeft = _curry2(function mergeDeepLeft(lObj, rObj) {
  6677. return mergeDeepWithKey(function (k, lVal, rVal) {
  6678. return lVal;
  6679. }, lObj, rObj);
  6680. });
  6681. /**
  6682. * Creates a new object with the own properties of the first object merged with
  6683. * the own properties of the second object. If a key exists in both objects:
  6684. * - and both values are objects, the two values will be recursively merged
  6685. * - otherwise the value from the second object will be used.
  6686. *
  6687. * @func
  6688. * @memberOf R
  6689. * @since v0.24.0
  6690. * @category Object
  6691. * @sig {a} -> {a} -> {a}
  6692. * @param {Object} lObj
  6693. * @param {Object} rObj
  6694. * @return {Object}
  6695. * @see R.merge, R.mergeDeepLeft, R.mergeDeepWith, R.mergeDeepWithKey
  6696. * @example
  6697. *
  6698. * R.mergeDeepRight({ name: 'fred', age: 10, contact: { email: 'moo@example.com' }},
  6699. * { age: 40, contact: { email: 'baa@example.com' }});
  6700. * //=> { name: 'fred', age: 40, contact: { email: 'baa@example.com' }}
  6701. */
  6702. var mergeDeepRight = _curry2(function mergeDeepRight(lObj, rObj) {
  6703. return mergeDeepWithKey(function (k, lVal, rVal) {
  6704. return rVal;
  6705. }, lObj, rObj);
  6706. });
  6707. /**
  6708. * Creates a new object with the own properties of the two provided objects.
  6709. * If a key exists in both objects:
  6710. * - and both associated values are also objects then the values will be
  6711. * recursively merged.
  6712. * - otherwise the provided function is applied to associated values using the
  6713. * resulting value as the new value associated with the key.
  6714. * If a key only exists in one object, the value will be associated with the key
  6715. * of the resulting object.
  6716. *
  6717. * @func
  6718. * @memberOf R
  6719. * @since v0.24.0
  6720. * @category Object
  6721. * @sig ((a, a) -> a) -> {a} -> {a} -> {a}
  6722. * @param {Function} fn
  6723. * @param {Object} lObj
  6724. * @param {Object} rObj
  6725. * @return {Object}
  6726. * @see R.mergeWith, R.mergeDeepWithKey
  6727. * @example
  6728. *
  6729. * R.mergeDeepWith(R.concat,
  6730. * { a: true, c: { values: [10, 20] }},
  6731. * { b: true, c: { values: [15, 35] }});
  6732. * //=> { a: true, b: true, c: { values: [10, 20, 15, 35] }}
  6733. */
  6734. var mergeDeepWith = _curry3(function mergeDeepWith(fn, lObj, rObj) {
  6735. return mergeDeepWithKey(function (k, lVal, rVal) {
  6736. return fn(lVal, rVal);
  6737. }, lObj, rObj);
  6738. });
  6739. /**
  6740. * Create a new object with the own properties of the first object merged with
  6741. * the own properties of the second object. If a key exists in both objects,
  6742. * the value from the first object will be used.
  6743. *
  6744. * @func
  6745. * @memberOf R
  6746. * @since v0.26.0
  6747. * @category Object
  6748. * @sig {k: v} -> {k: v} -> {k: v}
  6749. * @param {Object} l
  6750. * @param {Object} r
  6751. * @return {Object}
  6752. * @see R.mergeRight, R.mergeDeepLeft, R.mergeWith, R.mergeWithKey
  6753. * @example
  6754. *
  6755. * R.mergeLeft({ 'age': 40 }, { 'name': 'fred', 'age': 10 });
  6756. * //=> { 'name': 'fred', 'age': 40 }
  6757. *
  6758. * const resetToDefault = R.mergeLeft({x: 0});
  6759. * resetToDefault({x: 5, y: 2}); //=> {x: 0, y: 2}
  6760. * @symb R.mergeLeft(a, b) = {...b, ...a}
  6761. */
  6762. var mergeLeft = _curry2(function mergeLeft(l, r) {
  6763. return _objectAssign$1({}, r, l);
  6764. });
  6765. /**
  6766. * Create a new object with the own properties of the first object merged with
  6767. * the own properties of the second object. If a key exists in both objects,
  6768. * the value from the second object will be used.
  6769. *
  6770. * @func
  6771. * @memberOf R
  6772. * @since v0.26.0
  6773. * @category Object
  6774. * @sig {k: v} -> {k: v} -> {k: v}
  6775. * @param {Object} l
  6776. * @param {Object} r
  6777. * @return {Object}
  6778. * @see R.mergeLeft, R.mergeDeepRight, R.mergeWith, R.mergeWithKey
  6779. * @example
  6780. *
  6781. * R.mergeRight({ 'name': 'fred', 'age': 10 }, { 'age': 40 });
  6782. * //=> { 'name': 'fred', 'age': 40 }
  6783. *
  6784. * const withDefaults = R.mergeRight({x: 0, y: 0});
  6785. * withDefaults({y: 2}); //=> {x: 0, y: 2}
  6786. * @symb R.mergeRight(a, b) = {...a, ...b}
  6787. */
  6788. var mergeRight = _curry2(function mergeRight(l, r) {
  6789. return _objectAssign$1({}, l, r);
  6790. });
  6791. /**
  6792. * Creates a new object with the own properties of the two provided objects. If
  6793. * a key exists in both objects, the provided function is applied to the values
  6794. * associated with the key in each object, with the result being used as the
  6795. * value associated with the key in the returned object.
  6796. *
  6797. * @func
  6798. * @memberOf R
  6799. * @since v0.19.0
  6800. * @category Object
  6801. * @sig ((a, a) -> a) -> {a} -> {a} -> {a}
  6802. * @param {Function} fn
  6803. * @param {Object} l
  6804. * @param {Object} r
  6805. * @return {Object}
  6806. * @see R.mergeDeepWith, R.merge, R.mergeWithKey
  6807. * @example
  6808. *
  6809. * R.mergeWith(R.concat,
  6810. * { a: true, values: [10, 20] },
  6811. * { b: true, values: [15, 35] });
  6812. * //=> { a: true, b: true, values: [10, 20, 15, 35] }
  6813. */
  6814. var mergeWith = _curry3(function mergeWith(fn, l, r) {
  6815. return mergeWithKey(function (_, _l, _r) {
  6816. return fn(_l, _r);
  6817. }, l, r);
  6818. });
  6819. /**
  6820. * Returns the smaller of its two arguments.
  6821. *
  6822. * @func
  6823. * @memberOf R
  6824. * @since v0.1.0
  6825. * @category Relation
  6826. * @sig Ord a => a -> a -> a
  6827. * @param {*} a
  6828. * @param {*} b
  6829. * @return {*}
  6830. * @see R.minBy, R.max
  6831. * @example
  6832. *
  6833. * R.min(789, 123); //=> 123
  6834. * R.min('a', 'b'); //=> 'a'
  6835. */
  6836. var min = _curry2(function min(a, b) {
  6837. if (a === b) {
  6838. return a;
  6839. }
  6840. function safeMin(x, y) {
  6841. if (x < y !== y < x) {
  6842. return y < x ? y : x;
  6843. }
  6844. return undefined;
  6845. }
  6846. var minByValue = safeMin(a, b);
  6847. if (minByValue !== undefined) {
  6848. return minByValue;
  6849. }
  6850. var minByType = safeMin(_typeof(a), _typeof(b));
  6851. if (minByType !== undefined) {
  6852. return minByType === _typeof(a) ? a : b;
  6853. }
  6854. var stringA = toString$1(a);
  6855. var minByStringValue = safeMin(stringA, toString$1(b));
  6856. if (minByStringValue !== undefined) {
  6857. return minByStringValue === stringA ? a : b;
  6858. }
  6859. return a;
  6860. });
  6861. /**
  6862. * Takes a function and two values, and returns whichever value produces the
  6863. * smaller result when passed to the provided function.
  6864. *
  6865. * @func
  6866. * @memberOf R
  6867. * @since v0.8.0
  6868. * @category Relation
  6869. * @sig Ord b => (a -> b) -> a -> a -> a
  6870. * @param {Function} f
  6871. * @param {*} a
  6872. * @param {*} b
  6873. * @return {*}
  6874. * @see R.min, R.maxBy
  6875. * @example
  6876. *
  6877. * // square :: Number -> Number
  6878. * const square = n => n * n;
  6879. *
  6880. * R.minBy(square, -3, 2); //=> 2
  6881. *
  6882. * R.reduce(R.minBy(square), Infinity, [3, -5, 4, 1, -2]); //=> 1
  6883. * R.reduce(R.minBy(square), Infinity, []); //=> Infinity
  6884. */
  6885. var minBy = _curry3(function minBy(f, a, b) {
  6886. var resultB = f(b);
  6887. return min(f(a), resultB) === resultB ? b : a;
  6888. });
  6889. /**
  6890. * Makes a shallow clone of an object, applying the given fn to the specified
  6891. * property with the given value. Note that this copies and flattens prototype
  6892. * properties onto the new object as well. All non-primitive properties are
  6893. * copied by reference.
  6894. *
  6895. * @private
  6896. * @param {String|Number} prop The property name to set
  6897. * @param {Function} fn The function to apply to the property
  6898. * @param {Object|Array} obj The object to clone
  6899. * @return {Object|Array} A new object equivalent to the original except for the changed property.
  6900. */
  6901. function _modify(prop, fn, obj) {
  6902. if (_isInteger(prop) && _isArray(obj)) {
  6903. var arr = [].concat(obj);
  6904. arr[prop] = fn(arr[prop]);
  6905. return arr;
  6906. }
  6907. var result = {};
  6908. for (var p in obj) {
  6909. result[p] = obj[p];
  6910. }
  6911. result[prop] = fn(result[prop]);
  6912. return result;
  6913. }
  6914. /**
  6915. * Creates a shallow clone of the passed object by applying an `fn` function
  6916. * to the value at the given path.
  6917. *
  6918. * The function will not be invoked, and the object will not change
  6919. * if its corresponding path does not exist in the object.
  6920. * All non-primitive properties are copied to the new object by reference.
  6921. *
  6922. * @func
  6923. * @memberOf R
  6924. * @since v0.28.0
  6925. * @category Object
  6926. * @sig [Idx] -> (v -> v) -> {k: v} -> {k: v}
  6927. * @param {Array} path The path to be modified.
  6928. * @param {Function} fn The function to apply to the path.
  6929. * @param {Object} object The object to be transformed.
  6930. * @return {Object} The transformed object.
  6931. * @example
  6932. *
  6933. * const person = {name: 'James', address: { zipCode: '90216' }};
  6934. * R.modifyPath(['address', 'zipCode'], R.reverse, person); //=> {name: 'James', address: { zipCode: '61209' }}
  6935. *
  6936. * // Can handle arrays too
  6937. * const person = {name: 'James', addresses: [{ zipCode: '90216' }]};
  6938. * R.modifyPath(['addresses', 0, 'zipCode'], R.reverse, person); //=> {name: 'James', addresses: [{ zipCode: '61209' }]}
  6939. */
  6940. var modifyPath = _curry3(function modifyPath(path, fn, object) {
  6941. if (!_isObject(object) && !_isArray(object) || path.length === 0) {
  6942. return object;
  6943. }
  6944. var idx = path[0];
  6945. if (!_has(idx, object)) {
  6946. return object;
  6947. }
  6948. if (path.length === 1) {
  6949. return _modify(idx, fn, object);
  6950. }
  6951. var val = modifyPath(Array.prototype.slice.call(path, 1), fn, object[idx]);
  6952. if (val === object[idx]) {
  6953. return object;
  6954. }
  6955. return _assoc(idx, val, object);
  6956. });
  6957. /**
  6958. * Creates a copy of the passed object by applying an `fn` function to the given `prop` property.
  6959. *
  6960. * The function will not be invoked, and the object will not change
  6961. * if its corresponding property does not exist in the object.
  6962. * All non-primitive properties are copied to the new object by reference.
  6963. *
  6964. * @func
  6965. * @memberOf R
  6966. * @since v0.28.0
  6967. * @category Object
  6968. * @sig Idx -> (v -> v) -> {k: v} -> {k: v}
  6969. * @param {String|Number} prop The property to be modified.
  6970. * @param {Function} fn The function to apply to the property.
  6971. * @param {Object} object The object to be transformed.
  6972. * @return {Object} The transformed object.
  6973. * @example
  6974. *
  6975. * const person = {name: 'James', age: 20, pets: ['dog', 'cat']};
  6976. * R.modify('age', R.add(1), person); //=> {name: 'James', age: 21, pets: ['dog', 'cat']}
  6977. * R.modify('pets', R.append('turtle'), person); //=> {name: 'James', age: 20, pets: ['dog', 'cat', 'turtle']}
  6978. */
  6979. var modify = _curry3(function modify(prop, fn, object) {
  6980. return modifyPath([prop], fn, object);
  6981. });
  6982. /**
  6983. * Divides the first parameter by the second and returns the remainder. Note
  6984. * that this function preserves the JavaScript-style behavior for modulo. For
  6985. * mathematical modulo see [`mathMod`](#mathMod).
  6986. *
  6987. * @func
  6988. * @memberOf R
  6989. * @since v0.1.1
  6990. * @category Math
  6991. * @sig Number -> Number -> Number
  6992. * @param {Number} a The value to the divide.
  6993. * @param {Number} b The pseudo-modulus
  6994. * @return {Number} The result of `b % a`.
  6995. * @see R.mathMod
  6996. * @example
  6997. *
  6998. * R.modulo(17, 3); //=> 2
  6999. * // JS behavior:
  7000. * R.modulo(-17, 3); //=> -2
  7001. * R.modulo(17, -3); //=> 2
  7002. *
  7003. * const isOdd = R.modulo(R.__, 2);
  7004. * isOdd(42); //=> 0
  7005. * isOdd(21); //=> 1
  7006. */
  7007. var modulo = _curry2(function modulo(a, b) {
  7008. return a % b;
  7009. });
  7010. /**
  7011. * Move an item, at index `from`, to index `to`, in a list of elements.
  7012. * A new list will be created containing the new elements order.
  7013. *
  7014. * @func
  7015. * @memberOf R
  7016. * @since v0.27.1
  7017. * @category List
  7018. * @sig Number -> Number -> [a] -> [a]
  7019. * @param {Number} from The source index
  7020. * @param {Number} to The destination index
  7021. * @param {Array} list The list which will serve to realise the move
  7022. * @return {Array} The new list reordered
  7023. * @example
  7024. *
  7025. * R.move(0, 2, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['b', 'c', 'a', 'd', 'e', 'f']
  7026. * R.move(-1, 0, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['f', 'a', 'b', 'c', 'd', 'e'] list rotation
  7027. */
  7028. var move = _curry3(function (from, to, list) {
  7029. var length = list.length;
  7030. var result = list.slice();
  7031. var positiveFrom = from < 0 ? length + from : from;
  7032. var positiveTo = to < 0 ? length + to : to;
  7033. var item = result.splice(positiveFrom, 1);
  7034. return positiveFrom < 0 || positiveFrom >= list.length || positiveTo < 0 || positiveTo >= list.length ? list : [].concat(result.slice(0, positiveTo)).concat(item).concat(result.slice(positiveTo, list.length));
  7035. });
  7036. /**
  7037. * Multiplies two numbers. Equivalent to `a * b` but curried.
  7038. *
  7039. * @func
  7040. * @memberOf R
  7041. * @since v0.1.0
  7042. * @category Math
  7043. * @sig Number -> Number -> Number
  7044. * @param {Number} a The first value.
  7045. * @param {Number} b The second value.
  7046. * @return {Number} The result of `a * b`.
  7047. * @see R.divide
  7048. * @example
  7049. *
  7050. * const double = R.multiply(2);
  7051. * const triple = R.multiply(3);
  7052. * double(3); //=> 6
  7053. * triple(4); //=> 12
  7054. * R.multiply(2, 5); //=> 10
  7055. */
  7056. var multiply = _curry2(function multiply(a, b) {
  7057. return a * b;
  7058. });
  7059. var _this = undefined;
  7060. /**
  7061. * Takes a function `f` and an object, and returns a function `g`.
  7062. * When applied, `g` returns the result of applying `f` to the object
  7063. * provided initially merged deeply (right) with the object provided as an argument to `g`.
  7064. *
  7065. * @func
  7066. * @memberOf R
  7067. * @since v0.28.0
  7068. * @category Function
  7069. * @sig (({ a, b, c, ..., n }) -> x) -> { a, b, c, ...} -> ({ d, e, f, ..., n } -> x)
  7070. * @param {Function} f
  7071. * @param {Object} props
  7072. * @return {Function}
  7073. * @see R.partial, R.partialRight, R.curry, R.mergeDeepRight
  7074. * @example
  7075. *
  7076. * const multiply2 = ({ a, b }) => a * b;
  7077. * const double = R.partialObject(multiply2, { a: 2 });
  7078. * double({ b: 2 }); //=> 4
  7079. *
  7080. * const greet = ({ salutation, title, firstName, lastName }) =>
  7081. * salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
  7082. *
  7083. * const sayHello = R.partialObject(greet, { salutation: 'Hello' });
  7084. * const sayHelloToMs = R.partialObject(sayHello, { title: 'Ms.' });
  7085. * sayHelloToMs({ firstName: 'Jane', lastName: 'Jones' }); //=> 'Hello, Ms. Jane Jones!'
  7086. * @symb R.partialObject(f, { a, b })({ c, d }) = f({ a, b, c, d })
  7087. */
  7088. var partialObject = _curry2(function (f, o) {
  7089. return function (props) {
  7090. return f.call(_this, mergeDeepRight(o, props));
  7091. };
  7092. });
  7093. /**
  7094. * Negates its argument.
  7095. *
  7096. * @func
  7097. * @memberOf R
  7098. * @since v0.9.0
  7099. * @category Math
  7100. * @sig Number -> Number
  7101. * @param {Number} n
  7102. * @return {Number}
  7103. * @example
  7104. *
  7105. * R.negate(42); //=> -42
  7106. */
  7107. var negate = _curry1(function negate(n) {
  7108. return -n;
  7109. });
  7110. /**
  7111. * Returns `true` if no elements of the list match the predicate, `false`
  7112. * otherwise.
  7113. *
  7114. * Dispatches to the `all` method of the second argument, if present.
  7115. *
  7116. * Acts as a transducer if a transformer is given in list position.
  7117. *
  7118. * @func
  7119. * @memberOf R
  7120. * @since v0.12.0
  7121. * @category List
  7122. * @sig (a -> Boolean) -> [a] -> Boolean
  7123. * @param {Function} fn The predicate function.
  7124. * @param {Array} list The array to consider.
  7125. * @return {Boolean} `true` if the predicate is not satisfied by every element, `false` otherwise.
  7126. * @see R.all, R.any
  7127. * @example
  7128. *
  7129. * const isEven = n => n % 2 === 0;
  7130. * const isOdd = n => n % 2 !== 0;
  7131. *
  7132. * R.none(isEven, [1, 3, 5, 7, 9, 11]); //=> true
  7133. * R.none(isOdd, [1, 3, 5, 7, 8, 11]); //=> false
  7134. */
  7135. var none = _curry2(function none(fn, input) {
  7136. return all(_complement(fn), input);
  7137. });
  7138. /**
  7139. * Returns a function which returns its nth argument.
  7140. *
  7141. * @func
  7142. * @memberOf R
  7143. * @since v0.9.0
  7144. * @category Function
  7145. * @sig Number -> *... -> *
  7146. * @param {Number} n
  7147. * @return {Function}
  7148. * @example
  7149. *
  7150. * R.nthArg(1)('a', 'b', 'c'); //=> 'b'
  7151. * R.nthArg(-1)('a', 'b', 'c'); //=> 'c'
  7152. * @symb R.nthArg(-1)(a, b, c) = c
  7153. * @symb R.nthArg(0)(a, b, c) = a
  7154. * @symb R.nthArg(1)(a, b, c) = b
  7155. */
  7156. var nthArg = _curry1(function nthArg(n) {
  7157. var arity = n < 0 ? 1 : n + 1;
  7158. return curryN(arity, function () {
  7159. return nth(n, arguments);
  7160. });
  7161. });
  7162. /**
  7163. * `o` is a curried composition function that returns a unary function.
  7164. * Like [`compose`](#compose), `o` performs right-to-left function composition.
  7165. * Unlike [`compose`](#compose), the rightmost function passed to `o` will be
  7166. * invoked with only one argument. Also, unlike [`compose`](#compose), `o` is
  7167. * limited to accepting only 2 unary functions. The name o was chosen because
  7168. * of its similarity to the mathematical composition operator ∘.
  7169. *
  7170. * @func
  7171. * @memberOf R
  7172. * @since v0.24.0
  7173. * @category Function
  7174. * @sig (b -> c) -> (a -> b) -> a -> c
  7175. * @param {Function} f
  7176. * @param {Function} g
  7177. * @return {Function}
  7178. * @see R.compose, R.pipe
  7179. * @example
  7180. *
  7181. * const classyGreeting = name => "The name's " + name.last + ", " + name.first + " " + name.last
  7182. * const yellGreeting = R.o(R.toUpper, classyGreeting);
  7183. * yellGreeting({first: 'James', last: 'Bond'}); //=> "THE NAME'S BOND, JAMES BOND"
  7184. *
  7185. * R.o(R.multiply(10), R.add(10))(-4) //=> 60
  7186. *
  7187. * @symb R.o(f, g, x) = f(g(x))
  7188. */
  7189. var o = _curry3(function o(f, g, x) {
  7190. return f(g(x));
  7191. });
  7192. /**
  7193. * Given a constructor and a value, returns a new instance of that constructor
  7194. * containing the value.
  7195. *
  7196. * Dispatches to the `fantasy-land/of` method of the constructor first (if present)
  7197. * or to the `of` method last (if present). When neither are present, wraps the
  7198. * value in an array.
  7199. *
  7200. * Note this `of` is different from the ES6 `of`; See
  7201. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
  7202. *
  7203. * @func
  7204. * @memberOf R
  7205. * @since v0.3.0
  7206. * @category Function
  7207. * @sig (* -> {*}) -> a -> {a}
  7208. * @param {Object} Ctor A constructor
  7209. * @param {*} val any value
  7210. * @return {*} An instance of the `Ctor` wrapping `val`.
  7211. * @example
  7212. *
  7213. * R.of(Array, 42); //=> [42]
  7214. * R.of(Array, [42]); //=> [[42]]
  7215. * R.of(Maybe, 42); //=> Maybe.Just(42)
  7216. */
  7217. var of = _curry2(function of(Ctor, val) {
  7218. return typeof Ctor['fantasy-land/of'] === 'function' ? Ctor['fantasy-land/of'](val) : typeof Ctor.of === 'function' ? Ctor.of(val) : [val];
  7219. });
  7220. /**
  7221. * Returns a partial copy of an object omitting the keys specified.
  7222. *
  7223. * @func
  7224. * @memberOf R
  7225. * @since v0.1.0
  7226. * @category Object
  7227. * @sig [String] -> {String: *} -> {String: *}
  7228. * @param {Array} names an array of String property names to omit from the new object
  7229. * @param {Object} obj The object to copy from
  7230. * @return {Object} A new object with properties from `names` not on it.
  7231. * @see R.pick
  7232. * @example
  7233. *
  7234. * R.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, c: 3}
  7235. */
  7236. var omit = _curry2(function omit(names, obj) {
  7237. var result = {};
  7238. var index = {};
  7239. var idx = 0;
  7240. var len = names.length;
  7241. while (idx < len) {
  7242. index[names[idx]] = 1;
  7243. idx += 1;
  7244. }
  7245. for (var prop in obj) {
  7246. if (!index.hasOwnProperty(prop)) {
  7247. result[prop] = obj[prop];
  7248. }
  7249. }
  7250. return result;
  7251. });
  7252. /**
  7253. * Takes a binary function `f`, a unary function `g`, and two values.
  7254. * Applies `g` to each value, then applies the result of each to `f`.
  7255. *
  7256. * Also known as the P combinator.
  7257. *
  7258. * @func
  7259. * @memberOf R
  7260. * @since v0.28.0
  7261. * @category Function
  7262. * @sig ((a, a) -> b) -> (c -> a) -> c -> c -> b
  7263. * @param {Function} f a binary function
  7264. * @param {Function} g a unary function
  7265. * @param {any} a any value
  7266. * @param {any} b any value
  7267. * @return {any} The result of `f`
  7268. * @example
  7269. *
  7270. * const eqBy = R.on((a, b) => a === b);
  7271. * eqBy(R.prop('a'), {b:0, a:1}, {a:1}) //=> true;
  7272. *
  7273. * const containsInsensitive = R.on(R.includes, R.toLower);
  7274. * containsInsensitive('o', 'FOO'); //=> true
  7275. * @symb R.on(f, g, a, b) = f(g(a), g(b))
  7276. */
  7277. var on = _curryN(4, [], function on(f, g, a, b) {
  7278. return f(g(a), g(b));
  7279. });
  7280. /**
  7281. * Accepts a function `fn` and returns a function that guards invocation of
  7282. * `fn` such that `fn` can only ever be called once, no matter how many times
  7283. * the returned function is invoked. The first value calculated is returned in
  7284. * subsequent invocations.
  7285. *
  7286. * @func
  7287. * @memberOf R
  7288. * @since v0.1.0
  7289. * @category Function
  7290. * @sig (a... -> b) -> (a... -> b)
  7291. * @param {Function} fn The function to wrap in a call-only-once wrapper.
  7292. * @return {Function} The wrapped function.
  7293. * @example
  7294. *
  7295. * const addOneOnce = R.once(x => x + 1);
  7296. * addOneOnce(10); //=> 11
  7297. * addOneOnce(addOneOnce(50)); //=> 11
  7298. */
  7299. var once = _curry1(function once(fn) {
  7300. var called = false;
  7301. var result;
  7302. return _arity(fn.length, function () {
  7303. if (called) {
  7304. return result;
  7305. }
  7306. called = true;
  7307. result = fn.apply(this, arguments);
  7308. return result;
  7309. });
  7310. });
  7311. function _assertPromise(name, p) {
  7312. if (p == null || !_isFunction(p.then)) {
  7313. throw new TypeError('`' + name + '` expected a Promise, received ' + _toString(p, []));
  7314. }
  7315. }
  7316. /**
  7317. * Returns the result of applying the onFailure function to the value inside
  7318. * a failed promise. This is useful for handling rejected promises
  7319. * inside function compositions.
  7320. *
  7321. * @func
  7322. * @memberOf R
  7323. * @since v0.26.0
  7324. * @category Function
  7325. * @sig (e -> b) -> (Promise e a) -> (Promise e b)
  7326. * @sig (e -> (Promise f b)) -> (Promise e a) -> (Promise f b)
  7327. * @param {Function} onFailure The function to apply. Can return a value or a promise of a value.
  7328. * @param {Promise} p
  7329. * @return {Promise} The result of calling `p.then(null, onFailure)`
  7330. * @see R.andThen
  7331. * @example
  7332. *
  7333. * const failedFetch = id => Promise.reject('bad ID');
  7334. * const useDefault = () => ({ firstName: 'Bob', lastName: 'Loblaw' });
  7335. *
  7336. * //recoverFromFailure :: String -> Promise ({ firstName, lastName })
  7337. * const recoverFromFailure = R.pipe(
  7338. * failedFetch,
  7339. * R.otherwise(useDefault),
  7340. * R.andThen(R.pick(['firstName', 'lastName'])),
  7341. * );
  7342. * recoverFromFailure(12345).then(console.log);
  7343. */
  7344. var otherwise = _curry2(function otherwise(f, p) {
  7345. _assertPromise('otherwise', p);
  7346. return p.then(null, f);
  7347. });
  7348. // transforms the held value with the provided function.
  7349. var Identity = function Identity(x) {
  7350. return {
  7351. value: x,
  7352. map: function map(f) {
  7353. return Identity(f(x));
  7354. }
  7355. };
  7356. };
  7357. /**
  7358. * Returns the result of "setting" the portion of the given data structure
  7359. * focused by the given lens to the result of applying the given function to
  7360. * the focused value.
  7361. *
  7362. * @func
  7363. * @memberOf R
  7364. * @since v0.16.0
  7365. * @category Object
  7366. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  7367. * @sig Lens s a -> (a -> a) -> s -> s
  7368. * @param {Lens} lens
  7369. * @param {*} v
  7370. * @param {*} x
  7371. * @return {*}
  7372. * @see R.view, R.set, R.lens, R.lensIndex, R.lensProp, R.lensPath
  7373. * @example
  7374. *
  7375. * const headLens = R.lensIndex(0);
  7376. *
  7377. * R.over(headLens, R.toUpper, ['foo', 'bar', 'baz']); //=> ['FOO', 'bar', 'baz']
  7378. */
  7379. var over = _curry3(function over(lens, f, x) {
  7380. // The value returned by the getter function is first transformed with `f`,
  7381. // then set as the value of an `Identity`. This is then mapped over with the
  7382. // setter function of the lens.
  7383. return lens(function (y) {
  7384. return Identity(f(y));
  7385. })(x).value;
  7386. });
  7387. /**
  7388. * Takes two arguments, `fst` and `snd`, and returns `[fst, snd]`.
  7389. *
  7390. * @func
  7391. * @memberOf R
  7392. * @since v0.18.0
  7393. * @category List
  7394. * @sig a -> b -> (a,b)
  7395. * @param {*} fst
  7396. * @param {*} snd
  7397. * @return {Array}
  7398. * @see R.objOf, R.of
  7399. * @example
  7400. *
  7401. * R.pair('foo', 'bar'); //=> ['foo', 'bar']
  7402. */
  7403. var pair = _curry2(function pair(fst, snd) {
  7404. return [fst, snd];
  7405. });
  7406. function _createPartialApplicator(concat) {
  7407. return _curry2(function (fn, args) {
  7408. return _arity(Math.max(0, fn.length - args.length), function () {
  7409. return fn.apply(this, concat(args, arguments));
  7410. });
  7411. });
  7412. }
  7413. /**
  7414. * Takes a function `f` and a list of arguments, and returns a function `g`.
  7415. * When applied, `g` returns the result of applying `f` to the arguments
  7416. * provided initially followed by the arguments provided to `g`.
  7417. *
  7418. * @func
  7419. * @memberOf R
  7420. * @since v0.10.0
  7421. * @category Function
  7422. * @sig ((a, b, c, ..., n) -> x) -> [a, b, c, ...] -> ((d, e, f, ..., n) -> x)
  7423. * @param {Function} f
  7424. * @param {Array} args
  7425. * @return {Function}
  7426. * @see R.partialRight, R.curry
  7427. * @example
  7428. *
  7429. * const multiply2 = (a, b) => a * b;
  7430. * const double = R.partial(multiply2, [2]);
  7431. * double(3); //=> 6
  7432. *
  7433. * const greet = (salutation, title, firstName, lastName) =>
  7434. * salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
  7435. *
  7436. * const sayHello = R.partial(greet, ['Hello']);
  7437. * const sayHelloToMs = R.partial(sayHello, ['Ms.']);
  7438. * sayHelloToMs('Jane', 'Jones'); //=> 'Hello, Ms. Jane Jones!'
  7439. * @symb R.partial(f, [a, b])(c, d) = f(a, b, c, d)
  7440. */
  7441. var partial = _createPartialApplicator(_concat);
  7442. /**
  7443. * Takes a function `f` and a list of arguments, and returns a function `g`.
  7444. * When applied, `g` returns the result of applying `f` to the arguments
  7445. * provided to `g` followed by the arguments provided initially.
  7446. *
  7447. * @func
  7448. * @memberOf R
  7449. * @since v0.10.0
  7450. * @category Function
  7451. * @sig ((a, b, c, ..., n) -> x) -> [d, e, f, ..., n] -> ((a, b, c, ...) -> x)
  7452. * @param {Function} f
  7453. * @param {Array} args
  7454. * @return {Function}
  7455. * @see R.partial
  7456. * @example
  7457. *
  7458. * const greet = (salutation, title, firstName, lastName) =>
  7459. * salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
  7460. *
  7461. * const greetMsJaneJones = R.partialRight(greet, ['Ms.', 'Jane', 'Jones']);
  7462. *
  7463. * greetMsJaneJones('Hello'); //=> 'Hello, Ms. Jane Jones!'
  7464. * @symb R.partialRight(f, [a, b])(c, d) = f(c, d, a, b)
  7465. */
  7466. var partialRight = _createPartialApplicator(flip(_concat));
  7467. /**
  7468. * Takes a predicate and a list or other `Filterable` object and returns the
  7469. * pair of filterable objects of the same type of elements which do and do not
  7470. * satisfy, the predicate, respectively. Filterable objects include plain objects or any object
  7471. * that has a filter method such as `Array`.
  7472. *
  7473. * @func
  7474. * @memberOf R
  7475. * @since v0.1.4
  7476. * @category List
  7477. * @sig Filterable f => (a -> Boolean) -> f a -> [f a, f a]
  7478. * @param {Function} pred A predicate to determine which side the element belongs to.
  7479. * @param {Array} filterable the list (or other filterable) to partition.
  7480. * @return {Array} An array, containing first the subset of elements that satisfy the
  7481. * predicate, and second the subset of elements that do not satisfy.
  7482. * @see R.filter, R.reject
  7483. * @example
  7484. *
  7485. * R.partition(R.includes('s'), ['sss', 'ttt', 'foo', 'bars']);
  7486. * // => [ [ 'sss', 'bars' ], [ 'ttt', 'foo' ] ]
  7487. *
  7488. * R.partition(R.includes('s'), { a: 'sss', b: 'ttt', foo: 'bars' });
  7489. * // => [ { a: 'sss', foo: 'bars' }, { b: 'ttt' } ]
  7490. */
  7491. var partition = juxt([filter, reject]);
  7492. /**
  7493. * Determines whether a nested path on an object has a specific value, in
  7494. * [`R.equals`](#equals) terms. Most likely used to filter a list.
  7495. *
  7496. * @func
  7497. * @memberOf R
  7498. * @since v0.7.0
  7499. * @category Relation
  7500. * @typedefn Idx = String | Int | Symbol
  7501. * @sig a -> [Idx] -> {a} -> Boolean
  7502. * @param {*} val The value to compare the nested property with
  7503. * @param {Array} path The path of the nested property to use
  7504. * @param {Object} obj The object to check the nested property in
  7505. * @return {Boolean} `true` if the value equals the nested object property,
  7506. * `false` otherwise.
  7507. * @see R.whereEq, R.propEq, R.pathSatisfies, R.equals
  7508. * @example
  7509. *
  7510. * const user1 = { address: { zipCode: 90210 } };
  7511. * const user2 = { address: { zipCode: 55555 } };
  7512. * const user3 = { name: 'Bob' };
  7513. * const users = [ user1, user2, user3 ];
  7514. * const isFamous = R.pathEq(90210, ['address', 'zipCode']);
  7515. * R.filter(isFamous, users); //=> [ user1 ]
  7516. */
  7517. var pathEq = _curry3(function pathEq(val, _path, obj) {
  7518. return equals(path(_path, obj), val);
  7519. });
  7520. /**
  7521. * If the given, non-null object has a value at the given path, returns the
  7522. * value at that path. Otherwise returns the provided default value.
  7523. *
  7524. * @func
  7525. * @memberOf R
  7526. * @since v0.18.0
  7527. * @category Object
  7528. * @typedefn Idx = String | Int | Symbol
  7529. * @sig a -> [Idx] -> {a} -> a
  7530. * @param {*} d The default value.
  7531. * @param {Array} p The path to use.
  7532. * @param {Object} obj The object to retrieve the nested property from.
  7533. * @return {*} The data at `path` of the supplied object or the default value.
  7534. * @example
  7535. *
  7536. * R.pathOr('N/A', ['a', 'b'], {a: {b: 2}}); //=> 2
  7537. * R.pathOr('N/A', ['a', 'b'], {c: {b: 2}}); //=> "N/A"
  7538. */
  7539. var pathOr = _curry3(function pathOr(d, p, obj) {
  7540. return defaultTo(d, path(p, obj));
  7541. });
  7542. /**
  7543. * Returns `true` if the specified object property at given path satisfies the
  7544. * given predicate; `false` otherwise.
  7545. *
  7546. * @func
  7547. * @memberOf R
  7548. * @since v0.19.0
  7549. * @category Logic
  7550. * @typedefn Idx = String | Int | Symbol
  7551. * @sig (a -> Boolean) -> [Idx] -> {a} -> Boolean
  7552. * @param {Function} pred
  7553. * @param {Array} propPath
  7554. * @param {*} obj
  7555. * @return {Boolean}
  7556. * @see R.propSatisfies, R.path
  7557. * @example
  7558. *
  7559. * R.pathSatisfies(y => y > 0, ['x', 'y'], {x: {y: 2}}); //=> true
  7560. * R.pathSatisfies(R.is(Object), [], {x: {y: 2}}); //=> true
  7561. */
  7562. var pathSatisfies = _curry3(function pathSatisfies(pred, propPath, obj) {
  7563. return pred(path(propPath, obj));
  7564. });
  7565. /**
  7566. * Returns a partial copy of an object containing only the keys specified. If
  7567. * the key does not exist, the property is ignored.
  7568. *
  7569. * @func
  7570. * @memberOf R
  7571. * @since v0.1.0
  7572. * @category Object
  7573. * @sig [k] -> {k: v} -> {k: v}
  7574. * @param {Array} names an array of String property names to copy onto a new object
  7575. * @param {Object} obj The object to copy from
  7576. * @return {Object} A new object with only properties from `names` on it.
  7577. * @see R.omit, R.props
  7578. * @example
  7579. *
  7580. * R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
  7581. * R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1}
  7582. */
  7583. var pick = _curry2(function pick(names, obj) {
  7584. var result = {};
  7585. var idx = 0;
  7586. while (idx < names.length) {
  7587. if (names[idx] in obj) {
  7588. result[names[idx]] = obj[names[idx]];
  7589. }
  7590. idx += 1;
  7591. }
  7592. return result;
  7593. });
  7594. /**
  7595. * Similar to `pick` except that this one includes a `key: undefined` pair for
  7596. * properties that don't exist.
  7597. *
  7598. * @func
  7599. * @memberOf R
  7600. * @since v0.1.0
  7601. * @category Object
  7602. * @sig [k] -> {k: v} -> {k: v}
  7603. * @param {Array} names an array of String property names to copy onto a new object
  7604. * @param {Object} obj The object to copy from
  7605. * @return {Object} A new object with only properties from `names` on it.
  7606. * @see R.pick
  7607. * @example
  7608. *
  7609. * R.pickAll(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
  7610. * R.pickAll(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, e: undefined, f: undefined}
  7611. */
  7612. var pickAll = _curry2(function pickAll(names, obj) {
  7613. var result = {};
  7614. var idx = 0;
  7615. var len = names.length;
  7616. while (idx < len) {
  7617. var name = names[idx];
  7618. result[name] = obj[name];
  7619. idx += 1;
  7620. }
  7621. return result;
  7622. });
  7623. /**
  7624. * Returns a partial copy of an object containing only the keys that satisfy
  7625. * the supplied predicate.
  7626. *
  7627. * @func
  7628. * @memberOf R
  7629. * @since v0.8.0
  7630. * @category Object
  7631. * @sig ((v, k) -> Boolean) -> {k: v} -> {k: v}
  7632. * @param {Function} pred A predicate to determine whether or not a key
  7633. * should be included on the output object.
  7634. * @param {Object} obj The object to copy from
  7635. * @return {Object} A new object with only properties that satisfy `pred`
  7636. * on it.
  7637. * @see R.pick, R.filter
  7638. * @example
  7639. *
  7640. * const isUpperCase = (val, key) => key.toUpperCase() === key;
  7641. * R.pickBy(isUpperCase, {a: 1, b: 2, A: 3, B: 4}); //=> {A: 3, B: 4}
  7642. */
  7643. var pickBy = _curry2(function pickBy(test, obj) {
  7644. var result = {};
  7645. for (var prop in obj) {
  7646. if (test(obj[prop], prop, obj)) {
  7647. result[prop] = obj[prop];
  7648. }
  7649. }
  7650. return result;
  7651. });
  7652. /**
  7653. * Returns a new list with the given element at the front, followed by the
  7654. * contents of the list.
  7655. *
  7656. * @func
  7657. * @memberOf R
  7658. * @since v0.1.0
  7659. * @category List
  7660. * @sig a -> [a] -> [a]
  7661. * @param {*} el The item to add to the head of the output list.
  7662. * @param {Array} list The array to add to the tail of the output list.
  7663. * @return {Array} A new array.
  7664. * @see R.append
  7665. * @example
  7666. *
  7667. * R.prepend('fee', ['fi', 'fo', 'fum']); //=> ['fee', 'fi', 'fo', 'fum']
  7668. */
  7669. var prepend = _curry2(function prepend(el, list) {
  7670. return _concat([el], list);
  7671. });
  7672. /**
  7673. * Multiplies together all the elements of a list.
  7674. *
  7675. * @func
  7676. * @memberOf R
  7677. * @since v0.1.0
  7678. * @category Math
  7679. * @sig [Number] -> Number
  7680. * @param {Array} list An array of numbers
  7681. * @return {Number} The product of all the numbers in the list.
  7682. * @see R.reduce
  7683. * @example
  7684. *
  7685. * R.product([2,4,6,8,100,1]); //=> 38400
  7686. */
  7687. var product = reduce(multiply, 1);
  7688. /**
  7689. * Accepts a function `fn` and a list of transformer functions and returns a
  7690. * new curried function. When the new function is invoked, it calls the
  7691. * function `fn` with parameters consisting of the result of calling each
  7692. * supplied handler on successive arguments to the new function.
  7693. *
  7694. * If more arguments are passed to the returned function than transformer
  7695. * functions, those arguments are passed directly to `fn` as additional
  7696. * parameters. If you expect additional arguments that don't need to be
  7697. * transformed, although you can ignore them, it's best to pass an identity
  7698. * function so that the new function reports the correct arity.
  7699. *
  7700. * @func
  7701. * @memberOf R
  7702. * @since v0.1.0
  7703. * @category Function
  7704. * @sig ((x1, x2, ...) -> z) -> [(a -> x1), (b -> x2), ...] -> (a -> b -> ... -> z)
  7705. * @param {Function} fn The function to wrap.
  7706. * @param {Array} transformers A list of transformer functions
  7707. * @return {Function} The wrapped function.
  7708. * @see R.converge
  7709. * @example
  7710. *
  7711. * R.useWith(Math.pow, [R.identity, R.identity])(3, 4); //=> 81
  7712. * R.useWith(Math.pow, [R.identity, R.identity])(3)(4); //=> 81
  7713. * R.useWith(Math.pow, [R.dec, R.inc])(3, 4); //=> 32
  7714. * R.useWith(Math.pow, [R.dec, R.inc])(3)(4); //=> 32
  7715. * @symb R.useWith(f, [g, h])(a, b) = f(g(a), h(b))
  7716. */
  7717. var useWith = _curry2(function useWith(fn, transformers) {
  7718. return curryN(transformers.length, function () {
  7719. var args = [];
  7720. var idx = 0;
  7721. while (idx < transformers.length) {
  7722. args.push(transformers[idx].call(this, arguments[idx]));
  7723. idx += 1;
  7724. }
  7725. return fn.apply(this, args.concat(Array.prototype.slice.call(arguments, transformers.length)));
  7726. });
  7727. });
  7728. /**
  7729. * Reasonable analog to SQL `select` statement.
  7730. *
  7731. * @func
  7732. * @memberOf R
  7733. * @since v0.1.0
  7734. * @category Object
  7735. * @category Relation
  7736. * @sig [k] -> [{k: v}] -> [{k: v}]
  7737. * @param {Array} props The property names to project
  7738. * @param {Array} objs The objects to query
  7739. * @return {Array} An array of objects with just the `props` properties.
  7740. * @see R.pluck, R.props, R.prop
  7741. * @example
  7742. *
  7743. * const abby = {name: 'Abby', age: 7, hair: 'blond', grade: 2};
  7744. * const fred = {name: 'Fred', age: 12, hair: 'brown', grade: 7};
  7745. * const kids = [abby, fred];
  7746. * R.project(['name', 'grade'], kids); //=> [{name: 'Abby', grade: 2}, {name: 'Fred', grade: 7}]
  7747. */
  7748. var project = useWith(_map, [pickAll, identity]); // passing `identity` gives correct arity
  7749. function _promap(f, g, profunctor) {
  7750. return function (x) {
  7751. return g(profunctor(f(x)));
  7752. };
  7753. }
  7754. function XPromap(f, g, xf) {
  7755. this.xf = xf;
  7756. this.f = f;
  7757. this.g = g;
  7758. }
  7759. XPromap.prototype['@@transducer/init'] = _xfBase.init;
  7760. XPromap.prototype['@@transducer/result'] = _xfBase.result;
  7761. XPromap.prototype['@@transducer/step'] = function (result, input) {
  7762. return this.xf['@@transducer/step'](result, _promap(this.f, this.g, input));
  7763. };
  7764. function _xpromap(f, g) {
  7765. return function (xf) {
  7766. return new XPromap(f, g, xf);
  7767. };
  7768. }
  7769. /**
  7770. * Takes two functions as pre- and post- processors respectively for a third function,
  7771. * i.e. `promap(f, g, h)(x) === g(h(f(x)))`.
  7772. *
  7773. * Dispatches to the `promap` method of the third argument, if present,
  7774. * according to the [FantasyLand Profunctor spec](https://github.com/fantasyland/fantasy-land#profunctor).
  7775. *
  7776. * Acts as a transducer if a transformer is given in profunctor position.
  7777. *
  7778. * @func
  7779. * @memberOf R
  7780. * @since v0.28.0
  7781. * @category Function
  7782. * @sig (a -> b) -> (c -> d) -> (b -> c) -> (a -> d)
  7783. * @sig Profunctor p => (a -> b) -> (c -> d) -> p b c -> p a d
  7784. * @param {Function} f The preprocessor function, a -> b
  7785. * @param {Function} g The postprocessor function, c -> d
  7786. * @param {Profunctor} profunctor The profunctor instance to be promapped, e.g. b -> c
  7787. * @return {Profunctor} The new profunctor instance, e.g. a -> d
  7788. * @see R.transduce
  7789. * @example
  7790. *
  7791. * const decodeChar = R.promap(s => s.charCodeAt(), String.fromCharCode, R.add(-8))
  7792. * const decodeString = R.promap(R.split(''), R.join(''), R.map(decodeChar))
  7793. * decodeString("ziuli") //=> "ramda"
  7794. *
  7795. * @symb R.promap(f, g, h) = x => g(h(f(x)))
  7796. * @symb R.promap(f, g, profunctor) = profunctor.promap(f, g)
  7797. */
  7798. var promap = _curry3(_dispatchable(['fantasy-land/promap', 'promap'], _xpromap, _promap));
  7799. /**
  7800. * Returns `true` if the specified object property is equal, in
  7801. * [`R.equals`](#equals) terms, to the given value; `false` otherwise.
  7802. * You can test multiple properties with [`R.whereEq`](#whereEq),
  7803. * and test nested path property with [`R.pathEq`](#pathEq).
  7804. *
  7805. * @func
  7806. * @memberOf R
  7807. * @since v0.1.0
  7808. * @category Relation
  7809. * @sig a -> String -> Object -> Boolean
  7810. * @param {*} val The value to compare the property with
  7811. * @param {String} name the specified object property's key
  7812. * @param {*} obj The object to check the property in
  7813. * @return {Boolean} `true` if the value equals the specified object property,
  7814. * `false` otherwise.
  7815. * @see R.whereEq, R.pathEq, R.propSatisfies, R.equals
  7816. * @example
  7817. *
  7818. * const abby = {name: 'Abby', age: 7, hair: 'blond'};
  7819. * const fred = {name: 'Fred', age: 12, hair: 'brown'};
  7820. * const rusty = {name: 'Rusty', age: 10, hair: 'brown'};
  7821. * const alois = {name: 'Alois', age: 15, disposition: 'surly'};
  7822. * const kids = [abby, fred, rusty, alois];
  7823. * const hasBrownHair = R.propEq('brown', 'hair');
  7824. * R.filter(hasBrownHair, kids); //=> [fred, rusty]
  7825. */
  7826. var propEq = _curry3(function propEq(val, name, obj) {
  7827. return equals(val, prop(name, obj));
  7828. });
  7829. /**
  7830. * Returns `true` if the specified object property is of the given type;
  7831. * `false` otherwise.
  7832. *
  7833. * @func
  7834. * @memberOf R
  7835. * @since v0.16.0
  7836. * @category Type
  7837. * @sig Type -> String -> Object -> Boolean
  7838. * @param {Function} type
  7839. * @param {String} name
  7840. * @param {*} obj
  7841. * @return {Boolean}
  7842. * @see R.is, R.propSatisfies
  7843. * @example
  7844. *
  7845. * R.propIs(Number, 'x', {x: 1, y: 2}); //=> true
  7846. * R.propIs(Number, 'x', {x: 'foo'}); //=> false
  7847. * R.propIs(Number, 'x', {}); //=> false
  7848. */
  7849. var propIs = _curry3(function propIs(type, name, obj) {
  7850. return is(type, prop(name, obj));
  7851. });
  7852. /**
  7853. * Return the specified property of the given non-null object if the property
  7854. * is present and it's value is not `null`, `undefined` or `NaN`.
  7855. *
  7856. * Otherwise the first argument is returned.
  7857. *
  7858. * @func
  7859. * @memberOf R
  7860. * @since v0.6.0
  7861. * @category Object
  7862. * @sig a -> String -> Object -> a
  7863. * @param {*} val The default value.
  7864. * @param {String} p The name of the property to return.
  7865. * @param {Object} obj The object to query.
  7866. * @return {*} The value of given property of the supplied object or the default value.
  7867. * @example
  7868. *
  7869. * const alice = {
  7870. * name: 'ALICE',
  7871. * age: 101
  7872. * };
  7873. * const favorite = R.prop('favoriteLibrary');
  7874. * const favoriteWithDefault = R.propOr('Ramda', 'favoriteLibrary');
  7875. *
  7876. * favorite(alice); //=> undefined
  7877. * favoriteWithDefault(alice); //=> 'Ramda'
  7878. */
  7879. var propOr = _curry3(function propOr(val, p, obj) {
  7880. return defaultTo(val, prop(p, obj));
  7881. });
  7882. /**
  7883. * Returns `true` if the specified object property satisfies the given
  7884. * predicate; `false` otherwise. You can test multiple properties with
  7885. * [`R.where`](#where).
  7886. *
  7887. * @func
  7888. * @memberOf R
  7889. * @since v0.16.0
  7890. * @category Logic
  7891. * @sig (a -> Boolean) -> String -> {String: a} -> Boolean
  7892. * @param {Function} pred
  7893. * @param {String} name
  7894. * @param {*} obj
  7895. * @return {Boolean}
  7896. * @see R.where, R.propEq, R.propIs
  7897. * @example
  7898. *
  7899. * R.propSatisfies(x => x > 0, 'x', {x: 1, y: 2}); //=> true
  7900. */
  7901. var propSatisfies = _curry3(function propSatisfies(pred, name, obj) {
  7902. return pred(prop(name, obj));
  7903. });
  7904. /**
  7905. * Acts as multiple `prop`: array of keys in, array of values out. Preserves
  7906. * order.
  7907. *
  7908. * @func
  7909. * @memberOf R
  7910. * @since v0.1.0
  7911. * @category Object
  7912. * @sig [k] -> {k: v} -> [v]
  7913. * @param {Array} ps The property names to fetch
  7914. * @param {Object} obj The object to query
  7915. * @return {Array} The corresponding values or partially applied function.
  7916. * @see R.prop, R.pluck, R.project
  7917. * @example
  7918. *
  7919. * R.props(['x', 'y'], {x: 1, y: 2}); //=> [1, 2]
  7920. * R.props(['c', 'a', 'b'], {b: 2, a: 1}); //=> [undefined, 1, 2]
  7921. *
  7922. * const fullName = R.compose(R.join(' '), R.props(['first', 'last']));
  7923. * fullName({last: 'Bullet-Tooth', age: 33, first: 'Tony'}); //=> 'Tony Bullet-Tooth'
  7924. */
  7925. var props = _curry2(function props(ps, obj) {
  7926. return ps.map(function (p) {
  7927. return path([p], obj);
  7928. });
  7929. });
  7930. /**
  7931. * Returns a list of numbers from `from` (inclusive) to `to` (exclusive).
  7932. *
  7933. * @func
  7934. * @memberOf R
  7935. * @since v0.1.0
  7936. * @category List
  7937. * @sig Number -> Number -> [Number]
  7938. * @param {Number} from The first number in the list.
  7939. * @param {Number} to One more than the last number in the list.
  7940. * @return {Array} The list of numbers in the set `[a, b)`.
  7941. * @example
  7942. *
  7943. * R.range(1, 5); //=> [1, 2, 3, 4]
  7944. * R.range(50, 53); //=> [50, 51, 52]
  7945. */
  7946. var range = _curry2(function range(from, to) {
  7947. if (!(_isNumber(from) && _isNumber(to))) {
  7948. throw new TypeError('Both arguments to range must be numbers');
  7949. }
  7950. var result = [];
  7951. var n = from;
  7952. while (n < to) {
  7953. result.push(n);
  7954. n += 1;
  7955. }
  7956. return result;
  7957. });
  7958. /**
  7959. * Returns a single item by iterating through the list, successively calling
  7960. * the iterator function and passing it an accumulator value and the current
  7961. * value from the array, and then passing the result to the next call.
  7962. *
  7963. * Similar to [`reduce`](#reduce), except moves through the input list from the
  7964. * right to the left.
  7965. *
  7966. * The iterator function receives two values: *(value, acc)*, while the arguments'
  7967. * order of `reduce`'s iterator function is *(acc, value)*. `reduceRight` may use [`reduced`](#reduced)
  7968. * to short circuit the iteration.
  7969. *
  7970. * Note: `R.reduceRight` does not skip deleted or unassigned indices (sparse
  7971. * arrays), unlike the native `Array.prototype.reduceRight` method. For more details
  7972. * on this behavior, see:
  7973. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight#Description
  7974. *
  7975. * Be cautious of mutating and returning the accumulator. If you reuse it across
  7976. * invocations, it will continue to accumulate onto the same value. The general
  7977. * recommendation is to always return a new value. If you can't do so for
  7978. * performance reasons, then be sure to reinitialize the accumulator on each
  7979. * invocation.
  7980. *
  7981. * @func
  7982. * @memberOf R
  7983. * @since v0.1.0
  7984. * @category List
  7985. * @sig ((a, b) -> b) -> b -> [a] -> b
  7986. * @param {Function} fn The iterator function. Receives two values, the current element from the array
  7987. * and the accumulator.
  7988. * @param {*} acc The accumulator value.
  7989. * @param {Array} list The list to iterate over.
  7990. * @return {*} The final, accumulated value.
  7991. * @see R.reduce, R.addIndex, R.reduced
  7992. * @example
  7993. *
  7994. * R.reduceRight(R.subtract, 0, [1, 2, 3, 4]) // => (1 - (2 - (3 - (4 - 0)))) = -2
  7995. * // - -2
  7996. * // / \ / \
  7997. * // 1 - 1 3
  7998. * // / \ / \
  7999. * // 2 - ==> 2 -1
  8000. * // / \ / \
  8001. * // 3 - 3 4
  8002. * // / \ / \
  8003. * // 4 0 4 0
  8004. *
  8005. * @symb R.reduceRight(f, a, [b, c, d]) = f(b, f(c, f(d, a)))
  8006. */
  8007. var reduceRight = _curry3(function reduceRight(fn, acc, list) {
  8008. var idx = list.length - 1;
  8009. while (idx >= 0) {
  8010. acc = fn(list[idx], acc);
  8011. if (acc && acc['@@transducer/reduced']) {
  8012. acc = acc['@@transducer/value'];
  8013. break;
  8014. }
  8015. idx -= 1;
  8016. }
  8017. return acc;
  8018. });
  8019. /**
  8020. * Like [`reduce`](#reduce), `reduceWhile` returns a single item by iterating
  8021. * through the list, successively calling the iterator function. `reduceWhile`
  8022. * also takes a predicate that is evaluated before each step. If the predicate
  8023. * returns `false`, it "short-circuits" the iteration and returns the current
  8024. * value of the accumulator. `reduceWhile` may alternatively be short-circuited
  8025. * via [`reduced`](#reduced).
  8026. *
  8027. * @func
  8028. * @memberOf R
  8029. * @since v0.22.0
  8030. * @category List
  8031. * @sig ((a, b) -> Boolean) -> ((a, b) -> a) -> a -> [b] -> a
  8032. * @param {Function} pred The predicate. It is passed the accumulator and the
  8033. * current element.
  8034. * @param {Function} fn The iterator function. Receives two values, the
  8035. * accumulator and the current element.
  8036. * @param {*} a The accumulator value.
  8037. * @param {Array} list The list to iterate over.
  8038. * @return {*} The final, accumulated value.
  8039. * @see R.reduce, R.reduced
  8040. * @example
  8041. *
  8042. * const isOdd = (acc, x) => x % 2 !== 0;
  8043. * const xs = [1, 3, 5, 60, 777, 800];
  8044. * R.reduceWhile(isOdd, R.add, 0, xs); //=> 9
  8045. *
  8046. * const ys = [2, 4, 6]
  8047. * R.reduceWhile(isOdd, R.add, 111, ys); //=> 111
  8048. */
  8049. var reduceWhile = _curryN(4, [], function _reduceWhile(pred, fn, a, list) {
  8050. var xf = _xwrap(function (acc, x) {
  8051. return pred(acc, x) ? fn(acc, x) : _reduced(acc);
  8052. });
  8053. return _xReduce(xf, a, list);
  8054. });
  8055. /**
  8056. * Returns a value wrapped to indicate that it is the final value of the reduce
  8057. * and transduce functions. The returned value should be considered a black
  8058. * box: the internal structure is not guaranteed to be stable.
  8059. *
  8060. * This optimization is available to the below functions:
  8061. * - [`reduce`](#reduce)
  8062. * - [`reduceWhile`](#reduceWhile)
  8063. * - [`reduceBy`](#reduceBy)
  8064. * - [`reduceRight`](#reduceRight)
  8065. * - [`transduce`](#transduce)
  8066. *
  8067. * @func
  8068. * @memberOf R
  8069. * @since v0.15.0
  8070. * @category List
  8071. * @sig a -> *
  8072. * @param {*} x The final value of the reduce.
  8073. * @return {*} The wrapped value.
  8074. * @see R.reduce, R.reduceWhile, R.reduceBy, R.reduceRight, R.transduce
  8075. * @example
  8076. *
  8077. * R.reduce(
  8078. * (acc, item) => item > 3 ? R.reduced(acc) : acc.concat(item),
  8079. * [],
  8080. * [1, 2, 3, 4, 5]) // [1, 2, 3]
  8081. */
  8082. var reduced = _curry1(_reduced);
  8083. /**
  8084. * Calls an input function `n` times, returning an array containing the results
  8085. * of those function calls.
  8086. *
  8087. * `fn` is passed one argument: The current value of `n`, which begins at `0`
  8088. * and is gradually incremented to `n - 1`.
  8089. *
  8090. * @func
  8091. * @memberOf R
  8092. * @since v0.2.3
  8093. * @category List
  8094. * @sig (Number -> a) -> Number -> [a]
  8095. * @param {Function} fn The function to invoke. Passed one argument, the current value of `n`.
  8096. * @param {Number} n A value between `0` and `n - 1`. Increments after each function call.
  8097. * @return {Array} An array containing the return values of all calls to `fn`.
  8098. * @see R.repeat
  8099. * @example
  8100. *
  8101. * R.times(R.identity, 5); //=> [0, 1, 2, 3, 4]
  8102. * @symb R.times(f, 0) = []
  8103. * @symb R.times(f, 1) = [f(0)]
  8104. * @symb R.times(f, 2) = [f(0), f(1)]
  8105. */
  8106. var times = _curry2(function times(fn, n) {
  8107. var len = Number(n);
  8108. var idx = 0;
  8109. var list;
  8110. if (len < 0 || isNaN(len)) {
  8111. throw new RangeError('n must be a non-negative number');
  8112. }
  8113. list = [];
  8114. while (idx < len) {
  8115. list.push(fn(idx));
  8116. idx += 1;
  8117. }
  8118. return list;
  8119. });
  8120. /**
  8121. * Returns a fixed list of size `n` containing a specified identical value.
  8122. *
  8123. * @func
  8124. * @memberOf R
  8125. * @since v0.1.1
  8126. * @category List
  8127. * @sig a -> n -> [a]
  8128. * @param {*} value The value to repeat.
  8129. * @param {Number} n The desired size of the output list.
  8130. * @return {Array} A new array containing `n` `value`s.
  8131. * @see R.times
  8132. * @example
  8133. *
  8134. * R.repeat('hi', 5); //=> ['hi', 'hi', 'hi', 'hi', 'hi']
  8135. *
  8136. * const obj = {};
  8137. * const repeatedObjs = R.repeat(obj, 5); //=> [{}, {}, {}, {}, {}]
  8138. * repeatedObjs[0] === repeatedObjs[1]; //=> true
  8139. * @symb R.repeat(a, 0) = []
  8140. * @symb R.repeat(a, 1) = [a]
  8141. * @symb R.repeat(a, 2) = [a, a]
  8142. */
  8143. var repeat = _curry2(function repeat(value, n) {
  8144. return times(always(value), n);
  8145. });
  8146. /**
  8147. * Replace a substring or regex match in a string with a replacement.
  8148. *
  8149. * The first two parameters correspond to the parameters of the
  8150. * `String.prototype.replace()` function, so the second parameter can also be a
  8151. * function.
  8152. *
  8153. * @func
  8154. * @memberOf R
  8155. * @since v0.7.0
  8156. * @category String
  8157. * @sig RegExp|String -> String -> String -> String
  8158. * @param {RegExp|String} pattern A regular expression or a substring to match.
  8159. * @param {String} replacement The string to replace the matches with.
  8160. * @param {String} str The String to do the search and replacement in.
  8161. * @return {String} The result.
  8162. * @example
  8163. *
  8164. * R.replace('foo', 'bar', 'foo foo foo'); //=> 'bar foo foo'
  8165. * R.replace(/foo/, 'bar', 'foo foo foo'); //=> 'bar foo foo'
  8166. *
  8167. * // Use the "g" (global) flag to replace all occurrences:
  8168. * R.replace(/foo/g, 'bar', 'foo foo foo'); //=> 'bar bar bar'
  8169. */
  8170. var replace = _curry3(function replace(regex, replacement, str) {
  8171. return str.replace(regex, replacement);
  8172. });
  8173. var tInit$1 = '@@transducer/init';
  8174. var tStep$1 = '@@transducer/step';
  8175. function XScan(reducer, acc, xf) {
  8176. this.xf = xf;
  8177. this.f = reducer;
  8178. this.acc = acc;
  8179. }
  8180. XScan.prototype[tInit$1] = function () {
  8181. return this.xf[tStep$1](this.xf[tInit$1](), this.acc);
  8182. };
  8183. XScan.prototype['@@transducer/result'] = _xfBase.result;
  8184. XScan.prototype[tStep$1] = function (result, input) {
  8185. if (result['@@transducer/reduced']) {
  8186. return result;
  8187. }
  8188. this.acc = this.f(this.acc, input);
  8189. return this.xf[tStep$1](result, this.acc);
  8190. };
  8191. var _xscan = _curry3(function _xscan(reducer, acc, xf) {
  8192. return new XScan(reducer, acc, xf);
  8193. });
  8194. /**
  8195. * Scan is similar to [`reduce`](#reduce), but returns a list of successively
  8196. * reduced values from the left.
  8197. *
  8198. * Acts as a transducer if a transformer is given in list position.
  8199. *
  8200. * @func
  8201. * @memberOf R
  8202. * @since v0.10.0
  8203. * @category List
  8204. * @sig ((a, b) -> a) -> a -> [b] -> [a]
  8205. * @param {Function} fn The iterator function. Receives two values, the accumulator and the
  8206. * current element from the array
  8207. * @param {*} acc The accumulator value.
  8208. * @param {Array} list The list to iterate over.
  8209. * @return {Array} A list of all intermediately reduced values.
  8210. * @see R.reduce, R.mapAccum
  8211. * @example
  8212. *
  8213. * const numbers = [1, 2, 3, 4];
  8214. * const factorials = R.scan(R.multiply, 1, numbers); //=> [1, 1, 2, 6, 24]
  8215. * @symb R.scan(f, a, [b, c]) = [a, f(a, b), f(f(a, b), c)]
  8216. */
  8217. var scan = _curry3(_dispatchable([], _xscan, function scan(fn, acc, list) {
  8218. var idx = 0;
  8219. var len = list.length;
  8220. var result = [acc];
  8221. while (idx < len) {
  8222. acc = fn(acc, list[idx]);
  8223. result[idx + 1] = acc;
  8224. idx += 1;
  8225. }
  8226. return result;
  8227. }));
  8228. /**
  8229. * Transforms a [Traversable](https://github.com/fantasyland/fantasy-land#traversable)
  8230. * of [Applicative](https://github.com/fantasyland/fantasy-land#applicative) into an
  8231. * Applicative of Traversable.
  8232. *
  8233. * Dispatches to the `"fantasy-land/traverse"` or the `traverse` method of the second argument, if present.
  8234. *
  8235. * @func
  8236. * @memberOf R
  8237. * @since v0.19.0
  8238. * @category List
  8239. * @sig fantasy-land/of :: TypeRep f => f ~> a -> f a
  8240. * @sig (Applicative f, Traversable t) => TypeRep f -> t (f a) -> f (t a)
  8241. * @sig (Applicative f, Traversable t) => (a -> f a) -> t (f a) -> f (t a)
  8242. * @param {Object|Function} TypeRepresentative with an `of` or `fantasy-land/of` method
  8243. * @param {*} traversable
  8244. * @return {*}
  8245. * @see R.traverse
  8246. * @example
  8247. *
  8248. * R.sequence(Maybe.of, [Just(1), Just(2), Just(3)]); //=> Just([1, 2, 3])
  8249. * R.sequence(Maybe.of, [Just(1), Just(2), Nothing()]); //=> Nothing()
  8250. *
  8251. * R.sequence(R.of(Array), Just([1, 2, 3])); //=> [Just(1), Just(2), Just(3)]
  8252. * R.sequence(R.of(Array), Nothing()); //=> [Nothing()]
  8253. */
  8254. var sequence = _curry2(function sequence(F, traversable) {
  8255. var of = typeof F['fantasy-land/of'] === 'function' ? F['fantasy-land/of'] : typeof F.of === 'function' ? F.of : F;
  8256. var TypeRep = {
  8257. 'fantasy-land/of': of
  8258. };
  8259. return typeof traversable['fantasy-land/traverse'] === 'function' ? traversable['fantasy-land/traverse'](TypeRep, _identity) : typeof traversable.traverse === 'function' ? traversable.traverse(TypeRep, _identity) : reduceRight(function (x, acc) {
  8260. return ap(map(prepend, x), acc);
  8261. }, of([]), traversable);
  8262. });
  8263. /**
  8264. * Returns the result of "setting" the portion of the given data structure
  8265. * focused by the given lens to the given value.
  8266. *
  8267. * @func
  8268. * @memberOf R
  8269. * @since v0.16.0
  8270. * @category Object
  8271. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  8272. * @sig Lens s a -> a -> s -> s
  8273. * @param {Lens} lens
  8274. * @param {*} v
  8275. * @param {*} x
  8276. * @return {*}
  8277. * @see R.view, R.over, R.lens, R.lensIndex, R.lensProp, R.lensPath
  8278. * @example
  8279. *
  8280. * const xLens = R.lensProp('x');
  8281. *
  8282. * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
  8283. * R.set(xLens, 8, {x: 1, y: 2}); //=> {x: 8, y: 2}
  8284. */
  8285. var set = _curry3(function set(lens, v, x) {
  8286. return over(lens, always(v), x);
  8287. });
  8288. /**
  8289. * Returns a copy of the list, sorted according to the comparator function,
  8290. * which should accept two values at a time and return a negative number if the
  8291. * first value is smaller, a positive number if it's larger, and zero if they
  8292. * are equal. Please note that this is a **copy** of the list. It does not
  8293. * modify the original.
  8294. *
  8295. * @func
  8296. * @memberOf R
  8297. * @since v0.1.0
  8298. * @category List
  8299. * @sig ((a, a) -> Number) -> [a] -> [a]
  8300. * @param {Function} comparator A sorting function :: a -> b -> Int
  8301. * @param {Array} list The list to sort
  8302. * @return {Array} a new array with its elements sorted by the comparator function.
  8303. * @see R.ascend, R.descend
  8304. * @example
  8305. *
  8306. * const diff = function(a, b) { return a - b; };
  8307. * R.sort(diff, [4,2,7,5]); //=> [2, 4, 5, 7]
  8308. */
  8309. var sort = _curry2(function sort(comparator, list) {
  8310. return Array.prototype.slice.call(list, 0).sort(comparator);
  8311. });
  8312. /**
  8313. * Sorts the list according to the supplied function.
  8314. *
  8315. * @func
  8316. * @memberOf R
  8317. * @since v0.1.0
  8318. * @category Relation
  8319. * @sig Ord b => (a -> b) -> [a] -> [a]
  8320. * @param {Function} fn
  8321. * @param {Array} list The list to sort.
  8322. * @return {Array} A new list sorted by the keys generated by `fn`.
  8323. * @example
  8324. *
  8325. * const sortByFirstItem = R.sortBy(R.prop(0));
  8326. * const pairs = [[-1, 1], [-2, 2], [-3, 3]];
  8327. * sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
  8328. *
  8329. * const sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name')));
  8330. * const alice = {
  8331. * name: 'ALICE',
  8332. * age: 101
  8333. * };
  8334. * const bob = {
  8335. * name: 'Bob',
  8336. * age: -10
  8337. * };
  8338. * const clara = {
  8339. * name: 'clara',
  8340. * age: 314.159
  8341. * };
  8342. * const people = [clara, bob, alice];
  8343. * sortByNameCaseInsensitive(people); //=> [alice, bob, clara]
  8344. */
  8345. var sortBy = _curry2(function sortBy(fn, list) {
  8346. return Array.prototype.slice.call(list, 0).sort(function (a, b) {
  8347. var aa = fn(a);
  8348. var bb = fn(b);
  8349. return aa < bb ? -1 : aa > bb ? 1 : 0;
  8350. });
  8351. });
  8352. /**
  8353. * Sorts a list according to a list of comparators.
  8354. *
  8355. * @func
  8356. * @memberOf R
  8357. * @since v0.23.0
  8358. * @category Relation
  8359. * @sig [(a, a) -> Number] -> [a] -> [a]
  8360. * @param {Array} functions A list of comparator functions.
  8361. * @param {Array} list The list to sort.
  8362. * @return {Array} A new list sorted according to the comarator functions.
  8363. * @see R.ascend, R.descend
  8364. * @example
  8365. *
  8366. * const alice = {
  8367. * name: 'alice',
  8368. * age: 40
  8369. * };
  8370. * const bob = {
  8371. * name: 'bob',
  8372. * age: 30
  8373. * };
  8374. * const clara = {
  8375. * name: 'clara',
  8376. * age: 40
  8377. * };
  8378. * const people = [clara, bob, alice];
  8379. * const ageNameSort = R.sortWith([
  8380. * R.descend(R.prop('age')),
  8381. * R.ascend(R.prop('name'))
  8382. * ]);
  8383. * ageNameSort(people); //=> [alice, clara, bob]
  8384. */
  8385. var sortWith = _curry2(function sortWith(fns, list) {
  8386. return Array.prototype.slice.call(list, 0).sort(function (a, b) {
  8387. var result = 0;
  8388. var i = 0;
  8389. while (result === 0 && i < fns.length) {
  8390. result = fns[i](a, b);
  8391. i += 1;
  8392. }
  8393. return result;
  8394. });
  8395. });
  8396. /**
  8397. * Splits a string into an array of strings based on the given
  8398. * separator.
  8399. *
  8400. * @func
  8401. * @memberOf R
  8402. * @since v0.1.0
  8403. * @category String
  8404. * @sig (String | RegExp) -> String -> [String]
  8405. * @param {String|RegExp} sep The pattern.
  8406. * @param {String} str The string to separate into an array.
  8407. * @return {Array} The array of strings from `str` separated by `sep`.
  8408. * @see R.join
  8409. * @example
  8410. *
  8411. * const pathComponents = R.split('/');
  8412. * R.tail(pathComponents('/usr/local/bin/node')); //=> ['usr', 'local', 'bin', 'node']
  8413. *
  8414. * R.split('.', 'a.b.c.xyz.d'); //=> ['a', 'b', 'c', 'xyz', 'd']
  8415. */
  8416. var split = invoker(1, 'split');
  8417. /**
  8418. * Splits a given list or string at a given index.
  8419. *
  8420. * @func
  8421. * @memberOf R
  8422. * @since v0.19.0
  8423. * @category List
  8424. * @sig Number -> [a] -> [[a], [a]]
  8425. * @sig Number -> String -> [String, String]
  8426. * @param {Number} index The index where the array/string is split.
  8427. * @param {Array|String} array The array/string to be split.
  8428. * @return {Array}
  8429. * @example
  8430. *
  8431. * R.splitAt(1, [1, 2, 3]); //=> [[1], [2, 3]]
  8432. * R.splitAt(5, 'hello world'); //=> ['hello', ' world']
  8433. * R.splitAt(-1, 'foobar'); //=> ['fooba', 'r']
  8434. */
  8435. var splitAt = _curry2(function splitAt(index, array) {
  8436. return [slice(0, index, array), slice(index, length(array), array)];
  8437. });
  8438. /**
  8439. * Splits a collection into slices of the specified length.
  8440. *
  8441. * @func
  8442. * @memberOf R
  8443. * @since v0.16.0
  8444. * @category List
  8445. * @sig Number -> [a] -> [[a]]
  8446. * @sig Number -> String -> [String]
  8447. * @param {Number} n
  8448. * @param {Array} list
  8449. * @return {Array}
  8450. * @example
  8451. *
  8452. * R.splitEvery(3, [1, 2, 3, 4, 5, 6, 7]); //=> [[1, 2, 3], [4, 5, 6], [7]]
  8453. * R.splitEvery(3, 'foobarbaz'); //=> ['foo', 'bar', 'baz']
  8454. */
  8455. var splitEvery = _curry2(function splitEvery(n, list) {
  8456. if (n <= 0) {
  8457. throw new Error('First argument to splitEvery must be a positive integer');
  8458. }
  8459. var result = [];
  8460. var idx = 0;
  8461. while (idx < list.length) {
  8462. result.push(slice(idx, idx += n, list));
  8463. }
  8464. return result;
  8465. });
  8466. /**
  8467. * Takes a list and a predicate and returns a pair of lists with the following properties:
  8468. *
  8469. * - the result of concatenating the two output lists is equivalent to the input list;
  8470. * - none of the elements of the first output list satisfies the predicate; and
  8471. * - if the second output list is non-empty, its first element satisfies the predicate.
  8472. *
  8473. * @func
  8474. * @memberOf R
  8475. * @since v0.19.0
  8476. * @category List
  8477. * @sig (a -> Boolean) -> [a] -> [[a], [a]]
  8478. * @param {Function} pred The predicate that determines where the array is split.
  8479. * @param {Array} list The array to be split.
  8480. * @return {Array}
  8481. * @example
  8482. *
  8483. * R.splitWhen(R.equals(2), [1, 2, 3, 1, 2, 3]); //=> [[1], [2, 3, 1, 2, 3]]
  8484. */
  8485. var splitWhen = _curry2(function splitWhen(pred, list) {
  8486. var idx = 0;
  8487. var len = list.length;
  8488. var prefix = [];
  8489. while (idx < len && !pred(list[idx])) {
  8490. prefix.push(list[idx]);
  8491. idx += 1;
  8492. }
  8493. return [prefix, Array.prototype.slice.call(list, idx)];
  8494. });
  8495. /**
  8496. * Splits an array into slices on every occurrence of a value.
  8497. *
  8498. * @func
  8499. * @memberOf R
  8500. * @since v0.26.1
  8501. * @category List
  8502. * @sig (a -> Boolean) -> [a] -> [[a]]
  8503. * @param {Function} pred The predicate that determines where the array is split.
  8504. * @param {Array} list The array to be split.
  8505. * @return {Array}
  8506. * @example
  8507. *
  8508. * R.splitWhenever(R.equals(2), [1, 2, 3, 2, 4, 5, 2, 6, 7]); //=> [[1], [3], [4, 5], [6, 7]]
  8509. */
  8510. var splitWhenever = _curryN(2, [], function splitWhenever(pred, list) {
  8511. var acc = [];
  8512. var curr = [];
  8513. for (var i = 0; i < list.length; i = i + 1) {
  8514. if (!pred(list[i])) {
  8515. curr.push(list[i]);
  8516. }
  8517. if ((i < list.length - 1 && pred(list[i + 1]) || i === list.length - 1) && curr.length > 0) {
  8518. acc.push(curr);
  8519. curr = [];
  8520. }
  8521. }
  8522. return acc;
  8523. });
  8524. /**
  8525. * Checks if a list starts with the provided sublist.
  8526. *
  8527. * Similarly, checks if a string starts with the provided substring.
  8528. *
  8529. * @func
  8530. * @memberOf R
  8531. * @since v0.24.0
  8532. * @category List
  8533. * @sig [a] -> [a] -> Boolean
  8534. * @sig String -> String -> Boolean
  8535. * @param {*} prefix
  8536. * @param {*} list
  8537. * @return {Boolean}
  8538. * @see R.endsWith
  8539. * @example
  8540. *
  8541. * R.startsWith('a', 'abc') //=> true
  8542. * R.startsWith('b', 'abc') //=> false
  8543. * R.startsWith(['a'], ['a', 'b', 'c']) //=> true
  8544. * R.startsWith(['b'], ['a', 'b', 'c']) //=> false
  8545. */
  8546. var startsWith = _curry2(function (prefix, list) {
  8547. return equals(take(prefix.length, list), prefix);
  8548. });
  8549. /**
  8550. * Subtracts its second argument from its first argument.
  8551. *
  8552. * @func
  8553. * @memberOf R
  8554. * @since v0.1.0
  8555. * @category Math
  8556. * @sig Number -> Number -> Number
  8557. * @param {Number} a The first value.
  8558. * @param {Number} b The second value.
  8559. * @return {Number} The result of `a - b`.
  8560. * @see R.add
  8561. * @example
  8562. *
  8563. * R.subtract(10, 8); //=> 2
  8564. *
  8565. * const minus5 = R.subtract(R.__, 5);
  8566. * minus5(17); //=> 12
  8567. *
  8568. * const complementaryAngle = R.subtract(90);
  8569. * complementaryAngle(30); //=> 60
  8570. * complementaryAngle(72); //=> 18
  8571. */
  8572. var subtract = _curry2(function subtract(a, b) {
  8573. return Number(a) - Number(b);
  8574. });
  8575. var swapObject = function swapObject(indexA, indexB, o) {
  8576. var copy = clone(o);
  8577. var properties = Object.getOwnPropertyNames(copy);
  8578. if (properties.includes(indexA) && properties.includes(indexB)) {
  8579. var tmp = copy[indexA];
  8580. copy[indexA] = copy[indexB];
  8581. copy[indexB] = tmp;
  8582. }
  8583. return copy;
  8584. };
  8585. var swapList = function swapList(indexA, indexB, list) {
  8586. var length = list.length;
  8587. var result = list.slice();
  8588. var positiveIndexA = indexA < 0 ? length + indexA : indexA;
  8589. var positiveIndexB = indexB < 0 ? length + indexB : indexB;
  8590. var positiveMin = Math.min(positiveIndexA, positiveIndexB);
  8591. var positiveMax = Math.max(positiveIndexA, positiveIndexB);
  8592. if (positiveIndexA < 0 || positiveIndexA > length) {
  8593. return result;
  8594. }
  8595. if (positiveIndexB < 0 || positiveIndexB > length) {
  8596. return result;
  8597. }
  8598. if (positiveIndexA === positiveIndexB) {
  8599. return result;
  8600. }
  8601. result = [].concat(result.slice(0, positiveMin)).concat(result[positiveMax]).concat(result.slice(positiveMin + 1, positiveMax)).concat(result[positiveMin]).concat(result.slice(positiveMax + 1, length));
  8602. return result;
  8603. };
  8604. var swapString = function swapString(indexA, indexB, s) {
  8605. var result = swapList(indexA, indexB, s);
  8606. return _isArray(result) ? result.join('') : result;
  8607. };
  8608. /**
  8609. * Swap an item, at index `indexA` with another item, at index `indexB`, in an object or a list of elements.
  8610. * A new result will be created containing the new elements order.
  8611. *
  8612. * @func
  8613. * @memberOf R
  8614. * @since v0.29.0
  8615. * @category List
  8616. * @sig Number -> Number -> [a] -> [a]
  8617. * @param {Number|string|Object} indexA The first index
  8618. * @param {Number|string|Object} indexB The second index
  8619. * @param {Array|Object} o Either the object or list which will serve to realise the swap
  8620. * @return {Array|Object} The new object or list reordered
  8621. * @example
  8622. *
  8623. * R.swap(0, 2, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['c', 'b', 'a', 'd', 'e', 'f']
  8624. * R.swap(-1, 0, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['f', 'b', 'c', 'd', 'e', 'a'] list rotation
  8625. * R.swap('a', 'b', {a: 1, b: 2}); //=> {a: 2, b: 2}
  8626. * R.swap(0, 2, 'foo'); //=> 'oof'
  8627. */
  8628. var swap = _curry3(function (indexA, indexB, o) {
  8629. if (_isArray(o)) {
  8630. return swapList(indexA, indexB, o);
  8631. } else if (_isString(o)) {
  8632. return swapString(indexA, indexB, o);
  8633. } else {
  8634. return swapObject(indexA, indexB, o);
  8635. }
  8636. });
  8637. /**
  8638. * Finds the set (i.e. no duplicates) of all elements contained in the first or
  8639. * second list, but not both.
  8640. *
  8641. * @func
  8642. * @memberOf R
  8643. * @since v0.19.0
  8644. * @category Relation
  8645. * @sig [*] -> [*] -> [*]
  8646. * @param {Array} list1 The first list.
  8647. * @param {Array} list2 The second list.
  8648. * @return {Array} The elements in `list1` or `list2`, but not both.
  8649. * @see R.symmetricDifferenceWith, R.difference, R.differenceWith
  8650. * @example
  8651. *
  8652. * R.symmetricDifference([1,2,3,4], [7,6,5,4,3]); //=> [1,2,7,6,5]
  8653. * R.symmetricDifference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5,1,2]
  8654. */
  8655. var symmetricDifference = _curry2(function symmetricDifference(list1, list2) {
  8656. return concat(difference(list1, list2), difference(list2, list1));
  8657. });
  8658. /**
  8659. * Finds the set (i.e. no duplicates) of all elements contained in the first or
  8660. * second list, but not both. Duplication is determined according to the value
  8661. * returned by applying the supplied predicate to two list elements.
  8662. *
  8663. * @func
  8664. * @memberOf R
  8665. * @since v0.19.0
  8666. * @category Relation
  8667. * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
  8668. * @param {Function} pred A predicate used to test whether two items are equal.
  8669. * @param {Array} list1 The first list.
  8670. * @param {Array} list2 The second list.
  8671. * @return {Array} The elements in `list1` or `list2`, but not both.
  8672. * @see R.symmetricDifference, R.difference, R.differenceWith
  8673. * @example
  8674. *
  8675. * const eqA = R.eqBy(R.prop('a'));
  8676. * const l1 = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
  8677. * const l2 = [{a: 3}, {a: 4}, {a: 5}, {a: 6}];
  8678. * R.symmetricDifferenceWith(eqA, l1, l2); //=> [{a: 1}, {a: 2}, {a: 5}, {a: 6}]
  8679. */
  8680. var symmetricDifferenceWith = _curry3(function symmetricDifferenceWith(pred, list1, list2) {
  8681. return concat(differenceWith(pred, list1, list2), differenceWith(pred, list2, list1));
  8682. });
  8683. /**
  8684. * Returns a new list containing the last `n` elements of a given list, passing
  8685. * each value to the supplied predicate function, and terminating when the
  8686. * predicate function returns `false`. Excludes the element that caused the
  8687. * predicate function to fail. The predicate function is passed one argument:
  8688. * *(value)*.
  8689. *
  8690. * @func
  8691. * @memberOf R
  8692. * @since v0.16.0
  8693. * @category List
  8694. * @sig (a -> Boolean) -> [a] -> [a]
  8695. * @sig (a -> Boolean) -> String -> String
  8696. * @param {Function} fn The function called per iteration.
  8697. * @param {Array} xs The collection to iterate over.
  8698. * @return {Array} A new array.
  8699. * @see R.dropLastWhile, R.addIndex
  8700. * @example
  8701. *
  8702. * const isNotOne = x => x !== 1;
  8703. *
  8704. * R.takeLastWhile(isNotOne, [1, 2, 3, 4]); //=> [2, 3, 4]
  8705. *
  8706. * R.takeLastWhile(x => x !== 'R' , 'Ramda'); //=> 'amda'
  8707. */
  8708. var takeLastWhile = _curry2(function takeLastWhile(fn, xs) {
  8709. var idx = xs.length - 1;
  8710. while (idx >= 0 && fn(xs[idx])) {
  8711. idx -= 1;
  8712. }
  8713. return slice(idx + 1, Infinity, xs);
  8714. });
  8715. function XTakeWhile(f, xf) {
  8716. this.xf = xf;
  8717. this.f = f;
  8718. }
  8719. XTakeWhile.prototype['@@transducer/init'] = _xfBase.init;
  8720. XTakeWhile.prototype['@@transducer/result'] = _xfBase.result;
  8721. XTakeWhile.prototype['@@transducer/step'] = function (result, input) {
  8722. return this.f(input) ? this.xf['@@transducer/step'](result, input) : _reduced(result);
  8723. };
  8724. function _xtakeWhile(f) {
  8725. return function (xf) {
  8726. return new XTakeWhile(f, xf);
  8727. };
  8728. }
  8729. /**
  8730. * Returns a new list containing the first `n` elements of a given list,
  8731. * passing each value to the supplied predicate function, and terminating when
  8732. * the predicate function returns `false`. Excludes the element that caused the
  8733. * predicate function to fail. The predicate function is passed one argument:
  8734. * *(value)*.
  8735. *
  8736. * Dispatches to the `takeWhile` method of the second argument, if present.
  8737. *
  8738. * Acts as a transducer if a transformer is given in list position.
  8739. *
  8740. * @func
  8741. * @memberOf R
  8742. * @since v0.1.0
  8743. * @category List
  8744. * @sig (a -> Boolean) -> [a] -> [a]
  8745. * @sig (a -> Boolean) -> String -> String
  8746. * @param {Function} fn The function called per iteration.
  8747. * @param {Array} xs The collection to iterate over.
  8748. * @return {Array} A new array.
  8749. * @see R.dropWhile, R.transduce, R.addIndex
  8750. * @example
  8751. *
  8752. * const isNotFour = x => x !== 4;
  8753. *
  8754. * R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3]
  8755. *
  8756. * R.takeWhile(x => x !== 'd' , 'Ramda'); //=> 'Ram'
  8757. */
  8758. var takeWhile = _curry2(_dispatchable(['takeWhile'], _xtakeWhile, function takeWhile(fn, xs) {
  8759. var idx = 0;
  8760. var len = xs.length;
  8761. while (idx < len && fn(xs[idx])) {
  8762. idx += 1;
  8763. }
  8764. return slice(0, idx, xs);
  8765. }));
  8766. function XTap(f, xf) {
  8767. this.xf = xf;
  8768. this.f = f;
  8769. }
  8770. XTap.prototype['@@transducer/init'] = _xfBase.init;
  8771. XTap.prototype['@@transducer/result'] = _xfBase.result;
  8772. XTap.prototype['@@transducer/step'] = function (result, input) {
  8773. this.f(input);
  8774. return this.xf['@@transducer/step'](result, input);
  8775. };
  8776. function _xtap(f) {
  8777. return function (xf) {
  8778. return new XTap(f, xf);
  8779. };
  8780. }
  8781. /**
  8782. * Runs the given function with the supplied object, then returns the object.
  8783. *
  8784. * Acts as a transducer if a transformer is given as second parameter.
  8785. *
  8786. * @func
  8787. * @memberOf R
  8788. * @since v0.1.0
  8789. * @category Function
  8790. * @sig (a -> *) -> a -> a
  8791. * @param {Function} fn The function to call with `x`. The return value of `fn` will be thrown away.
  8792. * @param {*} x
  8793. * @return {*} `x`.
  8794. * @example
  8795. *
  8796. * const sayX = x => console.log('x is ' + x);
  8797. * R.tap(sayX, 100); //=> 100
  8798. * // logs 'x is 100'
  8799. * @symb R.tap(f, a) = (f(a), a)
  8800. */
  8801. var tap = _curry2(_dispatchable([], _xtap, function tap(fn, x) {
  8802. fn(x);
  8803. return x;
  8804. }));
  8805. function _isRegExp(x) {
  8806. return Object.prototype.toString.call(x) === '[object RegExp]';
  8807. }
  8808. /**
  8809. * Determines whether a given string matches a given regular expression.
  8810. *
  8811. * @func
  8812. * @memberOf R
  8813. * @since v0.12.0
  8814. * @category String
  8815. * @sig RegExp -> String -> Boolean
  8816. * @param {RegExp} pattern
  8817. * @param {String} str
  8818. * @return {Boolean}
  8819. * @see R.match
  8820. * @example
  8821. *
  8822. * R.test(/^x/, 'xyz'); //=> true
  8823. * R.test(/^y/, 'xyz'); //=> false
  8824. */
  8825. var test = _curry2(function test(pattern, str) {
  8826. if (!_isRegExp(pattern)) {
  8827. throw new TypeError('‘test’ requires a value of type RegExp as its first argument; received ' + toString$1(pattern));
  8828. }
  8829. return _cloneRegExp(pattern).test(str);
  8830. });
  8831. /**
  8832. * Returns the result of applying the onSuccess function to the value inside
  8833. * a successfully resolved promise. This is useful for working with promises
  8834. * inside function compositions.
  8835. *
  8836. * @func
  8837. * @memberOf R
  8838. * @since v0.27.1
  8839. * @category Function
  8840. * @sig (a -> b) -> (Promise e a) -> (Promise e b)
  8841. * @sig (a -> (Promise e b)) -> (Promise e a) -> (Promise e b)
  8842. * @param {Function} onSuccess The function to apply. Can return a value or a promise of a value.
  8843. * @param {Promise} p
  8844. * @return {Promise} The result of calling `p.then(onSuccess)`
  8845. * @see R.otherwise
  8846. * @example
  8847. *
  8848. * const makeQuery = email => ({ query: { email }});
  8849. * const fetchMember = request =>
  8850. * Promise.resolve({ firstName: 'Bob', lastName: 'Loblaw', id: 42 });
  8851. *
  8852. * //getMemberName :: String -> Promise ({ firstName, lastName })
  8853. * const getMemberName = R.pipe(
  8854. * makeQuery,
  8855. * fetchMember,
  8856. * R.andThen(R.pick(['firstName', 'lastName']))
  8857. * );
  8858. *
  8859. * getMemberName('bob@gmail.com').then(console.log);
  8860. */
  8861. var andThen = _curry2(function andThen(f, p) {
  8862. _assertPromise('andThen', p);
  8863. return p.then(f);
  8864. });
  8865. /**
  8866. * The lower case version of a string.
  8867. *
  8868. * @func
  8869. * @memberOf R
  8870. * @since v0.9.0
  8871. * @category String
  8872. * @sig String -> String
  8873. * @param {String} str The string to lower case.
  8874. * @return {String} The lower case version of `str`.
  8875. * @see R.toUpper
  8876. * @example
  8877. *
  8878. * R.toLower('XYZ'); //=> 'xyz'
  8879. */
  8880. var toLower = invoker(0, 'toLowerCase');
  8881. /**
  8882. * Converts an object into an array of key, value arrays. Only the object's
  8883. * own properties are used.
  8884. * Note that the order of the output array is not guaranteed to be consistent
  8885. * across different JS platforms.
  8886. *
  8887. * @func
  8888. * @memberOf R
  8889. * @since v0.4.0
  8890. * @category Object
  8891. * @sig {String: *} -> [[String,*]]
  8892. * @param {Object} obj The object to extract from
  8893. * @return {Array} An array of key, value arrays from the object's own properties.
  8894. * @see R.fromPairs, R.keys, R.values
  8895. * @example
  8896. *
  8897. * R.toPairs({a: 1, b: 2, c: 3}); //=> [['a', 1], ['b', 2], ['c', 3]]
  8898. */
  8899. var toPairs = _curry1(function toPairs(obj) {
  8900. var pairs = [];
  8901. for (var prop in obj) {
  8902. if (_has(prop, obj)) {
  8903. pairs[pairs.length] = [prop, obj[prop]];
  8904. }
  8905. }
  8906. return pairs;
  8907. });
  8908. /**
  8909. * Converts an object into an array of key, value arrays. The object's own
  8910. * properties and prototype properties are used. Note that the order of the
  8911. * output array is not guaranteed to be consistent across different JS
  8912. * platforms.
  8913. *
  8914. * @func
  8915. * @memberOf R
  8916. * @since v0.4.0
  8917. * @category Object
  8918. * @sig {String: *} -> [[String,*]]
  8919. * @param {Object} obj The object to extract from
  8920. * @return {Array} An array of key, value arrays from the object's own
  8921. * and prototype properties.
  8922. * @example
  8923. *
  8924. * const F = function() { this.x = 'X'; };
  8925. * F.prototype.y = 'Y';
  8926. * const f = new F();
  8927. * R.toPairsIn(f); //=> [['x','X'], ['y','Y']]
  8928. */
  8929. var toPairsIn = _curry1(function toPairsIn(obj) {
  8930. var pairs = [];
  8931. for (var prop in obj) {
  8932. pairs[pairs.length] = [prop, obj[prop]];
  8933. }
  8934. return pairs;
  8935. });
  8936. /**
  8937. * The upper case version of a string.
  8938. *
  8939. * @func
  8940. * @memberOf R
  8941. * @since v0.9.0
  8942. * @category String
  8943. * @sig String -> String
  8944. * @param {String} str The string to upper case.
  8945. * @return {String} The upper case version of `str`.
  8946. * @see R.toLower
  8947. * @example
  8948. *
  8949. * R.toUpper('abc'); //=> 'ABC'
  8950. */
  8951. var toUpper = invoker(0, 'toUpperCase');
  8952. /**
  8953. * Initializes a transducer using supplied iterator function. Returns a single
  8954. * item by iterating through the list, successively calling the transformed
  8955. * iterator function and passing it an accumulator value and the current value
  8956. * from the array, and then passing the result to the next call.
  8957. *
  8958. * The iterator function receives two values: *(acc, value)*. It will be
  8959. * wrapped as a transformer to initialize the transducer. A transformer can be
  8960. * passed directly in place of an iterator function. In both cases, iteration
  8961. * may be stopped early with the [`R.reduced`](#reduced) function.
  8962. *
  8963. * A transducer is a function that accepts a transformer and returns a
  8964. * transformer and can be composed directly.
  8965. *
  8966. * A transformer is an object that provides a 2-arity reducing iterator
  8967. * function, step, 0-arity initial value function, init, and 1-arity result
  8968. * extraction function, result. The step function is used as the iterator
  8969. * function in reduce. The result function is used to convert the final
  8970. * accumulator into the return type and in most cases is
  8971. * [`R.identity`](#identity). The init function can be used to provide an
  8972. * initial accumulator, but is ignored by transduce.
  8973. *
  8974. * The iteration is performed with [`R.reduce`](#reduce) after initializing the transducer.
  8975. *
  8976. * @func
  8977. * @memberOf R
  8978. * @since v0.12.0
  8979. * @category List
  8980. * @sig (c -> c) -> ((a, b) -> a) -> a -> [b] -> a
  8981. * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
  8982. * @param {Function} fn The iterator function. Receives two values, the accumulator and the
  8983. * current element from the array. Wrapped as transformer, if necessary, and used to
  8984. * initialize the transducer
  8985. * @param {*} acc The initial accumulator value.
  8986. * @param {Array} list The list to iterate over.
  8987. * @return {*} The final, accumulated value.
  8988. * @see R.reduce, R.reduced, R.into
  8989. * @example
  8990. *
  8991. * const numbers = [1, 2, 3, 4];
  8992. * const transducer = R.compose(R.map(R.add(1)), R.take(2));
  8993. * R.transduce(transducer, R.flip(R.append), [], numbers); //=> [2, 3]
  8994. *
  8995. * const isOdd = (x) => x % 2 !== 0;
  8996. * const firstOddTransducer = R.compose(R.filter(isOdd), R.take(1));
  8997. * R.transduce(firstOddTransducer, R.flip(R.append), [], R.range(0, 100)); //=> [1]
  8998. */
  8999. var transduce = curryN(4, function transduce(xf, fn, acc, list) {
  9000. return _xReduce(xf(typeof fn === 'function' ? _xwrap(fn) : fn), acc, list);
  9001. });
  9002. /**
  9003. * Transposes the rows and columns of a 2D list.
  9004. * When passed a list of `n` lists of length `x`,
  9005. * returns a list of `x` lists of length `n`.
  9006. *
  9007. *
  9008. * @func
  9009. * @memberOf R
  9010. * @since v0.19.0
  9011. * @category List
  9012. * @sig [[a]] -> [[a]]
  9013. * @param {Array} list A 2D list
  9014. * @return {Array} A 2D list
  9015. * @example
  9016. *
  9017. * R.transpose([[1, 'a'], [2, 'b'], [3, 'c']]) //=> [[1, 2, 3], ['a', 'b', 'c']]
  9018. * R.transpose([[1, 2, 3], ['a', 'b', 'c']]) //=> [[1, 'a'], [2, 'b'], [3, 'c']]
  9019. *
  9020. * // If some of the rows are shorter than the following rows, their elements are skipped:
  9021. * R.transpose([[10, 11], [20], [], [30, 31, 32]]) //=> [[10, 20, 30], [11, 31], [32]]
  9022. * @symb R.transpose([[a], [b], [c]]) = [a, b, c]
  9023. * @symb R.transpose([[a, b], [c, d]]) = [[a, c], [b, d]]
  9024. * @symb R.transpose([[a, b], [c]]) = [[a, c], [b]]
  9025. */
  9026. var transpose = _curry1(function transpose(outerlist) {
  9027. var i = 0;
  9028. var result = [];
  9029. while (i < outerlist.length) {
  9030. var innerlist = outerlist[i];
  9031. var j = 0;
  9032. while (j < innerlist.length) {
  9033. if (typeof result[j] === 'undefined') {
  9034. result[j] = [];
  9035. }
  9036. result[j].push(innerlist[j]);
  9037. j += 1;
  9038. }
  9039. i += 1;
  9040. }
  9041. return result;
  9042. });
  9043. /**
  9044. * Maps an [Applicative](https://github.com/fantasyland/fantasy-land#applicative)-returning
  9045. * function over a [Traversable](https://github.com/fantasyland/fantasy-land#traversable),
  9046. * then uses [`sequence`](#sequence) to transform the resulting Traversable of Applicative
  9047. * into an Applicative of Traversable.
  9048. *
  9049. * Dispatches to the `traverse` method of the third argument, if present.
  9050. *
  9051. * @func
  9052. * @memberOf R
  9053. * @since v0.19.0
  9054. * @category List
  9055. * @sig fantasy-land/of :: TypeRep f => f ~> a -> f a
  9056. * @sig (Applicative f, Traversable t) => TypeRep f -> (a -> f b) -> t a -> f (t b)
  9057. * @sig (Applicative f, Traversable t) => (b -> f b) -> (a -> f b) -> t a -> f (t b)
  9058. * @param {Object|Function} TypeRepresentative with an `of` or `fantasy-land/of` method
  9059. * @param {Function} f
  9060. * @param {*} traversable
  9061. * @return {*}
  9062. * @see R.sequence
  9063. * @example
  9064. *
  9065. * // Returns `Maybe.Nothing` if the given divisor is `0`
  9066. * const safeDiv = n => d => d === 0 ? Maybe.Nothing() : Maybe.Just(n / d)
  9067. *
  9068. * R.traverse(Maybe.of, safeDiv(10), [2, 4, 5]); //=> Maybe.Just([5, 2.5, 2])
  9069. * R.traverse(Maybe.of, safeDiv(10), [2, 0, 5]); //=> Maybe.Nothing
  9070. *
  9071. * // Using a Type Representative
  9072. * R.traverse(Maybe, safeDiv(10), Right(4)); //=> Just(Right(2.5))
  9073. * R.traverse(Maybe, safeDiv(10), Right(0)); //=> Nothing
  9074. * R.traverse(Maybe, safeDiv(10), Left("X")); //=> Just(Left("X"))
  9075. */
  9076. var traverse = _curry3(function traverse(F, f, traversable) {
  9077. var of = typeof F['fantasy-land/of'] === 'function' ? F['fantasy-land/of'] : typeof F.of === 'function' ? F.of : F;
  9078. var TypeRep = {
  9079. 'fantasy-land/of': of
  9080. };
  9081. return typeof traversable['fantasy-land/traverse'] === 'function' ? traversable['fantasy-land/traverse'](TypeRep, f) : typeof traversable.traverse === 'function' ? traversable.traverse(TypeRep, f) : sequence(TypeRep, map(f, traversable));
  9082. });
  9083. var ws = "\t\n\x0B\f\r \xA0\u1680\u2000\u2001\u2002\u2003" + "\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028" + "\u2029\uFEFF";
  9084. var zeroWidth = "\u200B";
  9085. var hasProtoTrim = typeof String.prototype.trim === 'function';
  9086. /**
  9087. * Removes (strips) whitespace from both ends of the string.
  9088. *
  9089. * @func
  9090. * @memberOf R
  9091. * @since v0.6.0
  9092. * @category String
  9093. * @sig String -> String
  9094. * @param {String} str The string to trim.
  9095. * @return {String} Trimmed version of `str`.
  9096. * @example
  9097. *
  9098. * R.trim(' xyz '); //=> 'xyz'
  9099. * R.map(R.trim, R.split(',', 'x, y, z')); //=> ['x', 'y', 'z']
  9100. */
  9101. var trim = !hasProtoTrim || ws.trim() || !zeroWidth.trim() ? _curry1(function trim(str) {
  9102. var beginRx = new RegExp('^[' + ws + '][' + ws + ']*');
  9103. var endRx = new RegExp('[' + ws + '][' + ws + ']*$');
  9104. return str.replace(beginRx, '').replace(endRx, '');
  9105. }) : _curry1(function trim(str) {
  9106. return str.trim();
  9107. });
  9108. /**
  9109. * `tryCatch` takes two functions, a `tryer` and a `catcher`. The returned
  9110. * function evaluates the `tryer`; if it does not throw, it simply returns the
  9111. * result. If the `tryer` *does* throw, the returned function evaluates the
  9112. * `catcher` function and returns its result. Note that for effective
  9113. * composition with this function, both the `tryer` and `catcher` functions
  9114. * must return the same type of results.
  9115. *
  9116. * @func
  9117. * @memberOf R
  9118. * @since v0.20.0
  9119. * @category Function
  9120. * @sig (...x -> a) -> ((e, ...x) -> a) -> (...x -> a)
  9121. * @param {Function} tryer The function that may throw.
  9122. * @param {Function} catcher The function that will be evaluated if `tryer` throws.
  9123. * @return {Function} A new function that will catch exceptions and send them to the catcher.
  9124. * @example
  9125. *
  9126. * R.tryCatch(R.prop('x'), R.F)({x: true}); //=> true
  9127. * R.tryCatch(() => { throw 'foo'}, R.always('caught'))('bar') // =>
  9128. * 'caught'
  9129. * R.tryCatch(R.times(R.identity), R.always([]))('s') // => []
  9130. * R.tryCatch(() => { throw 'this is not a valid value'}, (err, value)=>({error : err, value }))('bar') // => {'error': 'this is not a valid value', 'value': 'bar'}
  9131. */
  9132. var tryCatch = _curry2(function _tryCatch(tryer, catcher) {
  9133. return _arity(tryer.length, function () {
  9134. try {
  9135. return tryer.apply(this, arguments);
  9136. } catch (e) {
  9137. return catcher.apply(this, _concat([e], arguments));
  9138. }
  9139. });
  9140. });
  9141. /**
  9142. * Takes a function `fn`, which takes a single array argument, and returns a
  9143. * function which:
  9144. *
  9145. * - takes any number of positional arguments;
  9146. * - passes these arguments to `fn` as an array; and
  9147. * - returns the result.
  9148. *
  9149. * In other words, `R.unapply` derives a variadic function from a function which
  9150. * takes an array. `R.unapply` is the inverse of [`R.apply`](#apply).
  9151. *
  9152. * @func
  9153. * @memberOf R
  9154. * @since v0.8.0
  9155. * @category Function
  9156. * @sig ([*...] -> a) -> (*... -> a)
  9157. * @param {Function} fn
  9158. * @return {Function}
  9159. * @see R.apply
  9160. * @example
  9161. *
  9162. * R.unapply(JSON.stringify)(1, 2, 3); //=> '[1,2,3]'
  9163. * @symb R.unapply(f)(a, b) = f([a, b])
  9164. */
  9165. var unapply = _curry1(function unapply(fn) {
  9166. return function () {
  9167. return fn(Array.prototype.slice.call(arguments, 0));
  9168. };
  9169. });
  9170. /**
  9171. * Wraps a function of any arity (including nullary) in a function that accepts
  9172. * exactly 1 parameter. Any extraneous parameters will not be passed to the
  9173. * supplied function.
  9174. *
  9175. * @func
  9176. * @memberOf R
  9177. * @since v0.2.0
  9178. * @category Function
  9179. * @sig (a -> b -> c -> ... -> z) -> (a -> z)
  9180. * @param {Function} fn The function to wrap.
  9181. * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
  9182. * arity 1.
  9183. * @see R.binary, R.nAry
  9184. * @example
  9185. *
  9186. * const takesTwoArgs = function(a, b) {
  9187. * return [a, b];
  9188. * };
  9189. * takesTwoArgs.length; //=> 2
  9190. * takesTwoArgs(1, 2); //=> [1, 2]
  9191. *
  9192. * const takesOneArg = R.unary(takesTwoArgs);
  9193. * takesOneArg.length; //=> 1
  9194. * // Only 1 argument is passed to the wrapped function
  9195. * takesOneArg(1, 2); //=> [1, undefined]
  9196. * @symb R.unary(f)(a, b, c) = f(a)
  9197. */
  9198. var unary = _curry1(function unary(fn) {
  9199. return nAry(1, fn);
  9200. });
  9201. /**
  9202. * Returns a function of arity `n` from a (manually) curried function.
  9203. * Note that, the returned function is actually a ramda style
  9204. * curryied function, which can accept one or more arguments in each
  9205. * function calling.
  9206. *
  9207. * @func
  9208. * @memberOf R
  9209. * @since v0.14.0
  9210. * @category Function
  9211. * @sig Number -> (a -> b -> c ... -> z) -> ((a -> b -> c ...) -> z)
  9212. * @param {Number} length The arity for the returned function.
  9213. * @param {Function} fn The function to uncurry.
  9214. * @return {Function} A new function.
  9215. * @see R.curry, R.curryN
  9216. * @example
  9217. *
  9218. * const addFour = a => b => c => d => a + b + c + d;
  9219. *
  9220. * const uncurriedAddFour = R.uncurryN(4, addFour);
  9221. * uncurriedAddFour(1, 2, 3, 4); //=> 10
  9222. */
  9223. var uncurryN = _curry2(function uncurryN(depth, fn) {
  9224. return curryN(depth, function () {
  9225. var currentDepth = 1;
  9226. var value = fn;
  9227. var idx = 0;
  9228. var endIdx;
  9229. while (currentDepth <= depth && typeof value === 'function') {
  9230. endIdx = currentDepth === depth ? arguments.length : idx + value.length;
  9231. value = value.apply(this, Array.prototype.slice.call(arguments, idx, endIdx));
  9232. currentDepth += 1;
  9233. idx = endIdx;
  9234. }
  9235. return value;
  9236. });
  9237. });
  9238. /**
  9239. * Builds a list from a seed value. Accepts an iterator function, which returns
  9240. * either false to stop iteration or an array of length 2 containing the value
  9241. * to add to the resulting list and the seed to be used in the next call to the
  9242. * iterator function.
  9243. *
  9244. * The iterator function receives one argument: *(seed)*.
  9245. *
  9246. * @func
  9247. * @memberOf R
  9248. * @since v0.10.0
  9249. * @category List
  9250. * @sig (a -> [b]) -> * -> [b]
  9251. * @param {Function} fn The iterator function. receives one argument, `seed`, and returns
  9252. * either false to quit iteration or an array of length two to proceed. The element
  9253. * at index 0 of this array will be added to the resulting array, and the element
  9254. * at index 1 will be passed to the next call to `fn`.
  9255. * @param {*} seed The seed value.
  9256. * @return {Array} The final list.
  9257. * @example
  9258. *
  9259. * const f = n => n > 50 ? false : [-n, n + 10];
  9260. * R.unfold(f, 10); //=> [-10, -20, -30, -40, -50]
  9261. * @symb R.unfold(f, x) = [f(x)[0], f(f(x)[1])[0], f(f(f(x)[1])[1])[0], ...]
  9262. */
  9263. var unfold = _curry2(function unfold(fn, seed) {
  9264. var pair = fn(seed);
  9265. var result = [];
  9266. while (pair && pair.length) {
  9267. result[result.length] = pair[0];
  9268. pair = fn(pair[1]);
  9269. }
  9270. return result;
  9271. });
  9272. /**
  9273. * Combines two lists into a set (i.e. no duplicates) composed of the elements
  9274. * of each list.
  9275. *
  9276. * @func
  9277. * @memberOf R
  9278. * @since v0.1.0
  9279. * @category Relation
  9280. * @sig [*] -> [*] -> [*]
  9281. * @param {Array} as The first list.
  9282. * @param {Array} bs The second list.
  9283. * @return {Array} The first and second lists concatenated, with
  9284. * duplicates removed.
  9285. * @example
  9286. *
  9287. * R.union([1, 2, 3], [2, 3, 4]); //=> [1, 2, 3, 4]
  9288. */
  9289. var union = _curry2(compose(uniq, _concat));
  9290. function XUniqWith(pred, xf) {
  9291. this.xf = xf;
  9292. this.pred = pred;
  9293. this.items = [];
  9294. }
  9295. XUniqWith.prototype['@@transducer/init'] = _xfBase.init;
  9296. XUniqWith.prototype['@@transducer/result'] = _xfBase.result;
  9297. XUniqWith.prototype['@@transducer/step'] = function (result, input) {
  9298. if (_includesWith(this.pred, input, this.items)) {
  9299. return result;
  9300. } else {
  9301. this.items.push(input);
  9302. return this.xf['@@transducer/step'](result, input);
  9303. }
  9304. };
  9305. function _xuniqWith(pred) {
  9306. return function (xf) {
  9307. return new XUniqWith(pred, xf);
  9308. };
  9309. }
  9310. /**
  9311. * Returns a new list containing only one copy of each element in the original
  9312. * list, based upon the value returned by applying the supplied predicate to
  9313. * two list elements. Prefers the first item if two items compare equal based
  9314. * on the predicate.
  9315. *
  9316. * Acts as a transducer if a transformer is given in list position.
  9317. *
  9318. * @func
  9319. * @memberOf R
  9320. * @since v0.2.0
  9321. * @category List
  9322. * @sig ((a, a) -> Boolean) -> [a] -> [a]
  9323. * @param {Function} pred A predicate used to test whether two items are equal.
  9324. * @param {Array} list The array to consider.
  9325. * @return {Array} The list of unique items.
  9326. * @example
  9327. *
  9328. * const strEq = R.eqBy(String);
  9329. * R.uniqWith(strEq)([1, '1', 2, 1]); //=> [1, 2]
  9330. * R.uniqWith(strEq)([{}, {}]); //=> [{}]
  9331. * R.uniqWith(strEq)([1, '1', 1]); //=> [1]
  9332. * R.uniqWith(strEq)(['1', 1, 1]); //=> ['1']
  9333. */
  9334. var uniqWith = _curry2(_dispatchable([], _xuniqWith, function (pred, list) {
  9335. var idx = 0;
  9336. var len = list.length;
  9337. var result = [];
  9338. var item;
  9339. while (idx < len) {
  9340. item = list[idx];
  9341. if (!_includesWith(pred, item, result)) {
  9342. result[result.length] = item;
  9343. }
  9344. idx += 1;
  9345. }
  9346. return result;
  9347. }));
  9348. /**
  9349. * Combines two lists into a set (i.e. no duplicates) composed of the elements
  9350. * of each list. Duplication is determined according to the value returned by
  9351. * applying the supplied predicate to two list elements. If an element exists
  9352. * in both lists, the first element from the first list will be used.
  9353. *
  9354. * @func
  9355. * @memberOf R
  9356. * @since v0.1.0
  9357. * @category Relation
  9358. * @sig ((a, a) -> Boolean) -> [*] -> [*] -> [*]
  9359. * @param {Function} pred A predicate used to test whether two items are equal.
  9360. * @param {Array} list1 The first list.
  9361. * @param {Array} list2 The second list.
  9362. * @return {Array} The first and second lists concatenated, with
  9363. * duplicates removed.
  9364. * @see R.union
  9365. * @example
  9366. *
  9367. * const l1 = [{a: 1}, {a: 2}];
  9368. * const l2 = [{a: 1}, {a: 4}];
  9369. * R.unionWith(R.eqBy(R.prop('a')), l1, l2); //=> [{a: 1}, {a: 2}, {a: 4}]
  9370. */
  9371. var unionWith = _curry3(function unionWith(pred, list1, list2) {
  9372. return uniqWith(pred, _concat(list1, list2));
  9373. });
  9374. /**
  9375. * Tests the final argument by passing it to the given predicate function. If
  9376. * the predicate is not satisfied, the function will return the result of
  9377. * calling the `whenFalseFn` function with the same argument. If the predicate
  9378. * is satisfied, the argument is returned as is.
  9379. *
  9380. * @func
  9381. * @memberOf R
  9382. * @since v0.18.0
  9383. * @category Logic
  9384. * @sig (a -> Boolean) -> (a -> b) -> a -> a | b
  9385. * @param {Function} pred A predicate function
  9386. * @param {Function} whenFalseFn A function to invoke when the `pred` evaluates
  9387. * to a falsy value.
  9388. * @param {*} x An object to test with the `pred` function and
  9389. * pass to `whenFalseFn` if necessary.
  9390. * @return {*} Either `x` or the result of applying `x` to `whenFalseFn`.
  9391. * @see R.ifElse, R.when, R.cond
  9392. * @example
  9393. *
  9394. * let safeInc = R.unless(R.isNil, R.inc);
  9395. * safeInc(null); //=> null
  9396. * safeInc(1); //=> 2
  9397. */
  9398. var unless = _curry3(function unless(pred, whenFalseFn, x) {
  9399. return pred(x) ? x : whenFalseFn(x);
  9400. });
  9401. /**
  9402. * Shorthand for `R.chain(R.identity)`, which removes one level of nesting from
  9403. * any [Chain](https://github.com/fantasyland/fantasy-land#chain).
  9404. *
  9405. * @func
  9406. * @memberOf R
  9407. * @since v0.3.0
  9408. * @category List
  9409. * @sig Chain c => c (c a) -> c a
  9410. * @param {*} list
  9411. * @return {*}
  9412. * @see R.flatten, R.chain
  9413. * @example
  9414. *
  9415. * R.unnest([1, [2], [[3]]]); //=> [1, 2, [3]]
  9416. * R.unnest([[1, 2], [3, 4], [5, 6]]); //=> [1, 2, 3, 4, 5, 6]
  9417. */
  9418. var unnest = chain(_identity);
  9419. /**
  9420. * Takes a predicate, a transformation function, and an initial value,
  9421. * and returns a value of the same type as the initial value.
  9422. * It does so by applying the transformation until the predicate is satisfied,
  9423. * at which point it returns the satisfactory value.
  9424. *
  9425. * @func
  9426. * @memberOf R
  9427. * @since v0.20.0
  9428. * @category Logic
  9429. * @sig (a -> Boolean) -> (a -> a) -> a -> a
  9430. * @param {Function} pred A predicate function
  9431. * @param {Function} fn The iterator function
  9432. * @param {*} init Initial value
  9433. * @return {*} Final value that satisfies predicate
  9434. * @example
  9435. *
  9436. * R.until(R.gt(R.__, 100), R.multiply(2))(1) // => 128
  9437. */
  9438. var until = _curry3(function until(pred, fn, init) {
  9439. var val = init;
  9440. while (!pred(val)) {
  9441. val = fn(val);
  9442. }
  9443. return val;
  9444. });
  9445. /**
  9446. *
  9447. * Deconstructs an array field from the input documents to output a document for each element.
  9448. * Each output document is the input document with the value of the array field replaced by the element.
  9449. *
  9450. * @func
  9451. * @memberOf R
  9452. * @since v0.28.0
  9453. * @category Object
  9454. * @sig String -> {k: [v]} -> [{k: v}]
  9455. * @param {String} key The key to determine which property of the object should be unwind
  9456. * @param {Object} object The object containing list under property named as key which is to unwind
  9457. * @return {List} A new list of object containing the value of input key having list replaced by each element in the object.
  9458. * @example
  9459. *
  9460. * R.unwind('hobbies', {
  9461. * name: 'alice',
  9462. * hobbies: ['Golf', 'Hacking'],
  9463. * colors: ['red', 'green'],
  9464. * });
  9465. * // [
  9466. * // { name: 'alice', hobbies: 'Golf', colors: ['red', 'green'] },
  9467. * // { name: 'alice', hobbies: 'Hacking', colors: ['red', 'green'] }
  9468. * // ]
  9469. */
  9470. var unwind = _curry2(function (key, object) {
  9471. // If key is not in object or key is not as a list in object
  9472. if (!(key in object && _isArray(object[key]))) {
  9473. return [object];
  9474. } // Map over object[key] which is a list and assoc each element with key
  9475. return _map(function (item) {
  9476. return _assoc(key, item, object);
  9477. }, object[key]);
  9478. });
  9479. /**
  9480. * Returns a list of all the properties, including prototype properties, of the
  9481. * supplied object.
  9482. * Note that the order of the output array is not guaranteed to be consistent
  9483. * across different JS platforms.
  9484. *
  9485. * @func
  9486. * @memberOf R
  9487. * @since v0.2.0
  9488. * @category Object
  9489. * @sig {k: v} -> [v]
  9490. * @param {Object} obj The object to extract values from
  9491. * @return {Array} An array of the values of the object's own and prototype properties.
  9492. * @see R.values, R.keysIn
  9493. * @example
  9494. *
  9495. * const F = function() { this.x = 'X'; };
  9496. * F.prototype.y = 'Y';
  9497. * const f = new F();
  9498. * R.valuesIn(f); //=> ['X', 'Y']
  9499. */
  9500. var valuesIn = _curry1(function valuesIn(obj) {
  9501. var prop;
  9502. var vs = [];
  9503. for (prop in obj) {
  9504. vs[vs.length] = obj[prop];
  9505. }
  9506. return vs;
  9507. });
  9508. var Const = function Const(x) {
  9509. return {
  9510. value: x,
  9511. 'fantasy-land/map': function fantasyLandMap() {
  9512. return this;
  9513. }
  9514. };
  9515. };
  9516. /**
  9517. * Returns a "view" of the given data structure, determined by the given lens.
  9518. * The lens's focus determines which portion of the data structure is visible.
  9519. *
  9520. * @func
  9521. * @memberOf R
  9522. * @since v0.16.0
  9523. * @category Object
  9524. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  9525. * @sig Lens s a -> s -> a
  9526. * @param {Lens} lens
  9527. * @param {*} x
  9528. * @return {*}
  9529. * @see R.set, R.over, R.lens, R.lensIndex, R.lensProp, R.lensPath
  9530. * @example
  9531. *
  9532. * const xLens = R.lensProp('x');
  9533. *
  9534. * R.view(xLens, {x: 1, y: 2}); //=> 1
  9535. * R.view(xLens, {x: 4, y: 2}); //=> 4
  9536. */
  9537. var view = _curry2(function view(lens, x) {
  9538. // Using `Const` effectively ignores the setter function of the `lens`,
  9539. // leaving the value returned by the getter function unmodified.
  9540. return lens(Const)(x).value;
  9541. });
  9542. /**
  9543. * Tests the final argument by passing it to the given predicate function. If
  9544. * the predicate is satisfied, the function will return the result of calling
  9545. * the `whenTrueFn` function with the same argument. If the predicate is not
  9546. * satisfied, the argument is returned as is.
  9547. *
  9548. * @func
  9549. * @memberOf R
  9550. * @since v0.18.0
  9551. * @category Logic
  9552. * @sig (a -> Boolean) -> (a -> b) -> a -> a | b
  9553. * @param {Function} pred A predicate function
  9554. * @param {Function} whenTrueFn A function to invoke when the `condition`
  9555. * evaluates to a truthy value.
  9556. * @param {*} x An object to test with the `pred` function and
  9557. * pass to `whenTrueFn` if necessary.
  9558. * @return {*} Either `x` or the result of applying `x` to `whenTrueFn`.
  9559. * @see R.ifElse, R.unless, R.cond
  9560. * @example
  9561. *
  9562. * // truncate :: String -> String
  9563. * const truncate = R.when(
  9564. * R.propSatisfies(R.gt(R.__, 10), 'length'),
  9565. * R.pipe(R.take(10), R.append('…'), R.join(''))
  9566. * );
  9567. * truncate('12345'); //=> '12345'
  9568. * truncate('0123456789ABC'); //=> '0123456789…'
  9569. */
  9570. var when = _curry3(function when(pred, whenTrueFn, x) {
  9571. return pred(x) ? whenTrueFn(x) : x;
  9572. });
  9573. /**
  9574. * Takes a spec object and a test object; returns true if the test satisfies
  9575. * the spec. Each of the spec's own properties must be a predicate function.
  9576. * Each predicate is applied to the value of the corresponding property of the
  9577. * test object. `where` returns true if all the predicates return true, false
  9578. * otherwise.
  9579. *
  9580. * `where` is well suited to declaratively expressing constraints for other
  9581. * functions such as [`filter`](#filter) and [`find`](#find).
  9582. *
  9583. * @func
  9584. * @memberOf R
  9585. * @since v0.1.1
  9586. * @category Object
  9587. * @sig {String: (* -> Boolean)} -> {String: *} -> Boolean
  9588. * @param {Object} spec
  9589. * @param {Object} testObj
  9590. * @return {Boolean}
  9591. * @see R.propSatisfies, R.whereEq
  9592. * @example
  9593. *
  9594. * // pred :: Object -> Boolean
  9595. * const pred = R.where({
  9596. * a: R.equals('foo'),
  9597. * b: R.complement(R.equals('bar')),
  9598. * x: R.gt(R.__, 10),
  9599. * y: R.lt(R.__, 20)
  9600. * });
  9601. *
  9602. * pred({a: 'foo', b: 'xxx', x: 11, y: 19}); //=> true
  9603. * pred({a: 'xxx', b: 'xxx', x: 11, y: 19}); //=> false
  9604. * pred({a: 'foo', b: 'bar', x: 11, y: 19}); //=> false
  9605. * pred({a: 'foo', b: 'xxx', x: 10, y: 19}); //=> false
  9606. * pred({a: 'foo', b: 'xxx', x: 11, y: 20}); //=> false
  9607. */
  9608. var where = _curry2(function where(spec, testObj) {
  9609. for (var prop in spec) {
  9610. if (_has(prop, spec) && !spec[prop](testObj[prop])) {
  9611. return false;
  9612. }
  9613. }
  9614. return true;
  9615. });
  9616. /**
  9617. * Takes a spec object and a test object; each of the spec's own properties must be a predicate function.
  9618. * Each predicate is applied to the value of the corresponding property of the
  9619. * test object. `whereAny` returns true if at least one of the predicates return true,
  9620. * false otherwise.
  9621. *
  9622. * `whereAny` is well suited to declaratively expressing constraints for other
  9623. * functions such as [`filter`](#filter) and [`find`](#find).
  9624. *
  9625. * @func
  9626. * @memberOf R
  9627. * @since v0.28.0
  9628. * @category Object
  9629. * @sig {String: (* -> Boolean)} -> {String: *} -> Boolean
  9630. * @param {Object} spec
  9631. * @param {Object} testObj
  9632. * @return {Boolean}
  9633. * @see R.propSatisfies, R.where
  9634. * @example
  9635. *
  9636. * // pred :: Object -> Boolean
  9637. * const pred = R.whereAny({
  9638. * a: R.equals('foo'),
  9639. * b: R.complement(R.equals('xxx')),
  9640. * x: R.gt(R.__, 10),
  9641. * y: R.lt(R.__, 20)
  9642. * });
  9643. *
  9644. * pred({a: 'foo', b: 'xxx', x: 8, y: 34}); //=> true
  9645. * pred({a: 'xxx', b: 'xxx', x: 9, y: 21}); //=> false
  9646. * pred({a: 'bar', b: 'xxx', x: 10, y: 20}); //=> false
  9647. * pred({a: 'foo', b: 'bar', x: 10, y: 20}); //=> true
  9648. * pred({a: 'foo', b: 'xxx', x: 11, y: 20}); //=> true
  9649. */
  9650. var whereAny = _curry2(function whereAny(spec, testObj) {
  9651. for (var prop in spec) {
  9652. if (_has(prop, spec) && spec[prop](testObj[prop])) {
  9653. return true;
  9654. }
  9655. }
  9656. return false;
  9657. });
  9658. /**
  9659. * Takes a spec object and a test object; returns true if the test satisfies
  9660. * the spec, false otherwise. An object satisfies the spec if, for each of the
  9661. * spec's own properties, accessing that property of the object gives the same
  9662. * value (in [`R.equals`](#equals) terms) as accessing that property of the
  9663. * spec.
  9664. *
  9665. * `whereEq` is a specialization of [`where`](#where).
  9666. *
  9667. * @func
  9668. * @memberOf R
  9669. * @since v0.14.0
  9670. * @category Object
  9671. * @sig {String: *} -> {String: *} -> Boolean
  9672. * @param {Object} spec
  9673. * @param {Object} testObj
  9674. * @return {Boolean}
  9675. * @see R.propEq, R.where
  9676. * @example
  9677. *
  9678. * // pred :: Object -> Boolean
  9679. * const pred = R.whereEq({a: 1, b: 2});
  9680. *
  9681. * pred({a: 1}); //=> false
  9682. * pred({a: 1, b: 2}); //=> true
  9683. * pred({a: 1, b: 2, c: 3}); //=> true
  9684. * pred({a: 1, b: 1}); //=> false
  9685. */
  9686. var whereEq = _curry2(function whereEq(spec, testObj) {
  9687. return where(map(equals, spec), testObj);
  9688. });
  9689. /**
  9690. * Returns a new list without values in the first argument.
  9691. * [`R.equals`](#equals) is used to determine equality.
  9692. *
  9693. * Acts as a transducer if a transformer is given in list position.
  9694. *
  9695. * @func
  9696. * @memberOf R
  9697. * @since v0.19.0
  9698. * @category List
  9699. * @sig [a] -> [a] -> [a]
  9700. * @param {Array} list1 The values to be removed from `list2`.
  9701. * @param {Array} list2 The array to remove values from.
  9702. * @return {Array} The new array without values in `list1`.
  9703. * @see R.transduce, R.difference, R.remove
  9704. * @example
  9705. *
  9706. * R.without([1, 2], [1, 2, 1, 3, 4]); //=> [3, 4]
  9707. */
  9708. var without = _curry2(function without(xs, list) {
  9709. var toRemove = new _Set();
  9710. for (var i = 0; i < xs.length; i += 1) {
  9711. toRemove.add(xs[i]);
  9712. }
  9713. return reject(toRemove.has.bind(toRemove), list);
  9714. });
  9715. /**
  9716. * Exclusive disjunction logical operation.
  9717. * Returns `true` if one of the arguments is truthy and the other is falsy.
  9718. * Otherwise, it returns `false`.
  9719. *
  9720. * @func
  9721. * @memberOf R
  9722. * @since v0.27.1
  9723. * @category Logic
  9724. * @sig a -> b -> Boolean
  9725. * @param {Any} a
  9726. * @param {Any} b
  9727. * @return {Boolean} true if one of the arguments is truthy and the other is falsy
  9728. * @see R.or, R.and
  9729. * @example
  9730. *
  9731. * R.xor(true, true); //=> false
  9732. * R.xor(true, false); //=> true
  9733. * R.xor(false, true); //=> true
  9734. * R.xor(false, false); //=> false
  9735. */
  9736. var xor = _curry2(function xor(a, b) {
  9737. return Boolean(!a ^ !b);
  9738. });
  9739. /**
  9740. * Creates a new list out of the two supplied by creating each possible pair
  9741. * from the lists.
  9742. *
  9743. * @func
  9744. * @memberOf R
  9745. * @since v0.1.0
  9746. * @category List
  9747. * @sig [a] -> [b] -> [[a,b]]
  9748. * @param {Array} as The first list.
  9749. * @param {Array} bs The second list.
  9750. * @return {Array} The list made by combining each possible pair from
  9751. * `as` and `bs` into pairs (`[a, b]`).
  9752. * @example
  9753. *
  9754. * R.xprod([1, 2], ['a', 'b']); //=> [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
  9755. * @symb R.xprod([a, b], [c, d]) = [[a, c], [a, d], [b, c], [b, d]]
  9756. */
  9757. var xprod = _curry2(function xprod(a, b) {
  9758. // = xprodWith(prepend); (takes about 3 times as long...)
  9759. var idx = 0;
  9760. var ilen = a.length;
  9761. var j;
  9762. var jlen = b.length;
  9763. var result = [];
  9764. while (idx < ilen) {
  9765. j = 0;
  9766. while (j < jlen) {
  9767. result[result.length] = [a[idx], b[j]];
  9768. j += 1;
  9769. }
  9770. idx += 1;
  9771. }
  9772. return result;
  9773. });
  9774. /**
  9775. * Creates a new list out of the two supplied by pairing up equally-positioned
  9776. * items from both lists. The returned list is truncated to the length of the
  9777. * shorter of the two input lists.
  9778. * Note: `zip` is equivalent to `zipWith(function(a, b) { return [a, b] })`.
  9779. *
  9780. * @func
  9781. * @memberOf R
  9782. * @since v0.1.0
  9783. * @category List
  9784. * @sig [a] -> [b] -> [[a,b]]
  9785. * @param {Array} list1 The first array to consider.
  9786. * @param {Array} list2 The second array to consider.
  9787. * @return {Array} The list made by pairing up same-indexed elements of `list1` and `list2`.
  9788. * @example
  9789. *
  9790. * R.zip([1, 2, 3], ['a', 'b', 'c']); //=> [[1, 'a'], [2, 'b'], [3, 'c']]
  9791. * @symb R.zip([a, b, c], [d, e, f]) = [[a, d], [b, e], [c, f]]
  9792. */
  9793. var zip = _curry2(function zip(a, b) {
  9794. var rv = [];
  9795. var idx = 0;
  9796. var len = Math.min(a.length, b.length);
  9797. while (idx < len) {
  9798. rv[idx] = [a[idx], b[idx]];
  9799. idx += 1;
  9800. }
  9801. return rv;
  9802. });
  9803. /**
  9804. * Creates a new object out of a list of keys and a list of values.
  9805. * Key/value pairing is truncated to the length of the shorter of the two lists.
  9806. * Note: `zipObj` is equivalent to `pipe(zip, fromPairs)`.
  9807. *
  9808. * @func
  9809. * @memberOf R
  9810. * @since v0.3.0
  9811. * @category List
  9812. * @sig [String] -> [*] -> {String: *}
  9813. * @param {Array} keys The array that will be properties on the output object.
  9814. * @param {Array} values The list of values on the output object.
  9815. * @return {Object} The object made by pairing up same-indexed elements of `keys` and `values`.
  9816. * @example
  9817. *
  9818. * R.zipObj(['a', 'b', 'c'], [1, 2, 3]); //=> {a: 1, b: 2, c: 3}
  9819. */
  9820. var zipObj = _curry2(function zipObj(keys, values) {
  9821. var idx = 0;
  9822. var len = Math.min(keys.length, values.length);
  9823. var out = {};
  9824. while (idx < len) {
  9825. out[keys[idx]] = values[idx];
  9826. idx += 1;
  9827. }
  9828. return out;
  9829. });
  9830. /**
  9831. * Creates a new list out of the two supplied by applying the function to each
  9832. * equally-positioned pair in the lists. The returned list is truncated to the
  9833. * length of the shorter of the two input lists.
  9834. *
  9835. * @function
  9836. * @memberOf R
  9837. * @since v0.1.0
  9838. * @category List
  9839. * @sig ((a, b) -> c) -> [a] -> [b] -> [c]
  9840. * @param {Function} fn The function used to combine the two elements into one value.
  9841. * @param {Array} list1 The first array to consider.
  9842. * @param {Array} list2 The second array to consider.
  9843. * @return {Array} The list made by combining same-indexed elements of `list1` and `list2`
  9844. * using `fn`.
  9845. * @example
  9846. *
  9847. * const f = (x, y) => {
  9848. * // ...
  9849. * };
  9850. * R.zipWith(f, [1, 2, 3], ['a', 'b', 'c']);
  9851. * //=> [f(1, 'a'), f(2, 'b'), f(3, 'c')]
  9852. * @symb R.zipWith(fn, [a, b, c], [d, e, f]) = [fn(a, d), fn(b, e), fn(c, f)]
  9853. */
  9854. var zipWith = _curry3(function zipWith(fn, a, b) {
  9855. var rv = [];
  9856. var idx = 0;
  9857. var len = Math.min(a.length, b.length);
  9858. while (idx < len) {
  9859. rv[idx] = fn(a[idx], b[idx]);
  9860. idx += 1;
  9861. }
  9862. return rv;
  9863. });
  9864. /**
  9865. * Creates a thunk out of a function. A thunk delays a calculation until
  9866. * its result is needed, providing lazy evaluation of arguments.
  9867. *
  9868. * @func
  9869. * @memberOf R
  9870. * @since v0.26.0
  9871. * @category Function
  9872. * @sig ((a, b, ..., j) -> k) -> (a, b, ..., j) -> (() -> k)
  9873. * @param {Function} fn A function to wrap in a thunk
  9874. * @return {Function} Expects arguments for `fn` and returns a new function
  9875. * that, when called, applies those arguments to `fn`.
  9876. * @see R.partial, R.partialRight
  9877. * @example
  9878. *
  9879. * R.thunkify(R.identity)(42)(); //=> 42
  9880. * R.thunkify((a, b) => a + b)(25, 17)(); //=> 42
  9881. */
  9882. var thunkify = _curry1(function thunkify(fn) {
  9883. return curryN(fn.length, function createThunk() {
  9884. var fnArgs = arguments;
  9885. return function invokeThunk() {
  9886. return fn.apply(this, fnArgs);
  9887. };
  9888. });
  9889. });
  9890. exports.F = F;
  9891. exports.T = T;
  9892. exports.__ = __;
  9893. exports.add = add;
  9894. exports.addIndex = addIndex;
  9895. exports.addIndexRight = addIndexRight;
  9896. exports.adjust = adjust;
  9897. exports.all = all;
  9898. exports.allPass = allPass;
  9899. exports.always = always;
  9900. exports.and = and;
  9901. exports.andThen = andThen;
  9902. exports.any = any;
  9903. exports.anyPass = anyPass;
  9904. exports.ap = ap;
  9905. exports.aperture = aperture;
  9906. exports.append = append;
  9907. exports.apply = apply;
  9908. exports.applySpec = applySpec;
  9909. exports.applyTo = applyTo;
  9910. exports.ascend = ascend;
  9911. exports.assoc = assoc;
  9912. exports.assocPath = assocPath;
  9913. exports.binary = binary;
  9914. exports.bind = bind;
  9915. exports.both = both;
  9916. exports.call = call;
  9917. exports.chain = chain;
  9918. exports.clamp = clamp;
  9919. exports.clone = clone;
  9920. exports.collectBy = collectBy;
  9921. exports.comparator = comparator;
  9922. exports.complement = complement;
  9923. exports.compose = compose;
  9924. exports.composeWith = composeWith;
  9925. exports.concat = concat;
  9926. exports.cond = cond;
  9927. exports.construct = construct;
  9928. exports.constructN = constructN;
  9929. exports.converge = converge;
  9930. exports.count = count;
  9931. exports.countBy = countBy;
  9932. exports.curry = curry;
  9933. exports.curryN = curryN;
  9934. exports.dec = dec;
  9935. exports.defaultTo = defaultTo;
  9936. exports.descend = descend;
  9937. exports.difference = difference;
  9938. exports.differenceWith = differenceWith;
  9939. exports.dissoc = dissoc;
  9940. exports.dissocPath = dissocPath;
  9941. exports.divide = divide;
  9942. exports.drop = drop;
  9943. exports.dropLast = dropLast$1;
  9944. exports.dropLastWhile = dropLastWhile$1;
  9945. exports.dropRepeats = dropRepeats;
  9946. exports.dropRepeatsBy = dropRepeatsBy;
  9947. exports.dropRepeatsWith = dropRepeatsWith;
  9948. exports.dropWhile = dropWhile;
  9949. exports.either = either;
  9950. exports.empty = empty;
  9951. exports.endsWith = endsWith;
  9952. exports.eqBy = eqBy;
  9953. exports.eqProps = eqProps;
  9954. exports.equals = equals;
  9955. exports.evolve = evolve;
  9956. exports.filter = filter;
  9957. exports.find = find;
  9958. exports.findIndex = findIndex;
  9959. exports.findLast = findLast;
  9960. exports.findLastIndex = findLastIndex;
  9961. exports.flatten = flatten;
  9962. exports.flip = flip;
  9963. exports.forEach = forEach;
  9964. exports.forEachObjIndexed = forEachObjIndexed;
  9965. exports.fromPairs = fromPairs;
  9966. exports.groupBy = groupBy;
  9967. exports.groupWith = groupWith;
  9968. exports.gt = gt;
  9969. exports.gte = gte;
  9970. exports.has = has;
  9971. exports.hasIn = hasIn;
  9972. exports.hasPath = hasPath;
  9973. exports.head = head;
  9974. exports.identical = identical;
  9975. exports.identity = identity;
  9976. exports.ifElse = ifElse;
  9977. exports.inc = inc;
  9978. exports.includes = includes;
  9979. exports.indexBy = indexBy;
  9980. exports.indexOf = indexOf;
  9981. exports.init = init;
  9982. exports.innerJoin = innerJoin;
  9983. exports.insert = insert;
  9984. exports.insertAll = insertAll;
  9985. exports.intersection = intersection;
  9986. exports.intersperse = intersperse;
  9987. exports.into = into;
  9988. exports.invert = invert;
  9989. exports.invertObj = invertObj;
  9990. exports.invoker = invoker;
  9991. exports.is = is;
  9992. exports.isEmpty = isEmpty;
  9993. exports.isNil = isNil;
  9994. exports.isNotNil = isNotNil;
  9995. exports.join = join;
  9996. exports.juxt = juxt;
  9997. exports.keys = keys;
  9998. exports.keysIn = keysIn;
  9999. exports.last = last;
  10000. exports.lastIndexOf = lastIndexOf;
  10001. exports.length = length;
  10002. exports.lens = lens;
  10003. exports.lensIndex = lensIndex;
  10004. exports.lensPath = lensPath;
  10005. exports.lensProp = lensProp;
  10006. exports.lift = lift;
  10007. exports.liftN = liftN;
  10008. exports.lt = lt;
  10009. exports.lte = lte;
  10010. exports.map = map;
  10011. exports.mapAccum = mapAccum;
  10012. exports.mapAccumRight = mapAccumRight;
  10013. exports.mapObjIndexed = mapObjIndexed;
  10014. exports.match = match;
  10015. exports.mathMod = mathMod;
  10016. exports.max = max;
  10017. exports.maxBy = maxBy;
  10018. exports.mean = mean;
  10019. exports.median = median;
  10020. exports.memoizeWith = memoizeWith;
  10021. exports.mergeAll = mergeAll;
  10022. exports.mergeDeepLeft = mergeDeepLeft;
  10023. exports.mergeDeepRight = mergeDeepRight;
  10024. exports.mergeDeepWith = mergeDeepWith;
  10025. exports.mergeDeepWithKey = mergeDeepWithKey;
  10026. exports.mergeLeft = mergeLeft;
  10027. exports.mergeRight = mergeRight;
  10028. exports.mergeWith = mergeWith;
  10029. exports.mergeWithKey = mergeWithKey;
  10030. exports.min = min;
  10031. exports.minBy = minBy;
  10032. exports.modify = modify;
  10033. exports.modifyPath = modifyPath;
  10034. exports.modulo = modulo;
  10035. exports.move = move;
  10036. exports.multiply = multiply;
  10037. exports.nAry = nAry;
  10038. exports.negate = negate;
  10039. exports.none = none;
  10040. exports.not = not;
  10041. exports.nth = nth;
  10042. exports.nthArg = nthArg;
  10043. exports.o = o;
  10044. exports.objOf = objOf;
  10045. exports.of = of;
  10046. exports.omit = omit;
  10047. exports.on = on;
  10048. exports.once = once;
  10049. exports.or = or;
  10050. exports.otherwise = otherwise;
  10051. exports.over = over;
  10052. exports.pair = pair;
  10053. exports.partial = partial;
  10054. exports.partialObject = partialObject;
  10055. exports.partialRight = partialRight;
  10056. exports.partition = partition;
  10057. exports.path = path;
  10058. exports.pathEq = pathEq;
  10059. exports.pathOr = pathOr;
  10060. exports.pathSatisfies = pathSatisfies;
  10061. exports.paths = paths;
  10062. exports.pick = pick;
  10063. exports.pickAll = pickAll;
  10064. exports.pickBy = pickBy;
  10065. exports.pipe = pipe;
  10066. exports.pipeWith = pipeWith;
  10067. exports.pluck = pluck;
  10068. exports.prepend = prepend;
  10069. exports.product = product;
  10070. exports.project = project;
  10071. exports.promap = promap;
  10072. exports.prop = prop;
  10073. exports.propEq = propEq;
  10074. exports.propIs = propIs;
  10075. exports.propOr = propOr;
  10076. exports.propSatisfies = propSatisfies;
  10077. exports.props = props;
  10078. exports.range = range;
  10079. exports.reduce = reduce;
  10080. exports.reduceBy = reduceBy;
  10081. exports.reduceRight = reduceRight;
  10082. exports.reduceWhile = reduceWhile;
  10083. exports.reduced = reduced;
  10084. exports.reject = reject;
  10085. exports.remove = remove;
  10086. exports.repeat = repeat;
  10087. exports.replace = replace;
  10088. exports.reverse = reverse;
  10089. exports.scan = scan;
  10090. exports.sequence = sequence;
  10091. exports.set = set;
  10092. exports.slice = slice;
  10093. exports.sort = sort;
  10094. exports.sortBy = sortBy;
  10095. exports.sortWith = sortWith;
  10096. exports.split = split;
  10097. exports.splitAt = splitAt;
  10098. exports.splitEvery = splitEvery;
  10099. exports.splitWhen = splitWhen;
  10100. exports.splitWhenever = splitWhenever;
  10101. exports.startsWith = startsWith;
  10102. exports.subtract = subtract;
  10103. exports.sum = sum;
  10104. exports.swap = swap;
  10105. exports.symmetricDifference = symmetricDifference;
  10106. exports.symmetricDifferenceWith = symmetricDifferenceWith;
  10107. exports.tail = tail;
  10108. exports.take = take;
  10109. exports.takeLast = takeLast;
  10110. exports.takeLastWhile = takeLastWhile;
  10111. exports.takeWhile = takeWhile;
  10112. exports.tap = tap;
  10113. exports.test = test;
  10114. exports.thunkify = thunkify;
  10115. exports.times = times;
  10116. exports.toLower = toLower;
  10117. exports.toPairs = toPairs;
  10118. exports.toPairsIn = toPairsIn;
  10119. exports.toString = toString$1;
  10120. exports.toUpper = toUpper;
  10121. exports.transduce = transduce;
  10122. exports.transpose = transpose;
  10123. exports.traverse = traverse;
  10124. exports.trim = trim;
  10125. exports.tryCatch = tryCatch;
  10126. exports.type = type;
  10127. exports.unapply = unapply;
  10128. exports.unary = unary;
  10129. exports.uncurryN = uncurryN;
  10130. exports.unfold = unfold;
  10131. exports.union = union;
  10132. exports.unionWith = unionWith;
  10133. exports.uniq = uniq;
  10134. exports.uniqBy = uniqBy;
  10135. exports.uniqWith = uniqWith;
  10136. exports.unless = unless;
  10137. exports.unnest = unnest;
  10138. exports.until = until;
  10139. exports.unwind = unwind;
  10140. exports.update = update;
  10141. exports.useWith = useWith;
  10142. exports.values = values;
  10143. exports.valuesIn = valuesIn;
  10144. exports.view = view;
  10145. exports.when = when;
  10146. exports.where = where;
  10147. exports.whereAny = whereAny;
  10148. exports.whereEq = whereEq;
  10149. exports.without = without;
  10150. exports.xor = xor;
  10151. exports.xprod = xprod;
  10152. exports.zip = zip;
  10153. exports.zipObj = zipObj;
  10154. exports.zipWith = zipWith;
  10155. Object.defineProperty(exports, '__esModule', { value: true });
  10156. })));