ramda.js 279 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568
  1. // Ramda v0.25.0
  2. // https://github.com/ramda/ramda
  3. // (c) 2013-2018 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. (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() {return false;};
  26. /**
  27. * A function that always returns `true`. Any passed in parameters are ignored.
  28. *
  29. * @func
  30. * @memberOf R
  31. * @since v0.9.0
  32. * @category Function
  33. * @sig * -> Boolean
  34. * @param {*}
  35. * @return {Boolean}
  36. * @see R.F
  37. * @example
  38. *
  39. * R.T(); //=> true
  40. */
  41. var T = function() {return true;};
  42. /**
  43. * A special placeholder value used to specify "gaps" within curried functions,
  44. * allowing partial application of any combination of arguments, regardless of
  45. * their positions.
  46. *
  47. * If `g` is a curried ternary function and `_` is `R.__`, the following are
  48. * equivalent:
  49. *
  50. * - `g(1, 2, 3)`
  51. * - `g(_, 2, 3)(1)`
  52. * - `g(_, _, 3)(1)(2)`
  53. * - `g(_, _, 3)(1, 2)`
  54. * - `g(_, 2, _)(1, 3)`
  55. * - `g(_, 2)(1)(3)`
  56. * - `g(_, 2)(1, 3)`
  57. * - `g(_, 2)(_, 3)(1)`
  58. *
  59. * @name __
  60. * @constant
  61. * @memberOf R
  62. * @since v0.6.0
  63. * @category Function
  64. * @example
  65. *
  66. * const greet = R.replace('{name}', R.__, 'Hello, {name}!');
  67. * greet('Alice'); //=> 'Hello, Alice!'
  68. */
  69. var __ = {'@@functional/placeholder': true};
  70. function _isPlaceholder(a) {
  71. return a != null &&
  72. typeof a === 'object' &&
  73. a['@@functional/placeholder'] === true;
  74. }
  75. /**
  76. * Optimized internal one-arity curry function.
  77. *
  78. * @private
  79. * @category Function
  80. * @param {Function} fn The function to curry.
  81. * @return {Function} The curried function.
  82. */
  83. function _curry1(fn) {
  84. return function f1(a) {
  85. if (arguments.length === 0 || _isPlaceholder(a)) {
  86. return f1;
  87. } else {
  88. return fn.apply(this, arguments);
  89. }
  90. };
  91. }
  92. /**
  93. * Optimized internal two-arity curry function.
  94. *
  95. * @private
  96. * @category Function
  97. * @param {Function} fn The function to curry.
  98. * @return {Function} The curried function.
  99. */
  100. function _curry2(fn) {
  101. return function f2(a, b) {
  102. switch (arguments.length) {
  103. case 0:
  104. return f2;
  105. case 1:
  106. return _isPlaceholder(a) ? f2
  107. : _curry1(function(_b) { return fn(a, _b); });
  108. default:
  109. return _isPlaceholder(a) && _isPlaceholder(b) ? f2
  110. : _isPlaceholder(a) ? _curry1(function(_a) { return fn(_a, b); })
  111. : _isPlaceholder(b) ? _curry1(function(_b) { return fn(a, _b); })
  112. : fn(a, b);
  113. }
  114. };
  115. }
  116. /**
  117. * Adds two values.
  118. *
  119. * @func
  120. * @memberOf R
  121. * @since v0.1.0
  122. * @category Math
  123. * @sig Number -> Number -> Number
  124. * @param {Number} a
  125. * @param {Number} b
  126. * @return {Number}
  127. * @see R.subtract
  128. * @example
  129. *
  130. * R.add(2, 3); //=> 5
  131. * R.add(7)(10); //=> 17
  132. */
  133. var add = _curry2(function add(a, b) {
  134. return Number(a) + Number(b);
  135. });
  136. /**
  137. * Private `concat` function to merge two array-like objects.
  138. *
  139. * @private
  140. * @param {Array|Arguments} [set1=[]] An array-like object.
  141. * @param {Array|Arguments} [set2=[]] An array-like object.
  142. * @return {Array} A new, merged array.
  143. * @example
  144. *
  145. * _concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
  146. */
  147. function _concat(set1, set2) {
  148. set1 = set1 || [];
  149. set2 = set2 || [];
  150. var idx;
  151. var len1 = set1.length;
  152. var len2 = set2.length;
  153. var result = [];
  154. idx = 0;
  155. while (idx < len1) {
  156. result[result.length] = set1[idx];
  157. idx += 1;
  158. }
  159. idx = 0;
  160. while (idx < len2) {
  161. result[result.length] = set2[idx];
  162. idx += 1;
  163. }
  164. return result;
  165. }
  166. function _arity(n, fn) {
  167. /* eslint-disable no-unused-vars */
  168. switch (n) {
  169. case 0: return function() { return fn.apply(this, arguments); };
  170. case 1: return function(a0) { return fn.apply(this, arguments); };
  171. case 2: return function(a0, a1) { return fn.apply(this, arguments); };
  172. case 3: return function(a0, a1, a2) { return fn.apply(this, arguments); };
  173. case 4: return function(a0, a1, a2, a3) { return fn.apply(this, arguments); };
  174. case 5: return function(a0, a1, a2, a3, a4) { return fn.apply(this, arguments); };
  175. case 6: return function(a0, a1, a2, a3, a4, a5) { return fn.apply(this, arguments); };
  176. case 7: return function(a0, a1, a2, a3, a4, a5, a6) { return fn.apply(this, arguments); };
  177. case 8: return function(a0, a1, a2, a3, a4, a5, a6, a7) { return fn.apply(this, arguments); };
  178. case 9: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8) { return fn.apply(this, arguments); };
  179. case 10: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) { return fn.apply(this, arguments); };
  180. default: throw new Error('First argument to _arity must be a non-negative integer no greater than ten');
  181. }
  182. }
  183. /**
  184. * Internal curryN function.
  185. *
  186. * @private
  187. * @category Function
  188. * @param {Number} length The arity of the curried function.
  189. * @param {Array} received An array of arguments received thus far.
  190. * @param {Function} fn The function to curry.
  191. * @return {Function} The curried function.
  192. */
  193. function _curryN(length, received, fn) {
  194. return function() {
  195. var combined = [];
  196. var argsIdx = 0;
  197. var left = length;
  198. var combinedIdx = 0;
  199. while (combinedIdx < received.length || argsIdx < arguments.length) {
  200. var result;
  201. if (combinedIdx < received.length &&
  202. (!_isPlaceholder(received[combinedIdx]) ||
  203. argsIdx >= arguments.length)) {
  204. result = received[combinedIdx];
  205. } else {
  206. result = arguments[argsIdx];
  207. argsIdx += 1;
  208. }
  209. combined[combinedIdx] = result;
  210. if (!_isPlaceholder(result)) {
  211. left -= 1;
  212. }
  213. combinedIdx += 1;
  214. }
  215. return left <= 0 ? fn.apply(this, combined)
  216. : _arity(left, _curryN(length, combined, fn));
  217. };
  218. }
  219. /**
  220. * Returns a curried equivalent of the provided function, with the specified
  221. * arity. The curried function has two unusual capabilities. First, its
  222. * arguments needn't be provided one at a time. If `g` is `R.curryN(3, f)`, the
  223. * following are equivalent:
  224. *
  225. * - `g(1)(2)(3)`
  226. * - `g(1)(2, 3)`
  227. * - `g(1, 2)(3)`
  228. * - `g(1, 2, 3)`
  229. *
  230. * Secondly, the special placeholder value [`R.__`](#__) may be used to specify
  231. * "gaps", allowing partial application of any combination of arguments,
  232. * regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
  233. * the following are equivalent:
  234. *
  235. * - `g(1, 2, 3)`
  236. * - `g(_, 2, 3)(1)`
  237. * - `g(_, _, 3)(1)(2)`
  238. * - `g(_, _, 3)(1, 2)`
  239. * - `g(_, 2)(1)(3)`
  240. * - `g(_, 2)(1, 3)`
  241. * - `g(_, 2)(_, 3)(1)`
  242. *
  243. * @func
  244. * @memberOf R
  245. * @since v0.5.0
  246. * @category Function
  247. * @sig Number -> (* -> a) -> (* -> a)
  248. * @param {Number} length The arity for the returned function.
  249. * @param {Function} fn The function to curry.
  250. * @return {Function} A new, curried function.
  251. * @see R.curry
  252. * @example
  253. *
  254. * const sumArgs = (...args) => R.sum(args);
  255. *
  256. * const curriedAddFourNumbers = R.curryN(4, sumArgs);
  257. * const f = curriedAddFourNumbers(1, 2);
  258. * const g = f(3);
  259. * g(4); //=> 10
  260. */
  261. var curryN = _curry2(function curryN(length, fn) {
  262. if (length === 1) {
  263. return _curry1(fn);
  264. }
  265. return _arity(length, _curryN(length, [], fn));
  266. });
  267. /**
  268. * Creates a new list iteration function from an existing one by adding two new
  269. * parameters to its callback function: the current index, and the entire list.
  270. *
  271. * This would turn, for instance, [`R.map`](#map) function into one that
  272. * more closely resembles `Array.prototype.map`. Note that this will only work
  273. * for functions in which the iteration callback function is the first
  274. * parameter, and where the list is the last parameter. (This latter might be
  275. * unimportant if the list parameter is not used.)
  276. *
  277. * @func
  278. * @memberOf R
  279. * @since v0.15.0
  280. * @category Function
  281. * @category List
  282. * @sig ((a ... -> b) ... -> [a] -> *) -> ((a ..., Int, [a] -> b) ... -> [a] -> *)
  283. * @param {Function} fn A list iteration function that does not pass index or list to its callback
  284. * @return {Function} An altered list iteration function that passes (item, index, list) to its callback
  285. * @example
  286. *
  287. * const mapIndexed = R.addIndex(R.map);
  288. * mapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']);
  289. * //=> ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']
  290. */
  291. var addIndex = _curry1(function addIndex(fn) {
  292. return curryN(fn.length, function() {
  293. var idx = 0;
  294. var origFn = arguments[0];
  295. var list = arguments[arguments.length - 1];
  296. var args = Array.prototype.slice.call(arguments, 0);
  297. args[0] = function() {
  298. var result = origFn.apply(this, _concat(arguments, [idx, list]));
  299. idx += 1;
  300. return result;
  301. };
  302. return fn.apply(this, args);
  303. });
  304. });
  305. /**
  306. * Optimized internal three-arity curry function.
  307. *
  308. * @private
  309. * @category Function
  310. * @param {Function} fn The function to curry.
  311. * @return {Function} The curried function.
  312. */
  313. function _curry3(fn) {
  314. return function f3(a, b, c) {
  315. switch (arguments.length) {
  316. case 0:
  317. return f3;
  318. case 1:
  319. return _isPlaceholder(a) ? f3
  320. : _curry2(function(_b, _c) { return fn(a, _b, _c); });
  321. case 2:
  322. return _isPlaceholder(a) && _isPlaceholder(b) ? f3
  323. : _isPlaceholder(a) ? _curry2(function(_a, _c) { return fn(_a, b, _c); })
  324. : _isPlaceholder(b) ? _curry2(function(_b, _c) { return fn(a, _b, _c); })
  325. : _curry1(function(_c) { return fn(a, b, _c); });
  326. default:
  327. return _isPlaceholder(a) && _isPlaceholder(b) && _isPlaceholder(c) ? f3
  328. : _isPlaceholder(a) && _isPlaceholder(b) ? _curry2(function(_a, _b) { return fn(_a, _b, c); })
  329. : _isPlaceholder(a) && _isPlaceholder(c) ? _curry2(function(_a, _c) { return fn(_a, b, _c); })
  330. : _isPlaceholder(b) && _isPlaceholder(c) ? _curry2(function(_b, _c) { return fn(a, _b, _c); })
  331. : _isPlaceholder(a) ? _curry1(function(_a) { return fn(_a, b, c); })
  332. : _isPlaceholder(b) ? _curry1(function(_b) { return fn(a, _b, c); })
  333. : _isPlaceholder(c) ? _curry1(function(_c) { return fn(a, b, _c); })
  334. : fn(a, b, c);
  335. }
  336. };
  337. }
  338. /**
  339. * Applies a function to the value at the given index of an array, returning a
  340. * new copy of the array with the element at the given index replaced with the
  341. * result of the function application.
  342. *
  343. * @func
  344. * @memberOf R
  345. * @since v0.14.0
  346. * @category List
  347. * @sig Number -> (a -> a) -> [a] -> [a]
  348. * @param {Number} idx The index.
  349. * @param {Function} fn The function to apply.
  350. * @param {Array|Arguments} list An array-like object whose value
  351. * at the supplied index will be replaced.
  352. * @return {Array} A copy of the supplied array-like object with
  353. * the element at index `idx` replaced with the value
  354. * returned by applying `fn` to the existing element.
  355. * @see R.update
  356. * @example
  357. *
  358. * R.adjust(1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'B', 'c', 'd']
  359. * R.adjust(-1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c', 'D']
  360. * @symb R.adjust(-1, f, [a, b]) = [a, f(b)]
  361. * @symb R.adjust(0, f, [a, b]) = [f(a), b]
  362. */
  363. var adjust = _curry3(function adjust(idx, fn, list) {
  364. if (idx >= list.length || idx < -list.length) {
  365. return list;
  366. }
  367. var start = idx < 0 ? list.length : 0;
  368. var _idx = start + idx;
  369. var _list = _concat(list);
  370. _list[_idx] = fn(list[_idx]);
  371. return _list;
  372. });
  373. /**
  374. * Tests whether or not an object is an array.
  375. *
  376. * @private
  377. * @param {*} val The object to test.
  378. * @return {Boolean} `true` if `val` is an array, `false` otherwise.
  379. * @example
  380. *
  381. * _isArray([]); //=> true
  382. * _isArray(null); //=> false
  383. * _isArray({}); //=> false
  384. */
  385. var _isArray = Array.isArray || function _isArray(val) {
  386. return (val != null &&
  387. val.length >= 0 &&
  388. Object.prototype.toString.call(val) === '[object Array]');
  389. };
  390. function _isTransformer(obj) {
  391. return obj != null && typeof obj['@@transducer/step'] === 'function';
  392. }
  393. /**
  394. * Returns a function that dispatches with different strategies based on the
  395. * object in list position (last argument). If it is an array, executes [fn].
  396. * Otherwise, if it has a function with one of the given method names, it will
  397. * execute that function (functor case). Otherwise, if it is a transformer,
  398. * uses transducer [xf] to return a new transformer (transducer case).
  399. * Otherwise, it will default to executing [fn].
  400. *
  401. * @private
  402. * @param {Array} methodNames properties to check for a custom implementation
  403. * @param {Function} xf transducer to initialize if object is transformer
  404. * @param {Function} fn default ramda implementation
  405. * @return {Function} A function that dispatches on object in list position
  406. */
  407. function _dispatchable(methodNames, xf, fn) {
  408. return function() {
  409. if (arguments.length === 0) {
  410. return fn();
  411. }
  412. var args = Array.prototype.slice.call(arguments, 0);
  413. var obj = args.pop();
  414. if (!_isArray(obj)) {
  415. var idx = 0;
  416. while (idx < methodNames.length) {
  417. if (typeof obj[methodNames[idx]] === 'function') {
  418. return obj[methodNames[idx]].apply(obj, args);
  419. }
  420. idx += 1;
  421. }
  422. if (_isTransformer(obj)) {
  423. var transducer = xf.apply(null, args);
  424. return transducer(obj);
  425. }
  426. }
  427. return fn.apply(this, arguments);
  428. };
  429. }
  430. function _reduced(x) {
  431. return x && x['@@transducer/reduced'] ? x :
  432. {
  433. '@@transducer/value': x,
  434. '@@transducer/reduced': true
  435. };
  436. }
  437. var _xfBase = {
  438. init: function() {
  439. return this.xf['@@transducer/init']();
  440. },
  441. result: function(result) {
  442. return this.xf['@@transducer/result'](result);
  443. }
  444. };
  445. function XAll(f, xf) {
  446. this.xf = xf;
  447. this.f = f;
  448. this.all = true;
  449. }
  450. XAll.prototype['@@transducer/init'] = _xfBase.init;
  451. XAll.prototype['@@transducer/result'] = function(result) {
  452. if (this.all) {
  453. result = this.xf['@@transducer/step'](result, true);
  454. }
  455. return this.xf['@@transducer/result'](result);
  456. };
  457. XAll.prototype['@@transducer/step'] = function(result, input) {
  458. if (!this.f(input)) {
  459. this.all = false;
  460. result = _reduced(this.xf['@@transducer/step'](result, false));
  461. }
  462. return result;
  463. };
  464. var _xall = _curry2(function _xall(f, xf) { return new XAll(f, xf); });
  465. /**
  466. * Returns `true` if all elements of the list match the predicate, `false` if
  467. * there are any that don't.
  468. *
  469. * Dispatches to the `all` method of the second argument, if present.
  470. *
  471. * Acts as a transducer if a transformer is given in list position.
  472. *
  473. * @func
  474. * @memberOf R
  475. * @since v0.1.0
  476. * @category List
  477. * @sig (a -> Boolean) -> [a] -> Boolean
  478. * @param {Function} fn The predicate function.
  479. * @param {Array} list The array to consider.
  480. * @return {Boolean} `true` if the predicate is satisfied by every element, `false`
  481. * otherwise.
  482. * @see R.any, R.none, R.transduce
  483. * @example
  484. *
  485. * const equals3 = R.equals(3);
  486. * R.all(equals3)([3, 3, 3, 3]); //=> true
  487. * R.all(equals3)([3, 3, 1, 3]); //=> false
  488. */
  489. var all = _curry2(_dispatchable(['all'], _xall, function all(fn, list) {
  490. var idx = 0;
  491. while (idx < list.length) {
  492. if (!fn(list[idx])) {
  493. return false;
  494. }
  495. idx += 1;
  496. }
  497. return true;
  498. }));
  499. /**
  500. * Returns the larger of its two arguments.
  501. *
  502. * @func
  503. * @memberOf R
  504. * @since v0.1.0
  505. * @category Relation
  506. * @sig Ord a => a -> a -> a
  507. * @param {*} a
  508. * @param {*} b
  509. * @return {*}
  510. * @see R.maxBy, R.min
  511. * @example
  512. *
  513. * R.max(789, 123); //=> 789
  514. * R.max('a', 'b'); //=> 'b'
  515. */
  516. var max = _curry2(function max(a, b) { return b > a ? b : a; });
  517. function _map(fn, functor) {
  518. var idx = 0;
  519. var len = functor.length;
  520. var result = Array(len);
  521. while (idx < len) {
  522. result[idx] = fn(functor[idx]);
  523. idx += 1;
  524. }
  525. return result;
  526. }
  527. function _isString(x) {
  528. return Object.prototype.toString.call(x) === '[object String]';
  529. }
  530. /**
  531. * Tests whether or not an object is similar to an array.
  532. *
  533. * @private
  534. * @category Type
  535. * @category List
  536. * @sig * -> Boolean
  537. * @param {*} x The object to test.
  538. * @return {Boolean} `true` if `x` has a numeric length property and extreme indices defined; `false` otherwise.
  539. * @example
  540. *
  541. * _isArrayLike([]); //=> true
  542. * _isArrayLike(true); //=> false
  543. * _isArrayLike({}); //=> false
  544. * _isArrayLike({length: 10}); //=> false
  545. * _isArrayLike({0: 'zero', 9: 'nine', length: 10}); //=> true
  546. */
  547. var _isArrayLike = _curry1(function isArrayLike(x) {
  548. if (_isArray(x)) { return true; }
  549. if (!x) { return false; }
  550. if (typeof x !== 'object') { return false; }
  551. if (_isString(x)) { return false; }
  552. if (x.nodeType === 1) { return !!x.length; }
  553. if (x.length === 0) { return true; }
  554. if (x.length > 0) {
  555. return x.hasOwnProperty(0) && x.hasOwnProperty(x.length - 1);
  556. }
  557. return false;
  558. });
  559. function XWrap(fn) {
  560. this.f = fn;
  561. }
  562. XWrap.prototype['@@transducer/init'] = function() {
  563. throw new Error('init not implemented on XWrap');
  564. };
  565. XWrap.prototype['@@transducer/result'] = function(acc) { return acc; };
  566. XWrap.prototype['@@transducer/step'] = function(acc, x) {
  567. return this.f(acc, x);
  568. };
  569. function _xwrap(fn) { return new XWrap(fn); }
  570. /**
  571. * Creates a function that is bound to a context.
  572. * Note: `R.bind` does not provide the additional argument-binding capabilities of
  573. * [Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).
  574. *
  575. * @func
  576. * @memberOf R
  577. * @since v0.6.0
  578. * @category Function
  579. * @category Object
  580. * @sig (* -> *) -> {*} -> (* -> *)
  581. * @param {Function} fn The function to bind to context
  582. * @param {Object} thisObj The context to bind `fn` to
  583. * @return {Function} A function that will execute in the context of `thisObj`.
  584. * @see R.partial
  585. * @example
  586. *
  587. * const log = R.bind(console.log, console);
  588. * R.pipe(R.assoc('a', 2), R.tap(log), R.assoc('a', 3))({a: 1}); //=> {a: 3}
  589. * // logs {a: 2}
  590. * @symb R.bind(f, o)(a, b) = f.call(o, a, b)
  591. */
  592. var bind = _curry2(function bind(fn, thisObj) {
  593. return _arity(fn.length, function() {
  594. return fn.apply(thisObj, arguments);
  595. });
  596. });
  597. function _arrayReduce(xf, acc, list) {
  598. var idx = 0;
  599. var len = list.length;
  600. while (idx < len) {
  601. acc = xf['@@transducer/step'](acc, list[idx]);
  602. if (acc && acc['@@transducer/reduced']) {
  603. acc = acc['@@transducer/value'];
  604. break;
  605. }
  606. idx += 1;
  607. }
  608. return xf['@@transducer/result'](acc);
  609. }
  610. function _iterableReduce(xf, acc, iter) {
  611. var step = iter.next();
  612. while (!step.done) {
  613. acc = xf['@@transducer/step'](acc, step.value);
  614. if (acc && acc['@@transducer/reduced']) {
  615. acc = acc['@@transducer/value'];
  616. break;
  617. }
  618. step = iter.next();
  619. }
  620. return xf['@@transducer/result'](acc);
  621. }
  622. function _methodReduce(xf, acc, obj, methodName) {
  623. return xf['@@transducer/result'](obj[methodName](bind(xf['@@transducer/step'], xf), acc));
  624. }
  625. var symIterator = (typeof Symbol !== 'undefined') ? Symbol.iterator : '@@iterator';
  626. function _reduce(fn, acc, list) {
  627. if (typeof fn === 'function') {
  628. fn = _xwrap(fn);
  629. }
  630. if (_isArrayLike(list)) {
  631. return _arrayReduce(fn, acc, list);
  632. }
  633. if (typeof list['fantasy-land/reduce'] === 'function') {
  634. return _methodReduce(fn, acc, list, 'fantasy-land/reduce');
  635. }
  636. if (list[symIterator] != null) {
  637. return _iterableReduce(fn, acc, list[symIterator]());
  638. }
  639. if (typeof list.next === 'function') {
  640. return _iterableReduce(fn, acc, list);
  641. }
  642. if (typeof list.reduce === 'function') {
  643. return _methodReduce(fn, acc, list, 'reduce');
  644. }
  645. throw new TypeError('reduce: list must be array or iterable');
  646. }
  647. function XMap(f, xf) {
  648. this.xf = xf;
  649. this.f = f;
  650. }
  651. XMap.prototype['@@transducer/init'] = _xfBase.init;
  652. XMap.prototype['@@transducer/result'] = _xfBase.result;
  653. XMap.prototype['@@transducer/step'] = function(result, input) {
  654. return this.xf['@@transducer/step'](result, this.f(input));
  655. };
  656. var _xmap = _curry2(function _xmap(f, xf) { return new XMap(f, xf); });
  657. function _has(prop, obj) {
  658. return Object.prototype.hasOwnProperty.call(obj, prop);
  659. }
  660. var toString = Object.prototype.toString;
  661. var _isArguments = (function() {
  662. return toString.call(arguments) === '[object Arguments]' ?
  663. function _isArguments(x) { return toString.call(x) === '[object Arguments]'; } :
  664. function _isArguments(x) { return _has('callee', x); };
  665. }());
  666. // cover IE < 9 keys issues
  667. var hasEnumBug = !({toString: null}).propertyIsEnumerable('toString');
  668. var nonEnumerableProps = ['constructor', 'valueOf', 'isPrototypeOf', 'toString',
  669. 'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
  670. // Safari bug
  671. var hasArgsEnumBug = (function() {
  672. 'use strict';
  673. return arguments.propertyIsEnumerable('length');
  674. }());
  675. var contains = function contains(list, item) {
  676. var idx = 0;
  677. while (idx < list.length) {
  678. if (list[idx] === item) {
  679. return true;
  680. }
  681. idx += 1;
  682. }
  683. return false;
  684. };
  685. /**
  686. * Returns a list containing the names of all the enumerable own properties of
  687. * the supplied object.
  688. * Note that the order of the output array is not guaranteed to be consistent
  689. * across different JS platforms.
  690. *
  691. * @func
  692. * @memberOf R
  693. * @since v0.1.0
  694. * @category Object
  695. * @sig {k: v} -> [k]
  696. * @param {Object} obj The object to extract properties from
  697. * @return {Array} An array of the object's own properties.
  698. * @see R.keysIn, R.values
  699. * @example
  700. *
  701. * R.keys({a: 1, b: 2, c: 3}); //=> ['a', 'b', 'c']
  702. */
  703. var keys = typeof Object.keys === 'function' && !hasArgsEnumBug ?
  704. _curry1(function keys(obj) {
  705. return Object(obj) !== obj ? [] : Object.keys(obj);
  706. }) :
  707. _curry1(function keys(obj) {
  708. if (Object(obj) !== obj) {
  709. return [];
  710. }
  711. var prop, nIdx;
  712. var ks = [];
  713. var checkArgsLength = hasArgsEnumBug && _isArguments(obj);
  714. for (prop in obj) {
  715. if (_has(prop, obj) && (!checkArgsLength || prop !== 'length')) {
  716. ks[ks.length] = prop;
  717. }
  718. }
  719. if (hasEnumBug) {
  720. nIdx = nonEnumerableProps.length - 1;
  721. while (nIdx >= 0) {
  722. prop = nonEnumerableProps[nIdx];
  723. if (_has(prop, obj) && !contains(ks, prop)) {
  724. ks[ks.length] = prop;
  725. }
  726. nIdx -= 1;
  727. }
  728. }
  729. return ks;
  730. });
  731. /**
  732. * Takes a function and
  733. * a [functor](https://github.com/fantasyland/fantasy-land#functor),
  734. * applies the function to each of the functor's values, and returns
  735. * a functor of the same shape.
  736. *
  737. * Ramda provides suitable `map` implementations for `Array` and `Object`,
  738. * so this function may be applied to `[1, 2, 3]` or `{x: 1, y: 2, z: 3}`.
  739. *
  740. * Dispatches to the `map` method of the second argument, if present.
  741. *
  742. * Acts as a transducer if a transformer is given in list position.
  743. *
  744. * Also treats functions as functors and will compose them together.
  745. *
  746. * @func
  747. * @memberOf R
  748. * @since v0.1.0
  749. * @category List
  750. * @sig Functor f => (a -> b) -> f a -> f b
  751. * @param {Function} fn The function to be called on every element of the input `list`.
  752. * @param {Array} list The list to be iterated over.
  753. * @return {Array} The new list.
  754. * @see R.transduce, R.addIndex
  755. * @example
  756. *
  757. * const double = x => x * 2;
  758. *
  759. * R.map(double, [1, 2, 3]); //=> [2, 4, 6]
  760. *
  761. * R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
  762. * @symb R.map(f, [a, b]) = [f(a), f(b)]
  763. * @symb R.map(f, { x: a, y: b }) = { x: f(a), y: f(b) }
  764. * @symb R.map(f, functor_o) = functor_o.map(f)
  765. */
  766. var map = _curry2(_dispatchable(['fantasy-land/map', 'map'], _xmap, function map(fn, functor) {
  767. switch (Object.prototype.toString.call(functor)) {
  768. case '[object Function]':
  769. return curryN(functor.length, function() {
  770. return fn.call(this, functor.apply(this, arguments));
  771. });
  772. case '[object Object]':
  773. return _reduce(function(acc, key) {
  774. acc[key] = fn(functor[key]);
  775. return acc;
  776. }, {}, keys(functor));
  777. default:
  778. return _map(fn, functor);
  779. }
  780. }));
  781. /**
  782. * Retrieve the value at a given path.
  783. *
  784. * @func
  785. * @memberOf R
  786. * @since v0.2.0
  787. * @category Object
  788. * @typedefn Idx = String | Int
  789. * @sig [Idx] -> {a} -> a | Undefined
  790. * @param {Array} path The path to use.
  791. * @param {Object} obj The object to retrieve the nested property from.
  792. * @return {*} The data at `path`.
  793. * @see R.prop
  794. * @example
  795. *
  796. * R.path(['a', 'b'], {a: {b: 2}}); //=> 2
  797. * R.path(['a', 'b'], {c: {b: 2}}); //=> undefined
  798. */
  799. var path = _curry2(function path(paths, obj) {
  800. var val = obj;
  801. var idx = 0;
  802. while (idx < paths.length) {
  803. if (val == null) {
  804. return;
  805. }
  806. val = val[paths[idx]];
  807. idx += 1;
  808. }
  809. return val;
  810. });
  811. /**
  812. * Returns a function that when supplied an object returns the indicated
  813. * property of that object, if it exists.
  814. *
  815. * @func
  816. * @memberOf R
  817. * @since v0.1.0
  818. * @category Object
  819. * @sig s -> {s: a} -> a | Undefined
  820. * @param {String} p The property name
  821. * @param {Object} obj The object to query
  822. * @return {*} The value at `obj.p`.
  823. * @see R.path
  824. * @example
  825. *
  826. * R.prop('x', {x: 100}); //=> 100
  827. * R.prop('x', {}); //=> undefined
  828. * R.compose(R.inc, R.prop('x'))({ x: 3 }) //=> 4
  829. */
  830. var prop = _curry2(function prop(p, obj) { return path([p], obj); });
  831. /**
  832. * Returns a new list by plucking the same named property off all objects in
  833. * the list supplied.
  834. *
  835. * `pluck` will work on
  836. * any [functor](https://github.com/fantasyland/fantasy-land#functor) in
  837. * addition to arrays, as it is equivalent to `R.map(R.prop(k), f)`.
  838. *
  839. * @func
  840. * @memberOf R
  841. * @since v0.1.0
  842. * @category List
  843. * @sig Functor f => k -> f {k: v} -> f v
  844. * @param {Number|String} key The key name to pluck off of each object.
  845. * @param {Array} f The array or functor to consider.
  846. * @return {Array} The list of values for the given key.
  847. * @see R.props
  848. * @example
  849. *
  850. * var getAges = R.pluck('age');
  851. * getAges([{name: 'fred', age: 29}, {name: 'wilma', age: 27}]); //=> [29, 27]
  852. *
  853. * R.pluck(0, [[1, 2], [3, 4]]); //=> [1, 3]
  854. * R.pluck('val', {a: {val: 3}, b: {val: 5}}); //=> {a: 3, b: 5}
  855. * @symb R.pluck('x', [{x: 1, y: 2}, {x: 3, y: 4}, {x: 5, y: 6}]) = [1, 3, 5]
  856. * @symb R.pluck(0, [[1, 2], [3, 4], [5, 6]]) = [1, 3, 5]
  857. */
  858. var pluck = _curry2(function pluck(p, list) {
  859. return map(prop(p), list);
  860. });
  861. /**
  862. * Returns a single item by iterating through the list, successively calling
  863. * the iterator function and passing it an accumulator value and the current
  864. * value from the array, and then passing the result to the next call.
  865. *
  866. * The iterator function receives two values: *(acc, value)*. It may use
  867. * [`R.reduced`](#reduced) to shortcut the iteration.
  868. *
  869. * The arguments' order of [`reduceRight`](#reduceRight)'s iterator function
  870. * is *(value, acc)*.
  871. *
  872. * Note: `R.reduce` does not skip deleted or unassigned indices (sparse
  873. * arrays), unlike the native `Array.prototype.reduce` method. For more details
  874. * on this behavior, see:
  875. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Description
  876. *
  877. * Dispatches to the `reduce` method of the third argument, if present. When
  878. * doing so, it is up to the user to handle the [`R.reduced`](#reduced)
  879. * shortcuting, as this is not implemented by `reduce`.
  880. *
  881. * @func
  882. * @memberOf R
  883. * @since v0.1.0
  884. * @category List
  885. * @sig ((a, b) -> a) -> a -> [b] -> a
  886. * @param {Function} fn The iterator function. Receives two values, the accumulator and the
  887. * current element from the array.
  888. * @param {*} acc The accumulator value.
  889. * @param {Array} list The list to iterate over.
  890. * @return {*} The final, accumulated value.
  891. * @see R.reduced, R.addIndex, R.reduceRight
  892. * @example
  893. *
  894. * R.reduce(R.subtract, 0, [1, 2, 3, 4]) // => ((((0 - 1) - 2) - 3) - 4) = -10
  895. * // - -10
  896. * // / \ / \
  897. * // - 4 -6 4
  898. * // / \ / \
  899. * // - 3 ==> -3 3
  900. * // / \ / \
  901. * // - 2 -1 2
  902. * // / \ / \
  903. * // 0 1 0 1
  904. *
  905. * @symb R.reduce(f, a, [b, c, d]) = f(f(f(a, b), c), d)
  906. */
  907. var reduce = _curry3(_reduce);
  908. /**
  909. * Takes a list of predicates and returns a predicate that returns true for a
  910. * given list of arguments if every one of the provided predicates is satisfied
  911. * by those arguments.
  912. *
  913. * The function returned is a curried function whose arity matches that of the
  914. * highest-arity predicate.
  915. *
  916. * @func
  917. * @memberOf R
  918. * @since v0.9.0
  919. * @category Logic
  920. * @sig [(*... -> Boolean)] -> (*... -> Boolean)
  921. * @param {Array} predicates An array of predicates to check
  922. * @return {Function} The combined predicate
  923. * @see R.anyPass
  924. * @example
  925. *
  926. * const isQueen = R.propEq('rank', 'Q');
  927. * const isSpade = R.propEq('suit', '♠︎');
  928. * const isQueenOfSpades = R.allPass([isQueen, isSpade]);
  929. *
  930. * isQueenOfSpades({rank: 'Q', suit: '♣︎'}); //=> false
  931. * isQueenOfSpades({rank: 'Q', suit: '♠︎'}); //=> true
  932. */
  933. var allPass = _curry1(function allPass(preds) {
  934. return curryN(reduce(max, 0, pluck('length', preds)), function() {
  935. var idx = 0;
  936. var len = preds.length;
  937. while (idx < len) {
  938. if (!preds[idx].apply(this, arguments)) {
  939. return false;
  940. }
  941. idx += 1;
  942. }
  943. return true;
  944. });
  945. });
  946. /**
  947. * Returns a function that always returns the given value. Note that for
  948. * non-primitives the value returned is a reference to the original value.
  949. *
  950. * This function is known as `const`, `constant`, or `K` (for K combinator) in
  951. * other languages and libraries.
  952. *
  953. * @func
  954. * @memberOf R
  955. * @since v0.1.0
  956. * @category Function
  957. * @sig a -> (* -> a)
  958. * @param {*} val The value to wrap in a function
  959. * @return {Function} A Function :: * -> val.
  960. * @example
  961. *
  962. * const t = R.always('Tee');
  963. * t(); //=> 'Tee'
  964. */
  965. var always = _curry1(function always(val) {
  966. return function() {
  967. return val;
  968. };
  969. });
  970. /**
  971. * Returns `true` if both arguments are `true`; `false` otherwise.
  972. *
  973. * @func
  974. * @memberOf R
  975. * @since v0.1.0
  976. * @category Logic
  977. * @sig a -> b -> a | b
  978. * @param {Any} a
  979. * @param {Any} b
  980. * @return {Any} the first argument if it is falsy, otherwise the second argument.
  981. * @see R.both
  982. * @example
  983. *
  984. * R.and(true, true); //=> true
  985. * R.and(true, false); //=> false
  986. * R.and(false, true); //=> false
  987. * R.and(false, false); //=> false
  988. */
  989. var and = _curry2(function and(a, b) {
  990. return a && b;
  991. });
  992. function XAny(f, xf) {
  993. this.xf = xf;
  994. this.f = f;
  995. this.any = false;
  996. }
  997. XAny.prototype['@@transducer/init'] = _xfBase.init;
  998. XAny.prototype['@@transducer/result'] = function(result) {
  999. if (!this.any) {
  1000. result = this.xf['@@transducer/step'](result, false);
  1001. }
  1002. return this.xf['@@transducer/result'](result);
  1003. };
  1004. XAny.prototype['@@transducer/step'] = function(result, input) {
  1005. if (this.f(input)) {
  1006. this.any = true;
  1007. result = _reduced(this.xf['@@transducer/step'](result, true));
  1008. }
  1009. return result;
  1010. };
  1011. var _xany = _curry2(function _xany(f, xf) { return new XAny(f, xf); });
  1012. /**
  1013. * Returns `true` if at least one of the elements of the list match the predicate,
  1014. * `false` otherwise.
  1015. *
  1016. * Dispatches to the `any` method of the second argument, if present.
  1017. *
  1018. * Acts as a transducer if a transformer is given in list position.
  1019. *
  1020. * @func
  1021. * @memberOf R
  1022. * @since v0.1.0
  1023. * @category List
  1024. * @sig (a -> Boolean) -> [a] -> Boolean
  1025. * @param {Function} fn The predicate function.
  1026. * @param {Array} list The array to consider.
  1027. * @return {Boolean} `true` if the predicate is satisfied by at least one element, `false`
  1028. * otherwise.
  1029. * @see R.all, R.none, R.transduce
  1030. * @example
  1031. *
  1032. * const lessThan0 = R.flip(R.lt)(0);
  1033. * const lessThan2 = R.flip(R.lt)(2);
  1034. * R.any(lessThan0)([1, 2]); //=> false
  1035. * R.any(lessThan2)([1, 2]); //=> true
  1036. */
  1037. var any = _curry2(_dispatchable(['any'], _xany, function any(fn, list) {
  1038. var idx = 0;
  1039. while (idx < list.length) {
  1040. if (fn(list[idx])) {
  1041. return true;
  1042. }
  1043. idx += 1;
  1044. }
  1045. return false;
  1046. }));
  1047. /**
  1048. * Takes a list of predicates and returns a predicate that returns true for a
  1049. * given list of arguments if at least one of the provided predicates is
  1050. * satisfied by those arguments.
  1051. *
  1052. * The function returned is a curried function whose arity matches that of the
  1053. * highest-arity predicate.
  1054. *
  1055. * @func
  1056. * @memberOf R
  1057. * @since v0.9.0
  1058. * @category Logic
  1059. * @sig [(*... -> Boolean)] -> (*... -> Boolean)
  1060. * @param {Array} predicates An array of predicates to check
  1061. * @return {Function} The combined predicate
  1062. * @see R.allPass
  1063. * @example
  1064. *
  1065. * const isClub = R.propEq('suit', '♣');
  1066. * const isSpade = R.propEq('suit', '♠');
  1067. * const isBlackCard = R.anyPass([isClub, isSpade]);
  1068. *
  1069. * isBlackCard({rank: '10', suit: '♣'}); //=> true
  1070. * isBlackCard({rank: 'Q', suit: '♠'}); //=> true
  1071. * isBlackCard({rank: 'Q', suit: '♦'}); //=> false
  1072. */
  1073. var anyPass = _curry1(function anyPass(preds) {
  1074. return curryN(reduce(max, 0, pluck('length', preds)), function() {
  1075. var idx = 0;
  1076. var len = preds.length;
  1077. while (idx < len) {
  1078. if (preds[idx].apply(this, arguments)) {
  1079. return true;
  1080. }
  1081. idx += 1;
  1082. }
  1083. return false;
  1084. });
  1085. });
  1086. /**
  1087. * ap applies a list of functions to a list of values.
  1088. *
  1089. * Dispatches to the `ap` method of the second argument, if present. Also
  1090. * treats curried functions as applicatives.
  1091. *
  1092. * @func
  1093. * @memberOf R
  1094. * @since v0.3.0
  1095. * @category Function
  1096. * @sig [a -> b] -> [a] -> [b]
  1097. * @sig Apply f => f (a -> b) -> f a -> f b
  1098. * @sig (r -> a -> b) -> (r -> a) -> (r -> b)
  1099. * @param {*} applyF
  1100. * @param {*} applyX
  1101. * @return {*}
  1102. * @example
  1103. *
  1104. * R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6]
  1105. * R.ap([R.concat('tasty '), R.toUpper], ['pizza', 'salad']); //=> ["tasty pizza", "tasty salad", "PIZZA", "SALAD"]
  1106. *
  1107. * // R.ap can also be used as S combinator
  1108. * // when only two functions are passed
  1109. * R.ap(R.concat, R.toUpper)('Ramda') //=> 'RamdaRAMDA'
  1110. * @symb R.ap([f, g], [a, b]) = [f(a), f(b), g(a), g(b)]
  1111. */
  1112. var ap = _curry2(function ap(applyF, applyX) {
  1113. return (
  1114. typeof applyX['fantasy-land/ap'] === 'function' ?
  1115. applyX['fantasy-land/ap'](applyF) :
  1116. typeof applyF.ap === 'function' ?
  1117. applyF.ap(applyX) :
  1118. typeof applyF === 'function' ?
  1119. function(x) { return applyF(x)(applyX(x)); } :
  1120. // else
  1121. _reduce(function(acc, f) { return _concat(acc, map(f, applyX)); }, [], applyF)
  1122. );
  1123. });
  1124. function _aperture(n, list) {
  1125. var idx = 0;
  1126. var limit = list.length - (n - 1);
  1127. var acc = new Array(limit >= 0 ? limit : 0);
  1128. while (idx < limit) {
  1129. acc[idx] = Array.prototype.slice.call(list, idx, idx + n);
  1130. idx += 1;
  1131. }
  1132. return acc;
  1133. }
  1134. function XAperture(n, xf) {
  1135. this.xf = xf;
  1136. this.pos = 0;
  1137. this.full = false;
  1138. this.acc = new Array(n);
  1139. }
  1140. XAperture.prototype['@@transducer/init'] = _xfBase.init;
  1141. XAperture.prototype['@@transducer/result'] = function(result) {
  1142. this.acc = null;
  1143. return this.xf['@@transducer/result'](result);
  1144. };
  1145. XAperture.prototype['@@transducer/step'] = function(result, input) {
  1146. this.store(input);
  1147. return this.full ? this.xf['@@transducer/step'](result, this.getCopy()) : result;
  1148. };
  1149. XAperture.prototype.store = function(input) {
  1150. this.acc[this.pos] = input;
  1151. this.pos += 1;
  1152. if (this.pos === this.acc.length) {
  1153. this.pos = 0;
  1154. this.full = true;
  1155. }
  1156. };
  1157. XAperture.prototype.getCopy = function() {
  1158. return _concat(Array.prototype.slice.call(this.acc, this.pos),
  1159. Array.prototype.slice.call(this.acc, 0, this.pos));
  1160. };
  1161. var _xaperture = _curry2(function _xaperture(n, xf) { return new XAperture(n, xf); });
  1162. /**
  1163. * Returns a new list, composed of n-tuples of consecutive elements. If `n` is
  1164. * greater than the length of the list, an empty list is returned.
  1165. *
  1166. * Acts as a transducer if a transformer is given in list position.
  1167. *
  1168. * @func
  1169. * @memberOf R
  1170. * @since v0.12.0
  1171. * @category List
  1172. * @sig Number -> [a] -> [[a]]
  1173. * @param {Number} n The size of the tuples to create
  1174. * @param {Array} list The list to split into `n`-length tuples
  1175. * @return {Array} The resulting list of `n`-length tuples
  1176. * @see R.transduce
  1177. * @example
  1178. *
  1179. * R.aperture(2, [1, 2, 3, 4, 5]); //=> [[1, 2], [2, 3], [3, 4], [4, 5]]
  1180. * R.aperture(3, [1, 2, 3, 4, 5]); //=> [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
  1181. * R.aperture(7, [1, 2, 3, 4, 5]); //=> []
  1182. */
  1183. var aperture = _curry2(_dispatchable([], _xaperture, _aperture));
  1184. /**
  1185. * Returns a new list containing the contents of the given list, followed by
  1186. * the given element.
  1187. *
  1188. * @func
  1189. * @memberOf R
  1190. * @since v0.1.0
  1191. * @category List
  1192. * @sig a -> [a] -> [a]
  1193. * @param {*} el The element to add to the end of the new list.
  1194. * @param {Array} list The list of elements to add a new item to.
  1195. * list.
  1196. * @return {Array} A new list containing the elements of the old list followed by `el`.
  1197. * @see R.prepend
  1198. * @example
  1199. *
  1200. * R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
  1201. * R.append('tests', []); //=> ['tests']
  1202. * R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]
  1203. */
  1204. var append = _curry2(function append(el, list) {
  1205. return _concat(list, [el]);
  1206. });
  1207. /**
  1208. * Applies function `fn` to the argument list `args`. This is useful for
  1209. * creating a fixed-arity function from a variadic function. `fn` should be a
  1210. * bound function if context is significant.
  1211. *
  1212. * @func
  1213. * @memberOf R
  1214. * @since v0.7.0
  1215. * @category Function
  1216. * @sig (*... -> a) -> [*] -> a
  1217. * @param {Function} fn The function which will be called with `args`
  1218. * @param {Array} args The arguments to call `fn` with
  1219. * @return {*} result The result, equivalent to `fn(...args)`
  1220. * @see R.call, R.unapply
  1221. * @example
  1222. *
  1223. * const nums = [1, 2, 3, -99, 42, 6, 7];
  1224. * R.apply(Math.max, nums); //=> 42
  1225. * @symb R.apply(f, [a, b, c]) = f(a, b, c)
  1226. */
  1227. var apply = _curry2(function apply(fn, args) {
  1228. return fn.apply(this, args);
  1229. });
  1230. /**
  1231. * Returns a list of all the enumerable own properties of the supplied object.
  1232. * Note that the order of the output array is not guaranteed across different
  1233. * JS platforms.
  1234. *
  1235. * @func
  1236. * @memberOf R
  1237. * @since v0.1.0
  1238. * @category Object
  1239. * @sig {k: v} -> [v]
  1240. * @param {Object} obj The object to extract values from
  1241. * @return {Array} An array of the values of the object's own properties.
  1242. * @see R.valuesIn, R.keys
  1243. * @example
  1244. *
  1245. * R.values({a: 1, b: 2, c: 3}); //=> [1, 2, 3]
  1246. */
  1247. var values = _curry1(function values(obj) {
  1248. var props = keys(obj);
  1249. var len = props.length;
  1250. var vals = [];
  1251. var idx = 0;
  1252. while (idx < len) {
  1253. vals[idx] = obj[props[idx]];
  1254. idx += 1;
  1255. }
  1256. return vals;
  1257. });
  1258. // Use custom mapValues function to avoid issues with specs that include a "map" key and R.map
  1259. // delegating calls to .map
  1260. function mapValues(fn, obj) {
  1261. return keys(obj).reduce(function(acc, key) {
  1262. acc[key] = fn(obj[key]);
  1263. return acc;
  1264. }, {});
  1265. }
  1266. /**
  1267. * Given a spec object recursively mapping properties to functions, creates a
  1268. * function producing an object of the same structure, by mapping each property
  1269. * to the result of calling its associated function with the supplied arguments.
  1270. *
  1271. * @func
  1272. * @memberOf R
  1273. * @since v0.20.0
  1274. * @category Function
  1275. * @sig {k: ((a, b, ..., m) -> v)} -> ((a, b, ..., m) -> {k: v})
  1276. * @param {Object} spec an object recursively mapping properties to functions for
  1277. * producing the values for these properties.
  1278. * @return {Function} A function that returns an object of the same structure
  1279. * as `spec', with each property set to the value returned by calling its
  1280. * associated function with the supplied arguments.
  1281. * @see R.converge, R.juxt
  1282. * @example
  1283. *
  1284. * const getMetrics = R.applySpec({
  1285. * sum: R.add,
  1286. * nested: { mul: R.multiply }
  1287. * });
  1288. * getMetrics(2, 4); // => { sum: 6, nested: { mul: 8 } }
  1289. * @symb R.applySpec({ x: f, y: { z: g } })(a, b) = { x: f(a, b), y: { z: g(a, b) } }
  1290. */
  1291. var applySpec = _curry1(function applySpec(spec) {
  1292. spec = mapValues(
  1293. function(v) { return typeof v == 'function' ? v : applySpec(v); },
  1294. spec
  1295. );
  1296. return curryN(
  1297. reduce(max, 0, pluck('length', values(spec))),
  1298. function() {
  1299. var args = arguments;
  1300. return mapValues(function(f) { return apply(f, args); }, spec);
  1301. });
  1302. });
  1303. /**
  1304. * Takes a value and applies a function to it.
  1305. *
  1306. * This function is also known as the `thrush` combinator.
  1307. *
  1308. * @func
  1309. * @memberOf R
  1310. * @since v0.25.0
  1311. * @category Function
  1312. * @sig a -> (a -> b) -> b
  1313. * @param {*} x The value
  1314. * @param {Function} f The function to apply
  1315. * @return {*} The result of applying `f` to `x`
  1316. * @example
  1317. *
  1318. * const t42 = R.applyTo(42);
  1319. * t42(R.identity); //=> 42
  1320. * t42(R.add(1)); //=> 43
  1321. */
  1322. var applyTo = _curry2(function applyTo(x, f) { return f(x); });
  1323. /**
  1324. * Makes an ascending comparator function out of a function that returns a value
  1325. * that can be compared with `<` and `>`.
  1326. *
  1327. * @func
  1328. * @memberOf R
  1329. * @since v0.23.0
  1330. * @category Function
  1331. * @sig Ord b => (a -> b) -> a -> a -> Number
  1332. * @param {Function} fn A function of arity one that returns a value that can be compared
  1333. * @param {*} a The first item to be compared.
  1334. * @param {*} b The second item to be compared.
  1335. * @return {Number} `-1` if fn(a) < fn(b), `1` if fn(b) < fn(a), otherwise `0`
  1336. * @see R.descend
  1337. * @example
  1338. *
  1339. * const byAge = R.ascend(R.prop('age'));
  1340. * const people = [
  1341. * { name: 'Emma', age: 70 },
  1342. * { name: 'Peter', age: 78 },
  1343. * { name: 'Mikhail', age: 62 },
  1344. * ];
  1345. * const peopleByYoungestFirst = R.sort(byAge, people);
  1346. * //=> [{ name: 'Mikhail', age: 62 },{ name: 'Emma', age: 70 }, { name: 'Peter', age: 78 }]
  1347. */
  1348. var ascend = _curry3(function ascend(fn, a, b) {
  1349. var aa = fn(a);
  1350. var bb = fn(b);
  1351. return aa < bb ? -1 : aa > bb ? 1 : 0;
  1352. });
  1353. /**
  1354. * Makes a shallow clone of an object, setting or overriding the specified
  1355. * property with the given value. Note that this copies and flattens prototype
  1356. * properties onto the new object as well. All non-primitive properties are
  1357. * copied by reference.
  1358. *
  1359. * @func
  1360. * @memberOf R
  1361. * @since v0.8.0
  1362. * @category Object
  1363. * @sig String -> a -> {k: v} -> {k: v}
  1364. * @param {String} prop The property name to set
  1365. * @param {*} val The new value
  1366. * @param {Object} obj The object to clone
  1367. * @return {Object} A new object equivalent to the original except for the changed property.
  1368. * @see R.dissoc, R.pick
  1369. * @example
  1370. *
  1371. * R.assoc('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3}
  1372. */
  1373. var assoc = _curry3(function assoc(prop, val, obj) {
  1374. var result = {};
  1375. for (var p in obj) {
  1376. result[p] = obj[p];
  1377. }
  1378. result[prop] = val;
  1379. return result;
  1380. });
  1381. /**
  1382. * Determine if the passed argument is an integer.
  1383. *
  1384. * @private
  1385. * @param {*} n
  1386. * @category Type
  1387. * @return {Boolean}
  1388. */
  1389. var _isInteger = Number.isInteger || function _isInteger(n) {
  1390. return (n << 0) === n;
  1391. };
  1392. /**
  1393. * Checks if the input value is `null` or `undefined`.
  1394. *
  1395. * @func
  1396. * @memberOf R
  1397. * @since v0.9.0
  1398. * @category Type
  1399. * @sig * -> Boolean
  1400. * @param {*} x The value to test.
  1401. * @return {Boolean} `true` if `x` is `undefined` or `null`, otherwise `false`.
  1402. * @example
  1403. *
  1404. * R.isNil(null); //=> true
  1405. * R.isNil(undefined); //=> true
  1406. * R.isNil(0); //=> false
  1407. * R.isNil([]); //=> false
  1408. */
  1409. var isNil = _curry1(function isNil(x) { return x == null; });
  1410. /**
  1411. * Makes a shallow clone of an object, setting or overriding the nodes required
  1412. * to create the given path, and placing the specific value at the tail end of
  1413. * that path. Note that this copies and flattens prototype properties onto the
  1414. * new object as well. All non-primitive properties are copied by reference.
  1415. *
  1416. * @func
  1417. * @memberOf R
  1418. * @since v0.8.0
  1419. * @category Object
  1420. * @typedefn Idx = String | Int
  1421. * @sig [Idx] -> a -> {a} -> {a}
  1422. * @param {Array} path the path to set
  1423. * @param {*} val The new value
  1424. * @param {Object} obj The object to clone
  1425. * @return {Object} A new object equivalent to the original except along the specified path.
  1426. * @see R.dissocPath
  1427. * @example
  1428. *
  1429. * R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}); //=> {a: {b: {c: 42}}}
  1430. *
  1431. * // Any missing or non-object keys in path will be overridden
  1432. * R.assocPath(['a', 'b', 'c'], 42, {a: 5}); //=> {a: {b: {c: 42}}}
  1433. */
  1434. var assocPath = _curry3(function assocPath(path, val, obj) {
  1435. if (path.length === 0) {
  1436. return val;
  1437. }
  1438. var idx = path[0];
  1439. if (path.length > 1) {
  1440. var nextObj = (!isNil(obj) && _has(idx, obj)) ? obj[idx] : _isInteger(path[1]) ? [] : {};
  1441. val = assocPath(Array.prototype.slice.call(path, 1), val, nextObj);
  1442. }
  1443. if (_isInteger(idx) && _isArray(obj)) {
  1444. var arr = [].concat(obj);
  1445. arr[idx] = val;
  1446. return arr;
  1447. } else {
  1448. return assoc(idx, val, obj);
  1449. }
  1450. });
  1451. /**
  1452. * Wraps a function of any arity (including nullary) in a function that accepts
  1453. * exactly `n` parameters. Any extraneous parameters will not be passed to the
  1454. * supplied function.
  1455. *
  1456. * @func
  1457. * @memberOf R
  1458. * @since v0.1.0
  1459. * @category Function
  1460. * @sig Number -> (* -> a) -> (* -> a)
  1461. * @param {Number} n The desired arity of the new function.
  1462. * @param {Function} fn The function to wrap.
  1463. * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
  1464. * arity `n`.
  1465. * @see R.binary, R.unary
  1466. * @example
  1467. *
  1468. * const takesTwoArgs = (a, b) => [a, b];
  1469. *
  1470. * takesTwoArgs.length; //=> 2
  1471. * takesTwoArgs(1, 2); //=> [1, 2]
  1472. *
  1473. * const takesOneArg = R.nAry(1, takesTwoArgs);
  1474. * takesOneArg.length; //=> 1
  1475. * // Only `n` arguments are passed to the wrapped function
  1476. * takesOneArg(1, 2); //=> [1, undefined]
  1477. * @symb R.nAry(0, f)(a, b) = f()
  1478. * @symb R.nAry(1, f)(a, b) = f(a)
  1479. * @symb R.nAry(2, f)(a, b) = f(a, b)
  1480. */
  1481. var nAry = _curry2(function nAry(n, fn) {
  1482. switch (n) {
  1483. case 0: return function() {return fn.call(this);};
  1484. case 1: return function(a0) {return fn.call(this, a0);};
  1485. case 2: return function(a0, a1) {return fn.call(this, a0, a1);};
  1486. case 3: return function(a0, a1, a2) {return fn.call(this, a0, a1, a2);};
  1487. case 4: return function(a0, a1, a2, a3) {return fn.call(this, a0, a1, a2, a3);};
  1488. case 5: return function(a0, a1, a2, a3, a4) {return fn.call(this, a0, a1, a2, a3, a4);};
  1489. case 6: return function(a0, a1, a2, a3, a4, a5) {return fn.call(this, a0, a1, a2, a3, a4, a5);};
  1490. case 7: return function(a0, a1, a2, a3, a4, a5, a6) {return fn.call(this, a0, a1, a2, a3, a4, a5, a6);};
  1491. case 8: return function(a0, a1, a2, a3, a4, a5, a6, a7) {return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7);};
  1492. case 9: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8) {return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8);};
  1493. case 10: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);};
  1494. default: throw new Error('First argument to nAry must be a non-negative integer no greater than ten');
  1495. }
  1496. });
  1497. /**
  1498. * Wraps a function of any arity (including nullary) in a function that accepts
  1499. * exactly 2 parameters. Any extraneous parameters will not be passed to the
  1500. * supplied function.
  1501. *
  1502. * @func
  1503. * @memberOf R
  1504. * @since v0.2.0
  1505. * @category Function
  1506. * @sig (* -> c) -> (a, b -> c)
  1507. * @param {Function} fn The function to wrap.
  1508. * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
  1509. * arity 2.
  1510. * @see R.nAry, R.unary
  1511. * @example
  1512. *
  1513. * const takesThreeArgs = function(a, b, c) {
  1514. * return [a, b, c];
  1515. * };
  1516. * takesThreeArgs.length; //=> 3
  1517. * takesThreeArgs(1, 2, 3); //=> [1, 2, 3]
  1518. *
  1519. * const takesTwoArgs = R.binary(takesThreeArgs);
  1520. * takesTwoArgs.length; //=> 2
  1521. * // Only 2 arguments are passed to the wrapped function
  1522. * takesTwoArgs(1, 2, 3); //=> [1, 2, undefined]
  1523. * @symb R.binary(f)(a, b, c) = f(a, b)
  1524. */
  1525. var binary = _curry1(function binary(fn) {
  1526. return nAry(2, fn);
  1527. });
  1528. function _isFunction(x) {
  1529. return Object.prototype.toString.call(x) === '[object Function]';
  1530. }
  1531. /**
  1532. * "lifts" a function to be the specified arity, so that it may "map over" that
  1533. * many lists, Functions or other objects that satisfy the [FantasyLand Apply spec](https://github.com/fantasyland/fantasy-land#apply).
  1534. *
  1535. * @func
  1536. * @memberOf R
  1537. * @since v0.7.0
  1538. * @category Function
  1539. * @sig Number -> (*... -> *) -> ([*]... -> [*])
  1540. * @param {Function} fn The function to lift into higher context
  1541. * @return {Function} The lifted function.
  1542. * @see R.lift, R.ap
  1543. * @example
  1544. *
  1545. * const madd3 = R.liftN(3, (...args) => R.sum(args));
  1546. * madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
  1547. */
  1548. var liftN = _curry2(function liftN(arity, fn) {
  1549. var lifted = curryN(arity, fn);
  1550. return curryN(arity, function() {
  1551. return _reduce(ap, map(lifted, arguments[0]), Array.prototype.slice.call(arguments, 1));
  1552. });
  1553. });
  1554. /**
  1555. * "lifts" a function of arity > 1 so that it may "map over" a list, Function or other
  1556. * object that satisfies the [FantasyLand Apply spec](https://github.com/fantasyland/fantasy-land#apply).
  1557. *
  1558. * @func
  1559. * @memberOf R
  1560. * @since v0.7.0
  1561. * @category Function
  1562. * @sig (*... -> *) -> ([*]... -> [*])
  1563. * @param {Function} fn The function to lift into higher context
  1564. * @return {Function} The lifted function.
  1565. * @see R.liftN
  1566. * @example
  1567. *
  1568. * const madd3 = R.lift((a, b, c) => a + b + c);
  1569. *
  1570. * madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
  1571. *
  1572. * const madd5 = R.lift((a, b, c, d, e) => a + b + c + d + e);
  1573. *
  1574. * madd5([1,2], [3], [4, 5], [6], [7, 8]); //=> [21, 22, 22, 23, 22, 23, 23, 24]
  1575. */
  1576. var lift = _curry1(function lift(fn) {
  1577. return liftN(fn.length, fn);
  1578. });
  1579. /**
  1580. * A function which calls the two provided functions and returns the `&&`
  1581. * of the results.
  1582. * It returns the result of the first function if it is false-y and the result
  1583. * of the second function otherwise. Note that this is short-circuited,
  1584. * meaning that the second function will not be invoked if the first returns a
  1585. * false-y value.
  1586. *
  1587. * In addition to functions, `R.both` also accepts any fantasy-land compatible
  1588. * applicative functor.
  1589. *
  1590. * @func
  1591. * @memberOf R
  1592. * @since v0.12.0
  1593. * @category Logic
  1594. * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
  1595. * @param {Function} f A predicate
  1596. * @param {Function} g Another predicate
  1597. * @return {Function} a function that applies its arguments to `f` and `g` and `&&`s their outputs together.
  1598. * @see R.and
  1599. * @example
  1600. *
  1601. * const gt10 = R.gt(R.__, 10)
  1602. * const lt20 = R.lt(R.__, 20)
  1603. * const f = R.both(gt10, lt20);
  1604. * f(15); //=> true
  1605. * f(30); //=> false
  1606. *
  1607. * R.both(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(false)
  1608. * R.both([false, false, 'a'], [11]); //=> [false, false, 11]
  1609. */
  1610. var both = _curry2(function both(f, g) {
  1611. return _isFunction(f) ?
  1612. function _both() {
  1613. return f.apply(this, arguments) && g.apply(this, arguments);
  1614. } :
  1615. lift(and)(f, g);
  1616. });
  1617. /**
  1618. * Returns a curried equivalent of the provided function. The curried function
  1619. * has two unusual capabilities. First, its arguments needn't be provided one
  1620. * at a time. If `f` is a ternary function and `g` is `R.curry(f)`, the
  1621. * following are equivalent:
  1622. *
  1623. * - `g(1)(2)(3)`
  1624. * - `g(1)(2, 3)`
  1625. * - `g(1, 2)(3)`
  1626. * - `g(1, 2, 3)`
  1627. *
  1628. * Secondly, the special placeholder value [`R.__`](#__) may be used to specify
  1629. * "gaps", allowing partial application of any combination of arguments,
  1630. * regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
  1631. * the following are equivalent:
  1632. *
  1633. * - `g(1, 2, 3)`
  1634. * - `g(_, 2, 3)(1)`
  1635. * - `g(_, _, 3)(1)(2)`
  1636. * - `g(_, _, 3)(1, 2)`
  1637. * - `g(_, 2)(1)(3)`
  1638. * - `g(_, 2)(1, 3)`
  1639. * - `g(_, 2)(_, 3)(1)`
  1640. *
  1641. * @func
  1642. * @memberOf R
  1643. * @since v0.1.0
  1644. * @category Function
  1645. * @sig (* -> a) -> (* -> a)
  1646. * @param {Function} fn The function to curry.
  1647. * @return {Function} A new, curried function.
  1648. * @see R.curryN, R.partial
  1649. * @example
  1650. *
  1651. * const addFourNumbers = (a, b, c, d) => a + b + c + d;
  1652. *
  1653. * const curriedAddFourNumbers = R.curry(addFourNumbers);
  1654. * const f = curriedAddFourNumbers(1, 2);
  1655. * const g = f(3);
  1656. * g(4); //=> 10
  1657. */
  1658. var curry = _curry1(function curry(fn) {
  1659. return curryN(fn.length, fn);
  1660. });
  1661. /**
  1662. * Returns the result of calling its first argument with the remaining
  1663. * arguments. This is occasionally useful as a converging function for
  1664. * [`R.converge`](#converge): the first branch can produce a function while the
  1665. * remaining branches produce values to be passed to that function as its
  1666. * arguments.
  1667. *
  1668. * @func
  1669. * @memberOf R
  1670. * @since v0.9.0
  1671. * @category Function
  1672. * @sig (*... -> a),*... -> a
  1673. * @param {Function} fn The function to apply to the remaining arguments.
  1674. * @param {...*} args Any number of positional arguments.
  1675. * @return {*}
  1676. * @see R.apply
  1677. * @example
  1678. *
  1679. * R.call(R.add, 1, 2); //=> 3
  1680. *
  1681. * const indentN = R.pipe(R.repeat(' '),
  1682. * R.join(''),
  1683. * R.replace(/^(?!$)/gm));
  1684. *
  1685. * const format = R.converge(R.call, [
  1686. * R.pipe(R.prop('indent'), indentN),
  1687. * R.prop('value')
  1688. * ]);
  1689. *
  1690. * format({indent: 2, value: 'foo\nbar\nbaz\n'}); //=> ' foo\n bar\n baz\n'
  1691. * @symb R.call(f, a, b) = f(a, b)
  1692. */
  1693. var call = curry(function call(fn) {
  1694. return fn.apply(this, Array.prototype.slice.call(arguments, 1));
  1695. });
  1696. /**
  1697. * `_makeFlat` is a helper function that returns a one-level or fully recursive
  1698. * function based on the flag passed in.
  1699. *
  1700. * @private
  1701. */
  1702. function _makeFlat(recursive) {
  1703. return function flatt(list) {
  1704. var value, jlen, j;
  1705. var result = [];
  1706. var idx = 0;
  1707. var ilen = list.length;
  1708. while (idx < ilen) {
  1709. if (_isArrayLike(list[idx])) {
  1710. value = recursive ? flatt(list[idx]) : list[idx];
  1711. j = 0;
  1712. jlen = value.length;
  1713. while (j < jlen) {
  1714. result[result.length] = value[j];
  1715. j += 1;
  1716. }
  1717. } else {
  1718. result[result.length] = list[idx];
  1719. }
  1720. idx += 1;
  1721. }
  1722. return result;
  1723. };
  1724. }
  1725. function _forceReduced(x) {
  1726. return {
  1727. '@@transducer/value': x,
  1728. '@@transducer/reduced': true
  1729. };
  1730. }
  1731. var preservingReduced = function(xf) {
  1732. return {
  1733. '@@transducer/init': _xfBase.init,
  1734. '@@transducer/result': function(result) {
  1735. return xf['@@transducer/result'](result);
  1736. },
  1737. '@@transducer/step': function(result, input) {
  1738. var ret = xf['@@transducer/step'](result, input);
  1739. return ret['@@transducer/reduced'] ? _forceReduced(ret) : ret;
  1740. }
  1741. };
  1742. };
  1743. var _flatCat = function _xcat(xf) {
  1744. var rxf = preservingReduced(xf);
  1745. return {
  1746. '@@transducer/init': _xfBase.init,
  1747. '@@transducer/result': function(result) {
  1748. return rxf['@@transducer/result'](result);
  1749. },
  1750. '@@transducer/step': function(result, input) {
  1751. return !_isArrayLike(input) ? _reduce(rxf, result, [input]) : _reduce(rxf, result, input);
  1752. }
  1753. };
  1754. };
  1755. var _xchain = _curry2(function _xchain(f, xf) {
  1756. return map(f, _flatCat(xf));
  1757. });
  1758. /**
  1759. * `chain` maps a function over a list and concatenates the results. `chain`
  1760. * is also known as `flatMap` in some libraries.
  1761. *
  1762. * Dispatches to the `chain` method of the second argument, if present,
  1763. * according to the [FantasyLand Chain spec](https://github.com/fantasyland/fantasy-land#chain).
  1764. *
  1765. * If second argument is a function, `chain(f, g)(x)` is equivalent to `f(g(x), x)`.
  1766. *
  1767. * Acts as a transducer if a transformer is given in list position.
  1768. *
  1769. * @func
  1770. * @memberOf R
  1771. * @since v0.3.0
  1772. * @category List
  1773. * @sig Chain m => (a -> m b) -> m a -> m b
  1774. * @param {Function} fn The function to map with
  1775. * @param {Array} list The list to map over
  1776. * @return {Array} The result of flat-mapping `list` with `fn`
  1777. * @example
  1778. *
  1779. * const duplicate = n => [n, n];
  1780. * R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3]
  1781. *
  1782. * R.chain(R.append, R.head)([1, 2, 3]); //=> [1, 2, 3, 1]
  1783. */
  1784. var chain = _curry2(_dispatchable(['fantasy-land/chain', 'chain'], _xchain, function chain(fn, monad) {
  1785. if (typeof monad === 'function') {
  1786. return function(x) { return fn(monad(x))(x); };
  1787. }
  1788. return _makeFlat(false)(map(fn, monad));
  1789. }));
  1790. /**
  1791. * Restricts a number to be within a range.
  1792. *
  1793. * Also works for other ordered types such as Strings and Dates.
  1794. *
  1795. * @func
  1796. * @memberOf R
  1797. * @since v0.20.0
  1798. * @category Relation
  1799. * @sig Ord a => a -> a -> a -> a
  1800. * @param {Number} minimum The lower limit of the clamp (inclusive)
  1801. * @param {Number} maximum The upper limit of the clamp (inclusive)
  1802. * @param {Number} value Value to be clamped
  1803. * @return {Number} Returns `minimum` when `val < minimum`, `maximum` when `val > maximum`, returns `val` otherwise
  1804. * @example
  1805. *
  1806. * R.clamp(1, 10, -5) // => 1
  1807. * R.clamp(1, 10, 15) // => 10
  1808. * R.clamp(1, 10, 4) // => 4
  1809. */
  1810. var clamp = _curry3(function clamp(min, max, value) {
  1811. if (min > max) {
  1812. throw new Error('min must not be greater than max in clamp(min, max, value)');
  1813. }
  1814. return value < min ? min :
  1815. value > max ? max :
  1816. value;
  1817. });
  1818. function _cloneRegExp(pattern) {
  1819. return new RegExp(pattern.source, (pattern.global ? 'g' : '') +
  1820. (pattern.ignoreCase ? 'i' : '') +
  1821. (pattern.multiline ? 'm' : '') +
  1822. (pattern.sticky ? 'y' : '') +
  1823. (pattern.unicode ? 'u' : ''));
  1824. }
  1825. /**
  1826. * Gives a single-word string description of the (native) type of a value,
  1827. * returning such answers as 'Object', 'Number', 'Array', or 'Null'. Does not
  1828. * attempt to distinguish user Object types any further, reporting them all as
  1829. * 'Object'.
  1830. *
  1831. * @func
  1832. * @memberOf R
  1833. * @since v0.8.0
  1834. * @category Type
  1835. * @sig (* -> {*}) -> String
  1836. * @param {*} val The value to test
  1837. * @return {String}
  1838. * @example
  1839. *
  1840. * R.type({}); //=> "Object"
  1841. * R.type(1); //=> "Number"
  1842. * R.type(false); //=> "Boolean"
  1843. * R.type('s'); //=> "String"
  1844. * R.type(null); //=> "Null"
  1845. * R.type([]); //=> "Array"
  1846. * R.type(/[A-z]/); //=> "RegExp"
  1847. * R.type(() => {}); //=> "Function"
  1848. * R.type(undefined); //=> "Undefined"
  1849. */
  1850. var type = _curry1(function type(val) {
  1851. return val === null ? 'Null' :
  1852. val === undefined ? 'Undefined' :
  1853. Object.prototype.toString.call(val).slice(8, -1);
  1854. });
  1855. /**
  1856. * Copies an object.
  1857. *
  1858. * @private
  1859. * @param {*} value The value to be copied
  1860. * @param {Array} refFrom Array containing the source references
  1861. * @param {Array} refTo Array containing the copied source references
  1862. * @param {Boolean} deep Whether or not to perform deep cloning.
  1863. * @return {*} The copied value.
  1864. */
  1865. function _clone(value, refFrom, refTo, deep) {
  1866. var copy = function copy(copiedValue) {
  1867. var len = refFrom.length;
  1868. var idx = 0;
  1869. while (idx < len) {
  1870. if (value === refFrom[idx]) {
  1871. return refTo[idx];
  1872. }
  1873. idx += 1;
  1874. }
  1875. refFrom[idx + 1] = value;
  1876. refTo[idx + 1] = copiedValue;
  1877. for (var key in value) {
  1878. copiedValue[key] = deep ?
  1879. _clone(value[key], refFrom, refTo, true) : value[key];
  1880. }
  1881. return copiedValue;
  1882. };
  1883. switch (type(value)) {
  1884. case 'Object': return copy({});
  1885. case 'Array': return copy([]);
  1886. case 'Date': return new Date(value.valueOf());
  1887. case 'RegExp': return _cloneRegExp(value);
  1888. default: return value;
  1889. }
  1890. }
  1891. /**
  1892. * Creates a deep copy of the value which may contain (nested) `Array`s and
  1893. * `Object`s, `Number`s, `String`s, `Boolean`s and `Date`s. `Function`s are
  1894. * assigned by reference rather than copied
  1895. *
  1896. * Dispatches to a `clone` method if present.
  1897. *
  1898. * @func
  1899. * @memberOf R
  1900. * @since v0.1.0
  1901. * @category Object
  1902. * @sig {*} -> {*}
  1903. * @param {*} value The object or array to clone
  1904. * @return {*} A deeply cloned copy of `val`
  1905. * @example
  1906. *
  1907. * const objects = [{}, {}, {}];
  1908. * const objectsClone = R.clone(objects);
  1909. * objects === objectsClone; //=> false
  1910. * objects[0] === objectsClone[0]; //=> false
  1911. */
  1912. var clone = _curry1(function clone(value) {
  1913. return value != null && typeof value.clone === 'function' ?
  1914. value.clone() :
  1915. _clone(value, [], [], true);
  1916. });
  1917. /**
  1918. * Makes a comparator function out of a function that reports whether the first
  1919. * element is less than the second.
  1920. *
  1921. * @func
  1922. * @memberOf R
  1923. * @since v0.1.0
  1924. * @category Function
  1925. * @sig ((a, b) -> Boolean) -> ((a, b) -> Number)
  1926. * @param {Function} pred A predicate function of arity two which will return `true` if the first argument
  1927. * is less than the second, `false` otherwise
  1928. * @return {Function} A Function :: a -> b -> Int that returns `-1` if a < b, `1` if b < a, otherwise `0`
  1929. * @example
  1930. *
  1931. * const byAge = R.comparator((a, b) => a.age < b.age);
  1932. * const people = [
  1933. * { name: 'Emma', age: 70 },
  1934. * { name: 'Peter', age: 78 },
  1935. * { name: 'Mikhail', age: 62 },
  1936. * ];
  1937. * const peopleByIncreasingAge = R.sort(byAge, people);
  1938. * //=> [{ name: 'Mikhail', age: 62 },{ name: 'Emma', age: 70 }, { name: 'Peter', age: 78 }]
  1939. */
  1940. var comparator = _curry1(function comparator(pred) {
  1941. return function(a, b) {
  1942. return pred(a, b) ? -1 : pred(b, a) ? 1 : 0;
  1943. };
  1944. });
  1945. /**
  1946. * A function that returns the `!` of its argument. It will return `true` when
  1947. * passed false-y value, and `false` when passed a truth-y one.
  1948. *
  1949. * @func
  1950. * @memberOf R
  1951. * @since v0.1.0
  1952. * @category Logic
  1953. * @sig * -> Boolean
  1954. * @param {*} a any value
  1955. * @return {Boolean} the logical inverse of passed argument.
  1956. * @see R.complement
  1957. * @example
  1958. *
  1959. * R.not(true); //=> false
  1960. * R.not(false); //=> true
  1961. * R.not(0); //=> true
  1962. * R.not(1); //=> false
  1963. */
  1964. var not = _curry1(function not(a) {
  1965. return !a;
  1966. });
  1967. /**
  1968. * Takes a function `f` and returns a function `g` such that if called with the same arguments
  1969. * when `f` returns a "truthy" value, `g` returns `false` and when `f` returns a "falsy" value `g` returns `true`.
  1970. *
  1971. * `R.complement` may be applied to any functor
  1972. *
  1973. * @func
  1974. * @memberOf R
  1975. * @since v0.12.0
  1976. * @category Logic
  1977. * @sig (*... -> *) -> (*... -> Boolean)
  1978. * @param {Function} f
  1979. * @return {Function}
  1980. * @see R.not
  1981. * @example
  1982. *
  1983. * const isNotNil = R.complement(R.isNil);
  1984. * isNil(null); //=> true
  1985. * isNotNil(null); //=> false
  1986. * isNil(7); //=> false
  1987. * isNotNil(7); //=> true
  1988. */
  1989. var complement = lift(not);
  1990. function _pipe(f, g) {
  1991. return function() {
  1992. return g.call(this, f.apply(this, arguments));
  1993. };
  1994. }
  1995. /**
  1996. * This checks whether a function has a [methodname] function. If it isn't an
  1997. * array it will execute that function otherwise it will default to the ramda
  1998. * implementation.
  1999. *
  2000. * @private
  2001. * @param {Function} fn ramda implemtation
  2002. * @param {String} methodname property to check for a custom implementation
  2003. * @return {Object} Whatever the return value of the method is.
  2004. */
  2005. function _checkForMethod(methodname, fn) {
  2006. return function() {
  2007. var length = arguments.length;
  2008. if (length === 0) {
  2009. return fn();
  2010. }
  2011. var obj = arguments[length - 1];
  2012. return (_isArray(obj) || typeof obj[methodname] !== 'function') ?
  2013. fn.apply(this, arguments) :
  2014. obj[methodname].apply(obj, Array.prototype.slice.call(arguments, 0, length - 1));
  2015. };
  2016. }
  2017. /**
  2018. * Returns the elements of the given list or string (or object with a `slice`
  2019. * method) from `fromIndex` (inclusive) to `toIndex` (exclusive).
  2020. *
  2021. * Dispatches to the `slice` method of the third argument, if present.
  2022. *
  2023. * @func
  2024. * @memberOf R
  2025. * @since v0.1.4
  2026. * @category List
  2027. * @sig Number -> Number -> [a] -> [a]
  2028. * @sig Number -> Number -> String -> String
  2029. * @param {Number} fromIndex The start index (inclusive).
  2030. * @param {Number} toIndex The end index (exclusive).
  2031. * @param {*} list
  2032. * @return {*}
  2033. * @example
  2034. *
  2035. * R.slice(1, 3, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
  2036. * R.slice(1, Infinity, ['a', 'b', 'c', 'd']); //=> ['b', 'c', 'd']
  2037. * R.slice(0, -1, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c']
  2038. * R.slice(-3, -1, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
  2039. * R.slice(0, 3, 'ramda'); //=> 'ram'
  2040. */
  2041. var slice = _curry3(_checkForMethod('slice', function slice(fromIndex, toIndex, list) {
  2042. return Array.prototype.slice.call(list, fromIndex, toIndex);
  2043. }));
  2044. /**
  2045. * Returns all but the first element of the given list or string (or object
  2046. * with a `tail` method).
  2047. *
  2048. * Dispatches to the `slice` method of the first argument, if present.
  2049. *
  2050. * @func
  2051. * @memberOf R
  2052. * @since v0.1.0
  2053. * @category List
  2054. * @sig [a] -> [a]
  2055. * @sig String -> String
  2056. * @param {*} list
  2057. * @return {*}
  2058. * @see R.head, R.init, R.last
  2059. * @example
  2060. *
  2061. * R.tail([1, 2, 3]); //=> [2, 3]
  2062. * R.tail([1, 2]); //=> [2]
  2063. * R.tail([1]); //=> []
  2064. * R.tail([]); //=> []
  2065. *
  2066. * R.tail('abc'); //=> 'bc'
  2067. * R.tail('ab'); //=> 'b'
  2068. * R.tail('a'); //=> ''
  2069. * R.tail(''); //=> ''
  2070. */
  2071. var tail = _curry1(_checkForMethod('tail', slice(1, Infinity)));
  2072. /**
  2073. * Performs left-to-right function composition. The leftmost function may have
  2074. * any arity; the remaining functions must be unary.
  2075. *
  2076. * In some libraries this function is named `sequence`.
  2077. *
  2078. * **Note:** The result of pipe is not automatically curried.
  2079. *
  2080. * @func
  2081. * @memberOf R
  2082. * @since v0.1.0
  2083. * @category Function
  2084. * @sig (((a, b, ..., n) -> o), (o -> p), ..., (x -> y), (y -> z)) -> ((a, b, ..., n) -> z)
  2085. * @param {...Function} functions
  2086. * @return {Function}
  2087. * @see R.compose
  2088. * @example
  2089. *
  2090. * const f = R.pipe(Math.pow, R.negate, R.inc);
  2091. *
  2092. * f(3, 4); // -(3^4) + 1
  2093. * @symb R.pipe(f, g, h)(a, b) = h(g(f(a, b)))
  2094. */
  2095. function pipe() {
  2096. if (arguments.length === 0) {
  2097. throw new Error('pipe requires at least one argument');
  2098. }
  2099. return _arity(arguments[0].length,
  2100. reduce(_pipe, arguments[0], tail(arguments)));
  2101. }
  2102. /**
  2103. * Returns a new list or string with the elements or characters in reverse
  2104. * order.
  2105. *
  2106. * @func
  2107. * @memberOf R
  2108. * @since v0.1.0
  2109. * @category List
  2110. * @sig [a] -> [a]
  2111. * @sig String -> String
  2112. * @param {Array|String} list
  2113. * @return {Array|String}
  2114. * @example
  2115. *
  2116. * R.reverse([1, 2, 3]); //=> [3, 2, 1]
  2117. * R.reverse([1, 2]); //=> [2, 1]
  2118. * R.reverse([1]); //=> [1]
  2119. * R.reverse([]); //=> []
  2120. *
  2121. * R.reverse('abc'); //=> 'cba'
  2122. * R.reverse('ab'); //=> 'ba'
  2123. * R.reverse('a'); //=> 'a'
  2124. * R.reverse(''); //=> ''
  2125. */
  2126. var reverse = _curry1(function reverse(list) {
  2127. return _isString(list) ? list.split('').reverse().join('') :
  2128. Array.prototype.slice.call(list, 0).reverse();
  2129. });
  2130. /**
  2131. * Performs right-to-left function composition. The rightmost function may have
  2132. * any arity; the remaining functions must be unary.
  2133. *
  2134. * **Note:** The result of compose is not automatically curried.
  2135. *
  2136. * @func
  2137. * @memberOf R
  2138. * @since v0.1.0
  2139. * @category Function
  2140. * @sig ((y -> z), (x -> y), ..., (o -> p), ((a, b, ..., n) -> o)) -> ((a, b, ..., n) -> z)
  2141. * @param {...Function} ...functions The functions to compose
  2142. * @return {Function}
  2143. * @see R.pipe
  2144. * @example
  2145. *
  2146. * const classyGreeting = (firstName, lastName) => "The name's " + lastName + ", " + firstName + " " + lastName
  2147. * const yellGreeting = R.compose(R.toUpper, classyGreeting);
  2148. * yellGreeting('James', 'Bond'); //=> "THE NAME'S BOND, JAMES BOND"
  2149. *
  2150. * R.compose(Math.abs, R.add(1), R.multiply(2))(-4) //=> 7
  2151. *
  2152. * @symb R.compose(f, g, h)(a, b) = f(g(h(a, b)))
  2153. */
  2154. function compose() {
  2155. if (arguments.length === 0) {
  2156. throw new Error('compose requires at least one argument');
  2157. }
  2158. return pipe.apply(this, reverse(arguments));
  2159. }
  2160. /**
  2161. * Returns the right-to-left Kleisli composition of the provided functions,
  2162. * each of which must return a value of a type supported by [`chain`](#chain).
  2163. *
  2164. * `R.composeK(h, g, f)` is equivalent to `R.compose(R.chain(h), R.chain(g), f)`.
  2165. *
  2166. * @func
  2167. * @memberOf R
  2168. * @since v0.16.0
  2169. * @category Function
  2170. * @sig Chain m => ((y -> m z), (x -> m y), ..., (a -> m b)) -> (a -> m z)
  2171. * @param {...Function} ...functions The functions to compose
  2172. * @return {Function}
  2173. * @see R.pipeK
  2174. * @example
  2175. *
  2176. * // get :: String -> Object -> Maybe *
  2177. * const get = R.curry((propName, obj) => Maybe(obj[propName]))
  2178. *
  2179. * // getStateCode :: Maybe String -> Maybe String
  2180. * const getStateCode = R.composeK(
  2181. * R.compose(Maybe.of, R.toUpper),
  2182. * get('state'),
  2183. * get('address'),
  2184. * get('user'),
  2185. * );
  2186. * getStateCode({"user":{"address":{"state":"ny"}}}); //=> Maybe.Just("NY")
  2187. * getStateCode({}); //=> Maybe.Nothing()
  2188. * @symb R.composeK(f, g, h)(a) = R.chain(f, R.chain(g, h(a)))
  2189. */
  2190. function composeK() {
  2191. if (arguments.length === 0) {
  2192. throw new Error('composeK requires at least one argument');
  2193. }
  2194. var init = Array.prototype.slice.call(arguments);
  2195. var last = init.pop();
  2196. return compose(compose.apply(this, map(chain, init)), last);
  2197. }
  2198. function _pipeP(f, g) {
  2199. return function() {
  2200. var ctx = this;
  2201. return f.apply(ctx, arguments).then(function(x) {
  2202. return g.call(ctx, x);
  2203. });
  2204. };
  2205. }
  2206. /**
  2207. * Performs left-to-right composition of one or more Promise-returning
  2208. * functions. The leftmost function may have any arity; the remaining functions
  2209. * must be unary.
  2210. *
  2211. * @func
  2212. * @memberOf R
  2213. * @since v0.10.0
  2214. * @category Function
  2215. * @sig ((a -> Promise b), (b -> Promise c), ..., (y -> Promise z)) -> (a -> Promise z)
  2216. * @param {...Function} functions
  2217. * @return {Function}
  2218. * @see R.composeP
  2219. * @example
  2220. *
  2221. * // followersForUser :: String -> Promise [User]
  2222. * const followersForUser = R.pipeP(db.getUserById, db.getFollowers);
  2223. */
  2224. function pipeP() {
  2225. if (arguments.length === 0) {
  2226. throw new Error('pipeP requires at least one argument');
  2227. }
  2228. return _arity(arguments[0].length,
  2229. reduce(_pipeP, arguments[0], tail(arguments)));
  2230. }
  2231. /**
  2232. * Performs right-to-left composition of one or more Promise-returning
  2233. * functions. The rightmost function may have any arity; the remaining
  2234. * functions must be unary.
  2235. *
  2236. * @func
  2237. * @memberOf R
  2238. * @since v0.10.0
  2239. * @category Function
  2240. * @sig ((y -> Promise z), (x -> Promise y), ..., (a -> Promise b)) -> (a -> Promise z)
  2241. * @param {...Function} functions The functions to compose
  2242. * @return {Function}
  2243. * @see R.pipeP
  2244. * @example
  2245. *
  2246. * const db = {
  2247. * users: {
  2248. * JOE: {
  2249. * name: 'Joe',
  2250. * followers: ['STEVE', 'SUZY']
  2251. * }
  2252. * }
  2253. * }
  2254. *
  2255. * // We'll pretend to do a db lookup which returns a promise
  2256. * const lookupUser = (userId) => Promise.resolve(db.users[userId])
  2257. * const lookupFollowers = (user) => Promise.resolve(user.followers)
  2258. * lookupUser('JOE').then(lookupFollowers)
  2259. *
  2260. * // followersForUser :: String -> Promise [UserId]
  2261. * const followersForUser = R.composeP(lookupFollowers, lookupUser);
  2262. * followersForUser('JOE').then(followers => console.log('Followers:', followers))
  2263. * // Followers: ["STEVE","SUZY"]
  2264. */
  2265. function composeP() {
  2266. if (arguments.length === 0) {
  2267. throw new Error('composeP requires at least one argument');
  2268. }
  2269. return pipeP.apply(this, reverse(arguments));
  2270. }
  2271. /**
  2272. * Returns the nth element of the given list or string. If n is negative the
  2273. * element at index length + n is returned.
  2274. *
  2275. * @func
  2276. * @memberOf R
  2277. * @since v0.1.0
  2278. * @category List
  2279. * @sig Number -> [a] -> a | Undefined
  2280. * @sig Number -> String -> String
  2281. * @param {Number} offset
  2282. * @param {*} list
  2283. * @return {*}
  2284. * @example
  2285. *
  2286. * const list = ['foo', 'bar', 'baz', 'quux'];
  2287. * R.nth(1, list); //=> 'bar'
  2288. * R.nth(-1, list); //=> 'quux'
  2289. * R.nth(-99, list); //=> undefined
  2290. *
  2291. * R.nth(2, 'abc'); //=> 'c'
  2292. * R.nth(3, 'abc'); //=> ''
  2293. * @symb R.nth(-1, [a, b, c]) = c
  2294. * @symb R.nth(0, [a, b, c]) = a
  2295. * @symb R.nth(1, [a, b, c]) = b
  2296. */
  2297. var nth = _curry2(function nth(offset, list) {
  2298. var idx = offset < 0 ? list.length + offset : offset;
  2299. return _isString(list) ? list.charAt(idx) : list[idx];
  2300. });
  2301. /**
  2302. * Returns the first element of the given list or string. In some libraries
  2303. * this function is named `first`.
  2304. *
  2305. * @func
  2306. * @memberOf R
  2307. * @since v0.1.0
  2308. * @category List
  2309. * @sig [a] -> a | Undefined
  2310. * @sig String -> String
  2311. * @param {Array|String} list
  2312. * @return {*}
  2313. * @see R.tail, R.init, R.last
  2314. * @example
  2315. *
  2316. * R.head(['fi', 'fo', 'fum']); //=> 'fi'
  2317. * R.head([]); //=> undefined
  2318. *
  2319. * R.head('abc'); //=> 'a'
  2320. * R.head(''); //=> ''
  2321. */
  2322. var head = nth(0);
  2323. function _identity(x) { return x; }
  2324. /**
  2325. * A function that does nothing but return the parameter supplied to it. Good
  2326. * as a default or placeholder function.
  2327. *
  2328. * @func
  2329. * @memberOf R
  2330. * @since v0.1.0
  2331. * @category Function
  2332. * @sig a -> a
  2333. * @param {*} x The value to return.
  2334. * @return {*} The input value, `x`.
  2335. * @example
  2336. *
  2337. * R.identity(1); //=> 1
  2338. *
  2339. * const obj = {};
  2340. * R.identity(obj) === obj; //=> true
  2341. * @symb R.identity(a) = a
  2342. */
  2343. var identity = _curry1(_identity);
  2344. /**
  2345. * Performs left-to-right function composition using transforming function. The leftmost function may have
  2346. * any arity; the remaining functions must be unary.
  2347. *
  2348. * **Note:** The result of pipeWith is not automatically curried.
  2349. *
  2350. * @func
  2351. * @memberOf R
  2352. * @category Function
  2353. * @sig ((* -> *), [((a, b, ..., n) -> o), (o -> p), ..., (x -> y), (y -> z)]) -> ((a, b, ..., n) -> z)
  2354. * @param {...Function} functions
  2355. * @return {Function}
  2356. * @see R.composeWith, R.pipe
  2357. * @example
  2358. *
  2359. * const pipeWhileNotNil = R.pipeWith((f, res) => R.isNil(res) ? res : f(res));
  2360. * const f = pipeWhileNotNil([Math.pow, R.negate, R.inc])
  2361. *
  2362. * f(3, 4); // -(3^4) + 1
  2363. * @symb R.pipeWith(f)([g, h, i])(...args) = f(i, f(h, f(g, ...args)))
  2364. */
  2365. var pipeWith = _curry2(function pipeWith(xf, list) {
  2366. if (list.length <= 0) {
  2367. return identity;
  2368. }
  2369. const headList = head(list);
  2370. const tailList = tail(list);
  2371. return _arity(headList.length, function() {
  2372. return _reduce(
  2373. function(result, f) {
  2374. return xf.call(this, f, result);
  2375. },
  2376. headList.apply(this, arguments),
  2377. tailList
  2378. );
  2379. });
  2380. });
  2381. /**
  2382. * Performs right-to-left function composition using transforming function. The rightmost function may have
  2383. * any arity; the remaining functions must be unary.
  2384. *
  2385. * **Note:** The result of compose is not automatically curried.
  2386. *
  2387. * @func
  2388. * @memberOf R
  2389. * @category Function
  2390. * @sig ((* -> *), [(y -> z), (x -> y), ..., (o -> p), ((a, b, ..., n) -> o)]) -> ((a, b, ..., n) -> z)
  2391. * @param {...Function} ...functions The functions to compose
  2392. * @return {Function}
  2393. * @see R.compose, R.pipeWith
  2394. * @example
  2395. *
  2396. * const composeWhileNotNil = R.composeWith((f, res) => R.isNil(res) ? res : f(res));
  2397. *
  2398. * composeWhileNotNil([R.inc, R.prop('age')])({age: 1}) //=> 2
  2399. * composeWhileNotNil([R.inc, R.prop('age')])({}) //=> null
  2400. *
  2401. * @symb R.composeWith(f)([g, h, i])(...args) = f(g, f(h, f(i, ...args)))
  2402. */
  2403. var composeWith = _curry2(function composeWith(xf, list) {
  2404. return pipeWith.apply(this, [xf, reverse(list)]);
  2405. });
  2406. function _arrayFromIterator(iter) {
  2407. var list = [];
  2408. var next;
  2409. while (!(next = iter.next()).done) {
  2410. list.push(next.value);
  2411. }
  2412. return list;
  2413. }
  2414. function _includesWith(pred, x, list) {
  2415. var idx = 0;
  2416. var len = list.length;
  2417. while (idx < len) {
  2418. if (pred(x, list[idx])) {
  2419. return true;
  2420. }
  2421. idx += 1;
  2422. }
  2423. return false;
  2424. }
  2425. function _functionName(f) {
  2426. // String(x => x) evaluates to "x => x", so the pattern may not match.
  2427. var match = String(f).match(/^function (\w*)/);
  2428. return match == null ? '' : match[1];
  2429. }
  2430. // Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
  2431. function _objectIs(a, b) {
  2432. // SameValue algorithm
  2433. if (a === b) { // Steps 1-5, 7-10
  2434. // Steps 6.b-6.e: +0 != -0
  2435. return a !== 0 || 1 / a === 1 / b;
  2436. } else {
  2437. // Step 6.a: NaN == NaN
  2438. return a !== a && b !== b;
  2439. }
  2440. }
  2441. var _objectIs$1 = typeof Object.is === 'function' ? Object.is : _objectIs;
  2442. /**
  2443. * private _uniqContentEquals function.
  2444. * That function is checking equality of 2 iterator contents with 2 assumptions
  2445. * - iterators lengths are the same
  2446. * - iterators values are unique
  2447. *
  2448. * false-positive result will be returned for comparision of, e.g.
  2449. * - [1,2,3] and [1,2,3,4]
  2450. * - [1,1,1] and [1,2,3]
  2451. * */
  2452. function _uniqContentEquals(aIterator, bIterator, stackA, stackB) {
  2453. var a = _arrayFromIterator(aIterator);
  2454. var b = _arrayFromIterator(bIterator);
  2455. function eq(_a, _b) {
  2456. return _equals(_a, _b, stackA.slice(), stackB.slice());
  2457. }
  2458. // if *a* array contains any element that is not included in *b*
  2459. return !_includesWith(function(b, aItem) {
  2460. return !_includesWith(eq, aItem, b);
  2461. }, b, a);
  2462. }
  2463. function _equals(a, b, stackA, stackB) {
  2464. if (_objectIs$1(a, b)) {
  2465. return true;
  2466. }
  2467. var typeA = type(a);
  2468. if (typeA !== type(b)) {
  2469. return false;
  2470. }
  2471. if (a == null || b == null) {
  2472. return false;
  2473. }
  2474. if (typeof a['fantasy-land/equals'] === 'function' || typeof b['fantasy-land/equals'] === 'function') {
  2475. return typeof a['fantasy-land/equals'] === 'function' && a['fantasy-land/equals'](b) &&
  2476. typeof b['fantasy-land/equals'] === 'function' && b['fantasy-land/equals'](a);
  2477. }
  2478. if (typeof a.equals === 'function' || typeof b.equals === 'function') {
  2479. return typeof a.equals === 'function' && a.equals(b) &&
  2480. typeof b.equals === 'function' && b.equals(a);
  2481. }
  2482. switch (typeA) {
  2483. case 'Arguments':
  2484. case 'Array':
  2485. case 'Object':
  2486. if (typeof a.constructor === 'function' &&
  2487. _functionName(a.constructor) === 'Promise') {
  2488. return a === b;
  2489. }
  2490. break;
  2491. case 'Boolean':
  2492. case 'Number':
  2493. case 'String':
  2494. if (!(typeof a === typeof b && _objectIs$1(a.valueOf(), b.valueOf()))) {
  2495. return false;
  2496. }
  2497. break;
  2498. case 'Date':
  2499. if (!_objectIs$1(a.valueOf(), b.valueOf())) {
  2500. return false;
  2501. }
  2502. break;
  2503. case 'Error':
  2504. return a.name === b.name && a.message === b.message;
  2505. case 'RegExp':
  2506. if (!(a.source === b.source &&
  2507. a.global === b.global &&
  2508. a.ignoreCase === b.ignoreCase &&
  2509. a.multiline === b.multiline &&
  2510. a.sticky === b.sticky &&
  2511. a.unicode === b.unicode)) {
  2512. return false;
  2513. }
  2514. break;
  2515. }
  2516. var idx = stackA.length - 1;
  2517. while (idx >= 0) {
  2518. if (stackA[idx] === a) {
  2519. return stackB[idx] === b;
  2520. }
  2521. idx -= 1;
  2522. }
  2523. switch (typeA) {
  2524. case 'Map':
  2525. if (a.size !== b.size) {
  2526. return false;
  2527. }
  2528. return _uniqContentEquals(a.entries(), b.entries(), stackA.concat([a]), stackB.concat([b]));
  2529. case 'Set':
  2530. if (a.size !== b.size) {
  2531. return false;
  2532. }
  2533. return _uniqContentEquals(a.values(), b.values(), stackA.concat([a]), stackB.concat([b]));
  2534. case 'Arguments':
  2535. case 'Array':
  2536. case 'Object':
  2537. case 'Boolean':
  2538. case 'Number':
  2539. case 'String':
  2540. case 'Date':
  2541. case 'Error':
  2542. case 'RegExp':
  2543. case 'Int8Array':
  2544. case 'Uint8Array':
  2545. case 'Uint8ClampedArray':
  2546. case 'Int16Array':
  2547. case 'Uint16Array':
  2548. case 'Int32Array':
  2549. case 'Uint32Array':
  2550. case 'Float32Array':
  2551. case 'Float64Array':
  2552. case 'ArrayBuffer':
  2553. break;
  2554. default:
  2555. // Values of other types are only equal if identical.
  2556. return false;
  2557. }
  2558. var keysA = keys(a);
  2559. if (keysA.length !== keys(b).length) {
  2560. return false;
  2561. }
  2562. var extendedStackA = stackA.concat([a]);
  2563. var extendedStackB = stackB.concat([b]);
  2564. idx = keysA.length - 1;
  2565. while (idx >= 0) {
  2566. var key = keysA[idx];
  2567. if (!(_has(key, b) && _equals(b[key], a[key], extendedStackA, extendedStackB))) {
  2568. return false;
  2569. }
  2570. idx -= 1;
  2571. }
  2572. return true;
  2573. }
  2574. /**
  2575. * Returns `true` if its arguments are equivalent, `false` otherwise. Handles
  2576. * cyclical data structures.
  2577. *
  2578. * Dispatches symmetrically to the `equals` methods of both arguments, if
  2579. * present.
  2580. *
  2581. * @func
  2582. * @memberOf R
  2583. * @since v0.15.0
  2584. * @category Relation
  2585. * @sig a -> b -> Boolean
  2586. * @param {*} a
  2587. * @param {*} b
  2588. * @return {Boolean}
  2589. * @example
  2590. *
  2591. * R.equals(1, 1); //=> true
  2592. * R.equals(1, '1'); //=> false
  2593. * R.equals([1, 2, 3], [1, 2, 3]); //=> true
  2594. *
  2595. * const a = {}; a.v = a;
  2596. * const b = {}; b.v = b;
  2597. * R.equals(a, b); //=> true
  2598. */
  2599. var equals = _curry2(function equals(a, b) {
  2600. return _equals(a, b, [], []);
  2601. });
  2602. function _indexOf(list, a, idx) {
  2603. var inf, item;
  2604. // Array.prototype.indexOf doesn't exist below IE9
  2605. if (typeof list.indexOf === 'function') {
  2606. switch (typeof a) {
  2607. case 'number':
  2608. if (a === 0) {
  2609. // manually crawl the list to distinguish between +0 and -0
  2610. inf = 1 / a;
  2611. while (idx < list.length) {
  2612. item = list[idx];
  2613. if (item === 0 && 1 / item === inf) {
  2614. return idx;
  2615. }
  2616. idx += 1;
  2617. }
  2618. return -1;
  2619. } else if (a !== a) {
  2620. // NaN
  2621. while (idx < list.length) {
  2622. item = list[idx];
  2623. if (typeof item === 'number' && item !== item) {
  2624. return idx;
  2625. }
  2626. idx += 1;
  2627. }
  2628. return -1;
  2629. }
  2630. // non-zero numbers can utilise Set
  2631. return list.indexOf(a, idx);
  2632. // all these types can utilise Set
  2633. case 'string':
  2634. case 'boolean':
  2635. case 'function':
  2636. case 'undefined':
  2637. return list.indexOf(a, idx);
  2638. case 'object':
  2639. if (a === null) {
  2640. // null can utilise Set
  2641. return list.indexOf(a, idx);
  2642. }
  2643. }
  2644. }
  2645. // anything else not covered above, defer to R.equals
  2646. while (idx < list.length) {
  2647. if (equals(list[idx], a)) {
  2648. return idx;
  2649. }
  2650. idx += 1;
  2651. }
  2652. return -1;
  2653. }
  2654. function _includes(a, list) {
  2655. return _indexOf(list, a, 0) >= 0;
  2656. }
  2657. function _quote(s) {
  2658. var escaped = s
  2659. .replace(/\\/g, '\\\\')
  2660. .replace(/[\b]/g, '\\b') // \b matches word boundary; [\b] matches backspace
  2661. .replace(/\f/g, '\\f')
  2662. .replace(/\n/g, '\\n')
  2663. .replace(/\r/g, '\\r')
  2664. .replace(/\t/g, '\\t')
  2665. .replace(/\v/g, '\\v')
  2666. .replace(/\0/g, '\\0');
  2667. return '"' + escaped.replace(/"/g, '\\"') + '"';
  2668. }
  2669. /**
  2670. * Polyfill from <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString>.
  2671. */
  2672. var pad = function pad(n) { return (n < 10 ? '0' : '') + n; };
  2673. var _toISOString = typeof Date.prototype.toISOString === 'function' ?
  2674. function _toISOString(d) {
  2675. return d.toISOString();
  2676. } :
  2677. function _toISOString(d) {
  2678. return (
  2679. d.getUTCFullYear() + '-' +
  2680. pad(d.getUTCMonth() + 1) + '-' +
  2681. pad(d.getUTCDate()) + 'T' +
  2682. pad(d.getUTCHours()) + ':' +
  2683. pad(d.getUTCMinutes()) + ':' +
  2684. pad(d.getUTCSeconds()) + '.' +
  2685. (d.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) + 'Z'
  2686. );
  2687. };
  2688. function _complement(f) {
  2689. return function() {
  2690. return !f.apply(this, arguments);
  2691. };
  2692. }
  2693. function _filter(fn, list) {
  2694. var idx = 0;
  2695. var len = list.length;
  2696. var result = [];
  2697. while (idx < len) {
  2698. if (fn(list[idx])) {
  2699. result[result.length] = list[idx];
  2700. }
  2701. idx += 1;
  2702. }
  2703. return result;
  2704. }
  2705. function _isObject(x) {
  2706. return Object.prototype.toString.call(x) === '[object Object]';
  2707. }
  2708. function XFilter(f, xf) {
  2709. this.xf = xf;
  2710. this.f = f;
  2711. }
  2712. XFilter.prototype['@@transducer/init'] = _xfBase.init;
  2713. XFilter.prototype['@@transducer/result'] = _xfBase.result;
  2714. XFilter.prototype['@@transducer/step'] = function(result, input) {
  2715. return this.f(input) ? this.xf['@@transducer/step'](result, input) : result;
  2716. };
  2717. var _xfilter = _curry2(function _xfilter(f, xf) { return new XFilter(f, xf); });
  2718. /**
  2719. * Takes a predicate and a `Filterable`, and returns a new filterable of the
  2720. * same type containing the members of the given filterable which satisfy the
  2721. * given predicate. Filterable objects include plain objects or any object
  2722. * that has a filter method such as `Array`.
  2723. *
  2724. * Dispatches to the `filter` method of the second argument, if present.
  2725. *
  2726. * Acts as a transducer if a transformer is given in list position.
  2727. *
  2728. * @func
  2729. * @memberOf R
  2730. * @since v0.1.0
  2731. * @category List
  2732. * @sig Filterable f => (a -> Boolean) -> f a -> f a
  2733. * @param {Function} pred
  2734. * @param {Array} filterable
  2735. * @return {Array} Filterable
  2736. * @see R.reject, R.transduce, R.addIndex
  2737. * @example
  2738. *
  2739. * const isEven = n => n % 2 === 0;
  2740. *
  2741. * R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
  2742. *
  2743. * R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
  2744. */
  2745. var filter = _curry2(_dispatchable(['filter'], _xfilter, function(pred, filterable) {
  2746. return (
  2747. _isObject(filterable) ?
  2748. _reduce(function(acc, key) {
  2749. if (pred(filterable[key])) {
  2750. acc[key] = filterable[key];
  2751. }
  2752. return acc;
  2753. }, {}, keys(filterable)) :
  2754. // else
  2755. _filter(pred, filterable)
  2756. );
  2757. }));
  2758. /**
  2759. * The complement of [`filter`](#filter).
  2760. *
  2761. * Acts as a transducer if a transformer is given in list position. Filterable
  2762. * objects include plain objects or any object that has a filter method such
  2763. * as `Array`.
  2764. *
  2765. * @func
  2766. * @memberOf R
  2767. * @since v0.1.0
  2768. * @category List
  2769. * @sig Filterable f => (a -> Boolean) -> f a -> f a
  2770. * @param {Function} pred
  2771. * @param {Array} filterable
  2772. * @return {Array}
  2773. * @see R.filter, R.transduce, R.addIndex
  2774. * @example
  2775. *
  2776. * const isOdd = (n) => n % 2 === 1;
  2777. *
  2778. * R.reject(isOdd, [1, 2, 3, 4]); //=> [2, 4]
  2779. *
  2780. * R.reject(isOdd, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
  2781. */
  2782. var reject = _curry2(function reject(pred, filterable) {
  2783. return filter(_complement(pred), filterable);
  2784. });
  2785. function _toString(x, seen) {
  2786. var recur = function recur(y) {
  2787. var xs = seen.concat([x]);
  2788. return _includes(y, xs) ? '<Circular>' : _toString(y, xs);
  2789. };
  2790. // mapPairs :: (Object, [String]) -> [String]
  2791. var mapPairs = function(obj, keys$$1) {
  2792. return _map(function(k) { return _quote(k) + ': ' + recur(obj[k]); }, keys$$1.slice().sort());
  2793. };
  2794. switch (Object.prototype.toString.call(x)) {
  2795. case '[object Arguments]':
  2796. return '(function() { return arguments; }(' + _map(recur, x).join(', ') + '))';
  2797. case '[object Array]':
  2798. return '[' + _map(recur, x).concat(mapPairs(x, reject(function(k) { return /^\d+$/.test(k); }, keys(x)))).join(', ') + ']';
  2799. case '[object Boolean]':
  2800. return typeof x === 'object' ? 'new Boolean(' + recur(x.valueOf()) + ')' : x.toString();
  2801. case '[object Date]':
  2802. return 'new Date(' + (isNaN(x.valueOf()) ? recur(NaN) : _quote(_toISOString(x))) + ')';
  2803. case '[object Null]':
  2804. return 'null';
  2805. case '[object Number]':
  2806. return typeof x === 'object' ? 'new Number(' + recur(x.valueOf()) + ')' : 1 / x === -Infinity ? '-0' : x.toString(10);
  2807. case '[object String]':
  2808. return typeof x === 'object' ? 'new String(' + recur(x.valueOf()) + ')' : _quote(x);
  2809. case '[object Undefined]':
  2810. return 'undefined';
  2811. default:
  2812. if (typeof x.toString === 'function') {
  2813. var repr = x.toString();
  2814. if (repr !== '[object Object]') {
  2815. return repr;
  2816. }
  2817. }
  2818. return '{' + mapPairs(x, keys(x)).join(', ') + '}';
  2819. }
  2820. }
  2821. /**
  2822. * Returns the string representation of the given value. `eval`'ing the output
  2823. * should result in a value equivalent to the input value. Many of the built-in
  2824. * `toString` methods do not satisfy this requirement.
  2825. *
  2826. * If the given value is an `[object Object]` with a `toString` method other
  2827. * than `Object.prototype.toString`, this method is invoked with no arguments
  2828. * to produce the return value. This means user-defined constructor functions
  2829. * can provide a suitable `toString` method. For example:
  2830. *
  2831. * function Point(x, y) {
  2832. * this.x = x;
  2833. * this.y = y;
  2834. * }
  2835. *
  2836. * Point.prototype.toString = function() {
  2837. * return 'new Point(' + this.x + ', ' + this.y + ')';
  2838. * };
  2839. *
  2840. * R.toString(new Point(1, 2)); //=> 'new Point(1, 2)'
  2841. *
  2842. * @func
  2843. * @memberOf R
  2844. * @since v0.14.0
  2845. * @category String
  2846. * @sig * -> String
  2847. * @param {*} val
  2848. * @return {String}
  2849. * @example
  2850. *
  2851. * R.toString(42); //=> '42'
  2852. * R.toString('abc'); //=> '"abc"'
  2853. * R.toString([1, 2, 3]); //=> '[1, 2, 3]'
  2854. * R.toString({foo: 1, bar: 2, baz: 3}); //=> '{"bar": 2, "baz": 3, "foo": 1}'
  2855. * R.toString(new Date('2001-02-03T04:05:06Z')); //=> 'new Date("2001-02-03T04:05:06.000Z")'
  2856. */
  2857. var toString$1 = _curry1(function toString(val) { return _toString(val, []); });
  2858. /**
  2859. * Returns the result of concatenating the given lists or strings.
  2860. *
  2861. * Note: `R.concat` expects both arguments to be of the same type,
  2862. * unlike the native `Array.prototype.concat` method. It will throw
  2863. * an error if you `concat` an Array with a non-Array value.
  2864. *
  2865. * Dispatches to the `concat` method of the first argument, if present.
  2866. * Can also concatenate two members of a [fantasy-land
  2867. * compatible semigroup](https://github.com/fantasyland/fantasy-land#semigroup).
  2868. *
  2869. * @func
  2870. * @memberOf R
  2871. * @since v0.1.0
  2872. * @category List
  2873. * @sig [a] -> [a] -> [a]
  2874. * @sig String -> String -> String
  2875. * @param {Array|String} firstList The first list
  2876. * @param {Array|String} secondList The second list
  2877. * @return {Array|String} A list consisting of the elements of `firstList` followed by the elements of
  2878. * `secondList`.
  2879. *
  2880. * @example
  2881. *
  2882. * R.concat('ABC', 'DEF'); // 'ABCDEF'
  2883. * R.concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
  2884. * R.concat([], []); //=> []
  2885. */
  2886. var concat = _curry2(function concat(a, b) {
  2887. if (_isArray(a)) {
  2888. if (_isArray(b)) {
  2889. return a.concat(b);
  2890. }
  2891. throw new TypeError(toString$1(b) + ' is not an array');
  2892. }
  2893. if (_isString(a)) {
  2894. if (_isString(b)) {
  2895. return a + b;
  2896. }
  2897. throw new TypeError(toString$1(b) + ' is not a string');
  2898. }
  2899. if (a != null && _isFunction(a['fantasy-land/concat'])) {
  2900. return a['fantasy-land/concat'](b);
  2901. }
  2902. if (a != null && _isFunction(a.concat)) {
  2903. return a.concat(b);
  2904. }
  2905. throw new TypeError(toString$1(a) + ' does not have a method named "concat" or "fantasy-land/concat"');
  2906. });
  2907. /**
  2908. * Returns a function, `fn`, which encapsulates `if/else, if/else, ...` logic.
  2909. * `R.cond` takes a list of [predicate, transformer] pairs. All of the arguments
  2910. * to `fn` are applied to each of the predicates in turn until one returns a
  2911. * "truthy" value, at which point `fn` returns the result of applying its
  2912. * arguments to the corresponding transformer. If none of the predicates
  2913. * matches, `fn` returns undefined.
  2914. *
  2915. * @func
  2916. * @memberOf R
  2917. * @since v0.6.0
  2918. * @category Logic
  2919. * @sig [[(*... -> Boolean),(*... -> *)]] -> (*... -> *)
  2920. * @param {Array} pairs A list of [predicate, transformer]
  2921. * @return {Function}
  2922. * @see R.ifElse, R.unless, R.when
  2923. * @example
  2924. *
  2925. * const fn = R.cond([
  2926. * [R.equals(0), R.always('water freezes at 0°C')],
  2927. * [R.equals(100), R.always('water boils at 100°C')],
  2928. * [R.T, temp => 'nothing special happens at ' + temp + '°C']
  2929. * ]);
  2930. * fn(0); //=> 'water freezes at 0°C'
  2931. * fn(50); //=> 'nothing special happens at 50°C'
  2932. * fn(100); //=> 'water boils at 100°C'
  2933. */
  2934. var cond = _curry1(function cond(pairs) {
  2935. var arity = reduce(max,
  2936. 0,
  2937. map(function(pair) { return pair[0].length; }, pairs));
  2938. return _arity(arity, function() {
  2939. var idx = 0;
  2940. while (idx < pairs.length) {
  2941. if (pairs[idx][0].apply(this, arguments)) {
  2942. return pairs[idx][1].apply(this, arguments);
  2943. }
  2944. idx += 1;
  2945. }
  2946. });
  2947. });
  2948. /**
  2949. * Wraps a constructor function inside a curried function that can be called
  2950. * with the same arguments and returns the same type. The arity of the function
  2951. * returned is specified to allow using variadic constructor functions.
  2952. *
  2953. * @func
  2954. * @memberOf R
  2955. * @since v0.4.0
  2956. * @category Function
  2957. * @sig Number -> (* -> {*}) -> (* -> {*})
  2958. * @param {Number} n The arity of the constructor function.
  2959. * @param {Function} Fn The constructor function to wrap.
  2960. * @return {Function} A wrapped, curried constructor function.
  2961. * @example
  2962. *
  2963. * // Variadic Constructor function
  2964. * function Salad() {
  2965. * this.ingredients = arguments;
  2966. * }
  2967. *
  2968. * Salad.prototype.recipe = function() {
  2969. * const instructions = R.map(ingredient => 'Add a dollop of ' + ingredient, this.ingredients);
  2970. * return R.join('\n', instructions);
  2971. * };
  2972. *
  2973. * const ThreeLayerSalad = R.constructN(3, Salad);
  2974. *
  2975. * // Notice we no longer need the 'new' keyword, and the constructor is curried for 3 arguments.
  2976. * const salad = ThreeLayerSalad('Mayonnaise')('Potato Chips')('Ketchup');
  2977. *
  2978. * console.log(salad.recipe());
  2979. * // Add a dollop of Mayonnaise
  2980. * // Add a dollop of Potato Chips
  2981. * // Add a dollop of Ketchup
  2982. */
  2983. var constructN = _curry2(function constructN(n, Fn) {
  2984. if (n > 10) {
  2985. throw new Error('Constructor with greater than ten arguments');
  2986. }
  2987. if (n === 0) {
  2988. return function() { return new Fn(); };
  2989. }
  2990. return curry(nAry(n, function($0, $1, $2, $3, $4, $5, $6, $7, $8, $9) {
  2991. switch (arguments.length) {
  2992. case 1: return new Fn($0);
  2993. case 2: return new Fn($0, $1);
  2994. case 3: return new Fn($0, $1, $2);
  2995. case 4: return new Fn($0, $1, $2, $3);
  2996. case 5: return new Fn($0, $1, $2, $3, $4);
  2997. case 6: return new Fn($0, $1, $2, $3, $4, $5);
  2998. case 7: return new Fn($0, $1, $2, $3, $4, $5, $6);
  2999. case 8: return new Fn($0, $1, $2, $3, $4, $5, $6, $7);
  3000. case 9: return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8);
  3001. case 10: return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8, $9);
  3002. }
  3003. }));
  3004. });
  3005. /**
  3006. * Wraps a constructor function inside a curried function that can be called
  3007. * with the same arguments and returns the same type.
  3008. *
  3009. * @func
  3010. * @memberOf R
  3011. * @since v0.1.0
  3012. * @category Function
  3013. * @sig (* -> {*}) -> (* -> {*})
  3014. * @param {Function} fn The constructor function to wrap.
  3015. * @return {Function} A wrapped, curried constructor function.
  3016. * @see R.invoker
  3017. * @example
  3018. *
  3019. * // Constructor function
  3020. * function Animal(kind) {
  3021. * this.kind = kind;
  3022. * };
  3023. * Animal.prototype.sighting = function() {
  3024. * return "It's a " + this.kind + "!";
  3025. * }
  3026. *
  3027. * const AnimalConstructor = R.construct(Animal)
  3028. *
  3029. * // Notice we no longer need the 'new' keyword:
  3030. * AnimalConstructor('Pig'); //=> {"kind": "Pig", "sighting": function (){...}};
  3031. *
  3032. * const animalTypes = ["Lion", "Tiger", "Bear"];
  3033. * const animalSighting = R.invoker(0, 'sighting');
  3034. * const sightNewAnimal = R.compose(animalSighting, AnimalConstructor);
  3035. * R.map(sightNewAnimal, animalTypes); //=> ["It's a Lion!", "It's a Tiger!", "It's a Bear!"]
  3036. */
  3037. var construct = _curry1(function construct(Fn) {
  3038. return constructN(Fn.length, Fn);
  3039. });
  3040. /**
  3041. * Returns `true` if the specified value is equal, in [`R.equals`](#equals)
  3042. * terms, to at least one element of the given list; `false` otherwise.
  3043. * Works also with strings.
  3044. *
  3045. * @func
  3046. * @memberOf R
  3047. * @since v0.1.0
  3048. * @category List
  3049. * @sig a -> [a] -> Boolean
  3050. * @param {Object} a The item to compare against.
  3051. * @param {Array} list The array to consider.
  3052. * @return {Boolean} `true` if an equivalent item is in the list, `false` otherwise.
  3053. * @see R.includes
  3054. * @deprecated since v0.26.0
  3055. * @example
  3056. *
  3057. * R.contains(3, [1, 2, 3]); //=> true
  3058. * R.contains(4, [1, 2, 3]); //=> false
  3059. * R.contains({ name: 'Fred' }, [{ name: 'Fred' }]); //=> true
  3060. * R.contains([42], [[42]]); //=> true
  3061. * R.contains('ba', 'banana'); //=>true
  3062. */
  3063. var contains$1 = _curry2(_includes);
  3064. /**
  3065. * Accepts a converging function and a list of branching functions and returns
  3066. * a new function. The arity of the new function is the same as the arity of
  3067. * the longest branching function. When invoked, this new function is applied
  3068. * to some arguments, and each branching function is applied to those same
  3069. * arguments. The results of each branching function are passed as arguments
  3070. * to the converging function to produce the return value.
  3071. *
  3072. * @func
  3073. * @memberOf R
  3074. * @since v0.4.2
  3075. * @category Function
  3076. * @sig ((x1, x2, ...) -> z) -> [((a, b, ...) -> x1), ((a, b, ...) -> x2), ...] -> (a -> b -> ... -> z)
  3077. * @param {Function} after A function. `after` will be invoked with the return values of
  3078. * `fn1` and `fn2` as its arguments.
  3079. * @param {Array} functions A list of functions.
  3080. * @return {Function} A new function.
  3081. * @see R.useWith
  3082. * @example
  3083. *
  3084. * const average = R.converge(R.divide, [R.sum, R.length])
  3085. * average([1, 2, 3, 4, 5, 6, 7]) //=> 4
  3086. *
  3087. * const strangeConcat = R.converge(R.concat, [R.toUpper, R.toLower])
  3088. * strangeConcat("Yodel") //=> "YODELyodel"
  3089. *
  3090. * @symb R.converge(f, [g, h])(a, b) = f(g(a, b), h(a, b))
  3091. */
  3092. var converge = _curry2(function converge(after, fns) {
  3093. return curryN(reduce(max, 0, pluck('length', fns)), function() {
  3094. var args = arguments;
  3095. var context = this;
  3096. return after.apply(context, _map(function(fn) {
  3097. return fn.apply(context, args);
  3098. }, fns));
  3099. });
  3100. });
  3101. function XReduceBy(valueFn, valueAcc, keyFn, xf) {
  3102. this.valueFn = valueFn;
  3103. this.valueAcc = valueAcc;
  3104. this.keyFn = keyFn;
  3105. this.xf = xf;
  3106. this.inputs = {};
  3107. }
  3108. XReduceBy.prototype['@@transducer/init'] = _xfBase.init;
  3109. XReduceBy.prototype['@@transducer/result'] = function(result) {
  3110. var key;
  3111. for (key in this.inputs) {
  3112. if (_has(key, this.inputs)) {
  3113. result = this.xf['@@transducer/step'](result, this.inputs[key]);
  3114. if (result['@@transducer/reduced']) {
  3115. result = result['@@transducer/value'];
  3116. break;
  3117. }
  3118. }
  3119. }
  3120. this.inputs = null;
  3121. return this.xf['@@transducer/result'](result);
  3122. };
  3123. XReduceBy.prototype['@@transducer/step'] = function(result, input) {
  3124. var key = this.keyFn(input);
  3125. this.inputs[key] = this.inputs[key] || [key, this.valueAcc];
  3126. this.inputs[key][1] = this.valueFn(this.inputs[key][1], input);
  3127. return result;
  3128. };
  3129. var _xreduceBy = _curryN(4, [],
  3130. function _xreduceBy(valueFn, valueAcc, keyFn, xf) {
  3131. return new XReduceBy(valueFn, valueAcc, keyFn, xf);
  3132. });
  3133. /**
  3134. * Groups the elements of the list according to the result of calling
  3135. * the String-returning function `keyFn` on each element and reduces the elements
  3136. * of each group to a single value via the reducer function `valueFn`.
  3137. *
  3138. * This function is basically a more general [`groupBy`](#groupBy) function.
  3139. *
  3140. * Acts as a transducer if a transformer is given in list position.
  3141. *
  3142. * @func
  3143. * @memberOf R
  3144. * @since v0.20.0
  3145. * @category List
  3146. * @sig ((a, b) -> a) -> a -> (b -> String) -> [b] -> {String: a}
  3147. * @param {Function} valueFn The function that reduces the elements of each group to a single
  3148. * value. Receives two values, accumulator for a particular group and the current element.
  3149. * @param {*} acc The (initial) accumulator value for each group.
  3150. * @param {Function} keyFn The function that maps the list's element into a key.
  3151. * @param {Array} list The array to group.
  3152. * @return {Object} An object with the output of `keyFn` for keys, mapped to the output of
  3153. * `valueFn` for elements which produced that key when passed to `keyFn`.
  3154. * @see R.groupBy, R.reduce
  3155. * @example
  3156. *
  3157. * const groupNames = (acc, {name}) => acc.concat(name)
  3158. * const toGrade = ({score}) =>
  3159. * score < 65 ? 'F' :
  3160. * score < 70 ? 'D' :
  3161. * score < 80 ? 'C' :
  3162. * score < 90 ? 'B' : 'A'
  3163. *
  3164. * var students = [
  3165. * {name: 'Abby', score: 83},
  3166. * {name: 'Bart', score: 62},
  3167. * {name: 'Curt', score: 88},
  3168. * {name: 'Dora', score: 92},
  3169. * ]
  3170. *
  3171. * reduceBy(groupNames, [], toGrade, students)
  3172. * //=> {"A": ["Dora"], "B": ["Abby", "Curt"], "F": ["Bart"]}
  3173. */
  3174. var reduceBy = _curryN(4, [], _dispatchable([], _xreduceBy,
  3175. function reduceBy(valueFn, valueAcc, keyFn, list) {
  3176. return _reduce(function(acc, elt) {
  3177. var key = keyFn(elt);
  3178. acc[key] = valueFn(_has(key, acc) ? acc[key] : valueAcc, elt);
  3179. return acc;
  3180. }, {}, list);
  3181. }));
  3182. /**
  3183. * Counts the elements of a list according to how many match each value of a
  3184. * key generated by the supplied function. Returns an object mapping the keys
  3185. * produced by `fn` to the number of occurrences in the list. Note that all
  3186. * keys are coerced to strings because of how JavaScript objects work.
  3187. *
  3188. * Acts as a transducer if a transformer is given in list position.
  3189. *
  3190. * @func
  3191. * @memberOf R
  3192. * @since v0.1.0
  3193. * @category Relation
  3194. * @sig (a -> String) -> [a] -> {*}
  3195. * @param {Function} fn The function used to map values to keys.
  3196. * @param {Array} list The list to count elements from.
  3197. * @return {Object} An object mapping keys to number of occurrences in the list.
  3198. * @example
  3199. *
  3200. * const numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
  3201. * R.countBy(Math.floor)(numbers); //=> {'1': 3, '2': 2, '3': 1}
  3202. *
  3203. * const letters = ['a', 'b', 'A', 'a', 'B', 'c'];
  3204. * R.countBy(R.toLower)(letters); //=> {'a': 3, 'b': 2, 'c': 1}
  3205. */
  3206. var countBy = reduceBy(function(acc, elem) { return acc + 1; }, 0);
  3207. /**
  3208. * Decrements its argument.
  3209. *
  3210. * @func
  3211. * @memberOf R
  3212. * @since v0.9.0
  3213. * @category Math
  3214. * @sig Number -> Number
  3215. * @param {Number} n
  3216. * @return {Number} n - 1
  3217. * @see R.inc
  3218. * @example
  3219. *
  3220. * R.dec(42); //=> 41
  3221. */
  3222. var dec = add(-1);
  3223. /**
  3224. * Returns the second argument if it is not `null`, `undefined` or `NaN`;
  3225. * otherwise the first argument is returned.
  3226. *
  3227. * @func
  3228. * @memberOf R
  3229. * @since v0.10.0
  3230. * @category Logic
  3231. * @sig a -> b -> a | b
  3232. * @param {a} default The default value.
  3233. * @param {b} val `val` will be returned instead of `default` unless `val` is `null`, `undefined` or `NaN`.
  3234. * @return {*} The second value if it is not `null`, `undefined` or `NaN`, otherwise the default value
  3235. * @example
  3236. *
  3237. * const defaultTo42 = R.defaultTo(42);
  3238. *
  3239. * defaultTo42(null); //=> 42
  3240. * defaultTo42(undefined); //=> 42
  3241. * defaultTo42(false); //=> false
  3242. * defaultTo42('Ramda'); //=> 'Ramda'
  3243. * // parseInt('string') results in NaN
  3244. * defaultTo42(parseInt('string')); //=> 42
  3245. */
  3246. var defaultTo = _curry2(function defaultTo(d, v) {
  3247. return v == null || v !== v ? d : v;
  3248. });
  3249. /**
  3250. * Makes a descending comparator function out of a function that returns a value
  3251. * that can be compared with `<` and `>`.
  3252. *
  3253. * @func
  3254. * @memberOf R
  3255. * @since v0.23.0
  3256. * @category Function
  3257. * @sig Ord b => (a -> b) -> a -> a -> Number
  3258. * @param {Function} fn A function of arity one that returns a value that can be compared
  3259. * @param {*} a The first item to be compared.
  3260. * @param {*} b The second item to be compared.
  3261. * @return {Number} `-1` if fn(a) > fn(b), `1` if fn(b) > fn(a), otherwise `0`
  3262. * @see R.ascend
  3263. * @example
  3264. *
  3265. * const byAge = R.descend(R.prop('age'));
  3266. * const people = [
  3267. * { name: 'Emma', age: 70 },
  3268. * { name: 'Peter', age: 78 },
  3269. * { name: 'Mikhail', age: 62 },
  3270. * ];
  3271. * const peopleByOldestFirst = R.sort(byAge, people);
  3272. * //=> [{ name: 'Peter', age: 78 }, { name: 'Emma', age: 70 }, { name: 'Mikhail', age: 62 }]
  3273. */
  3274. var descend = _curry3(function descend(fn, a, b) {
  3275. var aa = fn(a);
  3276. var bb = fn(b);
  3277. return aa > bb ? -1 : aa < bb ? 1 : 0;
  3278. });
  3279. function _Set() {
  3280. /* globals Set */
  3281. this._nativeSet = typeof Set === 'function' ? new Set() : null;
  3282. this._items = {};
  3283. }
  3284. // until we figure out why jsdoc chokes on this
  3285. // @param item The item to add to the Set
  3286. // @returns {boolean} true if the item did not exist prior, otherwise false
  3287. //
  3288. _Set.prototype.add = function(item) {
  3289. return !hasOrAdd(item, true, this);
  3290. };
  3291. //
  3292. // @param item The item to check for existence in the Set
  3293. // @returns {boolean} true if the item exists in the Set, otherwise false
  3294. //
  3295. _Set.prototype.has = function(item) {
  3296. return hasOrAdd(item, false, this);
  3297. };
  3298. //
  3299. // Combines the logic for checking whether an item is a member of the set and
  3300. // for adding a new item to the set.
  3301. //
  3302. // @param item The item to check or add to the Set instance.
  3303. // @param shouldAdd If true, the item will be added to the set if it doesn't
  3304. // already exist.
  3305. // @param set The set instance to check or add to.
  3306. // @return {boolean} true if the item already existed, otherwise false.
  3307. //
  3308. function hasOrAdd(item, shouldAdd, set) {
  3309. var type = typeof item;
  3310. var prevSize, newSize;
  3311. switch (type) {
  3312. case 'string':
  3313. case 'number':
  3314. // distinguish between +0 and -0
  3315. if (item === 0 && 1 / item === -Infinity) {
  3316. if (set._items['-0']) {
  3317. return true;
  3318. } else {
  3319. if (shouldAdd) {
  3320. set._items['-0'] = true;
  3321. }
  3322. return false;
  3323. }
  3324. }
  3325. // these types can all utilise the native Set
  3326. if (set._nativeSet !== null) {
  3327. if (shouldAdd) {
  3328. prevSize = set._nativeSet.size;
  3329. set._nativeSet.add(item);
  3330. newSize = set._nativeSet.size;
  3331. return newSize === prevSize;
  3332. } else {
  3333. return set._nativeSet.has(item);
  3334. }
  3335. } else {
  3336. if (!(type in set._items)) {
  3337. if (shouldAdd) {
  3338. set._items[type] = {};
  3339. set._items[type][item] = true;
  3340. }
  3341. return false;
  3342. } else if (item in set._items[type]) {
  3343. return true;
  3344. } else {
  3345. if (shouldAdd) {
  3346. set._items[type][item] = true;
  3347. }
  3348. return false;
  3349. }
  3350. }
  3351. case 'boolean':
  3352. // set._items['boolean'] holds a two element array
  3353. // representing [ falseExists, trueExists ]
  3354. if (type in set._items) {
  3355. var bIdx = item ? 1 : 0;
  3356. if (set._items[type][bIdx]) {
  3357. return true;
  3358. } else {
  3359. if (shouldAdd) {
  3360. set._items[type][bIdx] = true;
  3361. }
  3362. return false;
  3363. }
  3364. } else {
  3365. if (shouldAdd) {
  3366. set._items[type] = item ? [false, true] : [true, false];
  3367. }
  3368. return false;
  3369. }
  3370. case 'function':
  3371. // compare functions for reference equality
  3372. if (set._nativeSet !== null) {
  3373. if (shouldAdd) {
  3374. prevSize = set._nativeSet.size;
  3375. set._nativeSet.add(item);
  3376. newSize = set._nativeSet.size;
  3377. return newSize === prevSize;
  3378. } else {
  3379. return set._nativeSet.has(item);
  3380. }
  3381. } else {
  3382. if (!(type in set._items)) {
  3383. if (shouldAdd) {
  3384. set._items[type] = [item];
  3385. }
  3386. return false;
  3387. }
  3388. if (!_includes(item, set._items[type])) {
  3389. if (shouldAdd) {
  3390. set._items[type].push(item);
  3391. }
  3392. return false;
  3393. }
  3394. return true;
  3395. }
  3396. case 'undefined':
  3397. if (set._items[type]) {
  3398. return true;
  3399. } else {
  3400. if (shouldAdd) {
  3401. set._items[type] = true;
  3402. }
  3403. return false;
  3404. }
  3405. case 'object':
  3406. if (item === null) {
  3407. if (!set._items['null']) {
  3408. if (shouldAdd) {
  3409. set._items['null'] = true;
  3410. }
  3411. return false;
  3412. }
  3413. return true;
  3414. }
  3415. /* falls through */
  3416. default:
  3417. // reduce the search size of heterogeneous sets by creating buckets
  3418. // for each type.
  3419. type = Object.prototype.toString.call(item);
  3420. if (!(type in set._items)) {
  3421. if (shouldAdd) {
  3422. set._items[type] = [item];
  3423. }
  3424. return false;
  3425. }
  3426. // scan through all previously applied items
  3427. if (!_includes(item, set._items[type])) {
  3428. if (shouldAdd) {
  3429. set._items[type].push(item);
  3430. }
  3431. return false;
  3432. }
  3433. return true;
  3434. }
  3435. }
  3436. /**
  3437. * Finds the set (i.e. no duplicates) of all elements in the first list not
  3438. * contained in the second list. Objects and Arrays are compared in terms of
  3439. * value equality, not reference equality.
  3440. *
  3441. * @func
  3442. * @memberOf R
  3443. * @since v0.1.0
  3444. * @category Relation
  3445. * @sig [*] -> [*] -> [*]
  3446. * @param {Array} list1 The first list.
  3447. * @param {Array} list2 The second list.
  3448. * @return {Array} The elements in `list1` that are not in `list2`.
  3449. * @see R.differenceWith, R.symmetricDifference, R.symmetricDifferenceWith, R.without
  3450. * @example
  3451. *
  3452. * R.difference([1,2,3,4], [7,6,5,4,3]); //=> [1,2]
  3453. * R.difference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5]
  3454. * R.difference([{a: 1}, {b: 2}], [{a: 1}, {c: 3}]) //=> [{b: 2}]
  3455. */
  3456. var difference = _curry2(function difference(first, second) {
  3457. var out = [];
  3458. var idx = 0;
  3459. var firstLen = first.length;
  3460. var secondLen = second.length;
  3461. var toFilterOut = new _Set();
  3462. for (var i = 0; i < secondLen; i += 1) {
  3463. toFilterOut.add(second[i]);
  3464. }
  3465. while (idx < firstLen) {
  3466. if (toFilterOut.add(first[idx])) {
  3467. out[out.length] = first[idx];
  3468. }
  3469. idx += 1;
  3470. }
  3471. return out;
  3472. });
  3473. /**
  3474. * Finds the set (i.e. no duplicates) of all elements in the first list not
  3475. * contained in the second list. Duplication is determined according to the
  3476. * value returned by applying the supplied predicate to two list elements.
  3477. *
  3478. * @func
  3479. * @memberOf R
  3480. * @since v0.1.0
  3481. * @category Relation
  3482. * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
  3483. * @param {Function} pred A predicate used to test whether two items are equal.
  3484. * @param {Array} list1 The first list.
  3485. * @param {Array} list2 The second list.
  3486. * @return {Array} The elements in `list1` that are not in `list2`.
  3487. * @see R.difference, R.symmetricDifference, R.symmetricDifferenceWith
  3488. * @example
  3489. *
  3490. * const cmp = (x, y) => x.a === y.a;
  3491. * const l1 = [{a: 1}, {a: 2}, {a: 3}];
  3492. * const l2 = [{a: 3}, {a: 4}];
  3493. * R.differenceWith(cmp, l1, l2); //=> [{a: 1}, {a: 2}]
  3494. */
  3495. var differenceWith = _curry3(function differenceWith(pred, first, second) {
  3496. var out = [];
  3497. var idx = 0;
  3498. var firstLen = first.length;
  3499. while (idx < firstLen) {
  3500. if (!_includesWith(pred, first[idx], second) &&
  3501. !_includesWith(pred, first[idx], out)) {
  3502. out.push(first[idx]);
  3503. }
  3504. idx += 1;
  3505. }
  3506. return out;
  3507. });
  3508. /**
  3509. * Returns a new object that does not contain a `prop` property.
  3510. *
  3511. * @func
  3512. * @memberOf R
  3513. * @since v0.10.0
  3514. * @category Object
  3515. * @sig String -> {k: v} -> {k: v}
  3516. * @param {String} prop The name of the property to dissociate
  3517. * @param {Object} obj The object to clone
  3518. * @return {Object} A new object equivalent to the original but without the specified property
  3519. * @see R.assoc, R.omit
  3520. * @example
  3521. *
  3522. * R.dissoc('b', {a: 1, b: 2, c: 3}); //=> {a: 1, c: 3}
  3523. */
  3524. var dissoc = _curry2(function dissoc(prop, obj) {
  3525. var result = {};
  3526. for (var p in obj) {
  3527. result[p] = obj[p];
  3528. }
  3529. delete result[prop];
  3530. return result;
  3531. });
  3532. /**
  3533. * Removes the sub-list of `list` starting at index `start` and containing
  3534. * `count` elements. _Note that this is not destructive_: it returns a copy of
  3535. * the list with the changes.
  3536. * <small>No lists have been harmed in the application of this function.</small>
  3537. *
  3538. * @func
  3539. * @memberOf R
  3540. * @since v0.2.2
  3541. * @category List
  3542. * @sig Number -> Number -> [a] -> [a]
  3543. * @param {Number} start The position to start removing elements
  3544. * @param {Number} count The number of elements to remove
  3545. * @param {Array} list The list to remove from
  3546. * @return {Array} A new Array with `count` elements from `start` removed.
  3547. * @see R.without
  3548. * @example
  3549. *
  3550. * R.remove(2, 3, [1,2,3,4,5,6,7,8]); //=> [1,2,6,7,8]
  3551. */
  3552. var remove = _curry3(function remove(start, count, list) {
  3553. var result = Array.prototype.slice.call(list, 0);
  3554. result.splice(start, count);
  3555. return result;
  3556. });
  3557. /**
  3558. * Returns a new copy of the array with the element at the provided index
  3559. * replaced with the given value.
  3560. *
  3561. * @func
  3562. * @memberOf R
  3563. * @since v0.14.0
  3564. * @category List
  3565. * @sig Number -> a -> [a] -> [a]
  3566. * @param {Number} idx The index to update.
  3567. * @param {*} x The value to exist at the given index of the returned array.
  3568. * @param {Array|Arguments} list The source array-like object to be updated.
  3569. * @return {Array} A copy of `list` with the value at index `idx` replaced with `x`.
  3570. * @see R.adjust
  3571. * @example
  3572. *
  3573. * R.update(1, '_', ['a', 'b', 'c']); //=> ['a', '_', 'c']
  3574. * R.update(-1, '_', ['a', 'b', 'c']); //=> ['a', 'b', '_']
  3575. * @symb R.update(-1, a, [b, c]) = [b, a]
  3576. * @symb R.update(0, a, [b, c]) = [a, c]
  3577. * @symb R.update(1, a, [b, c]) = [b, a]
  3578. */
  3579. var update = _curry3(function update(idx, x, list) {
  3580. return adjust(idx, always(x), list);
  3581. });
  3582. /**
  3583. * Makes a shallow clone of an object, omitting the property at the given path.
  3584. * Note that this copies and flattens prototype properties onto the new object
  3585. * as well. All non-primitive properties are copied by reference.
  3586. *
  3587. * @func
  3588. * @memberOf R
  3589. * @since v0.11.0
  3590. * @category Object
  3591. * @typedefn Idx = String | Int
  3592. * @sig [Idx] -> {k: v} -> {k: v}
  3593. * @param {Array} path The path to the value to omit
  3594. * @param {Object} obj The object to clone
  3595. * @return {Object} A new object without the property at path
  3596. * @see R.assocPath
  3597. * @example
  3598. *
  3599. * R.dissocPath(['a', 'b', 'c'], {a: {b: {c: 42}}}); //=> {a: {b: {}}}
  3600. */
  3601. var dissocPath = _curry2(function dissocPath(path, obj) {
  3602. switch (path.length) {
  3603. case 0:
  3604. return obj;
  3605. case 1:
  3606. return _isInteger(path[0]) && _isArray(obj) ? remove(path[0], 1, obj) : dissoc(path[0], obj);
  3607. default:
  3608. var head = path[0];
  3609. var tail = Array.prototype.slice.call(path, 1);
  3610. if (obj[head] == null) {
  3611. return obj;
  3612. } else if (_isInteger(head) && _isArray(obj)) {
  3613. return update(head, dissocPath(tail, obj[head]), obj);
  3614. } else {
  3615. return assoc(head, dissocPath(tail, obj[head]), obj);
  3616. }
  3617. }
  3618. });
  3619. /**
  3620. * Divides two numbers. Equivalent to `a / b`.
  3621. *
  3622. * @func
  3623. * @memberOf R
  3624. * @since v0.1.0
  3625. * @category Math
  3626. * @sig Number -> Number -> Number
  3627. * @param {Number} a The first value.
  3628. * @param {Number} b The second value.
  3629. * @return {Number} The result of `a / b`.
  3630. * @see R.multiply
  3631. * @example
  3632. *
  3633. * R.divide(71, 100); //=> 0.71
  3634. *
  3635. * const half = R.divide(R.__, 2);
  3636. * half(42); //=> 21
  3637. *
  3638. * const reciprocal = R.divide(1);
  3639. * reciprocal(4); //=> 0.25
  3640. */
  3641. var divide = _curry2(function divide(a, b) { return a / b; });
  3642. function XDrop(n, xf) {
  3643. this.xf = xf;
  3644. this.n = n;
  3645. }
  3646. XDrop.prototype['@@transducer/init'] = _xfBase.init;
  3647. XDrop.prototype['@@transducer/result'] = _xfBase.result;
  3648. XDrop.prototype['@@transducer/step'] = function(result, input) {
  3649. if (this.n > 0) {
  3650. this.n -= 1;
  3651. return result;
  3652. }
  3653. return this.xf['@@transducer/step'](result, input);
  3654. };
  3655. var _xdrop = _curry2(function _xdrop(n, xf) { return new XDrop(n, xf); });
  3656. /**
  3657. * Returns all but the first `n` elements of the given list, string, or
  3658. * transducer/transformer (or object with a `drop` method).
  3659. *
  3660. * Dispatches to the `drop` method of the second argument, if present.
  3661. *
  3662. * @func
  3663. * @memberOf R
  3664. * @since v0.1.0
  3665. * @category List
  3666. * @sig Number -> [a] -> [a]
  3667. * @sig Number -> String -> String
  3668. * @param {Number} n
  3669. * @param {*} list
  3670. * @return {*} A copy of list without the first `n` elements
  3671. * @see R.take, R.transduce, R.dropLast, R.dropWhile
  3672. * @example
  3673. *
  3674. * R.drop(1, ['foo', 'bar', 'baz']); //=> ['bar', 'baz']
  3675. * R.drop(2, ['foo', 'bar', 'baz']); //=> ['baz']
  3676. * R.drop(3, ['foo', 'bar', 'baz']); //=> []
  3677. * R.drop(4, ['foo', 'bar', 'baz']); //=> []
  3678. * R.drop(3, 'ramda'); //=> 'da'
  3679. */
  3680. var drop = _curry2(_dispatchable(['drop'], _xdrop, function drop(n, xs) {
  3681. return slice(Math.max(0, n), Infinity, xs);
  3682. }));
  3683. function XTake(n, xf) {
  3684. this.xf = xf;
  3685. this.n = n;
  3686. this.i = 0;
  3687. }
  3688. XTake.prototype['@@transducer/init'] = _xfBase.init;
  3689. XTake.prototype['@@transducer/result'] = _xfBase.result;
  3690. XTake.prototype['@@transducer/step'] = function(result, input) {
  3691. this.i += 1;
  3692. var ret = this.n === 0 ? result : this.xf['@@transducer/step'](result, input);
  3693. return this.n >= 0 && this.i >= this.n ? _reduced(ret) : ret;
  3694. };
  3695. var _xtake = _curry2(function _xtake(n, xf) { return new XTake(n, xf); });
  3696. /**
  3697. * Returns the first `n` elements of the given list, string, or
  3698. * transducer/transformer (or object with a `take` method).
  3699. *
  3700. * Dispatches to the `take` method of the second argument, if present.
  3701. *
  3702. * @func
  3703. * @memberOf R
  3704. * @since v0.1.0
  3705. * @category List
  3706. * @sig Number -> [a] -> [a]
  3707. * @sig Number -> String -> String
  3708. * @param {Number} n
  3709. * @param {*} list
  3710. * @return {*}
  3711. * @see R.drop
  3712. * @example
  3713. *
  3714. * R.take(1, ['foo', 'bar', 'baz']); //=> ['foo']
  3715. * R.take(2, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
  3716. * R.take(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  3717. * R.take(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  3718. * R.take(3, 'ramda'); //=> 'ram'
  3719. *
  3720. * const personnel = [
  3721. * 'Dave Brubeck',
  3722. * 'Paul Desmond',
  3723. * 'Eugene Wright',
  3724. * 'Joe Morello',
  3725. * 'Gerry Mulligan',
  3726. * 'Bob Bates',
  3727. * 'Joe Dodge',
  3728. * 'Ron Crotty'
  3729. * ];
  3730. *
  3731. * const takeFive = R.take(5);
  3732. * takeFive(personnel);
  3733. * //=> ['Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan']
  3734. * @symb R.take(-1, [a, b]) = [a, b]
  3735. * @symb R.take(0, [a, b]) = []
  3736. * @symb R.take(1, [a, b]) = [a]
  3737. * @symb R.take(2, [a, b]) = [a, b]
  3738. */
  3739. var take = _curry2(_dispatchable(['take'], _xtake, function take(n, xs) {
  3740. return slice(0, n < 0 ? Infinity : n, xs);
  3741. }));
  3742. function dropLast$2(n, xs) {
  3743. return take(n < xs.length ? xs.length - n : 0, xs);
  3744. }
  3745. function XDropLast(n, xf) {
  3746. this.xf = xf;
  3747. this.pos = 0;
  3748. this.full = false;
  3749. this.acc = new Array(n);
  3750. }
  3751. XDropLast.prototype['@@transducer/init'] = _xfBase.init;
  3752. XDropLast.prototype['@@transducer/result'] = function(result) {
  3753. this.acc = null;
  3754. return this.xf['@@transducer/result'](result);
  3755. };
  3756. XDropLast.prototype['@@transducer/step'] = function(result, input) {
  3757. if (this.full) {
  3758. result = this.xf['@@transducer/step'](result, this.acc[this.pos]);
  3759. }
  3760. this.store(input);
  3761. return result;
  3762. };
  3763. XDropLast.prototype.store = function(input) {
  3764. this.acc[this.pos] = input;
  3765. this.pos += 1;
  3766. if (this.pos === this.acc.length) {
  3767. this.pos = 0;
  3768. this.full = true;
  3769. }
  3770. };
  3771. var _xdropLast = _curry2(function _xdropLast(n, xf) { return new XDropLast(n, xf); });
  3772. /**
  3773. * Returns a list containing all but the last `n` elements of the given `list`.
  3774. *
  3775. * Acts as a transducer if a transformer is given in list position.
  3776. *
  3777. * @func
  3778. * @memberOf R
  3779. * @since v0.16.0
  3780. * @category List
  3781. * @sig Number -> [a] -> [a]
  3782. * @sig Number -> String -> String
  3783. * @param {Number} n The number of elements of `list` to skip.
  3784. * @param {Array} list The list of elements to consider.
  3785. * @return {Array} A copy of the list with only the first `list.length - n` elements
  3786. * @see R.takeLast, R.drop, R.dropWhile, R.dropLastWhile
  3787. * @example
  3788. *
  3789. * R.dropLast(1, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
  3790. * R.dropLast(2, ['foo', 'bar', 'baz']); //=> ['foo']
  3791. * R.dropLast(3, ['foo', 'bar', 'baz']); //=> []
  3792. * R.dropLast(4, ['foo', 'bar', 'baz']); //=> []
  3793. * R.dropLast(3, 'ramda'); //=> 'ra'
  3794. */
  3795. var dropLast = _curry2(_dispatchable([], _xdropLast, dropLast$2));
  3796. function dropLastWhile$2(pred, xs) {
  3797. var idx = xs.length - 1;
  3798. while (idx >= 0 && pred(xs[idx])) {
  3799. idx -= 1;
  3800. }
  3801. return slice(0, idx + 1, xs);
  3802. }
  3803. function XDropLastWhile(fn, xf) {
  3804. this.f = fn;
  3805. this.retained = [];
  3806. this.xf = xf;
  3807. }
  3808. XDropLastWhile.prototype['@@transducer/init'] = _xfBase.init;
  3809. XDropLastWhile.prototype['@@transducer/result'] = function(result) {
  3810. this.retained = null;
  3811. return this.xf['@@transducer/result'](result);
  3812. };
  3813. XDropLastWhile.prototype['@@transducer/step'] = function(result, input) {
  3814. return this.f(input) ? this.retain(result, input)
  3815. : this.flush(result, input);
  3816. };
  3817. XDropLastWhile.prototype.flush = function(result, input) {
  3818. result = _reduce(
  3819. this.xf['@@transducer/step'],
  3820. result,
  3821. this.retained
  3822. );
  3823. this.retained = [];
  3824. return this.xf['@@transducer/step'](result, input);
  3825. };
  3826. XDropLastWhile.prototype.retain = function(result, input) {
  3827. this.retained.push(input);
  3828. return result;
  3829. };
  3830. var _xdropLastWhile = _curry2(function _xdropLastWhile(fn, xf) { return new XDropLastWhile(fn, xf); });
  3831. /**
  3832. * Returns a new list excluding all the tailing elements of a given list which
  3833. * satisfy the supplied predicate function. It passes each value from the right
  3834. * to the supplied predicate function, skipping elements until the predicate
  3835. * function returns a `falsy` value. The predicate function is applied to one argument:
  3836. * *(value)*.
  3837. *
  3838. * Acts as a transducer if a transformer is given in list position.
  3839. *
  3840. * @func
  3841. * @memberOf R
  3842. * @since v0.16.0
  3843. * @category List
  3844. * @sig (a -> Boolean) -> [a] -> [a]
  3845. * @sig (a -> Boolean) -> String -> String
  3846. * @param {Function} predicate The function to be called on each element
  3847. * @param {Array} xs The collection to iterate over.
  3848. * @return {Array} A new array without any trailing elements that return `falsy` values from the `predicate`.
  3849. * @see R.takeLastWhile, R.addIndex, R.drop, R.dropWhile
  3850. * @example
  3851. *
  3852. * const lteThree = x => x <= 3;
  3853. *
  3854. * R.dropLastWhile(lteThree, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3, 4]
  3855. *
  3856. * R.dropLastWhile(x => x !== 'd' , 'Ramda'); //=> 'Ramd'
  3857. */
  3858. var dropLastWhile = _curry2(_dispatchable([], _xdropLastWhile, dropLastWhile$2));
  3859. function XDropRepeatsWith(pred, xf) {
  3860. this.xf = xf;
  3861. this.pred = pred;
  3862. this.lastValue = undefined;
  3863. this.seenFirstValue = false;
  3864. }
  3865. XDropRepeatsWith.prototype['@@transducer/init'] = _xfBase.init;
  3866. XDropRepeatsWith.prototype['@@transducer/result'] = _xfBase.result;
  3867. XDropRepeatsWith.prototype['@@transducer/step'] = function(result, input) {
  3868. var sameAsLast = false;
  3869. if (!this.seenFirstValue) {
  3870. this.seenFirstValue = true;
  3871. } else if (this.pred(this.lastValue, input)) {
  3872. sameAsLast = true;
  3873. }
  3874. this.lastValue = input;
  3875. return sameAsLast ? result : this.xf['@@transducer/step'](result, input);
  3876. };
  3877. var _xdropRepeatsWith = _curry2(function _xdropRepeatsWith(pred, xf) { return new XDropRepeatsWith(pred, xf); });
  3878. /**
  3879. * Returns the last element of the given list or string.
  3880. *
  3881. * @func
  3882. * @memberOf R
  3883. * @since v0.1.4
  3884. * @category List
  3885. * @sig [a] -> a | Undefined
  3886. * @sig String -> String
  3887. * @param {*} list
  3888. * @return {*}
  3889. * @see R.init, R.head, R.tail
  3890. * @example
  3891. *
  3892. * R.last(['fi', 'fo', 'fum']); //=> 'fum'
  3893. * R.last([]); //=> undefined
  3894. *
  3895. * R.last('abc'); //=> 'c'
  3896. * R.last(''); //=> ''
  3897. */
  3898. var last = nth(-1);
  3899. /**
  3900. * Returns a new list without any consecutively repeating elements. Equality is
  3901. * determined by applying the supplied predicate to each pair of consecutive elements. The
  3902. * first element in a series of equal elements will be preserved.
  3903. *
  3904. * Acts as a transducer if a transformer is given in list position.
  3905. *
  3906. * @func
  3907. * @memberOf R
  3908. * @since v0.14.0
  3909. * @category List
  3910. * @sig ((a, a) -> Boolean) -> [a] -> [a]
  3911. * @param {Function} pred A predicate used to test whether two items are equal.
  3912. * @param {Array} list The array to consider.
  3913. * @return {Array} `list` without repeating elements.
  3914. * @see R.transduce
  3915. * @example
  3916. *
  3917. * const l = [1, -1, 1, 3, 4, -4, -4, -5, 5, 3, 3];
  3918. * R.dropRepeatsWith(R.eqBy(Math.abs), l); //=> [1, 3, 4, -5, 3]
  3919. */
  3920. var dropRepeatsWith = _curry2(_dispatchable([], _xdropRepeatsWith, function dropRepeatsWith(pred, list) {
  3921. var result = [];
  3922. var idx = 1;
  3923. var len = list.length;
  3924. if (len !== 0) {
  3925. result[0] = list[0];
  3926. while (idx < len) {
  3927. if (!pred(last(result), list[idx])) {
  3928. result[result.length] = list[idx];
  3929. }
  3930. idx += 1;
  3931. }
  3932. }
  3933. return result;
  3934. }));
  3935. /**
  3936. * Returns a new list without any consecutively repeating elements.
  3937. * [`R.equals`](#equals) is used to determine equality.
  3938. *
  3939. * Acts as a transducer if a transformer is given in list position.
  3940. *
  3941. * @func
  3942. * @memberOf R
  3943. * @since v0.14.0
  3944. * @category List
  3945. * @sig [a] -> [a]
  3946. * @param {Array} list The array to consider.
  3947. * @return {Array} `list` without repeating elements.
  3948. * @see R.transduce
  3949. * @example
  3950. *
  3951. * R.dropRepeats([1, 1, 1, 2, 3, 4, 4, 2, 2]); //=> [1, 2, 3, 4, 2]
  3952. */
  3953. var dropRepeats = _curry1(_dispatchable([],
  3954. _xdropRepeatsWith(equals),
  3955. dropRepeatsWith(equals)));
  3956. function XDropWhile(f, xf) {
  3957. this.xf = xf;
  3958. this.f = f;
  3959. }
  3960. XDropWhile.prototype['@@transducer/init'] = _xfBase.init;
  3961. XDropWhile.prototype['@@transducer/result'] = _xfBase.result;
  3962. XDropWhile.prototype['@@transducer/step'] = function(result, input) {
  3963. if (this.f) {
  3964. if (this.f(input)) {
  3965. return result;
  3966. }
  3967. this.f = null;
  3968. }
  3969. return this.xf['@@transducer/step'](result, input);
  3970. };
  3971. var _xdropWhile = _curry2(function _xdropWhile(f, xf) { return new XDropWhile(f, xf); });
  3972. /**
  3973. * Returns a new list excluding the leading elements of a given list which
  3974. * satisfy the supplied predicate function. It passes each value to the supplied
  3975. * predicate function, skipping elements while the predicate function returns
  3976. * `true`. The predicate function is applied to one argument: *(value)*.
  3977. *
  3978. * Dispatches to the `dropWhile` method of the second argument, if present.
  3979. *
  3980. * Acts as a transducer if a transformer is given in list position.
  3981. *
  3982. * @func
  3983. * @memberOf R
  3984. * @since v0.9.0
  3985. * @category List
  3986. * @sig (a -> Boolean) -> [a] -> [a]
  3987. * @sig (a -> Boolean) -> String -> String
  3988. * @param {Function} fn The function called per iteration.
  3989. * @param {Array} xs The collection to iterate over.
  3990. * @return {Array} A new array.
  3991. * @see R.takeWhile, R.transduce, R.addIndex
  3992. * @example
  3993. *
  3994. * const lteTwo = x => x <= 2;
  3995. *
  3996. * R.dropWhile(lteTwo, [1, 2, 3, 4, 3, 2, 1]); //=> [3, 4, 3, 2, 1]
  3997. *
  3998. * R.dropWhile(x => x !== 'd' , 'Ramda'); //=> 'da'
  3999. */
  4000. var dropWhile = _curry2(_dispatchable(['dropWhile'], _xdropWhile, function dropWhile(pred, xs) {
  4001. var idx = 0;
  4002. var len = xs.length;
  4003. while (idx < len && pred(xs[idx])) {
  4004. idx += 1;
  4005. }
  4006. return slice(idx, Infinity, xs);
  4007. }));
  4008. /**
  4009. * Returns `true` if one or both of its arguments are `true`. Returns `false`
  4010. * if both arguments are `false`.
  4011. *
  4012. * @func
  4013. * @memberOf R
  4014. * @since v0.1.0
  4015. * @category Logic
  4016. * @sig a -> b -> a | b
  4017. * @param {Any} a
  4018. * @param {Any} b
  4019. * @return {Any} the first argument if truthy, otherwise the second argument.
  4020. * @see R.either
  4021. * @example
  4022. *
  4023. * R.or(true, true); //=> true
  4024. * R.or(true, false); //=> true
  4025. * R.or(false, true); //=> true
  4026. * R.or(false, false); //=> false
  4027. */
  4028. var or = _curry2(function or(a, b) {
  4029. return a || b;
  4030. });
  4031. /**
  4032. * A function wrapping calls to the two functions in an `||` operation,
  4033. * returning the result of the first function if it is truth-y and the result
  4034. * of the second function otherwise. Note that this is short-circuited,
  4035. * meaning that the second function will not be invoked if the first returns a
  4036. * truth-y value.
  4037. *
  4038. * In addition to functions, `R.either` also accepts any fantasy-land compatible
  4039. * applicative functor.
  4040. *
  4041. * @func
  4042. * @memberOf R
  4043. * @since v0.12.0
  4044. * @category Logic
  4045. * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
  4046. * @param {Function} f a predicate
  4047. * @param {Function} g another predicate
  4048. * @return {Function} a function that applies its arguments to `f` and `g` and `||`s their outputs together.
  4049. * @see R.or
  4050. * @example
  4051. *
  4052. * const gt10 = x => x > 10;
  4053. * const even = x => x % 2 === 0;
  4054. * const f = R.either(gt10, even);
  4055. * f(101); //=> true
  4056. * f(8); //=> true
  4057. *
  4058. * R.either(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(55)
  4059. * R.either([false, false, 'a'], [11]) // => [11, 11, "a"]
  4060. */
  4061. var either = _curry2(function either(f, g) {
  4062. return _isFunction(f) ?
  4063. function _either() {
  4064. return f.apply(this, arguments) || g.apply(this, arguments);
  4065. } :
  4066. lift(or)(f, g);
  4067. });
  4068. /**
  4069. * Returns the empty value of its argument's type. Ramda defines the empty
  4070. * value of Array (`[]`), Object (`{}`), String (`''`), and Arguments. Other
  4071. * types are supported if they define `<Type>.empty`,
  4072. * `<Type>.prototype.empty` or implement the
  4073. * [FantasyLand Monoid spec](https://github.com/fantasyland/fantasy-land#monoid).
  4074. *
  4075. * Dispatches to the `empty` method of the first argument, if present.
  4076. *
  4077. * @func
  4078. * @memberOf R
  4079. * @since v0.3.0
  4080. * @category Function
  4081. * @sig a -> a
  4082. * @param {*} x
  4083. * @return {*}
  4084. * @example
  4085. *
  4086. * R.empty(Just(42)); //=> Nothing()
  4087. * R.empty([1, 2, 3]); //=> []
  4088. * R.empty('unicorns'); //=> ''
  4089. * R.empty({x: 1, y: 2}); //=> {}
  4090. */
  4091. var empty = _curry1(function empty(x) {
  4092. return (
  4093. (x != null && typeof x['fantasy-land/empty'] === 'function') ?
  4094. x['fantasy-land/empty']() :
  4095. (x != null && x.constructor != null && typeof x.constructor['fantasy-land/empty'] === 'function') ?
  4096. x.constructor['fantasy-land/empty']() :
  4097. (x != null && typeof x.empty === 'function') ?
  4098. x.empty() :
  4099. (x != null && x.constructor != null && typeof x.constructor.empty === 'function') ?
  4100. x.constructor.empty() :
  4101. _isArray(x) ?
  4102. [] :
  4103. _isString(x) ?
  4104. '' :
  4105. _isObject(x) ?
  4106. {} :
  4107. _isArguments(x) ?
  4108. (function() { return arguments; }()) :
  4109. // else
  4110. void 0
  4111. );
  4112. });
  4113. /**
  4114. * Returns a new list containing the last `n` elements of the given list.
  4115. * If `n > list.length`, returns a list of `list.length` elements.
  4116. *
  4117. * @func
  4118. * @memberOf R
  4119. * @since v0.16.0
  4120. * @category List
  4121. * @sig Number -> [a] -> [a]
  4122. * @sig Number -> String -> String
  4123. * @param {Number} n The number of elements to return.
  4124. * @param {Array} xs The collection to consider.
  4125. * @return {Array}
  4126. * @see R.dropLast
  4127. * @example
  4128. *
  4129. * R.takeLast(1, ['foo', 'bar', 'baz']); //=> ['baz']
  4130. * R.takeLast(2, ['foo', 'bar', 'baz']); //=> ['bar', 'baz']
  4131. * R.takeLast(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  4132. * R.takeLast(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  4133. * R.takeLast(3, 'ramda'); //=> 'mda'
  4134. */
  4135. var takeLast = _curry2(function takeLast(n, xs) {
  4136. return drop(n >= 0 ? xs.length - n : 0, xs);
  4137. });
  4138. /**
  4139. * Checks if a list ends with the provided sublist.
  4140. *
  4141. * Similarly, checks if a string ends with the provided substring.
  4142. *
  4143. * @func
  4144. * @memberOf R
  4145. * @since v0.24.0
  4146. * @category List
  4147. * @sig [a] -> [a] -> Boolean
  4148. * @sig String -> String -> Boolean
  4149. * @param {*} suffix
  4150. * @param {*} list
  4151. * @return {Boolean}
  4152. * @see R.startsWith
  4153. * @example
  4154. *
  4155. * R.endsWith('c', 'abc') //=> true
  4156. * R.endsWith('b', 'abc') //=> false
  4157. * R.endsWith(['c'], ['a', 'b', 'c']) //=> true
  4158. * R.endsWith(['b'], ['a', 'b', 'c']) //=> false
  4159. */
  4160. var endsWith = _curry2(function(suffix, list) {
  4161. return equals(takeLast(suffix.length, list), suffix);
  4162. });
  4163. /**
  4164. * Takes a function and two values in its domain and returns `true` if the
  4165. * values map to the same value in the codomain; `false` otherwise.
  4166. *
  4167. * @func
  4168. * @memberOf R
  4169. * @since v0.18.0
  4170. * @category Relation
  4171. * @sig (a -> b) -> a -> a -> Boolean
  4172. * @param {Function} f
  4173. * @param {*} x
  4174. * @param {*} y
  4175. * @return {Boolean}
  4176. * @example
  4177. *
  4178. * R.eqBy(Math.abs, 5, -5); //=> true
  4179. */
  4180. var eqBy = _curry3(function eqBy(f, x, y) {
  4181. return equals(f(x), f(y));
  4182. });
  4183. /**
  4184. * Reports whether two objects have the same value, in [`R.equals`](#equals)
  4185. * terms, for the specified property. Useful as a curried predicate.
  4186. *
  4187. * @func
  4188. * @memberOf R
  4189. * @since v0.1.0
  4190. * @category Object
  4191. * @sig k -> {k: v} -> {k: v} -> Boolean
  4192. * @param {String} prop The name of the property to compare
  4193. * @param {Object} obj1
  4194. * @param {Object} obj2
  4195. * @return {Boolean}
  4196. *
  4197. * @example
  4198. *
  4199. * const o1 = { a: 1, b: 2, c: 3, d: 4 };
  4200. * const o2 = { a: 10, b: 20, c: 3, d: 40 };
  4201. * R.eqProps('a', o1, o2); //=> false
  4202. * R.eqProps('c', o1, o2); //=> true
  4203. */
  4204. var eqProps = _curry3(function eqProps(prop, obj1, obj2) {
  4205. return equals(obj1[prop], obj2[prop]);
  4206. });
  4207. /**
  4208. * Creates a new object by recursively evolving a shallow copy of `object`,
  4209. * according to the `transformation` functions. All non-primitive properties
  4210. * are copied by reference.
  4211. *
  4212. * A `transformation` function will not be invoked if its corresponding key
  4213. * does not exist in the evolved object.
  4214. *
  4215. * @func
  4216. * @memberOf R
  4217. * @since v0.9.0
  4218. * @category Object
  4219. * @sig {k: (v -> v)} -> {k: v} -> {k: v}
  4220. * @param {Object} transformations The object specifying transformation functions to apply
  4221. * to the object.
  4222. * @param {Object} object The object to be transformed.
  4223. * @return {Object} The transformed object.
  4224. * @example
  4225. *
  4226. * const tomato = {firstName: ' Tomato ', data: {elapsed: 100, remaining: 1400}, id:123};
  4227. * const transformations = {
  4228. * firstName: R.trim,
  4229. * lastName: R.trim, // Will not get invoked.
  4230. * data: {elapsed: R.add(1), remaining: R.add(-1)}
  4231. * };
  4232. * R.evolve(transformations, tomato); //=> {firstName: 'Tomato', data: {elapsed: 101, remaining: 1399}, id:123}
  4233. */
  4234. var evolve = _curry2(function evolve(transformations, object) {
  4235. var result = object instanceof Array ? [] : {};
  4236. var transformation, key, type;
  4237. for (key in object) {
  4238. transformation = transformations[key];
  4239. type = typeof transformation;
  4240. result[key] = type === 'function' ? transformation(object[key])
  4241. : transformation && type === 'object' ? evolve(transformation, object[key])
  4242. : object[key];
  4243. }
  4244. return result;
  4245. });
  4246. function XFind(f, xf) {
  4247. this.xf = xf;
  4248. this.f = f;
  4249. this.found = false;
  4250. }
  4251. XFind.prototype['@@transducer/init'] = _xfBase.init;
  4252. XFind.prototype['@@transducer/result'] = function(result) {
  4253. if (!this.found) {
  4254. result = this.xf['@@transducer/step'](result, void 0);
  4255. }
  4256. return this.xf['@@transducer/result'](result);
  4257. };
  4258. XFind.prototype['@@transducer/step'] = function(result, input) {
  4259. if (this.f(input)) {
  4260. this.found = true;
  4261. result = _reduced(this.xf['@@transducer/step'](result, input));
  4262. }
  4263. return result;
  4264. };
  4265. var _xfind = _curry2(function _xfind(f, xf) { return new XFind(f, xf); });
  4266. /**
  4267. * Returns the first element of the list which matches the predicate, or
  4268. * `undefined` if no element matches.
  4269. *
  4270. * Dispatches to the `find` method of the second argument, if present.
  4271. *
  4272. * Acts as a transducer if a transformer is given in list position.
  4273. *
  4274. * @func
  4275. * @memberOf R
  4276. * @since v0.1.0
  4277. * @category List
  4278. * @sig (a -> Boolean) -> [a] -> a | undefined
  4279. * @param {Function} fn The predicate function used to determine if the element is the
  4280. * desired one.
  4281. * @param {Array} list The array to consider.
  4282. * @return {Object} The element found, or `undefined`.
  4283. * @see R.transduce
  4284. * @example
  4285. *
  4286. * const xs = [{a: 1}, {a: 2}, {a: 3}];
  4287. * R.find(R.propEq('a', 2))(xs); //=> {a: 2}
  4288. * R.find(R.propEq('a', 4))(xs); //=> undefined
  4289. */
  4290. var find = _curry2(_dispatchable(['find'], _xfind, function find(fn, list) {
  4291. var idx = 0;
  4292. var len = list.length;
  4293. while (idx < len) {
  4294. if (fn(list[idx])) {
  4295. return list[idx];
  4296. }
  4297. idx += 1;
  4298. }
  4299. }));
  4300. function XFindIndex(f, xf) {
  4301. this.xf = xf;
  4302. this.f = f;
  4303. this.idx = -1;
  4304. this.found = false;
  4305. }
  4306. XFindIndex.prototype['@@transducer/init'] = _xfBase.init;
  4307. XFindIndex.prototype['@@transducer/result'] = function(result) {
  4308. if (!this.found) {
  4309. result = this.xf['@@transducer/step'](result, -1);
  4310. }
  4311. return this.xf['@@transducer/result'](result);
  4312. };
  4313. XFindIndex.prototype['@@transducer/step'] = function(result, input) {
  4314. this.idx += 1;
  4315. if (this.f(input)) {
  4316. this.found = true;
  4317. result = _reduced(this.xf['@@transducer/step'](result, this.idx));
  4318. }
  4319. return result;
  4320. };
  4321. var _xfindIndex = _curry2(function _xfindIndex(f, xf) { return new XFindIndex(f, xf); });
  4322. /**
  4323. * Returns the index of the first element of the list which matches the
  4324. * predicate, or `-1` if no element matches.
  4325. *
  4326. * Acts as a transducer if a transformer is given in list position.
  4327. *
  4328. * @func
  4329. * @memberOf R
  4330. * @since v0.1.1
  4331. * @category List
  4332. * @sig (a -> Boolean) -> [a] -> Number
  4333. * @param {Function} fn The predicate function used to determine if the element is the
  4334. * desired one.
  4335. * @param {Array} list The array to consider.
  4336. * @return {Number} The index of the element found, or `-1`.
  4337. * @see R.transduce
  4338. * @example
  4339. *
  4340. * const xs = [{a: 1}, {a: 2}, {a: 3}];
  4341. * R.findIndex(R.propEq('a', 2))(xs); //=> 1
  4342. * R.findIndex(R.propEq('a', 4))(xs); //=> -1
  4343. */
  4344. var findIndex = _curry2(_dispatchable([], _xfindIndex, function findIndex(fn, list) {
  4345. var idx = 0;
  4346. var len = list.length;
  4347. while (idx < len) {
  4348. if (fn(list[idx])) {
  4349. return idx;
  4350. }
  4351. idx += 1;
  4352. }
  4353. return -1;
  4354. }));
  4355. function XFindLast(f, xf) {
  4356. this.xf = xf;
  4357. this.f = f;
  4358. }
  4359. XFindLast.prototype['@@transducer/init'] = _xfBase.init;
  4360. XFindLast.prototype['@@transducer/result'] = function(result) {
  4361. return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.last));
  4362. };
  4363. XFindLast.prototype['@@transducer/step'] = function(result, input) {
  4364. if (this.f(input)) {
  4365. this.last = input;
  4366. }
  4367. return result;
  4368. };
  4369. var _xfindLast = _curry2(function _xfindLast(f, xf) { return new XFindLast(f, xf); });
  4370. /**
  4371. * Returns the last element of the list which matches the predicate, or
  4372. * `undefined` if no element matches.
  4373. *
  4374. * Acts as a transducer if a transformer is given in list position.
  4375. *
  4376. * @func
  4377. * @memberOf R
  4378. * @since v0.1.1
  4379. * @category List
  4380. * @sig (a -> Boolean) -> [a] -> a | undefined
  4381. * @param {Function} fn The predicate function used to determine if the element is the
  4382. * desired one.
  4383. * @param {Array} list The array to consider.
  4384. * @return {Object} The element found, or `undefined`.
  4385. * @see R.transduce
  4386. * @example
  4387. *
  4388. * const xs = [{a: 1, b: 0}, {a:1, b: 1}];
  4389. * R.findLast(R.propEq('a', 1))(xs); //=> {a: 1, b: 1}
  4390. * R.findLast(R.propEq('a', 4))(xs); //=> undefined
  4391. */
  4392. var findLast = _curry2(_dispatchable([], _xfindLast, function findLast(fn, list) {
  4393. var idx = list.length - 1;
  4394. while (idx >= 0) {
  4395. if (fn(list[idx])) {
  4396. return list[idx];
  4397. }
  4398. idx -= 1;
  4399. }
  4400. }));
  4401. function XFindLastIndex(f, xf) {
  4402. this.xf = xf;
  4403. this.f = f;
  4404. this.idx = -1;
  4405. this.lastIdx = -1;
  4406. }
  4407. XFindLastIndex.prototype['@@transducer/init'] = _xfBase.init;
  4408. XFindLastIndex.prototype['@@transducer/result'] = function(result) {
  4409. return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.lastIdx));
  4410. };
  4411. XFindLastIndex.prototype['@@transducer/step'] = function(result, input) {
  4412. this.idx += 1;
  4413. if (this.f(input)) {
  4414. this.lastIdx = this.idx;
  4415. }
  4416. return result;
  4417. };
  4418. var _xfindLastIndex = _curry2(function _xfindLastIndex(f, xf) { return new XFindLastIndex(f, xf); });
  4419. /**
  4420. * Returns the index of the last element of the list which matches the
  4421. * predicate, or `-1` if no element matches.
  4422. *
  4423. * Acts as a transducer if a transformer is given in list position.
  4424. *
  4425. * @func
  4426. * @memberOf R
  4427. * @since v0.1.1
  4428. * @category List
  4429. * @sig (a -> Boolean) -> [a] -> Number
  4430. * @param {Function} fn The predicate function used to determine if the element is the
  4431. * desired one.
  4432. * @param {Array} list The array to consider.
  4433. * @return {Number} The index of the element found, or `-1`.
  4434. * @see R.transduce
  4435. * @example
  4436. *
  4437. * const xs = [{a: 1, b: 0}, {a:1, b: 1}];
  4438. * R.findLastIndex(R.propEq('a', 1))(xs); //=> 1
  4439. * R.findLastIndex(R.propEq('a', 4))(xs); //=> -1
  4440. */
  4441. var findLastIndex = _curry2(_dispatchable([], _xfindLastIndex, function findLastIndex(fn, list) {
  4442. var idx = list.length - 1;
  4443. while (idx >= 0) {
  4444. if (fn(list[idx])) {
  4445. return idx;
  4446. }
  4447. idx -= 1;
  4448. }
  4449. return -1;
  4450. }));
  4451. /**
  4452. * Returns a new list by pulling every item out of it (and all its sub-arrays)
  4453. * and putting them in a new array, depth-first.
  4454. *
  4455. * @func
  4456. * @memberOf R
  4457. * @since v0.1.0
  4458. * @category List
  4459. * @sig [a] -> [b]
  4460. * @param {Array} list The array to consider.
  4461. * @return {Array} The flattened list.
  4462. * @see R.unnest
  4463. * @example
  4464. *
  4465. * R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
  4466. * //=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  4467. */
  4468. var flatten = _curry1(_makeFlat(true));
  4469. /**
  4470. * Returns a new function much like the supplied one, except that the first two
  4471. * arguments' order is reversed.
  4472. *
  4473. * @func
  4474. * @memberOf R
  4475. * @since v0.1.0
  4476. * @category Function
  4477. * @sig ((a, b, c, ...) -> z) -> (b -> a -> c -> ... -> z)
  4478. * @param {Function} fn The function to invoke with its first two parameters reversed.
  4479. * @return {*} The result of invoking `fn` with its first two parameters' order reversed.
  4480. * @example
  4481. *
  4482. * const mergeThree = (a, b, c) => [].concat(a, b, c);
  4483. *
  4484. * mergeThree(1, 2, 3); //=> [1, 2, 3]
  4485. *
  4486. * R.flip(mergeThree)(1, 2, 3); //=> [2, 1, 3]
  4487. * @symb R.flip(f)(a, b, c) = f(b, a, c)
  4488. */
  4489. var flip = _curry1(function flip(fn) {
  4490. return curryN(fn.length, function(a, b) {
  4491. var args = Array.prototype.slice.call(arguments, 0);
  4492. args[0] = b;
  4493. args[1] = a;
  4494. return fn.apply(this, args);
  4495. });
  4496. });
  4497. /**
  4498. * Iterate over an input `list`, calling a provided function `fn` for each
  4499. * element in the list.
  4500. *
  4501. * `fn` receives one argument: *(value)*.
  4502. *
  4503. * Note: `R.forEach` does not skip deleted or unassigned indices (sparse
  4504. * arrays), unlike the native `Array.prototype.forEach` method. For more
  4505. * details on this behavior, see:
  4506. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description
  4507. *
  4508. * Also note that, unlike `Array.prototype.forEach`, Ramda's `forEach` returns
  4509. * the original array. In some libraries this function is named `each`.
  4510. *
  4511. * Dispatches to the `forEach` method of the second argument, if present.
  4512. *
  4513. * @func
  4514. * @memberOf R
  4515. * @since v0.1.1
  4516. * @category List
  4517. * @sig (a -> *) -> [a] -> [a]
  4518. * @param {Function} fn The function to invoke. Receives one argument, `value`.
  4519. * @param {Array} list The list to iterate over.
  4520. * @return {Array} The original list.
  4521. * @see R.addIndex
  4522. * @example
  4523. *
  4524. * const printXPlusFive = x => console.log(x + 5);
  4525. * R.forEach(printXPlusFive, [1, 2, 3]); //=> [1, 2, 3]
  4526. * // logs 6
  4527. * // logs 7
  4528. * // logs 8
  4529. * @symb R.forEach(f, [a, b, c]) = [a, b, c]
  4530. */
  4531. var forEach = _curry2(_checkForMethod('forEach', function forEach(fn, list) {
  4532. var len = list.length;
  4533. var idx = 0;
  4534. while (idx < len) {
  4535. fn(list[idx]);
  4536. idx += 1;
  4537. }
  4538. return list;
  4539. }));
  4540. /**
  4541. * Iterate over an input `object`, calling a provided function `fn` for each
  4542. * key and value in the object.
  4543. *
  4544. * `fn` receives three argument: *(value, key, obj)*.
  4545. *
  4546. * @func
  4547. * @memberOf R
  4548. * @since v0.23.0
  4549. * @category Object
  4550. * @sig ((a, String, StrMap a) -> Any) -> StrMap a -> StrMap a
  4551. * @param {Function} fn The function to invoke. Receives three argument, `value`, `key`, `obj`.
  4552. * @param {Object} obj The object to iterate over.
  4553. * @return {Object} The original object.
  4554. * @example
  4555. *
  4556. * const printKeyConcatValue = (value, key) => console.log(key + ':' + value);
  4557. * R.forEachObjIndexed(printKeyConcatValue, {x: 1, y: 2}); //=> {x: 1, y: 2}
  4558. * // logs x:1
  4559. * // logs y:2
  4560. * @symb R.forEachObjIndexed(f, {x: a, y: b}) = {x: a, y: b}
  4561. */
  4562. var forEachObjIndexed = _curry2(function forEachObjIndexed(fn, obj) {
  4563. var keyList = keys(obj);
  4564. var idx = 0;
  4565. while (idx < keyList.length) {
  4566. var key = keyList[idx];
  4567. fn(obj[key], key, obj);
  4568. idx += 1;
  4569. }
  4570. return obj;
  4571. });
  4572. /**
  4573. * Creates a new object from a list key-value pairs. If a key appears in
  4574. * multiple pairs, the rightmost pair is included in the object.
  4575. *
  4576. * @func
  4577. * @memberOf R
  4578. * @since v0.3.0
  4579. * @category List
  4580. * @sig [[k,v]] -> {k: v}
  4581. * @param {Array} pairs An array of two-element arrays that will be the keys and values of the output object.
  4582. * @return {Object} The object made by pairing up `keys` and `values`.
  4583. * @see R.toPairs, R.pair
  4584. * @example
  4585. *
  4586. * R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3}
  4587. */
  4588. var fromPairs = _curry1(function fromPairs(pairs) {
  4589. var result = {};
  4590. var idx = 0;
  4591. while (idx < pairs.length) {
  4592. result[pairs[idx][0]] = pairs[idx][1];
  4593. idx += 1;
  4594. }
  4595. return result;
  4596. });
  4597. /**
  4598. * Splits a list into sub-lists stored in an object, based on the result of
  4599. * calling a String-returning function on each element, and grouping the
  4600. * results according to values returned.
  4601. *
  4602. * Dispatches to the `groupBy` method of the second argument, if present.
  4603. *
  4604. * Acts as a transducer if a transformer is given in list position.
  4605. *
  4606. * @func
  4607. * @memberOf R
  4608. * @since v0.1.0
  4609. * @category List
  4610. * @sig (a -> String) -> [a] -> {String: [a]}
  4611. * @param {Function} fn Function :: a -> String
  4612. * @param {Array} list The array to group
  4613. * @return {Object} An object with the output of `fn` for keys, mapped to arrays of elements
  4614. * that produced that key when passed to `fn`.
  4615. * @see R.reduceBy, R.transduce
  4616. * @example
  4617. *
  4618. * const byGrade = R.groupBy(function(student) {
  4619. * const score = student.score;
  4620. * return score < 65 ? 'F' :
  4621. * score < 70 ? 'D' :
  4622. * score < 80 ? 'C' :
  4623. * score < 90 ? 'B' : 'A';
  4624. * });
  4625. * const students = [{name: 'Abby', score: 84},
  4626. * {name: 'Eddy', score: 58},
  4627. * // ...
  4628. * {name: 'Jack', score: 69}];
  4629. * byGrade(students);
  4630. * // {
  4631. * // 'A': [{name: 'Dianne', score: 99}],
  4632. * // 'B': [{name: 'Abby', score: 84}]
  4633. * // // ...,
  4634. * // 'F': [{name: 'Eddy', score: 58}]
  4635. * // }
  4636. */
  4637. var groupBy = _curry2(_checkForMethod('groupBy', reduceBy(function(acc, item) {
  4638. if (acc == null) {
  4639. acc = [];
  4640. }
  4641. acc.push(item);
  4642. return acc;
  4643. }, null)));
  4644. /**
  4645. * Takes a list and returns a list of lists where each sublist's elements are
  4646. * all satisfied pairwise comparison according to the provided function.
  4647. * Only adjacent elements are passed to the comparison function.
  4648. *
  4649. * @func
  4650. * @memberOf R
  4651. * @since v0.21.0
  4652. * @category List
  4653. * @sig ((a, a) → Boolean) → [a] → [[a]]
  4654. * @param {Function} fn Function for determining whether two given (adjacent)
  4655. * elements should be in the same group
  4656. * @param {Array} list The array to group. Also accepts a string, which will be
  4657. * treated as a list of characters.
  4658. * @return {List} A list that contains sublists of elements,
  4659. * whose concatenations are equal to the original list.
  4660. * @example
  4661. *
  4662. * R.groupWith(R.equals, [0, 1, 1, 2, 3, 5, 8, 13, 21])
  4663. * //=> [[0], [1, 1], [2], [3], [5], [8], [13], [21]]
  4664. *
  4665. * R.groupWith((a, b) => a + 1 === b, [0, 1, 1, 2, 3, 5, 8, 13, 21])
  4666. * //=> [[0, 1], [1, 2, 3], [5], [8], [13], [21]]
  4667. *
  4668. * R.groupWith((a, b) => a % 2 === b % 2, [0, 1, 1, 2, 3, 5, 8, 13, 21])
  4669. * //=> [[0], [1, 1], [2], [3, 5], [8], [13, 21]]
  4670. *
  4671. * R.groupWith(R.eqBy(isVowel), 'aestiou')
  4672. * //=> ['ae', 'st', 'iou']
  4673. */
  4674. var groupWith = _curry2(function(fn, list) {
  4675. var res = [];
  4676. var idx = 0;
  4677. var len = list.length;
  4678. while (idx < len) {
  4679. var nextidx = idx + 1;
  4680. while (nextidx < len && fn(list[nextidx - 1], list[nextidx])) {
  4681. nextidx += 1;
  4682. }
  4683. res.push(list.slice(idx, nextidx));
  4684. idx = nextidx;
  4685. }
  4686. return res;
  4687. });
  4688. /**
  4689. * Returns `true` if the first argument is greater than the second; `false`
  4690. * otherwise.
  4691. *
  4692. * @func
  4693. * @memberOf R
  4694. * @since v0.1.0
  4695. * @category Relation
  4696. * @sig Ord a => a -> a -> Boolean
  4697. * @param {*} a
  4698. * @param {*} b
  4699. * @return {Boolean}
  4700. * @see R.lt
  4701. * @example
  4702. *
  4703. * R.gt(2, 1); //=> true
  4704. * R.gt(2, 2); //=> false
  4705. * R.gt(2, 3); //=> false
  4706. * R.gt('a', 'z'); //=> false
  4707. * R.gt('z', 'a'); //=> true
  4708. */
  4709. var gt = _curry2(function gt(a, b) { return a > b; });
  4710. /**
  4711. * Returns `true` if the first argument is greater than or equal to the second;
  4712. * `false` otherwise.
  4713. *
  4714. * @func
  4715. * @memberOf R
  4716. * @since v0.1.0
  4717. * @category Relation
  4718. * @sig Ord a => a -> a -> Boolean
  4719. * @param {Number} a
  4720. * @param {Number} b
  4721. * @return {Boolean}
  4722. * @see R.lte
  4723. * @example
  4724. *
  4725. * R.gte(2, 1); //=> true
  4726. * R.gte(2, 2); //=> true
  4727. * R.gte(2, 3); //=> false
  4728. * R.gte('a', 'z'); //=> false
  4729. * R.gte('z', 'a'); //=> true
  4730. */
  4731. var gte = _curry2(function gte(a, b) { return a >= b; });
  4732. /**
  4733. * Returns whether or not a path exists in an object. Only the object's
  4734. * own properties are checked.
  4735. *
  4736. * @func
  4737. * @memberOf R
  4738. * @since v0.26.0
  4739. * @category Object
  4740. * @typedefn Idx = String | Int
  4741. * @sig [Idx] -> {a} -> Boolean
  4742. * @param {Array} path The path to use.
  4743. * @param {Object} obj The object to check the path in.
  4744. * @return {Boolean} Whether the path exists.
  4745. * @see R.has
  4746. * @example
  4747. *
  4748. * R.hasPath(['a', 'b'], {a: {b: 2}}); // => true
  4749. * R.hasPath(['a', 'b'], {a: {b: undefined}}); // => true
  4750. * R.hasPath(['a', 'b'], {a: {c: 2}}); // => false
  4751. * R.hasPath(['a', 'b'], {}); // => false
  4752. */
  4753. var hasPath = _curry2(function hasPath(_path, obj) {
  4754. if (_path.length === 0) {
  4755. return false;
  4756. }
  4757. var val = obj;
  4758. var idx = 0;
  4759. while (idx < _path.length) {
  4760. if (_has(_path[idx], val)) {
  4761. val = val[_path[idx]];
  4762. idx += 1;
  4763. } else {
  4764. return false;
  4765. }
  4766. }
  4767. return true;
  4768. });
  4769. /**
  4770. * Returns whether or not an object has an own property with the specified name
  4771. *
  4772. * @func
  4773. * @memberOf R
  4774. * @since v0.7.0
  4775. * @category Object
  4776. * @sig s -> {s: x} -> Boolean
  4777. * @param {String} prop The name of the property to check for.
  4778. * @param {Object} obj The object to query.
  4779. * @return {Boolean} Whether the property exists.
  4780. * @example
  4781. *
  4782. * const hasName = R.has('name');
  4783. * hasName({name: 'alice'}); //=> true
  4784. * hasName({name: 'bob'}); //=> true
  4785. * hasName({}); //=> false
  4786. *
  4787. * const point = {x: 0, y: 0};
  4788. * const pointHas = R.has(R.__, point);
  4789. * pointHas('x'); //=> true
  4790. * pointHas('y'); //=> true
  4791. * pointHas('z'); //=> false
  4792. */
  4793. var has = _curry2(function has(prop, obj) {
  4794. return hasPath([prop], obj);
  4795. });
  4796. /**
  4797. * Returns whether or not an object or its prototype chain has a property with
  4798. * the specified name
  4799. *
  4800. * @func
  4801. * @memberOf R
  4802. * @since v0.7.0
  4803. * @category Object
  4804. * @sig s -> {s: x} -> Boolean
  4805. * @param {String} prop The name of the property to check for.
  4806. * @param {Object} obj The object to query.
  4807. * @return {Boolean} Whether the property exists.
  4808. * @example
  4809. *
  4810. * function Rectangle(width, height) {
  4811. * this.width = width;
  4812. * this.height = height;
  4813. * }
  4814. * Rectangle.prototype.area = function() {
  4815. * return this.width * this.height;
  4816. * };
  4817. *
  4818. * const square = new Rectangle(2, 2);
  4819. * R.hasIn('width', square); //=> true
  4820. * R.hasIn('area', square); //=> true
  4821. */
  4822. var hasIn = _curry2(function hasIn(prop, obj) {
  4823. return prop in obj;
  4824. });
  4825. /**
  4826. * Returns true if its arguments are identical, false otherwise. Values are
  4827. * identical if they reference the same memory. `NaN` is identical to `NaN`;
  4828. * `0` and `-0` are not identical.
  4829. *
  4830. * Note this is merely a curried version of ES6 `Object.is`.
  4831. *
  4832. * @func
  4833. * @memberOf R
  4834. * @since v0.15.0
  4835. * @category Relation
  4836. * @sig a -> a -> Boolean
  4837. * @param {*} a
  4838. * @param {*} b
  4839. * @return {Boolean}
  4840. * @example
  4841. *
  4842. * const o = {};
  4843. * R.identical(o, o); //=> true
  4844. * R.identical(1, 1); //=> true
  4845. * R.identical(1, '1'); //=> false
  4846. * R.identical([], []); //=> false
  4847. * R.identical(0, -0); //=> false
  4848. * R.identical(NaN, NaN); //=> true
  4849. */
  4850. var identical = _curry2(_objectIs$1);
  4851. /**
  4852. * Creates a function that will process either the `onTrue` or the `onFalse`
  4853. * function depending upon the result of the `condition` predicate.
  4854. *
  4855. * @func
  4856. * @memberOf R
  4857. * @since v0.8.0
  4858. * @category Logic
  4859. * @sig (*... -> Boolean) -> (*... -> *) -> (*... -> *) -> (*... -> *)
  4860. * @param {Function} condition A predicate function
  4861. * @param {Function} onTrue A function to invoke when the `condition` evaluates to a truthy value.
  4862. * @param {Function} onFalse A function to invoke when the `condition` evaluates to a falsy value.
  4863. * @return {Function} A new function that will process either the `onTrue` or the `onFalse`
  4864. * function depending upon the result of the `condition` predicate.
  4865. * @see R.unless, R.when, R.cond
  4866. * @example
  4867. *
  4868. * const incCount = R.ifElse(
  4869. * R.has('count'),
  4870. * R.over(R.lensProp('count'), R.inc),
  4871. * R.assoc('count', 1)
  4872. * );
  4873. * incCount({}); //=> { count: 1 }
  4874. * incCount({ count: 1 }); //=> { count: 2 }
  4875. */
  4876. var ifElse = _curry3(function ifElse(condition, onTrue, onFalse) {
  4877. return curryN(Math.max(condition.length, onTrue.length, onFalse.length),
  4878. function _ifElse() {
  4879. return condition.apply(this, arguments) ? onTrue.apply(this, arguments) : onFalse.apply(this, arguments);
  4880. }
  4881. );
  4882. });
  4883. /**
  4884. * Increments its argument.
  4885. *
  4886. * @func
  4887. * @memberOf R
  4888. * @since v0.9.0
  4889. * @category Math
  4890. * @sig Number -> Number
  4891. * @param {Number} n
  4892. * @return {Number} n + 1
  4893. * @see R.dec
  4894. * @example
  4895. *
  4896. * R.inc(42); //=> 43
  4897. */
  4898. var inc = add(1);
  4899. /**
  4900. * Returns `true` if the specified value is equal, in [`R.equals`](#equals)
  4901. * terms, to at least one element of the given list; `false` otherwise.
  4902. * Works also with strings.
  4903. *
  4904. * @func
  4905. * @memberOf R
  4906. * @since v0.1.0
  4907. * @category List
  4908. * @sig a -> [a] -> Boolean
  4909. * @param {Object} a The item to compare against.
  4910. * @param {Array} list The array to consider.
  4911. * @return {Boolean} `true` if an equivalent item is in the list, `false` otherwise.
  4912. * @see R.any
  4913. * @example
  4914. *
  4915. * R.includes(3, [1, 2, 3]); //=> true
  4916. * R.includes(4, [1, 2, 3]); //=> false
  4917. * R.includes({ name: 'Fred' }, [{ name: 'Fred' }]); //=> true
  4918. * R.includes([42], [[42]]); //=> true
  4919. * R.includes('ba', 'banana'); //=>true
  4920. */
  4921. var includes = _curry2(_includes);
  4922. /**
  4923. * Given a function that generates a key, turns a list of objects into an
  4924. * object indexing the objects by the given key. Note that if multiple
  4925. * objects generate the same value for the indexing key only the last value
  4926. * will be included in the generated object.
  4927. *
  4928. * Acts as a transducer if a transformer is given in list position.
  4929. *
  4930. * @func
  4931. * @memberOf R
  4932. * @since v0.19.0
  4933. * @category List
  4934. * @sig (a -> String) -> [{k: v}] -> {k: {k: v}}
  4935. * @param {Function} fn Function :: a -> String
  4936. * @param {Array} array The array of objects to index
  4937. * @return {Object} An object indexing each array element by the given property.
  4938. * @example
  4939. *
  4940. * const list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
  4941. * R.indexBy(R.prop('id'), list);
  4942. * //=> {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}}
  4943. */
  4944. var indexBy = reduceBy(function(acc, elem) { return elem; }, null);
  4945. /**
  4946. * Returns the position of the first occurrence of an item in an array, or -1
  4947. * if the item is not included in the array. [`R.equals`](#equals) is used to
  4948. * determine equality.
  4949. *
  4950. * @func
  4951. * @memberOf R
  4952. * @since v0.1.0
  4953. * @category List
  4954. * @sig a -> [a] -> Number
  4955. * @param {*} target The item to find.
  4956. * @param {Array} xs The array to search in.
  4957. * @return {Number} the index of the target, or -1 if the target is not found.
  4958. * @see R.lastIndexOf
  4959. * @example
  4960. *
  4961. * R.indexOf(3, [1,2,3,4]); //=> 2
  4962. * R.indexOf(10, [1,2,3,4]); //=> -1
  4963. */
  4964. var indexOf = _curry2(function indexOf(target, xs) {
  4965. return typeof xs.indexOf === 'function' && !_isArray(xs) ?
  4966. xs.indexOf(target) :
  4967. _indexOf(xs, target, 0);
  4968. });
  4969. /**
  4970. * Returns all but the last element of the given list or string.
  4971. *
  4972. * @func
  4973. * @memberOf R
  4974. * @since v0.9.0
  4975. * @category List
  4976. * @sig [a] -> [a]
  4977. * @sig String -> String
  4978. * @param {*} list
  4979. * @return {*}
  4980. * @see R.last, R.head, R.tail
  4981. * @example
  4982. *
  4983. * R.init([1, 2, 3]); //=> [1, 2]
  4984. * R.init([1, 2]); //=> [1]
  4985. * R.init([1]); //=> []
  4986. * R.init([]); //=> []
  4987. *
  4988. * R.init('abc'); //=> 'ab'
  4989. * R.init('ab'); //=> 'a'
  4990. * R.init('a'); //=> ''
  4991. * R.init(''); //=> ''
  4992. */
  4993. var init = slice(0, -1);
  4994. /**
  4995. * Takes a predicate `pred`, a list `xs`, and a list `ys`, and returns a list
  4996. * `xs'` comprising each of the elements of `xs` which is equal to one or more
  4997. * elements of `ys` according to `pred`.
  4998. *
  4999. * `pred` must be a binary function expecting an element from each list.
  5000. *
  5001. * `xs`, `ys`, and `xs'` are treated as sets, semantically, so ordering should
  5002. * not be significant, but since `xs'` is ordered the implementation guarantees
  5003. * that its values are in the same order as they appear in `xs`. Duplicates are
  5004. * not removed, so `xs'` may contain duplicates if `xs` contains duplicates.
  5005. *
  5006. * @func
  5007. * @memberOf R
  5008. * @since v0.24.0
  5009. * @category Relation
  5010. * @sig ((a, b) -> Boolean) -> [a] -> [b] -> [a]
  5011. * @param {Function} pred
  5012. * @param {Array} xs
  5013. * @param {Array} ys
  5014. * @return {Array}
  5015. * @see R.intersection
  5016. * @example
  5017. *
  5018. * R.innerJoin(
  5019. * (record, id) => record.id === id,
  5020. * [{id: 824, name: 'Richie Furay'},
  5021. * {id: 956, name: 'Dewey Martin'},
  5022. * {id: 313, name: 'Bruce Palmer'},
  5023. * {id: 456, name: 'Stephen Stills'},
  5024. * {id: 177, name: 'Neil Young'}],
  5025. * [177, 456, 999]
  5026. * );
  5027. * //=> [{id: 456, name: 'Stephen Stills'}, {id: 177, name: 'Neil Young'}]
  5028. */
  5029. var innerJoin = _curry3(function innerJoin(pred, xs, ys) {
  5030. return _filter(function(x) { return _includesWith(pred, x, ys); }, xs);
  5031. });
  5032. /**
  5033. * Inserts the supplied element into the list, at the specified `index`. _Note that
  5034. * this is not destructive_: it returns a copy of the list with the changes.
  5035. * <small>No lists have been harmed in the application of this function.</small>
  5036. *
  5037. * @func
  5038. * @memberOf R
  5039. * @since v0.2.2
  5040. * @category List
  5041. * @sig Number -> a -> [a] -> [a]
  5042. * @param {Number} index The position to insert the element
  5043. * @param {*} elt The element to insert into the Array
  5044. * @param {Array} list The list to insert into
  5045. * @return {Array} A new Array with `elt` inserted at `index`.
  5046. * @example
  5047. *
  5048. * R.insert(2, 'x', [1,2,3,4]); //=> [1,2,'x',3,4]
  5049. */
  5050. var insert = _curry3(function insert(idx, elt, list) {
  5051. idx = idx < list.length && idx >= 0 ? idx : list.length;
  5052. var result = Array.prototype.slice.call(list, 0);
  5053. result.splice(idx, 0, elt);
  5054. return result;
  5055. });
  5056. /**
  5057. * Inserts the sub-list into the list, at the specified `index`. _Note that this is not
  5058. * destructive_: it returns a copy of the list with the changes.
  5059. * <small>No lists have been harmed in the application of this function.</small>
  5060. *
  5061. * @func
  5062. * @memberOf R
  5063. * @since v0.9.0
  5064. * @category List
  5065. * @sig Number -> [a] -> [a] -> [a]
  5066. * @param {Number} index The position to insert the sub-list
  5067. * @param {Array} elts The sub-list to insert into the Array
  5068. * @param {Array} list The list to insert the sub-list into
  5069. * @return {Array} A new Array with `elts` inserted starting at `index`.
  5070. * @example
  5071. *
  5072. * R.insertAll(2, ['x','y','z'], [1,2,3,4]); //=> [1,2,'x','y','z',3,4]
  5073. */
  5074. var insertAll = _curry3(function insertAll(idx, elts, list) {
  5075. idx = idx < list.length && idx >= 0 ? idx : list.length;
  5076. return [].concat(Array.prototype.slice.call(list, 0, idx),
  5077. elts,
  5078. Array.prototype.slice.call(list, idx));
  5079. });
  5080. /**
  5081. * Returns a new list containing only one copy of each element in the original
  5082. * list, based upon the value returned by applying the supplied function to
  5083. * each list element. Prefers the first item if the supplied function produces
  5084. * the same value on two items. [`R.equals`](#equals) is used for comparison.
  5085. *
  5086. * @func
  5087. * @memberOf R
  5088. * @since v0.16.0
  5089. * @category List
  5090. * @sig (a -> b) -> [a] -> [a]
  5091. * @param {Function} fn A function used to produce a value to use during comparisons.
  5092. * @param {Array} list The array to consider.
  5093. * @return {Array} The list of unique items.
  5094. * @example
  5095. *
  5096. * R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]); //=> [-1, -5, 2, 10]
  5097. */
  5098. var uniqBy = _curry2(function uniqBy(fn, list) {
  5099. var set = new _Set();
  5100. var result = [];
  5101. var idx = 0;
  5102. var appliedItem, item;
  5103. while (idx < list.length) {
  5104. item = list[idx];
  5105. appliedItem = fn(item);
  5106. if (set.add(appliedItem)) {
  5107. result.push(item);
  5108. }
  5109. idx += 1;
  5110. }
  5111. return result;
  5112. });
  5113. /**
  5114. * Returns a new list containing only one copy of each element in the original
  5115. * list. [`R.equals`](#equals) is used to determine equality.
  5116. *
  5117. * @func
  5118. * @memberOf R
  5119. * @since v0.1.0
  5120. * @category List
  5121. * @sig [a] -> [a]
  5122. * @param {Array} list The array to consider.
  5123. * @return {Array} The list of unique items.
  5124. * @example
  5125. *
  5126. * R.uniq([1, 1, 2, 1]); //=> [1, 2]
  5127. * R.uniq([1, '1']); //=> [1, '1']
  5128. * R.uniq([[42], [42]]); //=> [[42]]
  5129. */
  5130. var uniq = uniqBy(identity);
  5131. /**
  5132. * Combines two lists into a set (i.e. no duplicates) composed of those
  5133. * elements common to both lists.
  5134. *
  5135. * @func
  5136. * @memberOf R
  5137. * @since v0.1.0
  5138. * @category Relation
  5139. * @sig [*] -> [*] -> [*]
  5140. * @param {Array} list1 The first list.
  5141. * @param {Array} list2 The second list.
  5142. * @return {Array} The list of elements found in both `list1` and `list2`.
  5143. * @see R.innerJoin
  5144. * @example
  5145. *
  5146. * R.intersection([1,2,3,4], [7,6,5,4,3]); //=> [4, 3]
  5147. */
  5148. var intersection = _curry2(function intersection(list1, list2) {
  5149. var lookupList, filteredList;
  5150. if (list1.length > list2.length) {
  5151. lookupList = list1;
  5152. filteredList = list2;
  5153. } else {
  5154. lookupList = list2;
  5155. filteredList = list1;
  5156. }
  5157. return uniq(_filter(flip(_includes)(lookupList), filteredList));
  5158. });
  5159. /**
  5160. * Creates a new list with the separator interposed between elements.
  5161. *
  5162. * Dispatches to the `intersperse` method of the second argument, if present.
  5163. *
  5164. * @func
  5165. * @memberOf R
  5166. * @since v0.14.0
  5167. * @category List
  5168. * @sig a -> [a] -> [a]
  5169. * @param {*} separator The element to add to the list.
  5170. * @param {Array} list The list to be interposed.
  5171. * @return {Array} The new list.
  5172. * @example
  5173. *
  5174. * R.intersperse('a', ['b', 'n', 'n', 's']); //=> ['b', 'a', 'n', 'a', 'n', 'a', 's']
  5175. */
  5176. var intersperse = _curry2(_checkForMethod('intersperse', function intersperse(separator, list) {
  5177. var out = [];
  5178. var idx = 0;
  5179. var length = list.length;
  5180. while (idx < length) {
  5181. if (idx === length - 1) {
  5182. out.push(list[idx]);
  5183. } else {
  5184. out.push(list[idx], separator);
  5185. }
  5186. idx += 1;
  5187. }
  5188. return out;
  5189. }));
  5190. // Based on https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
  5191. function _objectAssign(target) {
  5192. if (target == null) {
  5193. throw new TypeError('Cannot convert undefined or null to object');
  5194. }
  5195. var output = Object(target);
  5196. var idx = 1;
  5197. var length = arguments.length;
  5198. while (idx < length) {
  5199. var source = arguments[idx];
  5200. if (source != null) {
  5201. for (var nextKey in source) {
  5202. if (_has(nextKey, source)) {
  5203. output[nextKey] = source[nextKey];
  5204. }
  5205. }
  5206. }
  5207. idx += 1;
  5208. }
  5209. return output;
  5210. }
  5211. var _objectAssign$1 = typeof Object.assign === 'function' ? Object.assign : _objectAssign;
  5212. /**
  5213. * Creates an object containing a single key:value pair.
  5214. *
  5215. * @func
  5216. * @memberOf R
  5217. * @since v0.18.0
  5218. * @category Object
  5219. * @sig String -> a -> {String:a}
  5220. * @param {String} key
  5221. * @param {*} val
  5222. * @return {Object}
  5223. * @see R.pair
  5224. * @example
  5225. *
  5226. * const matchPhrases = R.compose(
  5227. * R.objOf('must'),
  5228. * R.map(R.objOf('match_phrase'))
  5229. * );
  5230. * matchPhrases(['foo', 'bar', 'baz']); //=> {must: [{match_phrase: 'foo'}, {match_phrase: 'bar'}, {match_phrase: 'baz'}]}
  5231. */
  5232. var objOf = _curry2(function objOf(key, val) {
  5233. var obj = {};
  5234. obj[key] = val;
  5235. return obj;
  5236. });
  5237. var _stepCatArray = {
  5238. '@@transducer/init': Array,
  5239. '@@transducer/step': function(xs, x) {
  5240. xs.push(x);
  5241. return xs;
  5242. },
  5243. '@@transducer/result': _identity
  5244. };
  5245. var _stepCatString = {
  5246. '@@transducer/init': String,
  5247. '@@transducer/step': function(a, b) { return a + b; },
  5248. '@@transducer/result': _identity
  5249. };
  5250. var _stepCatObject = {
  5251. '@@transducer/init': Object,
  5252. '@@transducer/step': function(result, input) {
  5253. return _objectAssign$1(
  5254. result,
  5255. _isArrayLike(input) ? objOf(input[0], input[1]) : input
  5256. );
  5257. },
  5258. '@@transducer/result': _identity
  5259. };
  5260. function _stepCat(obj) {
  5261. if (_isTransformer(obj)) {
  5262. return obj;
  5263. }
  5264. if (_isArrayLike(obj)) {
  5265. return _stepCatArray;
  5266. }
  5267. if (typeof obj === 'string') {
  5268. return _stepCatString;
  5269. }
  5270. if (typeof obj === 'object') {
  5271. return _stepCatObject;
  5272. }
  5273. throw new Error('Cannot create transformer for ' + obj);
  5274. }
  5275. /**
  5276. * Transforms the items of the list with the transducer and appends the
  5277. * transformed items to the accumulator using an appropriate iterator function
  5278. * based on the accumulator type.
  5279. *
  5280. * The accumulator can be an array, string, object or a transformer. Iterated
  5281. * items will be appended to arrays and concatenated to strings. Objects will
  5282. * be merged directly or 2-item arrays will be merged as key, value pairs.
  5283. *
  5284. * The accumulator can also be a transformer object that provides a 2-arity
  5285. * reducing iterator function, step, 0-arity initial value function, init, and
  5286. * 1-arity result extraction function result. The step function is used as the
  5287. * iterator function in reduce. The result function is used to convert the
  5288. * final accumulator into the return type and in most cases is R.identity. The
  5289. * init function is used to provide the initial accumulator.
  5290. *
  5291. * The iteration is performed with [`R.reduce`](#reduce) after initializing the
  5292. * transducer.
  5293. *
  5294. * @func
  5295. * @memberOf R
  5296. * @since v0.12.0
  5297. * @category List
  5298. * @sig a -> (b -> b) -> [c] -> a
  5299. * @param {*} acc The initial accumulator value.
  5300. * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
  5301. * @param {Array} list The list to iterate over.
  5302. * @return {*} The final, accumulated value.
  5303. * @see R.transduce
  5304. * @example
  5305. *
  5306. * const numbers = [1, 2, 3, 4];
  5307. * const transducer = R.compose(R.map(R.add(1)), R.take(2));
  5308. *
  5309. * R.into([], transducer, numbers); //=> [2, 3]
  5310. *
  5311. * const intoArray = R.into([]);
  5312. * intoArray(transducer, numbers); //=> [2, 3]
  5313. */
  5314. var into = _curry3(function into(acc, xf, list) {
  5315. return _isTransformer(acc) ?
  5316. _reduce(xf(acc), acc['@@transducer/init'](), list) :
  5317. _reduce(xf(_stepCat(acc)), _clone(acc, [], [], false), list);
  5318. });
  5319. /**
  5320. * Same as [`R.invertObj`](#invertObj), however this accounts for objects with
  5321. * duplicate values by putting the values into an array.
  5322. *
  5323. * @func
  5324. * @memberOf R
  5325. * @since v0.9.0
  5326. * @category Object
  5327. * @sig {s: x} -> {x: [ s, ... ]}
  5328. * @param {Object} obj The object or array to invert
  5329. * @return {Object} out A new object with keys in an array.
  5330. * @see R.invertObj
  5331. * @example
  5332. *
  5333. * const raceResultsByFirstName = {
  5334. * first: 'alice',
  5335. * second: 'jake',
  5336. * third: 'alice',
  5337. * };
  5338. * R.invert(raceResultsByFirstName);
  5339. * //=> { 'alice': ['first', 'third'], 'jake':['second'] }
  5340. */
  5341. var invert = _curry1(function invert(obj) {
  5342. var props = keys(obj);
  5343. var len = props.length;
  5344. var idx = 0;
  5345. var out = {};
  5346. while (idx < len) {
  5347. var key = props[idx];
  5348. var val = obj[key];
  5349. var list = _has(val, out) ? out[val] : (out[val] = []);
  5350. list[list.length] = key;
  5351. idx += 1;
  5352. }
  5353. return out;
  5354. });
  5355. /**
  5356. * Returns a new object with the keys of the given object as values, and the
  5357. * values of the given object, which are coerced to strings, as keys. Note
  5358. * that the last key found is preferred when handling the same value.
  5359. *
  5360. * @func
  5361. * @memberOf R
  5362. * @since v0.9.0
  5363. * @category Object
  5364. * @sig {s: x} -> {x: s}
  5365. * @param {Object} obj The object or array to invert
  5366. * @return {Object} out A new object
  5367. * @see R.invert
  5368. * @example
  5369. *
  5370. * const raceResults = {
  5371. * first: 'alice',
  5372. * second: 'jake'
  5373. * };
  5374. * R.invertObj(raceResults);
  5375. * //=> { 'alice': 'first', 'jake':'second' }
  5376. *
  5377. * // Alternatively:
  5378. * const raceResults = ['alice', 'jake'];
  5379. * R.invertObj(raceResults);
  5380. * //=> { 'alice': '0', 'jake':'1' }
  5381. */
  5382. var invertObj = _curry1(function invertObj(obj) {
  5383. var props = keys(obj);
  5384. var len = props.length;
  5385. var idx = 0;
  5386. var out = {};
  5387. while (idx < len) {
  5388. var key = props[idx];
  5389. out[obj[key]] = key;
  5390. idx += 1;
  5391. }
  5392. return out;
  5393. });
  5394. /**
  5395. * Turns a named method with a specified arity into a function that can be
  5396. * called directly supplied with arguments and a target object.
  5397. *
  5398. * The returned function is curried and accepts `arity + 1` parameters where
  5399. * the final parameter is the target object.
  5400. *
  5401. * @func
  5402. * @memberOf R
  5403. * @since v0.1.0
  5404. * @category Function
  5405. * @sig Number -> String -> (a -> b -> ... -> n -> Object -> *)
  5406. * @param {Number} arity Number of arguments the returned function should take
  5407. * before the target object.
  5408. * @param {String} method Name of the method to call.
  5409. * @return {Function} A new curried function.
  5410. * @see R.construct
  5411. * @example
  5412. *
  5413. * const sliceFrom = R.invoker(1, 'slice');
  5414. * sliceFrom(6, 'abcdefghijklm'); //=> 'ghijklm'
  5415. * const sliceFrom6 = R.invoker(2, 'slice')(6);
  5416. * sliceFrom6(8, 'abcdefghijklm'); //=> 'gh'
  5417. * @symb R.invoker(0, 'method')(o) = o['method']()
  5418. * @symb R.invoker(1, 'method')(a, o) = o['method'](a)
  5419. * @symb R.invoker(2, 'method')(a, b, o) = o['method'](a, b)
  5420. */
  5421. var invoker = _curry2(function invoker(arity, method) {
  5422. return curryN(arity + 1, function() {
  5423. var target = arguments[arity];
  5424. if (target != null && _isFunction(target[method])) {
  5425. return target[method].apply(target, Array.prototype.slice.call(arguments, 0, arity));
  5426. }
  5427. throw new TypeError(toString$1(target) + ' does not have a method named "' + method + '"');
  5428. });
  5429. });
  5430. /**
  5431. * See if an object (`val`) is an instance of the supplied constructor. This
  5432. * function will check up the inheritance chain, if any.
  5433. *
  5434. * @func
  5435. * @memberOf R
  5436. * @since v0.3.0
  5437. * @category Type
  5438. * @sig (* -> {*}) -> a -> Boolean
  5439. * @param {Object} ctor A constructor
  5440. * @param {*} val The value to test
  5441. * @return {Boolean}
  5442. * @example
  5443. *
  5444. * R.is(Object, {}); //=> true
  5445. * R.is(Number, 1); //=> true
  5446. * R.is(Object, 1); //=> false
  5447. * R.is(String, 's'); //=> true
  5448. * R.is(String, new String('')); //=> true
  5449. * R.is(Object, new String('')); //=> true
  5450. * R.is(Object, 's'); //=> false
  5451. * R.is(Number, {}); //=> false
  5452. */
  5453. var is = _curry2(function is(Ctor, val) {
  5454. return val != null && val.constructor === Ctor || val instanceof Ctor;
  5455. });
  5456. /**
  5457. * Returns `true` if the given value is its type's empty value; `false`
  5458. * otherwise.
  5459. *
  5460. * @func
  5461. * @memberOf R
  5462. * @since v0.1.0
  5463. * @category Logic
  5464. * @sig a -> Boolean
  5465. * @param {*} x
  5466. * @return {Boolean}
  5467. * @see R.empty
  5468. * @example
  5469. *
  5470. * R.isEmpty([1, 2, 3]); //=> false
  5471. * R.isEmpty([]); //=> true
  5472. * R.isEmpty(''); //=> true
  5473. * R.isEmpty(null); //=> false
  5474. * R.isEmpty({}); //=> true
  5475. * R.isEmpty({length: 0}); //=> false
  5476. */
  5477. var isEmpty = _curry1(function isEmpty(x) {
  5478. return x != null && equals(x, empty(x));
  5479. });
  5480. /**
  5481. * Returns a string made by inserting the `separator` between each element and
  5482. * concatenating all the elements into a single string.
  5483. *
  5484. * @func
  5485. * @memberOf R
  5486. * @since v0.1.0
  5487. * @category List
  5488. * @sig String -> [a] -> String
  5489. * @param {Number|String} separator The string used to separate the elements.
  5490. * @param {Array} xs The elements to join into a string.
  5491. * @return {String} str The string made by concatenating `xs` with `separator`.
  5492. * @see R.split
  5493. * @example
  5494. *
  5495. * const spacer = R.join(' ');
  5496. * spacer(['a', 2, 3.4]); //=> 'a 2 3.4'
  5497. * R.join('|', [1, 2, 3]); //=> '1|2|3'
  5498. */
  5499. var join = invoker(1, 'join');
  5500. /**
  5501. * juxt applies a list of functions to a list of values.
  5502. *
  5503. * @func
  5504. * @memberOf R
  5505. * @since v0.19.0
  5506. * @category Function
  5507. * @sig [(a, b, ..., m) -> n] -> ((a, b, ..., m) -> [n])
  5508. * @param {Array} fns An array of functions
  5509. * @return {Function} A function that returns a list of values after applying each of the original `fns` to its parameters.
  5510. * @see R.applySpec
  5511. * @example
  5512. *
  5513. * const getRange = R.juxt([Math.min, Math.max]);
  5514. * getRange(3, 4, 9, -3); //=> [-3, 9]
  5515. * @symb R.juxt([f, g, h])(a, b) = [f(a, b), g(a, b), h(a, b)]
  5516. */
  5517. var juxt = _curry1(function juxt(fns) {
  5518. return converge(function() { return Array.prototype.slice.call(arguments, 0); }, fns);
  5519. });
  5520. /**
  5521. * Returns a list containing the names of all the properties of the supplied
  5522. * object, including prototype properties.
  5523. * Note that the order of the output array is not guaranteed to be consistent
  5524. * across different JS platforms.
  5525. *
  5526. * @func
  5527. * @memberOf R
  5528. * @since v0.2.0
  5529. * @category Object
  5530. * @sig {k: v} -> [k]
  5531. * @param {Object} obj The object to extract properties from
  5532. * @return {Array} An array of the object's own and prototype properties.
  5533. * @see R.keys, R.valuesIn
  5534. * @example
  5535. *
  5536. * const F = function() { this.x = 'X'; };
  5537. * F.prototype.y = 'Y';
  5538. * const f = new F();
  5539. * R.keysIn(f); //=> ['x', 'y']
  5540. */
  5541. var keysIn = _curry1(function keysIn(obj) {
  5542. var prop;
  5543. var ks = [];
  5544. for (prop in obj) {
  5545. ks[ks.length] = prop;
  5546. }
  5547. return ks;
  5548. });
  5549. /**
  5550. * Returns the position of the last occurrence of an item in an array, or -1 if
  5551. * the item is not included in the array. [`R.equals`](#equals) is used to
  5552. * determine equality.
  5553. *
  5554. * @func
  5555. * @memberOf R
  5556. * @since v0.1.0
  5557. * @category List
  5558. * @sig a -> [a] -> Number
  5559. * @param {*} target The item to find.
  5560. * @param {Array} xs The array to search in.
  5561. * @return {Number} the index of the target, or -1 if the target is not found.
  5562. * @see R.indexOf
  5563. * @example
  5564. *
  5565. * R.lastIndexOf(3, [-1,3,3,0,1,2,3,4]); //=> 6
  5566. * R.lastIndexOf(10, [1,2,3,4]); //=> -1
  5567. */
  5568. var lastIndexOf = _curry2(function lastIndexOf(target, xs) {
  5569. if (typeof xs.lastIndexOf === 'function' && !_isArray(xs)) {
  5570. return xs.lastIndexOf(target);
  5571. } else {
  5572. var idx = xs.length - 1;
  5573. while (idx >= 0) {
  5574. if (equals(xs[idx], target)) {
  5575. return idx;
  5576. }
  5577. idx -= 1;
  5578. }
  5579. return -1;
  5580. }
  5581. });
  5582. function _isNumber(x) {
  5583. return Object.prototype.toString.call(x) === '[object Number]';
  5584. }
  5585. /**
  5586. * Returns the number of elements in the array by returning `list.length`.
  5587. *
  5588. * @func
  5589. * @memberOf R
  5590. * @since v0.3.0
  5591. * @category List
  5592. * @sig [a] -> Number
  5593. * @param {Array} list The array to inspect.
  5594. * @return {Number} The length of the array.
  5595. * @example
  5596. *
  5597. * R.length([]); //=> 0
  5598. * R.length([1, 2, 3]); //=> 3
  5599. */
  5600. var length = _curry1(function length(list) {
  5601. return list != null && _isNumber(list.length) ? list.length : NaN;
  5602. });
  5603. /**
  5604. * Returns a lens for the given getter and setter functions. The getter "gets"
  5605. * the value of the focus; the setter "sets" the value of the focus. The setter
  5606. * should not mutate the data structure.
  5607. *
  5608. * @func
  5609. * @memberOf R
  5610. * @since v0.8.0
  5611. * @category Object
  5612. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  5613. * @sig (s -> a) -> ((a, s) -> s) -> Lens s a
  5614. * @param {Function} getter
  5615. * @param {Function} setter
  5616. * @return {Lens}
  5617. * @see R.view, R.set, R.over, R.lensIndex, R.lensProp
  5618. * @example
  5619. *
  5620. * const xLens = R.lens(R.prop('x'), R.assoc('x'));
  5621. *
  5622. * R.view(xLens, {x: 1, y: 2}); //=> 1
  5623. * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
  5624. * R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
  5625. */
  5626. var lens = _curry2(function lens(getter, setter) {
  5627. return function(toFunctorFn) {
  5628. return function(target) {
  5629. return map(
  5630. function(focus) {
  5631. return setter(focus, target);
  5632. },
  5633. toFunctorFn(getter(target))
  5634. );
  5635. };
  5636. };
  5637. });
  5638. /**
  5639. * Returns a lens whose focus is the specified index.
  5640. *
  5641. * @func
  5642. * @memberOf R
  5643. * @since v0.14.0
  5644. * @category Object
  5645. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  5646. * @sig Number -> Lens s a
  5647. * @param {Number} n
  5648. * @return {Lens}
  5649. * @see R.view, R.set, R.over
  5650. * @example
  5651. *
  5652. * const headLens = R.lensIndex(0);
  5653. *
  5654. * R.view(headLens, ['a', 'b', 'c']); //=> 'a'
  5655. * R.set(headLens, 'x', ['a', 'b', 'c']); //=> ['x', 'b', 'c']
  5656. * R.over(headLens, R.toUpper, ['a', 'b', 'c']); //=> ['A', 'b', 'c']
  5657. */
  5658. var lensIndex = _curry1(function lensIndex(n) {
  5659. return lens(nth(n), update(n));
  5660. });
  5661. /**
  5662. * Returns a lens whose focus is the specified path.
  5663. *
  5664. * @func
  5665. * @memberOf R
  5666. * @since v0.19.0
  5667. * @category Object
  5668. * @typedefn Idx = String | Int
  5669. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  5670. * @sig [Idx] -> Lens s a
  5671. * @param {Array} path The path to use.
  5672. * @return {Lens}
  5673. * @see R.view, R.set, R.over
  5674. * @example
  5675. *
  5676. * const xHeadYLens = R.lensPath(['x', 0, 'y']);
  5677. *
  5678. * R.view(xHeadYLens, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  5679. * //=> 2
  5680. * R.set(xHeadYLens, 1, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  5681. * //=> {x: [{y: 1, z: 3}, {y: 4, z: 5}]}
  5682. * R.over(xHeadYLens, R.negate, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  5683. * //=> {x: [{y: -2, z: 3}, {y: 4, z: 5}]}
  5684. */
  5685. var lensPath = _curry1(function lensPath(p) {
  5686. return lens(path(p), assocPath(p));
  5687. });
  5688. /**
  5689. * Returns a lens whose focus is the specified property.
  5690. *
  5691. * @func
  5692. * @memberOf R
  5693. * @since v0.14.0
  5694. * @category Object
  5695. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  5696. * @sig String -> Lens s a
  5697. * @param {String} k
  5698. * @return {Lens}
  5699. * @see R.view, R.set, R.over
  5700. * @example
  5701. *
  5702. * const xLens = R.lensProp('x');
  5703. *
  5704. * R.view(xLens, {x: 1, y: 2}); //=> 1
  5705. * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
  5706. * R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
  5707. */
  5708. var lensProp = _curry1(function lensProp(k) {
  5709. return lens(prop(k), assoc(k));
  5710. });
  5711. /**
  5712. * Returns `true` if the first argument is less than the second; `false`
  5713. * otherwise.
  5714. *
  5715. * @func
  5716. * @memberOf R
  5717. * @since v0.1.0
  5718. * @category Relation
  5719. * @sig Ord a => a -> a -> Boolean
  5720. * @param {*} a
  5721. * @param {*} b
  5722. * @return {Boolean}
  5723. * @see R.gt
  5724. * @example
  5725. *
  5726. * R.lt(2, 1); //=> false
  5727. * R.lt(2, 2); //=> false
  5728. * R.lt(2, 3); //=> true
  5729. * R.lt('a', 'z'); //=> true
  5730. * R.lt('z', 'a'); //=> false
  5731. */
  5732. var lt = _curry2(function lt(a, b) { return a < b; });
  5733. /**
  5734. * Returns `true` if the first argument is less than or equal to the second;
  5735. * `false` otherwise.
  5736. *
  5737. * @func
  5738. * @memberOf R
  5739. * @since v0.1.0
  5740. * @category Relation
  5741. * @sig Ord a => a -> a -> Boolean
  5742. * @param {Number} a
  5743. * @param {Number} b
  5744. * @return {Boolean}
  5745. * @see R.gte
  5746. * @example
  5747. *
  5748. * R.lte(2, 1); //=> false
  5749. * R.lte(2, 2); //=> true
  5750. * R.lte(2, 3); //=> true
  5751. * R.lte('a', 'z'); //=> true
  5752. * R.lte('z', 'a'); //=> false
  5753. */
  5754. var lte = _curry2(function lte(a, b) { return a <= b; });
  5755. /**
  5756. * The `mapAccum` function behaves like a combination of map and reduce; it
  5757. * applies a function to each element of a list, passing an accumulating
  5758. * parameter from left to right, and returning a final value of this
  5759. * accumulator together with the new list.
  5760. *
  5761. * The iterator function receives two arguments, *acc* and *value*, and should
  5762. * return a tuple *[acc, value]*.
  5763. *
  5764. * @func
  5765. * @memberOf R
  5766. * @since v0.10.0
  5767. * @category List
  5768. * @sig ((acc, x) -> (acc, y)) -> acc -> [x] -> (acc, [y])
  5769. * @param {Function} fn The function to be called on every element of the input `list`.
  5770. * @param {*} acc The accumulator value.
  5771. * @param {Array} list The list to iterate over.
  5772. * @return {*} The final, accumulated value.
  5773. * @see R.scan, R.addIndex, R.mapAccumRight
  5774. * @example
  5775. *
  5776. * const digits = ['1', '2', '3', '4'];
  5777. * const appender = (a, b) => [a + b, a + b];
  5778. *
  5779. * R.mapAccum(appender, 0, digits); //=> ['01234', ['01', '012', '0123', '01234']]
  5780. * @symb R.mapAccum(f, a, [b, c, d]) = [
  5781. * f(f(f(a, b)[0], c)[0], d)[0],
  5782. * [
  5783. * f(a, b)[1],
  5784. * f(f(a, b)[0], c)[1],
  5785. * f(f(f(a, b)[0], c)[0], d)[1]
  5786. * ]
  5787. * ]
  5788. */
  5789. var mapAccum = _curry3(function mapAccum(fn, acc, list) {
  5790. var idx = 0;
  5791. var len = list.length;
  5792. var result = [];
  5793. var tuple = [acc];
  5794. while (idx < len) {
  5795. tuple = fn(tuple[0], list[idx]);
  5796. result[idx] = tuple[1];
  5797. idx += 1;
  5798. }
  5799. return [tuple[0], result];
  5800. });
  5801. /**
  5802. * The `mapAccumRight` function behaves like a combination of map and reduce; it
  5803. * applies a function to each element of a list, passing an accumulating
  5804. * parameter from right to left, and returning a final value of this
  5805. * accumulator together with the new list.
  5806. *
  5807. * Similar to [`mapAccum`](#mapAccum), except moves through the input list from
  5808. * the right to the left.
  5809. *
  5810. * The iterator function receives two arguments, *acc* and *value*, and should
  5811. * return a tuple *[acc, value]*.
  5812. *
  5813. * @func
  5814. * @memberOf R
  5815. * @since v0.10.0
  5816. * @category List
  5817. * @sig ((acc, x) -> (acc, y)) -> acc -> [x] -> (acc, [y])
  5818. * @param {Function} fn The function to be called on every element of the input `list`.
  5819. * @param {*} acc The accumulator value.
  5820. * @param {Array} list The list to iterate over.
  5821. * @return {*} The final, accumulated value.
  5822. * @see R.addIndex, R.mapAccum
  5823. * @example
  5824. *
  5825. * const digits = ['1', '2', '3', '4'];
  5826. * const appender = (a, b) => [b + a, b + a];
  5827. *
  5828. * R.mapAccumRight(appender, 5, digits); //=> ['12345', ['12345', '2345', '345', '45']]
  5829. * @symb R.mapAccumRight(f, a, [b, c, d]) = [
  5830. * f(f(f(a, d)[0], c)[0], b)[0],
  5831. * [
  5832. * f(a, d)[1],
  5833. * f(f(a, d)[0], c)[1],
  5834. * f(f(f(a, d)[0], c)[0], b)[1]
  5835. * ]
  5836. * ]
  5837. */
  5838. var mapAccumRight = _curry3(function mapAccumRight(fn, acc, list) {
  5839. var idx = list.length - 1;
  5840. var result = [];
  5841. var tuple = [acc];
  5842. while (idx >= 0) {
  5843. tuple = fn(tuple[0], list[idx]);
  5844. result[idx] = tuple[1];
  5845. idx -= 1;
  5846. }
  5847. return [tuple[0], result];
  5848. });
  5849. /**
  5850. * An Object-specific version of [`map`](#map). The function is applied to three
  5851. * arguments: *(value, key, obj)*. If only the value is significant, use
  5852. * [`map`](#map) instead.
  5853. *
  5854. * @func
  5855. * @memberOf R
  5856. * @since v0.9.0
  5857. * @category Object
  5858. * @sig ((*, String, Object) -> *) -> Object -> Object
  5859. * @param {Function} fn
  5860. * @param {Object} obj
  5861. * @return {Object}
  5862. * @see R.map
  5863. * @example
  5864. *
  5865. * const xyz = { x: 1, y: 2, z: 3 };
  5866. * const prependKeyAndDouble = (num, key, obj) => key + (num * 2);
  5867. *
  5868. * R.mapObjIndexed(prependKeyAndDouble, xyz); //=> { x: 'x2', y: 'y4', z: 'z6' }
  5869. */
  5870. var mapObjIndexed = _curry2(function mapObjIndexed(fn, obj) {
  5871. return _reduce(function(acc, key) {
  5872. acc[key] = fn(obj[key], key, obj);
  5873. return acc;
  5874. }, {}, keys(obj));
  5875. });
  5876. /**
  5877. * Tests a regular expression against a String. Note that this function will
  5878. * return an empty array when there are no matches. This differs from
  5879. * [`String.prototype.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)
  5880. * which returns `null` when there are no matches.
  5881. *
  5882. * @func
  5883. * @memberOf R
  5884. * @since v0.1.0
  5885. * @category String
  5886. * @sig RegExp -> String -> [String | Undefined]
  5887. * @param {RegExp} rx A regular expression.
  5888. * @param {String} str The string to match against
  5889. * @return {Array} The list of matches or empty array.
  5890. * @see R.test
  5891. * @example
  5892. *
  5893. * R.match(/([a-z]a)/g, 'bananas'); //=> ['ba', 'na', 'na']
  5894. * R.match(/a/, 'b'); //=> []
  5895. * R.match(/a/, null); //=> TypeError: null does not have a method named "match"
  5896. */
  5897. var match = _curry2(function match(rx, str) {
  5898. return str.match(rx) || [];
  5899. });
  5900. /**
  5901. * `mathMod` behaves like the modulo operator should mathematically, unlike the
  5902. * `%` operator (and by extension, [`R.modulo`](#modulo)). So while
  5903. * `-17 % 5` is `-2`, `mathMod(-17, 5)` is `3`. `mathMod` requires Integer
  5904. * arguments, and returns NaN when the modulus is zero or negative.
  5905. *
  5906. * @func
  5907. * @memberOf R
  5908. * @since v0.3.0
  5909. * @category Math
  5910. * @sig Number -> Number -> Number
  5911. * @param {Number} m The dividend.
  5912. * @param {Number} p the modulus.
  5913. * @return {Number} The result of `b mod a`.
  5914. * @see R.modulo
  5915. * @example
  5916. *
  5917. * R.mathMod(-17, 5); //=> 3
  5918. * R.mathMod(17, 5); //=> 2
  5919. * R.mathMod(17, -5); //=> NaN
  5920. * R.mathMod(17, 0); //=> NaN
  5921. * R.mathMod(17.2, 5); //=> NaN
  5922. * R.mathMod(17, 5.3); //=> NaN
  5923. *
  5924. * const clock = R.mathMod(R.__, 12);
  5925. * clock(15); //=> 3
  5926. * clock(24); //=> 0
  5927. *
  5928. * const seventeenMod = R.mathMod(17);
  5929. * seventeenMod(3); //=> 2
  5930. * seventeenMod(4); //=> 1
  5931. * seventeenMod(10); //=> 7
  5932. */
  5933. var mathMod = _curry2(function mathMod(m, p) {
  5934. if (!_isInteger(m)) { return NaN; }
  5935. if (!_isInteger(p) || p < 1) { return NaN; }
  5936. return ((m % p) + p) % p;
  5937. });
  5938. /**
  5939. * Takes a function and two values, and returns whichever value produces the
  5940. * larger result when passed to the provided function.
  5941. *
  5942. * @func
  5943. * @memberOf R
  5944. * @since v0.8.0
  5945. * @category Relation
  5946. * @sig Ord b => (a -> b) -> a -> a -> a
  5947. * @param {Function} f
  5948. * @param {*} a
  5949. * @param {*} b
  5950. * @return {*}
  5951. * @see R.max, R.minBy
  5952. * @example
  5953. *
  5954. * // square :: Number -> Number
  5955. * const square = n => n * n;
  5956. *
  5957. * R.maxBy(square, -3, 2); //=> -3
  5958. *
  5959. * R.reduce(R.maxBy(square), 0, [3, -5, 4, 1, -2]); //=> -5
  5960. * R.reduce(R.maxBy(square), 0, []); //=> 0
  5961. */
  5962. var maxBy = _curry3(function maxBy(f, a, b) {
  5963. return f(b) > f(a) ? b : a;
  5964. });
  5965. /**
  5966. * Adds together all the elements of a list.
  5967. *
  5968. * @func
  5969. * @memberOf R
  5970. * @since v0.1.0
  5971. * @category Math
  5972. * @sig [Number] -> Number
  5973. * @param {Array} list An array of numbers
  5974. * @return {Number} The sum of all the numbers in the list.
  5975. * @see R.reduce
  5976. * @example
  5977. *
  5978. * R.sum([2,4,6,8,100,1]); //=> 121
  5979. */
  5980. var sum = reduce(add, 0);
  5981. /**
  5982. * Returns the mean of the given list of numbers.
  5983. *
  5984. * @func
  5985. * @memberOf R
  5986. * @since v0.14.0
  5987. * @category Math
  5988. * @sig [Number] -> Number
  5989. * @param {Array} list
  5990. * @return {Number}
  5991. * @see R.median
  5992. * @example
  5993. *
  5994. * R.mean([2, 7, 9]); //=> 6
  5995. * R.mean([]); //=> NaN
  5996. */
  5997. var mean = _curry1(function mean(list) {
  5998. return sum(list) / list.length;
  5999. });
  6000. /**
  6001. * Returns the median of the given list of numbers.
  6002. *
  6003. * @func
  6004. * @memberOf R
  6005. * @since v0.14.0
  6006. * @category Math
  6007. * @sig [Number] -> Number
  6008. * @param {Array} list
  6009. * @return {Number}
  6010. * @see R.mean
  6011. * @example
  6012. *
  6013. * R.median([2, 9, 7]); //=> 7
  6014. * R.median([7, 2, 10, 9]); //=> 8
  6015. * R.median([]); //=> NaN
  6016. */
  6017. var median = _curry1(function median(list) {
  6018. var len = list.length;
  6019. if (len === 0) {
  6020. return NaN;
  6021. }
  6022. var width = 2 - len % 2;
  6023. var idx = (len - width) / 2;
  6024. return mean(Array.prototype.slice.call(list, 0).sort(function(a, b) {
  6025. return a < b ? -1 : a > b ? 1 : 0;
  6026. }).slice(idx, idx + width));
  6027. });
  6028. /**
  6029. * A customisable version of [`R.memoize`](#memoize). `memoizeWith` takes an
  6030. * additional function that will be applied to a given argument set and used to
  6031. * create the cache key under which the results of the function to be memoized
  6032. * will be stored. Care must be taken when implementing key generation to avoid
  6033. * clashes that may overwrite previous entries erroneously.
  6034. *
  6035. *
  6036. * @func
  6037. * @memberOf R
  6038. * @since v0.24.0
  6039. * @category Function
  6040. * @sig (*... -> String) -> (*... -> a) -> (*... -> a)
  6041. * @param {Function} fn The function to generate the cache key.
  6042. * @param {Function} fn The function to memoize.
  6043. * @return {Function} Memoized version of `fn`.
  6044. * @example
  6045. *
  6046. * let count = 0;
  6047. * const factorial = R.memoizeWith(R.identity, n => {
  6048. * count += 1;
  6049. * return R.product(R.range(1, n + 1));
  6050. * });
  6051. * factorial(5); //=> 120
  6052. * factorial(5); //=> 120
  6053. * factorial(5); //=> 120
  6054. * count; //=> 1
  6055. */
  6056. var memoizeWith = _curry2(function memoizeWith(mFn, fn) {
  6057. var cache = {};
  6058. return _arity(fn.length, function() {
  6059. var key = mFn.apply(this, arguments);
  6060. if (!_has(key, cache)) {
  6061. cache[key] = fn.apply(this, arguments);
  6062. }
  6063. return cache[key];
  6064. });
  6065. });
  6066. /**
  6067. * Create a new object with the own properties of the first object merged with
  6068. * the own properties of the second object. If a key exists in both objects,
  6069. * the value from the second object will be used.
  6070. *
  6071. * @func
  6072. * @memberOf R
  6073. * @since v0.1.0
  6074. * @category Object
  6075. * @sig {k: v} -> {k: v} -> {k: v}
  6076. * @param {Object} l
  6077. * @param {Object} r
  6078. * @return {Object}
  6079. * @see R.mergeRight, R.mergeDeepRight, R.mergeWith, R.mergeWithKey
  6080. * @deprecated
  6081. * @example
  6082. *
  6083. * R.merge({ 'name': 'fred', 'age': 10 }, { 'age': 40 });
  6084. * //=> { 'name': 'fred', 'age': 40 }
  6085. *
  6086. * const withDefaults = R.merge({x: 0, y: 0});
  6087. * withDefaults({y: 2}); //=> {x: 0, y: 2}
  6088. * @symb R.merge(a, b) = {...a, ...b}
  6089. */
  6090. var merge = _curry2(function merge(l, r) {
  6091. return _objectAssign$1({}, l, r);
  6092. });
  6093. /**
  6094. * Merges a list of objects together into one object.
  6095. *
  6096. * @func
  6097. * @memberOf R
  6098. * @since v0.10.0
  6099. * @category List
  6100. * @sig [{k: v}] -> {k: v}
  6101. * @param {Array} list An array of objects
  6102. * @return {Object} A merged object.
  6103. * @see R.reduce
  6104. * @example
  6105. *
  6106. * R.mergeAll([{foo:1},{bar:2},{baz:3}]); //=> {foo:1,bar:2,baz:3}
  6107. * R.mergeAll([{foo:1},{foo:2},{bar:2}]); //=> {foo:2,bar:2}
  6108. * @symb R.mergeAll([{ x: 1 }, { y: 2 }, { z: 3 }]) = { x: 1, y: 2, z: 3 }
  6109. */
  6110. var mergeAll = _curry1(function mergeAll(list) {
  6111. return _objectAssign$1.apply(null, [{}].concat(list));
  6112. });
  6113. /**
  6114. * Creates a new object with the own properties of the two provided objects. If
  6115. * a key exists in both objects, the provided function is applied to the key
  6116. * and the values associated with the key in each object, with the result being
  6117. * used as the value associated with the key in the returned object.
  6118. *
  6119. * @func
  6120. * @memberOf R
  6121. * @since v0.19.0
  6122. * @category Object
  6123. * @sig ((String, a, a) -> a) -> {a} -> {a} -> {a}
  6124. * @param {Function} fn
  6125. * @param {Object} l
  6126. * @param {Object} r
  6127. * @return {Object}
  6128. * @see R.mergeDeepWithKey, R.merge, R.mergeWith
  6129. * @example
  6130. *
  6131. * let concatValues = (k, l, r) => k == 'values' ? R.concat(l, r) : r
  6132. * R.mergeWithKey(concatValues,
  6133. * { a: true, thing: 'foo', values: [10, 20] },
  6134. * { b: true, thing: 'bar', values: [15, 35] });
  6135. * //=> { a: true, b: true, thing: 'bar', values: [10, 20, 15, 35] }
  6136. * @symb R.mergeWithKey(f, { x: 1, y: 2 }, { y: 5, z: 3 }) = { x: 1, y: f('y', 2, 5), z: 3 }
  6137. */
  6138. var mergeWithKey = _curry3(function mergeWithKey(fn, l, r) {
  6139. var result = {};
  6140. var k;
  6141. for (k in l) {
  6142. if (_has(k, l)) {
  6143. result[k] = _has(k, r) ? fn(k, l[k], r[k]) : l[k];
  6144. }
  6145. }
  6146. for (k in r) {
  6147. if (_has(k, r) && !(_has(k, result))) {
  6148. result[k] = r[k];
  6149. }
  6150. }
  6151. return result;
  6152. });
  6153. /**
  6154. * Creates a new object with the own properties of the two provided objects.
  6155. * If a key exists in both objects:
  6156. * - and both associated values are also objects then the values will be
  6157. * recursively merged.
  6158. * - otherwise the provided function is applied to the key and associated values
  6159. * using the resulting value as the new value associated with the key.
  6160. * If a key only exists in one object, the value will be associated with the key
  6161. * of the resulting object.
  6162. *
  6163. * @func
  6164. * @memberOf R
  6165. * @since v0.24.0
  6166. * @category Object
  6167. * @sig ((String, a, a) -> a) -> {a} -> {a} -> {a}
  6168. * @param {Function} fn
  6169. * @param {Object} lObj
  6170. * @param {Object} rObj
  6171. * @return {Object}
  6172. * @see R.mergeWithKey, R.mergeDeepWith
  6173. * @example
  6174. *
  6175. * let concatValues = (k, l, r) => k == 'values' ? R.concat(l, r) : r
  6176. * R.mergeDeepWithKey(concatValues,
  6177. * { a: true, c: { thing: 'foo', values: [10, 20] }},
  6178. * { b: true, c: { thing: 'bar', values: [15, 35] }});
  6179. * //=> { a: true, b: true, c: { thing: 'bar', values: [10, 20, 15, 35] }}
  6180. */
  6181. var mergeDeepWithKey = _curry3(function mergeDeepWithKey(fn, lObj, rObj) {
  6182. return mergeWithKey(function(k, lVal, rVal) {
  6183. if (_isObject(lVal) && _isObject(rVal)) {
  6184. return mergeDeepWithKey(fn, lVal, rVal);
  6185. } else {
  6186. return fn(k, lVal, rVal);
  6187. }
  6188. }, lObj, rObj);
  6189. });
  6190. /**
  6191. * Creates a new object with the own properties of the first object merged with
  6192. * the own properties of the second object. If a key exists in both objects:
  6193. * - and both values are objects, the two values will be recursively merged
  6194. * - otherwise the value from the first object will be used.
  6195. *
  6196. * @func
  6197. * @memberOf R
  6198. * @since v0.24.0
  6199. * @category Object
  6200. * @sig {a} -> {a} -> {a}
  6201. * @param {Object} lObj
  6202. * @param {Object} rObj
  6203. * @return {Object}
  6204. * @see R.merge, R.mergeDeepRight, R.mergeDeepWith, R.mergeDeepWithKey
  6205. * @example
  6206. *
  6207. * R.mergeDeepLeft({ name: 'fred', age: 10, contact: { email: 'moo@example.com' }},
  6208. * { age: 40, contact: { email: 'baa@example.com' }});
  6209. * //=> { name: 'fred', age: 10, contact: { email: 'moo@example.com' }}
  6210. */
  6211. var mergeDeepLeft = _curry2(function mergeDeepLeft(lObj, rObj) {
  6212. return mergeDeepWithKey(function(k, lVal, rVal) {
  6213. return lVal;
  6214. }, lObj, rObj);
  6215. });
  6216. /**
  6217. * Creates a new object with the own properties of the first object merged with
  6218. * the own properties of the second object. If a key exists in both objects:
  6219. * - and both values are objects, the two values will be recursively merged
  6220. * - otherwise the value from the second object will be used.
  6221. *
  6222. * @func
  6223. * @memberOf R
  6224. * @since v0.24.0
  6225. * @category Object
  6226. * @sig {a} -> {a} -> {a}
  6227. * @param {Object} lObj
  6228. * @param {Object} rObj
  6229. * @return {Object}
  6230. * @see R.merge, R.mergeDeepLeft, R.mergeDeepWith, R.mergeDeepWithKey
  6231. * @example
  6232. *
  6233. * R.mergeDeepRight({ name: 'fred', age: 10, contact: { email: 'moo@example.com' }},
  6234. * { age: 40, contact: { email: 'baa@example.com' }});
  6235. * //=> { name: 'fred', age: 40, contact: { email: 'baa@example.com' }}
  6236. */
  6237. var mergeDeepRight = _curry2(function mergeDeepRight(lObj, rObj) {
  6238. return mergeDeepWithKey(function(k, lVal, rVal) {
  6239. return rVal;
  6240. }, lObj, rObj);
  6241. });
  6242. /**
  6243. * Creates a new object with the own properties of the two provided objects.
  6244. * If a key exists in both objects:
  6245. * - and both associated values are also objects then the values will be
  6246. * recursively merged.
  6247. * - otherwise the provided function is applied to associated values using the
  6248. * resulting value as the new value associated with the key.
  6249. * If a key only exists in one object, the value will be associated with the key
  6250. * of the resulting object.
  6251. *
  6252. * @func
  6253. * @memberOf R
  6254. * @since v0.24.0
  6255. * @category Object
  6256. * @sig ((a, a) -> a) -> {a} -> {a} -> {a}
  6257. * @param {Function} fn
  6258. * @param {Object} lObj
  6259. * @param {Object} rObj
  6260. * @return {Object}
  6261. * @see R.mergeWith, R.mergeDeepWithKey
  6262. * @example
  6263. *
  6264. * R.mergeDeepWith(R.concat,
  6265. * { a: true, c: { values: [10, 20] }},
  6266. * { b: true, c: { values: [15, 35] }});
  6267. * //=> { a: true, b: true, c: { values: [10, 20, 15, 35] }}
  6268. */
  6269. var mergeDeepWith = _curry3(function mergeDeepWith(fn, lObj, rObj) {
  6270. return mergeDeepWithKey(function(k, lVal, rVal) {
  6271. return fn(lVal, rVal);
  6272. }, lObj, rObj);
  6273. });
  6274. /**
  6275. * Create a new object with the own properties of the first object merged with
  6276. * the own properties of the second object. If a key exists in both objects,
  6277. * the value from the first object will be used.
  6278. *
  6279. * @func
  6280. * @memberOf R
  6281. * @category Object
  6282. * @sig {k: v} -> {k: v} -> {k: v}
  6283. * @param {Object} l
  6284. * @param {Object} r
  6285. * @return {Object}
  6286. * @see R.mergeRight, R.mergeDeepLeft, R.mergeWith, R.mergeWithKey
  6287. * @example
  6288. *
  6289. * R.mergeLeft({ 'age': 40 }, { 'name': 'fred', 'age': 10 });
  6290. * //=> { 'name': 'fred', 'age': 40 }
  6291. *
  6292. * const resetToDefault = R.mergeLeft({x: 0});
  6293. * resetToDefault({x: 5, y: 2}); //=> {x: 0, y: 2}
  6294. * @symb R.mergeLeft(a, b) = {...b, ...a}
  6295. */
  6296. var mergeLeft = _curry2(function mergeLeft(l, r) {
  6297. return _objectAssign$1({}, r, l);
  6298. });
  6299. /**
  6300. * Create a new object with the own properties of the first object merged with
  6301. * the own properties of the second object. If a key exists in both objects,
  6302. * the value from the second object will be used.
  6303. *
  6304. * @func
  6305. * @memberOf R
  6306. * @category Object
  6307. * @sig {k: v} -> {k: v} -> {k: v}
  6308. * @param {Object} l
  6309. * @param {Object} r
  6310. * @return {Object}
  6311. * @see R.mergeLeft, R.mergeDeepRight, R.mergeWith, R.mergeWithKey
  6312. * @example
  6313. *
  6314. * R.mergeRight({ 'name': 'fred', 'age': 10 }, { 'age': 40 });
  6315. * //=> { 'name': 'fred', 'age': 40 }
  6316. *
  6317. * const withDefaults = R.mergeRight({x: 0, y: 0});
  6318. * withDefaults({y: 2}); //=> {x: 0, y: 2}
  6319. * @symb R.mergeRight(a, b) = {...a, ...b}
  6320. */
  6321. var mergeRight = _curry2(function mergeRight(l, r) {
  6322. return _objectAssign$1({}, l, r);
  6323. });
  6324. /**
  6325. * Creates a new object with the own properties of the two provided objects. If
  6326. * a key exists in both objects, the provided function is applied to the values
  6327. * associated with the key in each object, with the result being used as the
  6328. * value associated with the key in the returned object.
  6329. *
  6330. * @func
  6331. * @memberOf R
  6332. * @since v0.19.0
  6333. * @category Object
  6334. * @sig ((a, a) -> a) -> {a} -> {a} -> {a}
  6335. * @param {Function} fn
  6336. * @param {Object} l
  6337. * @param {Object} r
  6338. * @return {Object}
  6339. * @see R.mergeDeepWith, R.merge, R.mergeWithKey
  6340. * @example
  6341. *
  6342. * R.mergeWith(R.concat,
  6343. * { a: true, values: [10, 20] },
  6344. * { b: true, values: [15, 35] });
  6345. * //=> { a: true, b: true, values: [10, 20, 15, 35] }
  6346. */
  6347. var mergeWith = _curry3(function mergeWith(fn, l, r) {
  6348. return mergeWithKey(function(_, _l, _r) {
  6349. return fn(_l, _r);
  6350. }, l, r);
  6351. });
  6352. /**
  6353. * Returns the smaller of its two arguments.
  6354. *
  6355. * @func
  6356. * @memberOf R
  6357. * @since v0.1.0
  6358. * @category Relation
  6359. * @sig Ord a => a -> a -> a
  6360. * @param {*} a
  6361. * @param {*} b
  6362. * @return {*}
  6363. * @see R.minBy, R.max
  6364. * @example
  6365. *
  6366. * R.min(789, 123); //=> 123
  6367. * R.min('a', 'b'); //=> 'a'
  6368. */
  6369. var min = _curry2(function min(a, b) { return b < a ? b : a; });
  6370. /**
  6371. * Takes a function and two values, and returns whichever value produces the
  6372. * smaller result when passed to the provided function.
  6373. *
  6374. * @func
  6375. * @memberOf R
  6376. * @since v0.8.0
  6377. * @category Relation
  6378. * @sig Ord b => (a -> b) -> a -> a -> a
  6379. * @param {Function} f
  6380. * @param {*} a
  6381. * @param {*} b
  6382. * @return {*}
  6383. * @see R.min, R.maxBy
  6384. * @example
  6385. *
  6386. * // square :: Number -> Number
  6387. * const square = n => n * n;
  6388. *
  6389. * R.minBy(square, -3, 2); //=> 2
  6390. *
  6391. * R.reduce(R.minBy(square), Infinity, [3, -5, 4, 1, -2]); //=> 1
  6392. * R.reduce(R.minBy(square), Infinity, []); //=> Infinity
  6393. */
  6394. var minBy = _curry3(function minBy(f, a, b) {
  6395. return f(b) < f(a) ? b : a;
  6396. });
  6397. /**
  6398. * Divides the first parameter by the second and returns the remainder. Note
  6399. * that this function preserves the JavaScript-style behavior for modulo. For
  6400. * mathematical modulo see [`mathMod`](#mathMod).
  6401. *
  6402. * @func
  6403. * @memberOf R
  6404. * @since v0.1.1
  6405. * @category Math
  6406. * @sig Number -> Number -> Number
  6407. * @param {Number} a The value to the divide.
  6408. * @param {Number} b The pseudo-modulus
  6409. * @return {Number} The result of `b % a`.
  6410. * @see R.mathMod
  6411. * @example
  6412. *
  6413. * R.modulo(17, 3); //=> 2
  6414. * // JS behavior:
  6415. * R.modulo(-17, 3); //=> -2
  6416. * R.modulo(17, -3); //=> 2
  6417. *
  6418. * const isOdd = R.modulo(R.__, 2);
  6419. * isOdd(42); //=> 0
  6420. * isOdd(21); //=> 1
  6421. */
  6422. var modulo = _curry2(function modulo(a, b) { return a % b; });
  6423. /**
  6424. * Multiplies two numbers. Equivalent to `a * b` but curried.
  6425. *
  6426. * @func
  6427. * @memberOf R
  6428. * @since v0.1.0
  6429. * @category Math
  6430. * @sig Number -> Number -> Number
  6431. * @param {Number} a The first value.
  6432. * @param {Number} b The second value.
  6433. * @return {Number} The result of `a * b`.
  6434. * @see R.divide
  6435. * @example
  6436. *
  6437. * const double = R.multiply(2);
  6438. * const triple = R.multiply(3);
  6439. * double(3); //=> 6
  6440. * triple(4); //=> 12
  6441. * R.multiply(2, 5); //=> 10
  6442. */
  6443. var multiply = _curry2(function multiply(a, b) { return a * b; });
  6444. /**
  6445. * Negates its argument.
  6446. *
  6447. * @func
  6448. * @memberOf R
  6449. * @since v0.9.0
  6450. * @category Math
  6451. * @sig Number -> Number
  6452. * @param {Number} n
  6453. * @return {Number}
  6454. * @example
  6455. *
  6456. * R.negate(42); //=> -42
  6457. */
  6458. var negate = _curry1(function negate(n) { return -n; });
  6459. /**
  6460. * Returns `true` if no elements of the list match the predicate, `false`
  6461. * otherwise.
  6462. *
  6463. * Dispatches to the `all` method of the second argument, if present.
  6464. *
  6465. * Acts as a transducer if a transformer is given in list position.
  6466. *
  6467. * @func
  6468. * @memberOf R
  6469. * @since v0.12.0
  6470. * @category List
  6471. * @sig (a -> Boolean) -> [a] -> Boolean
  6472. * @param {Function} fn The predicate function.
  6473. * @param {Array} list The array to consider.
  6474. * @return {Boolean} `true` if the predicate is not satisfied by every element, `false` otherwise.
  6475. * @see R.all, R.any
  6476. * @example
  6477. *
  6478. * const isEven = n => n % 2 === 0;
  6479. * const isOdd = n => n % 2 === 1;
  6480. *
  6481. * R.none(isEven, [1, 3, 5, 7, 9, 11]); //=> true
  6482. * R.none(isOdd, [1, 3, 5, 7, 8, 11]); //=> false
  6483. */
  6484. const none = _curry2(function none(fn, input) {
  6485. return all(_complement(fn), input);
  6486. });
  6487. /**
  6488. * Returns a function which returns its nth argument.
  6489. *
  6490. * @func
  6491. * @memberOf R
  6492. * @since v0.9.0
  6493. * @category Function
  6494. * @sig Number -> *... -> *
  6495. * @param {Number} n
  6496. * @return {Function}
  6497. * @example
  6498. *
  6499. * R.nthArg(1)('a', 'b', 'c'); //=> 'b'
  6500. * R.nthArg(-1)('a', 'b', 'c'); //=> 'c'
  6501. * @symb R.nthArg(-1)(a, b, c) = c
  6502. * @symb R.nthArg(0)(a, b, c) = a
  6503. * @symb R.nthArg(1)(a, b, c) = b
  6504. */
  6505. var nthArg = _curry1(function nthArg(n) {
  6506. var arity = n < 0 ? 1 : n + 1;
  6507. return curryN(arity, function() {
  6508. return nth(n, arguments);
  6509. });
  6510. });
  6511. /**
  6512. * `o` is a curried composition function that returns a unary function.
  6513. * Like [`compose`](#compose), `o` performs right-to-left function composition.
  6514. * Unlike [`compose`](#compose), the rightmost function passed to `o` will be
  6515. * invoked with only one argument. Also, unlike [`compose`](#compose), `o` is
  6516. * limited to accepting only 2 unary functions. The name o was chosen because
  6517. * of its similarity to the mathematical composition operator ∘.
  6518. *
  6519. * @func
  6520. * @memberOf R
  6521. * @since v0.24.0
  6522. * @category Function
  6523. * @sig (b -> c) -> (a -> b) -> a -> c
  6524. * @param {Function} f
  6525. * @param {Function} g
  6526. * @return {Function}
  6527. * @see R.compose, R.pipe
  6528. * @example
  6529. *
  6530. * const classyGreeting = name => "The name's " + name.last + ", " + name.first + " " + name.last
  6531. * const yellGreeting = R.o(R.toUpper, classyGreeting);
  6532. * yellGreeting({first: 'James', last: 'Bond'}); //=> "THE NAME'S BOND, JAMES BOND"
  6533. *
  6534. * R.o(R.multiply(10), R.add(10))(-4) //=> 60
  6535. *
  6536. * @symb R.o(f, g, x) = f(g(x))
  6537. */
  6538. var o = _curry3(function o(f, g, x) {
  6539. return f(g(x));
  6540. });
  6541. function _of(x) { return [x]; }
  6542. /**
  6543. * Returns a singleton array containing the value provided.
  6544. *
  6545. * Note this `of` is different from the ES6 `of`; See
  6546. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
  6547. *
  6548. * @func
  6549. * @memberOf R
  6550. * @since v0.3.0
  6551. * @category Function
  6552. * @sig a -> [a]
  6553. * @param {*} x any value
  6554. * @return {Array} An array wrapping `x`.
  6555. * @example
  6556. *
  6557. * R.of(null); //=> [null]
  6558. * R.of([42]); //=> [[42]]
  6559. */
  6560. var of = _curry1(_of);
  6561. /**
  6562. * Returns a partial copy of an object omitting the keys specified.
  6563. *
  6564. * @func
  6565. * @memberOf R
  6566. * @since v0.1.0
  6567. * @category Object
  6568. * @sig [String] -> {String: *} -> {String: *}
  6569. * @param {Array} names an array of String property names to omit from the new object
  6570. * @param {Object} obj The object to copy from
  6571. * @return {Object} A new object with properties from `names` not on it.
  6572. * @see R.pick
  6573. * @example
  6574. *
  6575. * R.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, c: 3}
  6576. */
  6577. var omit = _curry2(function omit(names, obj) {
  6578. var result = {};
  6579. var index = {};
  6580. var idx = 0;
  6581. var len = names.length;
  6582. while (idx < len) {
  6583. index[names[idx]] = 1;
  6584. idx += 1;
  6585. }
  6586. for (var prop in obj) {
  6587. if (!index.hasOwnProperty(prop)) {
  6588. result[prop] = obj[prop];
  6589. }
  6590. }
  6591. return result;
  6592. });
  6593. /**
  6594. * Accepts a function `fn` and returns a function that guards invocation of
  6595. * `fn` such that `fn` can only ever be called once, no matter how many times
  6596. * the returned function is invoked. The first value calculated is returned in
  6597. * subsequent invocations.
  6598. *
  6599. * @func
  6600. * @memberOf R
  6601. * @since v0.1.0
  6602. * @category Function
  6603. * @sig (a... -> b) -> (a... -> b)
  6604. * @param {Function} fn The function to wrap in a call-only-once wrapper.
  6605. * @return {Function} The wrapped function.
  6606. * @example
  6607. *
  6608. * const addOneOnce = R.once(x => x + 1);
  6609. * addOneOnce(10); //=> 11
  6610. * addOneOnce(addOneOnce(50)); //=> 11
  6611. */
  6612. var once = _curry1(function once(fn) {
  6613. var called = false;
  6614. var result;
  6615. return _arity(fn.length, function() {
  6616. if (called) {
  6617. return result;
  6618. }
  6619. called = true;
  6620. result = fn.apply(this, arguments);
  6621. return result;
  6622. });
  6623. });
  6624. function _assertPromise(name, p) {
  6625. if (p == null || !_isFunction(p.then)) {
  6626. throw new TypeError('`' + name + '` expected a Promise, received ' + _toString(p, []));
  6627. }
  6628. }
  6629. /**
  6630. * Returns the result of applying the onFailure function to the value inside
  6631. * a failed promise. This is useful for handling rejected promises
  6632. * inside function compositions.
  6633. *
  6634. * @func
  6635. * @memberOf R
  6636. * @category Function
  6637. * @sig (e -> b) -> (Promise e a) -> (Promise e b)
  6638. * @sig (e -> (Promise f b)) -> (Promise e a) -> (Promise f b)
  6639. * @param {Function} onFailure The function to apply. Can return a value or a promise of a value.
  6640. * @param {Promise} p
  6641. * @return {Promise} The result of calling `p.then(null, onFailure)`
  6642. * @see R.then
  6643. * @example
  6644. *
  6645. * var failedFetch = (id) => Promise.reject('bad ID');
  6646. * var useDefault = () => ({ firstName: 'Bob', lastName: 'Loblaw' })
  6647. *
  6648. * //recoverFromFailure :: String -> Promise ({firstName, lastName})
  6649. * var recoverFromFailure = R.pipe(
  6650. * failedFetch(12345),
  6651. * R.otherwise(useDefault),
  6652. * R.then(R.pick(['firstName', 'lastName']))
  6653. * );
  6654. */
  6655. const otherwise = _curry2(function otherwise(f, p) {
  6656. _assertPromise('otherwise', p);
  6657. return p.then(null, f);
  6658. });
  6659. // `Identity` is a functor that holds a single value, where `map` simply
  6660. // transforms the held value with the provided function.
  6661. var Identity = function(x) {
  6662. return {value: x, map: function(f) { return Identity(f(x)); }};
  6663. };
  6664. /**
  6665. * Returns the result of "setting" the portion of the given data structure
  6666. * focused by the given lens to the result of applying the given function to
  6667. * the focused value.
  6668. *
  6669. * @func
  6670. * @memberOf R
  6671. * @since v0.16.0
  6672. * @category Object
  6673. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  6674. * @sig Lens s a -> (a -> a) -> s -> s
  6675. * @param {Lens} lens
  6676. * @param {*} v
  6677. * @param {*} x
  6678. * @return {*}
  6679. * @see R.prop, R.lensIndex, R.lensProp
  6680. * @example
  6681. *
  6682. * const headLens = R.lensIndex(0);
  6683. *
  6684. * R.over(headLens, R.toUpper, ['foo', 'bar', 'baz']); //=> ['FOO', 'bar', 'baz']
  6685. */
  6686. var over = _curry3(function over(lens, f, x) {
  6687. // The value returned by the getter function is first transformed with `f`,
  6688. // then set as the value of an `Identity`. This is then mapped over with the
  6689. // setter function of the lens.
  6690. return lens(function(y) { return Identity(f(y)); })(x).value;
  6691. });
  6692. /**
  6693. * Takes two arguments, `fst` and `snd`, and returns `[fst, snd]`.
  6694. *
  6695. * @func
  6696. * @memberOf R
  6697. * @since v0.18.0
  6698. * @category List
  6699. * @sig a -> b -> (a,b)
  6700. * @param {*} fst
  6701. * @param {*} snd
  6702. * @return {Array}
  6703. * @see R.objOf, R.of
  6704. * @example
  6705. *
  6706. * R.pair('foo', 'bar'); //=> ['foo', 'bar']
  6707. */
  6708. var pair = _curry2(function pair(fst, snd) { return [fst, snd]; });
  6709. function _createPartialApplicator(concat) {
  6710. return _curry2(function(fn, args) {
  6711. return _arity(Math.max(0, fn.length - args.length), function() {
  6712. return fn.apply(this, concat(args, arguments));
  6713. });
  6714. });
  6715. }
  6716. /**
  6717. * Takes a function `f` and a list of arguments, and returns a function `g`.
  6718. * When applied, `g` returns the result of applying `f` to the arguments
  6719. * provided initially followed by the arguments provided to `g`.
  6720. *
  6721. * @func
  6722. * @memberOf R
  6723. * @since v0.10.0
  6724. * @category Function
  6725. * @sig ((a, b, c, ..., n) -> x) -> [a, b, c, ...] -> ((d, e, f, ..., n) -> x)
  6726. * @param {Function} f
  6727. * @param {Array} args
  6728. * @return {Function}
  6729. * @see R.partialRight, R.curry
  6730. * @example
  6731. *
  6732. * const multiply2 = (a, b) => a * b;
  6733. * const double = R.partial(multiply2, [2]);
  6734. * double(2); //=> 4
  6735. *
  6736. * const greet = (salutation, title, firstName, lastName) =>
  6737. * salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
  6738. *
  6739. * const sayHello = R.partial(greet, ['Hello']);
  6740. * const sayHelloToMs = R.partial(sayHello, ['Ms.']);
  6741. * sayHelloToMs('Jane', 'Jones'); //=> 'Hello, Ms. Jane Jones!'
  6742. * @symb R.partial(f, [a, b])(c, d) = f(a, b, c, d)
  6743. */
  6744. var partial = _createPartialApplicator(_concat);
  6745. /**
  6746. * Takes a function `f` and a list of arguments, and returns a function `g`.
  6747. * When applied, `g` returns the result of applying `f` to the arguments
  6748. * provided to `g` followed by the arguments provided initially.
  6749. *
  6750. * @func
  6751. * @memberOf R
  6752. * @since v0.10.0
  6753. * @category Function
  6754. * @sig ((a, b, c, ..., n) -> x) -> [d, e, f, ..., n] -> ((a, b, c, ...) -> x)
  6755. * @param {Function} f
  6756. * @param {Array} args
  6757. * @return {Function}
  6758. * @see R.partial
  6759. * @example
  6760. *
  6761. * const greet = (salutation, title, firstName, lastName) =>
  6762. * salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
  6763. *
  6764. * const greetMsJaneJones = R.partialRight(greet, ['Ms.', 'Jane', 'Jones']);
  6765. *
  6766. * greetMsJaneJones('Hello'); //=> 'Hello, Ms. Jane Jones!'
  6767. * @symb R.partialRight(f, [a, b])(c, d) = f(c, d, a, b)
  6768. */
  6769. var partialRight = _createPartialApplicator(flip(_concat));
  6770. /**
  6771. * Takes a predicate and a list or other `Filterable` object and returns the
  6772. * pair of filterable objects of the same type of elements which do and do not
  6773. * satisfy, the predicate, respectively. Filterable objects include plain objects or any object
  6774. * that has a filter method such as `Array`.
  6775. *
  6776. * @func
  6777. * @memberOf R
  6778. * @since v0.1.4
  6779. * @category List
  6780. * @sig Filterable f => (a -> Boolean) -> f a -> [f a, f a]
  6781. * @param {Function} pred A predicate to determine which side the element belongs to.
  6782. * @param {Array} filterable the list (or other filterable) to partition.
  6783. * @return {Array} An array, containing first the subset of elements that satisfy the
  6784. * predicate, and second the subset of elements that do not satisfy.
  6785. * @see R.filter, R.reject
  6786. * @example
  6787. *
  6788. * R.partition(R.includes('s'), ['sss', 'ttt', 'foo', 'bars']);
  6789. * // => [ [ 'sss', 'bars' ], [ 'ttt', 'foo' ] ]
  6790. *
  6791. * R.partition(R.includes('s'), { a: 'sss', b: 'ttt', foo: 'bars' });
  6792. * // => [ { a: 'sss', foo: 'bars' }, { b: 'ttt' } ]
  6793. */
  6794. var partition = juxt([filter, reject]);
  6795. /**
  6796. * Determines whether a nested path on an object has a specific value, in
  6797. * [`R.equals`](#equals) terms. Most likely used to filter a list.
  6798. *
  6799. * @func
  6800. * @memberOf R
  6801. * @since v0.7.0
  6802. * @category Relation
  6803. * @typedefn Idx = String | Int
  6804. * @sig [Idx] -> a -> {a} -> Boolean
  6805. * @param {Array} path The path of the nested property to use
  6806. * @param {*} val The value to compare the nested property with
  6807. * @param {Object} obj The object to check the nested property in
  6808. * @return {Boolean} `true` if the value equals the nested object property,
  6809. * `false` otherwise.
  6810. * @example
  6811. *
  6812. * const user1 = { address: { zipCode: 90210 } };
  6813. * const user2 = { address: { zipCode: 55555 } };
  6814. * const user3 = { name: 'Bob' };
  6815. * const users = [ user1, user2, user3 ];
  6816. * const isFamous = R.pathEq(['address', 'zipCode'], 90210);
  6817. * R.filter(isFamous, users); //=> [ user1 ]
  6818. */
  6819. var pathEq = _curry3(function pathEq(_path, val, obj) {
  6820. return equals(path(_path, obj), val);
  6821. });
  6822. /**
  6823. * If the given, non-null object has a value at the given path, returns the
  6824. * value at that path. Otherwise returns the provided default value.
  6825. *
  6826. * @func
  6827. * @memberOf R
  6828. * @since v0.18.0
  6829. * @category Object
  6830. * @typedefn Idx = String | Int
  6831. * @sig a -> [Idx] -> {a} -> a
  6832. * @param {*} d The default value.
  6833. * @param {Array} p The path to use.
  6834. * @param {Object} obj The object to retrieve the nested property from.
  6835. * @return {*} The data at `path` of the supplied object or the default value.
  6836. * @example
  6837. *
  6838. * R.pathOr('N/A', ['a', 'b'], {a: {b: 2}}); //=> 2
  6839. * R.pathOr('N/A', ['a', 'b'], {c: {b: 2}}); //=> "N/A"
  6840. */
  6841. var pathOr = _curry3(function pathOr(d, p, obj) {
  6842. return defaultTo(d, path(p, obj));
  6843. });
  6844. /**
  6845. * Returns `true` if the specified object property at given path satisfies the
  6846. * given predicate; `false` otherwise.
  6847. *
  6848. * @func
  6849. * @memberOf R
  6850. * @since v0.19.0
  6851. * @category Logic
  6852. * @typedefn Idx = String | Int
  6853. * @sig (a -> Boolean) -> [Idx] -> {a} -> Boolean
  6854. * @param {Function} pred
  6855. * @param {Array} propPath
  6856. * @param {*} obj
  6857. * @return {Boolean}
  6858. * @see R.propSatisfies, R.path
  6859. * @example
  6860. *
  6861. * R.pathSatisfies(y => y > 0, ['x', 'y'], {x: {y: 2}}); //=> true
  6862. */
  6863. var pathSatisfies = _curry3(function pathSatisfies(pred, propPath, obj) {
  6864. return propPath.length > 0 && pred(path(propPath, obj));
  6865. });
  6866. /**
  6867. * Returns a partial copy of an object containing only the keys specified. If
  6868. * the key does not exist, the property is ignored.
  6869. *
  6870. * @func
  6871. * @memberOf R
  6872. * @since v0.1.0
  6873. * @category Object
  6874. * @sig [k] -> {k: v} -> {k: v}
  6875. * @param {Array} names an array of String property names to copy onto a new object
  6876. * @param {Object} obj The object to copy from
  6877. * @return {Object} A new object with only properties from `names` on it.
  6878. * @see R.omit, R.props
  6879. * @example
  6880. *
  6881. * R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
  6882. * R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1}
  6883. */
  6884. var pick = _curry2(function pick(names, obj) {
  6885. var result = {};
  6886. var idx = 0;
  6887. while (idx < names.length) {
  6888. if (names[idx] in obj) {
  6889. result[names[idx]] = obj[names[idx]];
  6890. }
  6891. idx += 1;
  6892. }
  6893. return result;
  6894. });
  6895. /**
  6896. * Similar to `pick` except that this one includes a `key: undefined` pair for
  6897. * properties that don't exist.
  6898. *
  6899. * @func
  6900. * @memberOf R
  6901. * @since v0.1.0
  6902. * @category Object
  6903. * @sig [k] -> {k: v} -> {k: v}
  6904. * @param {Array} names an array of String property names to copy onto a new object
  6905. * @param {Object} obj The object to copy from
  6906. * @return {Object} A new object with only properties from `names` on it.
  6907. * @see R.pick
  6908. * @example
  6909. *
  6910. * R.pickAll(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
  6911. * R.pickAll(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, e: undefined, f: undefined}
  6912. */
  6913. var pickAll = _curry2(function pickAll(names, obj) {
  6914. var result = {};
  6915. var idx = 0;
  6916. var len = names.length;
  6917. while (idx < len) {
  6918. var name = names[idx];
  6919. result[name] = obj[name];
  6920. idx += 1;
  6921. }
  6922. return result;
  6923. });
  6924. /**
  6925. * Returns a partial copy of an object containing only the keys that satisfy
  6926. * the supplied predicate.
  6927. *
  6928. * @func
  6929. * @memberOf R
  6930. * @since v0.8.0
  6931. * @category Object
  6932. * @sig ((v, k) -> Boolean) -> {k: v} -> {k: v}
  6933. * @param {Function} pred A predicate to determine whether or not a key
  6934. * should be included on the output object.
  6935. * @param {Object} obj The object to copy from
  6936. * @return {Object} A new object with only properties that satisfy `pred`
  6937. * on it.
  6938. * @see R.pick, R.filter
  6939. * @example
  6940. *
  6941. * const isUpperCase = (val, key) => key.toUpperCase() === key;
  6942. * R.pickBy(isUpperCase, {a: 1, b: 2, A: 3, B: 4}); //=> {A: 3, B: 4}
  6943. */
  6944. var pickBy = _curry2(function pickBy(test, obj) {
  6945. var result = {};
  6946. for (var prop in obj) {
  6947. if (test(obj[prop], prop, obj)) {
  6948. result[prop] = obj[prop];
  6949. }
  6950. }
  6951. return result;
  6952. });
  6953. /**
  6954. * Returns the left-to-right Kleisli composition of the provided functions,
  6955. * each of which must return a value of a type supported by [`chain`](#chain).
  6956. *
  6957. * `R.pipeK(f, g, h)` is equivalent to `R.pipe(f, R.chain(g), R.chain(h))`.
  6958. *
  6959. * @func
  6960. * @memberOf R
  6961. * @since v0.16.0
  6962. * @category Function
  6963. * @sig Chain m => ((a -> m b), (b -> m c), ..., (y -> m z)) -> (a -> m z)
  6964. * @param {...Function}
  6965. * @return {Function}
  6966. * @see R.composeK
  6967. * @example
  6968. *
  6969. * // parseJson :: String -> Maybe *
  6970. * // get :: String -> Object -> Maybe *
  6971. *
  6972. * // getStateCode :: Maybe String -> Maybe String
  6973. * const getStateCode = R.pipeK(
  6974. * parseJson,
  6975. * get('user'),
  6976. * get('address'),
  6977. * get('state'),
  6978. * R.compose(Maybe.of, R.toUpper)
  6979. * );
  6980. *
  6981. * getStateCode('{"user":{"address":{"state":"ny"}}}');
  6982. * //=> Just('NY')
  6983. * getStateCode('[Invalid JSON]');
  6984. * //=> Nothing()
  6985. * @symb R.pipeK(f, g, h)(a) = R.chain(h, R.chain(g, f(a)))
  6986. */
  6987. function pipeK() {
  6988. if (arguments.length === 0) {
  6989. throw new Error('pipeK requires at least one argument');
  6990. }
  6991. return composeK.apply(this, reverse(arguments));
  6992. }
  6993. /**
  6994. * Returns a new list with the given element at the front, followed by the
  6995. * contents of the list.
  6996. *
  6997. * @func
  6998. * @memberOf R
  6999. * @since v0.1.0
  7000. * @category List
  7001. * @sig a -> [a] -> [a]
  7002. * @param {*} el The item to add to the head of the output list.
  7003. * @param {Array} list The array to add to the tail of the output list.
  7004. * @return {Array} A new array.
  7005. * @see R.append
  7006. * @example
  7007. *
  7008. * R.prepend('fee', ['fi', 'fo', 'fum']); //=> ['fee', 'fi', 'fo', 'fum']
  7009. */
  7010. var prepend = _curry2(function prepend(el, list) {
  7011. return _concat([el], list);
  7012. });
  7013. /**
  7014. * Multiplies together all the elements of a list.
  7015. *
  7016. * @func
  7017. * @memberOf R
  7018. * @since v0.1.0
  7019. * @category Math
  7020. * @sig [Number] -> Number
  7021. * @param {Array} list An array of numbers
  7022. * @return {Number} The product of all the numbers in the list.
  7023. * @see R.reduce
  7024. * @example
  7025. *
  7026. * R.product([2,4,6,8,100,1]); //=> 38400
  7027. */
  7028. var product = reduce(multiply, 1);
  7029. /**
  7030. * Accepts a function `fn` and a list of transformer functions and returns a
  7031. * new curried function. When the new function is invoked, it calls the
  7032. * function `fn` with parameters consisting of the result of calling each
  7033. * supplied handler on successive arguments to the new function.
  7034. *
  7035. * If more arguments are passed to the returned function than transformer
  7036. * functions, those arguments are passed directly to `fn` as additional
  7037. * parameters. If you expect additional arguments that don't need to be
  7038. * transformed, although you can ignore them, it's best to pass an identity
  7039. * function so that the new function reports the correct arity.
  7040. *
  7041. * @func
  7042. * @memberOf R
  7043. * @since v0.1.0
  7044. * @category Function
  7045. * @sig ((x1, x2, ...) -> z) -> [(a -> x1), (b -> x2), ...] -> (a -> b -> ... -> z)
  7046. * @param {Function} fn The function to wrap.
  7047. * @param {Array} transformers A list of transformer functions
  7048. * @return {Function} The wrapped function.
  7049. * @see R.converge
  7050. * @example
  7051. *
  7052. * R.useWith(Math.pow, [R.identity, R.identity])(3, 4); //=> 81
  7053. * R.useWith(Math.pow, [R.identity, R.identity])(3)(4); //=> 81
  7054. * R.useWith(Math.pow, [R.dec, R.inc])(3, 4); //=> 32
  7055. * R.useWith(Math.pow, [R.dec, R.inc])(3)(4); //=> 32
  7056. * @symb R.useWith(f, [g, h])(a, b) = f(g(a), h(b))
  7057. */
  7058. var useWith = _curry2(function useWith(fn, transformers) {
  7059. return curryN(transformers.length, function() {
  7060. var args = [];
  7061. var idx = 0;
  7062. while (idx < transformers.length) {
  7063. args.push(transformers[idx].call(this, arguments[idx]));
  7064. idx += 1;
  7065. }
  7066. return fn.apply(this, args.concat(Array.prototype.slice.call(arguments, transformers.length)));
  7067. });
  7068. });
  7069. /**
  7070. * Reasonable analog to SQL `select` statement.
  7071. *
  7072. * @func
  7073. * @memberOf R
  7074. * @since v0.1.0
  7075. * @category Object
  7076. * @category Relation
  7077. * @sig [k] -> [{k: v}] -> [{k: v}]
  7078. * @param {Array} props The property names to project
  7079. * @param {Array} objs The objects to query
  7080. * @return {Array} An array of objects with just the `props` properties.
  7081. * @example
  7082. *
  7083. * const abby = {name: 'Abby', age: 7, hair: 'blond', grade: 2};
  7084. * const fred = {name: 'Fred', age: 12, hair: 'brown', grade: 7};
  7085. * const kids = [abby, fred];
  7086. * R.project(['name', 'grade'], kids); //=> [{name: 'Abby', grade: 2}, {name: 'Fred', grade: 7}]
  7087. */
  7088. var project = useWith(_map, [pickAll, identity]); // passing `identity` gives correct arity
  7089. /**
  7090. * Returns `true` if the specified object property is equal, in
  7091. * [`R.equals`](#equals) terms, to the given value; `false` otherwise.
  7092. * You can test multiple properties with [`R.whereEq`](#whereEq).
  7093. *
  7094. * @func
  7095. * @memberOf R
  7096. * @since v0.1.0
  7097. * @category Relation
  7098. * @sig String -> a -> Object -> Boolean
  7099. * @param {String} name
  7100. * @param {*} val
  7101. * @param {*} obj
  7102. * @return {Boolean}
  7103. * @see R.whereEq, R.propSatisfies, R.equals
  7104. * @example
  7105. *
  7106. * const abby = {name: 'Abby', age: 7, hair: 'blond'};
  7107. * const fred = {name: 'Fred', age: 12, hair: 'brown'};
  7108. * const rusty = {name: 'Rusty', age: 10, hair: 'brown'};
  7109. * const alois = {name: 'Alois', age: 15, disposition: 'surly'};
  7110. * const kids = [abby, fred, rusty, alois];
  7111. * const hasBrownHair = R.propEq('hair', 'brown');
  7112. * R.filter(hasBrownHair, kids); //=> [fred, rusty]
  7113. */
  7114. var propEq = _curry3(function propEq(name, val, obj) {
  7115. return equals(val, obj[name]);
  7116. });
  7117. /**
  7118. * Returns `true` if the specified object property is of the given type;
  7119. * `false` otherwise.
  7120. *
  7121. * @func
  7122. * @memberOf R
  7123. * @since v0.16.0
  7124. * @category Type
  7125. * @sig Type -> String -> Object -> Boolean
  7126. * @param {Function} type
  7127. * @param {String} name
  7128. * @param {*} obj
  7129. * @return {Boolean}
  7130. * @see R.is, R.propSatisfies
  7131. * @example
  7132. *
  7133. * R.propIs(Number, 'x', {x: 1, y: 2}); //=> true
  7134. * R.propIs(Number, 'x', {x: 'foo'}); //=> false
  7135. * R.propIs(Number, 'x', {}); //=> false
  7136. */
  7137. var propIs = _curry3(function propIs(type, name, obj) {
  7138. return is(type, obj[name]);
  7139. });
  7140. /**
  7141. * If the given, non-null object has an own property with the specified name,
  7142. * returns the value of that property. Otherwise returns the provided default
  7143. * value.
  7144. *
  7145. * @func
  7146. * @memberOf R
  7147. * @since v0.6.0
  7148. * @category Object
  7149. * @sig a -> String -> Object -> a
  7150. * @param {*} val The default value.
  7151. * @param {String} p The name of the property to return.
  7152. * @param {Object} obj The object to query.
  7153. * @return {*} The value of given property of the supplied object or the default value.
  7154. * @example
  7155. *
  7156. * const alice = {
  7157. * name: 'ALICE',
  7158. * age: 101
  7159. * };
  7160. * const favorite = R.prop('favoriteLibrary');
  7161. * const favoriteWithDefault = R.propOr('Ramda', 'favoriteLibrary');
  7162. *
  7163. * favorite(alice); //=> undefined
  7164. * favoriteWithDefault(alice); //=> 'Ramda'
  7165. */
  7166. var propOr = _curry3(function propOr(val, p, obj) {
  7167. return pathOr(val, [p], obj);
  7168. });
  7169. /**
  7170. * Returns `true` if the specified object property satisfies the given
  7171. * predicate; `false` otherwise. You can test multiple properties with
  7172. * [`R.where`](#where).
  7173. *
  7174. * @func
  7175. * @memberOf R
  7176. * @since v0.16.0
  7177. * @category Logic
  7178. * @sig (a -> Boolean) -> String -> {String: a} -> Boolean
  7179. * @param {Function} pred
  7180. * @param {String} name
  7181. * @param {*} obj
  7182. * @return {Boolean}
  7183. * @see R.where, R.propEq, R.propIs
  7184. * @example
  7185. *
  7186. * R.propSatisfies(x => x > 0, 'x', {x: 1, y: 2}); //=> true
  7187. */
  7188. var propSatisfies = _curry3(function propSatisfies(pred, name, obj) {
  7189. return pred(obj[name]);
  7190. });
  7191. /**
  7192. * Acts as multiple `prop`: array of keys in, array of values out. Preserves
  7193. * order.
  7194. *
  7195. * @func
  7196. * @memberOf R
  7197. * @since v0.1.0
  7198. * @category Object
  7199. * @sig [k] -> {k: v} -> [v]
  7200. * @param {Array} ps The property names to fetch
  7201. * @param {Object} obj The object to query
  7202. * @return {Array} The corresponding values or partially applied function.
  7203. * @example
  7204. *
  7205. * R.props(['x', 'y'], {x: 1, y: 2}); //=> [1, 2]
  7206. * R.props(['c', 'a', 'b'], {b: 2, a: 1}); //=> [undefined, 1, 2]
  7207. *
  7208. * const fullName = R.compose(R.join(' '), R.props(['first', 'last']));
  7209. * fullName({last: 'Bullet-Tooth', age: 33, first: 'Tony'}); //=> 'Tony Bullet-Tooth'
  7210. */
  7211. var props = _curry2(function props(ps, obj) {
  7212. var len = ps.length;
  7213. var out = [];
  7214. var idx = 0;
  7215. while (idx < len) {
  7216. out[idx] = obj[ps[idx]];
  7217. idx += 1;
  7218. }
  7219. return out;
  7220. });
  7221. /**
  7222. * Returns a list of numbers from `from` (inclusive) to `to` (exclusive).
  7223. *
  7224. * @func
  7225. * @memberOf R
  7226. * @since v0.1.0
  7227. * @category List
  7228. * @sig Number -> Number -> [Number]
  7229. * @param {Number} from The first number in the list.
  7230. * @param {Number} to One more than the last number in the list.
  7231. * @return {Array} The list of numbers in the set `[a, b)`.
  7232. * @example
  7233. *
  7234. * R.range(1, 5); //=> [1, 2, 3, 4]
  7235. * R.range(50, 53); //=> [50, 51, 52]
  7236. */
  7237. var range = _curry2(function range(from, to) {
  7238. if (!(_isNumber(from) && _isNumber(to))) {
  7239. throw new TypeError('Both arguments to range must be numbers');
  7240. }
  7241. var result = [];
  7242. var n = from;
  7243. while (n < to) {
  7244. result.push(n);
  7245. n += 1;
  7246. }
  7247. return result;
  7248. });
  7249. /**
  7250. * Returns a single item by iterating through the list, successively calling
  7251. * the iterator function and passing it an accumulator value and the current
  7252. * value from the array, and then passing the result to the next call.
  7253. *
  7254. * Similar to [`reduce`](#reduce), except moves through the input list from the
  7255. * right to the left.
  7256. *
  7257. * The iterator function receives two values: *(value, acc)*, while the arguments'
  7258. * order of `reduce`'s iterator function is *(acc, value)*.
  7259. *
  7260. * Note: `R.reduceRight` does not skip deleted or unassigned indices (sparse
  7261. * arrays), unlike the native `Array.prototype.reduceRight` method. For more details
  7262. * on this behavior, see:
  7263. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight#Description
  7264. *
  7265. * @func
  7266. * @memberOf R
  7267. * @since v0.1.0
  7268. * @category List
  7269. * @sig ((a, b) -> b) -> b -> [a] -> b
  7270. * @param {Function} fn The iterator function. Receives two values, the current element from the array
  7271. * and the accumulator.
  7272. * @param {*} acc The accumulator value.
  7273. * @param {Array} list The list to iterate over.
  7274. * @return {*} The final, accumulated value.
  7275. * @see R.reduce, R.addIndex
  7276. * @example
  7277. *
  7278. * R.reduceRight(R.subtract, 0, [1, 2, 3, 4]) // => (1 - (2 - (3 - (4 - 0)))) = -2
  7279. * // - -2
  7280. * // / \ / \
  7281. * // 1 - 1 3
  7282. * // / \ / \
  7283. * // 2 - ==> 2 -1
  7284. * // / \ / \
  7285. * // 3 - 3 4
  7286. * // / \ / \
  7287. * // 4 0 4 0
  7288. *
  7289. * @symb R.reduceRight(f, a, [b, c, d]) = f(b, f(c, f(d, a)))
  7290. */
  7291. var reduceRight = _curry3(function reduceRight(fn, acc, list) {
  7292. var idx = list.length - 1;
  7293. while (idx >= 0) {
  7294. acc = fn(list[idx], acc);
  7295. idx -= 1;
  7296. }
  7297. return acc;
  7298. });
  7299. /**
  7300. * Like [`reduce`](#reduce), `reduceWhile` returns a single item by iterating
  7301. * through the list, successively calling the iterator function. `reduceWhile`
  7302. * also takes a predicate that is evaluated before each step. If the predicate
  7303. * returns `false`, it "short-circuits" the iteration and returns the current
  7304. * value of the accumulator.
  7305. *
  7306. * @func
  7307. * @memberOf R
  7308. * @since v0.22.0
  7309. * @category List
  7310. * @sig ((a, b) -> Boolean) -> ((a, b) -> a) -> a -> [b] -> a
  7311. * @param {Function} pred The predicate. It is passed the accumulator and the
  7312. * current element.
  7313. * @param {Function} fn The iterator function. Receives two values, the
  7314. * accumulator and the current element.
  7315. * @param {*} a The accumulator value.
  7316. * @param {Array} list The list to iterate over.
  7317. * @return {*} The final, accumulated value.
  7318. * @see R.reduce, R.reduced
  7319. * @example
  7320. *
  7321. * const isOdd = (acc, x) => x % 2 === 1;
  7322. * const xs = [1, 3, 5, 60, 777, 800];
  7323. * R.reduceWhile(isOdd, R.add, 0, xs); //=> 9
  7324. *
  7325. * const ys = [2, 4, 6]
  7326. * R.reduceWhile(isOdd, R.add, 111, ys); //=> 111
  7327. */
  7328. var reduceWhile = _curryN(4, [], function _reduceWhile(pred, fn, a, list) {
  7329. return _reduce(function(acc, x) {
  7330. return pred(acc, x) ? fn(acc, x) : _reduced(acc);
  7331. }, a, list);
  7332. });
  7333. /**
  7334. * Returns a value wrapped to indicate that it is the final value of the reduce
  7335. * and transduce functions. The returned value should be considered a black
  7336. * box: the internal structure is not guaranteed to be stable.
  7337. *
  7338. * Note: this optimization is only available to the below functions:
  7339. * - [`reduce`](#reduce)
  7340. * - [`reduceWhile`](#reduceWhile)
  7341. * - [`transduce`](#transduce)
  7342. *
  7343. * @func
  7344. * @memberOf R
  7345. * @since v0.15.0
  7346. * @category List
  7347. * @sig a -> *
  7348. * @param {*} x The final value of the reduce.
  7349. * @return {*} The wrapped value.
  7350. * @see R.reduce, R.reduceWhile, R.transduce
  7351. * @example
  7352. *
  7353. * R.reduce(
  7354. * (acc, item) => item > 3 ? R.reduced(acc) : acc.concat(item),
  7355. * [],
  7356. * [1, 2, 3, 4, 5]) // [1, 2, 3]
  7357. */
  7358. var reduced = _curry1(_reduced);
  7359. /**
  7360. * Calls an input function `n` times, returning an array containing the results
  7361. * of those function calls.
  7362. *
  7363. * `fn` is passed one argument: The current value of `n`, which begins at `0`
  7364. * and is gradually incremented to `n - 1`.
  7365. *
  7366. * @func
  7367. * @memberOf R
  7368. * @since v0.2.3
  7369. * @category List
  7370. * @sig (Number -> a) -> Number -> [a]
  7371. * @param {Function} fn The function to invoke. Passed one argument, the current value of `n`.
  7372. * @param {Number} n A value between `0` and `n - 1`. Increments after each function call.
  7373. * @return {Array} An array containing the return values of all calls to `fn`.
  7374. * @see R.repeat
  7375. * @example
  7376. *
  7377. * R.times(R.identity, 5); //=> [0, 1, 2, 3, 4]
  7378. * @symb R.times(f, 0) = []
  7379. * @symb R.times(f, 1) = [f(0)]
  7380. * @symb R.times(f, 2) = [f(0), f(1)]
  7381. */
  7382. var times = _curry2(function times(fn, n) {
  7383. var len = Number(n);
  7384. var idx = 0;
  7385. var list;
  7386. if (len < 0 || isNaN(len)) {
  7387. throw new RangeError('n must be a non-negative number');
  7388. }
  7389. list = new Array(len);
  7390. while (idx < len) {
  7391. list[idx] = fn(idx);
  7392. idx += 1;
  7393. }
  7394. return list;
  7395. });
  7396. /**
  7397. * Returns a fixed list of size `n` containing a specified identical value.
  7398. *
  7399. * @func
  7400. * @memberOf R
  7401. * @since v0.1.1
  7402. * @category List
  7403. * @sig a -> n -> [a]
  7404. * @param {*} value The value to repeat.
  7405. * @param {Number} n The desired size of the output list.
  7406. * @return {Array} A new array containing `n` `value`s.
  7407. * @see R.times
  7408. * @example
  7409. *
  7410. * R.repeat('hi', 5); //=> ['hi', 'hi', 'hi', 'hi', 'hi']
  7411. *
  7412. * const obj = {};
  7413. * const repeatedObjs = R.repeat(obj, 5); //=> [{}, {}, {}, {}, {}]
  7414. * repeatedObjs[0] === repeatedObjs[1]; //=> true
  7415. * @symb R.repeat(a, 0) = []
  7416. * @symb R.repeat(a, 1) = [a]
  7417. * @symb R.repeat(a, 2) = [a, a]
  7418. */
  7419. var repeat = _curry2(function repeat(value, n) {
  7420. return times(always(value), n);
  7421. });
  7422. /**
  7423. * Replace a substring or regex match in a string with a replacement.
  7424. *
  7425. * The first two parameters correspond to the parameters of the
  7426. * `String.prototype.replace()` function, so the second parameter can also be a
  7427. * function.
  7428. *
  7429. * @func
  7430. * @memberOf R
  7431. * @since v0.7.0
  7432. * @category String
  7433. * @sig RegExp|String -> String -> String -> String
  7434. * @param {RegExp|String} pattern A regular expression or a substring to match.
  7435. * @param {String} replacement The string to replace the matches with.
  7436. * @param {String} str The String to do the search and replacement in.
  7437. * @return {String} The result.
  7438. * @example
  7439. *
  7440. * R.replace('foo', 'bar', 'foo foo foo'); //=> 'bar foo foo'
  7441. * R.replace(/foo/, 'bar', 'foo foo foo'); //=> 'bar foo foo'
  7442. *
  7443. * // Use the "g" (global) flag to replace all occurrences:
  7444. * R.replace(/foo/g, 'bar', 'foo foo foo'); //=> 'bar bar bar'
  7445. */
  7446. var replace = _curry3(function replace(regex, replacement, str) {
  7447. return str.replace(regex, replacement);
  7448. });
  7449. /**
  7450. * Scan is similar to [`reduce`](#reduce), but returns a list of successively
  7451. * reduced values from the left
  7452. *
  7453. * @func
  7454. * @memberOf R
  7455. * @since v0.10.0
  7456. * @category List
  7457. * @sig ((a, b) -> a) -> a -> [b] -> [a]
  7458. * @param {Function} fn The iterator function. Receives two values, the accumulator and the
  7459. * current element from the array
  7460. * @param {*} acc The accumulator value.
  7461. * @param {Array} list The list to iterate over.
  7462. * @return {Array} A list of all intermediately reduced values.
  7463. * @see R.reduce, R.mapAccum
  7464. * @example
  7465. *
  7466. * const numbers = [1, 2, 3, 4];
  7467. * const factorials = R.scan(R.multiply, 1, numbers); //=> [1, 1, 2, 6, 24]
  7468. * @symb R.scan(f, a, [b, c]) = [a, f(a, b), f(f(a, b), c)]
  7469. */
  7470. var scan = _curry3(function scan(fn, acc, list) {
  7471. var idx = 0;
  7472. var len = list.length;
  7473. var result = [acc];
  7474. while (idx < len) {
  7475. acc = fn(acc, list[idx]);
  7476. result[idx + 1] = acc;
  7477. idx += 1;
  7478. }
  7479. return result;
  7480. });
  7481. /**
  7482. * Transforms a [Traversable](https://github.com/fantasyland/fantasy-land#traversable)
  7483. * of [Applicative](https://github.com/fantasyland/fantasy-land#applicative) into an
  7484. * Applicative of Traversable.
  7485. *
  7486. * Dispatches to the `sequence` method of the second argument, if present.
  7487. *
  7488. * @func
  7489. * @memberOf R
  7490. * @since v0.19.0
  7491. * @category List
  7492. * @sig (Applicative f, Traversable t) => (a -> f a) -> t (f a) -> f (t a)
  7493. * @param {Function} of
  7494. * @param {*} traversable
  7495. * @return {*}
  7496. * @see R.traverse
  7497. * @example
  7498. *
  7499. * R.sequence(Maybe.of, [Just(1), Just(2), Just(3)]); //=> Just([1, 2, 3])
  7500. * R.sequence(Maybe.of, [Just(1), Just(2), Nothing()]); //=> Nothing()
  7501. *
  7502. * R.sequence(R.of, Just([1, 2, 3])); //=> [Just(1), Just(2), Just(3)]
  7503. * R.sequence(R.of, Nothing()); //=> [Nothing()]
  7504. */
  7505. var sequence = _curry2(function sequence(of, traversable) {
  7506. return typeof traversable.sequence === 'function' ?
  7507. traversable.sequence(of) :
  7508. reduceRight(function(x, acc) { return ap(map(prepend, x), acc); },
  7509. of([]),
  7510. traversable);
  7511. });
  7512. /**
  7513. * Returns the result of "setting" the portion of the given data structure
  7514. * focused by the given lens to the given value.
  7515. *
  7516. * @func
  7517. * @memberOf R
  7518. * @since v0.16.0
  7519. * @category Object
  7520. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  7521. * @sig Lens s a -> a -> s -> s
  7522. * @param {Lens} lens
  7523. * @param {*} v
  7524. * @param {*} x
  7525. * @return {*}
  7526. * @see R.prop, R.lensIndex, R.lensProp
  7527. * @example
  7528. *
  7529. * const xLens = R.lensProp('x');
  7530. *
  7531. * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
  7532. * R.set(xLens, 8, {x: 1, y: 2}); //=> {x: 8, y: 2}
  7533. */
  7534. var set = _curry3(function set(lens, v, x) {
  7535. return over(lens, always(v), x);
  7536. });
  7537. /**
  7538. * Returns a copy of the list, sorted according to the comparator function,
  7539. * which should accept two values at a time and return a negative number if the
  7540. * first value is smaller, a positive number if it's larger, and zero if they
  7541. * are equal. Please note that this is a **copy** of the list. It does not
  7542. * modify the original.
  7543. *
  7544. * @func
  7545. * @memberOf R
  7546. * @since v0.1.0
  7547. * @category List
  7548. * @sig ((a, a) -> Number) -> [a] -> [a]
  7549. * @param {Function} comparator A sorting function :: a -> b -> Int
  7550. * @param {Array} list The list to sort
  7551. * @return {Array} a new array with its elements sorted by the comparator function.
  7552. * @example
  7553. *
  7554. * const diff = function(a, b) { return a - b; };
  7555. * R.sort(diff, [4,2,7,5]); //=> [2, 4, 5, 7]
  7556. */
  7557. var sort = _curry2(function sort(comparator, list) {
  7558. return Array.prototype.slice.call(list, 0).sort(comparator);
  7559. });
  7560. /**
  7561. * Sorts the list according to the supplied function.
  7562. *
  7563. * @func
  7564. * @memberOf R
  7565. * @since v0.1.0
  7566. * @category Relation
  7567. * @sig Ord b => (a -> b) -> [a] -> [a]
  7568. * @param {Function} fn
  7569. * @param {Array} list The list to sort.
  7570. * @return {Array} A new list sorted by the keys generated by `fn`.
  7571. * @example
  7572. *
  7573. * const sortByFirstItem = R.sortBy(R.prop(0));
  7574. * const pairs = [[-1, 1], [-2, 2], [-3, 3]];
  7575. * sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
  7576. *
  7577. * const sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name')));
  7578. * const alice = {
  7579. * name: 'ALICE',
  7580. * age: 101
  7581. * };
  7582. * const bob = {
  7583. * name: 'Bob',
  7584. * age: -10
  7585. * };
  7586. * const clara = {
  7587. * name: 'clara',
  7588. * age: 314.159
  7589. * };
  7590. * const people = [clara, bob, alice];
  7591. * sortByNameCaseInsensitive(people); //=> [alice, bob, clara]
  7592. */
  7593. var sortBy = _curry2(function sortBy(fn, list) {
  7594. return Array.prototype.slice.call(list, 0).sort(function(a, b) {
  7595. var aa = fn(a);
  7596. var bb = fn(b);
  7597. return aa < bb ? -1 : aa > bb ? 1 : 0;
  7598. });
  7599. });
  7600. /**
  7601. * Sorts a list according to a list of comparators.
  7602. *
  7603. * @func
  7604. * @memberOf R
  7605. * @since v0.23.0
  7606. * @category Relation
  7607. * @sig [(a, a) -> Number] -> [a] -> [a]
  7608. * @param {Array} functions A list of comparator functions.
  7609. * @param {Array} list The list to sort.
  7610. * @return {Array} A new list sorted according to the comarator functions.
  7611. * @example
  7612. *
  7613. * const alice = {
  7614. * name: 'alice',
  7615. * age: 40
  7616. * };
  7617. * const bob = {
  7618. * name: 'bob',
  7619. * age: 30
  7620. * };
  7621. * const clara = {
  7622. * name: 'clara',
  7623. * age: 40
  7624. * };
  7625. * const people = [clara, bob, alice];
  7626. * const ageNameSort = R.sortWith([
  7627. * R.descend(R.prop('age')),
  7628. * R.ascend(R.prop('name'))
  7629. * ]);
  7630. * ageNameSort(people); //=> [alice, clara, bob]
  7631. */
  7632. var sortWith = _curry2(function sortWith(fns, list) {
  7633. return Array.prototype.slice.call(list, 0).sort(function(a, b) {
  7634. var result = 0;
  7635. var i = 0;
  7636. while (result === 0 && i < fns.length) {
  7637. result = fns[i](a, b);
  7638. i += 1;
  7639. }
  7640. return result;
  7641. });
  7642. });
  7643. /**
  7644. * Splits a string into an array of strings based on the given
  7645. * separator.
  7646. *
  7647. * @func
  7648. * @memberOf R
  7649. * @since v0.1.0
  7650. * @category String
  7651. * @sig (String | RegExp) -> String -> [String]
  7652. * @param {String|RegExp} sep The pattern.
  7653. * @param {String} str The string to separate into an array.
  7654. * @return {Array} The array of strings from `str` separated by `str`.
  7655. * @see R.join
  7656. * @example
  7657. *
  7658. * const pathComponents = R.split('/');
  7659. * R.tail(pathComponents('/usr/local/bin/node')); //=> ['usr', 'local', 'bin', 'node']
  7660. *
  7661. * R.split('.', 'a.b.c.xyz.d'); //=> ['a', 'b', 'c', 'xyz', 'd']
  7662. */
  7663. var split = invoker(1, 'split');
  7664. /**
  7665. * Splits a given list or string at a given index.
  7666. *
  7667. * @func
  7668. * @memberOf R
  7669. * @since v0.19.0
  7670. * @category List
  7671. * @sig Number -> [a] -> [[a], [a]]
  7672. * @sig Number -> String -> [String, String]
  7673. * @param {Number} index The index where the array/string is split.
  7674. * @param {Array|String} array The array/string to be split.
  7675. * @return {Array}
  7676. * @example
  7677. *
  7678. * R.splitAt(1, [1, 2, 3]); //=> [[1], [2, 3]]
  7679. * R.splitAt(5, 'hello world'); //=> ['hello', ' world']
  7680. * R.splitAt(-1, 'foobar'); //=> ['fooba', 'r']
  7681. */
  7682. var splitAt = _curry2(function splitAt(index, array) {
  7683. return [slice(0, index, array), slice(index, length(array), array)];
  7684. });
  7685. /**
  7686. * Splits a collection into slices of the specified length.
  7687. *
  7688. * @func
  7689. * @memberOf R
  7690. * @since v0.16.0
  7691. * @category List
  7692. * @sig Number -> [a] -> [[a]]
  7693. * @sig Number -> String -> [String]
  7694. * @param {Number} n
  7695. * @param {Array} list
  7696. * @return {Array}
  7697. * @example
  7698. *
  7699. * R.splitEvery(3, [1, 2, 3, 4, 5, 6, 7]); //=> [[1, 2, 3], [4, 5, 6], [7]]
  7700. * R.splitEvery(3, 'foobarbaz'); //=> ['foo', 'bar', 'baz']
  7701. */
  7702. var splitEvery = _curry2(function splitEvery(n, list) {
  7703. if (n <= 0) {
  7704. throw new Error('First argument to splitEvery must be a positive integer');
  7705. }
  7706. var result = [];
  7707. var idx = 0;
  7708. while (idx < list.length) {
  7709. result.push(slice(idx, idx += n, list));
  7710. }
  7711. return result;
  7712. });
  7713. /**
  7714. * Takes a list and a predicate and returns a pair of lists with the following properties:
  7715. *
  7716. * - the result of concatenating the two output lists is equivalent to the input list;
  7717. * - none of the elements of the first output list satisfies the predicate; and
  7718. * - if the second output list is non-empty, its first element satisfies the predicate.
  7719. *
  7720. * @func
  7721. * @memberOf R
  7722. * @since v0.19.0
  7723. * @category List
  7724. * @sig (a -> Boolean) -> [a] -> [[a], [a]]
  7725. * @param {Function} pred The predicate that determines where the array is split.
  7726. * @param {Array} list The array to be split.
  7727. * @return {Array}
  7728. * @example
  7729. *
  7730. * R.splitWhen(R.equals(2), [1, 2, 3, 1, 2, 3]); //=> [[1], [2, 3, 1, 2, 3]]
  7731. */
  7732. var splitWhen = _curry2(function splitWhen(pred, list) {
  7733. var idx = 0;
  7734. var len = list.length;
  7735. var prefix = [];
  7736. while (idx < len && !pred(list[idx])) {
  7737. prefix.push(list[idx]);
  7738. idx += 1;
  7739. }
  7740. return [prefix, Array.prototype.slice.call(list, idx)];
  7741. });
  7742. /**
  7743. * Checks if a list starts with the provided sublist.
  7744. *
  7745. * Similarly, checks if a string starts with the provided substring.
  7746. *
  7747. * @func
  7748. * @memberOf R
  7749. * @since v0.24.0
  7750. * @category List
  7751. * @sig [a] -> [a] -> Boolean
  7752. * @sig String -> String -> Boolean
  7753. * @param {*} prefix
  7754. * @param {*} list
  7755. * @return {Boolean}
  7756. * @see R.endsWith
  7757. * @example
  7758. *
  7759. * R.startsWith('a', 'abc') //=> true
  7760. * R.startsWith('b', 'abc') //=> false
  7761. * R.startsWith(['a'], ['a', 'b', 'c']) //=> true
  7762. * R.startsWith(['b'], ['a', 'b', 'c']) //=> false
  7763. */
  7764. var startsWith = _curry2(function(prefix, list) {
  7765. return equals(take(prefix.length, list), prefix);
  7766. });
  7767. /**
  7768. * Subtracts its second argument from its first argument.
  7769. *
  7770. * @func
  7771. * @memberOf R
  7772. * @since v0.1.0
  7773. * @category Math
  7774. * @sig Number -> Number -> Number
  7775. * @param {Number} a The first value.
  7776. * @param {Number} b The second value.
  7777. * @return {Number} The result of `a - b`.
  7778. * @see R.add
  7779. * @example
  7780. *
  7781. * R.subtract(10, 8); //=> 2
  7782. *
  7783. * const minus5 = R.subtract(R.__, 5);
  7784. * minus5(17); //=> 12
  7785. *
  7786. * const complementaryAngle = R.subtract(90);
  7787. * complementaryAngle(30); //=> 60
  7788. * complementaryAngle(72); //=> 18
  7789. */
  7790. var subtract = _curry2(function subtract(a, b) {
  7791. return Number(a) - Number(b);
  7792. });
  7793. /**
  7794. * Finds the set (i.e. no duplicates) of all elements contained in the first or
  7795. * second list, but not both.
  7796. *
  7797. * @func
  7798. * @memberOf R
  7799. * @since v0.19.0
  7800. * @category Relation
  7801. * @sig [*] -> [*] -> [*]
  7802. * @param {Array} list1 The first list.
  7803. * @param {Array} list2 The second list.
  7804. * @return {Array} The elements in `list1` or `list2`, but not both.
  7805. * @see R.symmetricDifferenceWith, R.difference, R.differenceWith
  7806. * @example
  7807. *
  7808. * R.symmetricDifference([1,2,3,4], [7,6,5,4,3]); //=> [1,2,7,6,5]
  7809. * R.symmetricDifference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5,1,2]
  7810. */
  7811. var symmetricDifference = _curry2(function symmetricDifference(list1, list2) {
  7812. return concat(difference(list1, list2), difference(list2, list1));
  7813. });
  7814. /**
  7815. * Finds the set (i.e. no duplicates) of all elements contained in the first or
  7816. * second list, but not both. Duplication is determined according to the value
  7817. * returned by applying the supplied predicate to two list elements.
  7818. *
  7819. * @func
  7820. * @memberOf R
  7821. * @since v0.19.0
  7822. * @category Relation
  7823. * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
  7824. * @param {Function} pred A predicate used to test whether two items are equal.
  7825. * @param {Array} list1 The first list.
  7826. * @param {Array} list2 The second list.
  7827. * @return {Array} The elements in `list1` or `list2`, but not both.
  7828. * @see R.symmetricDifference, R.difference, R.differenceWith
  7829. * @example
  7830. *
  7831. * const eqA = R.eqBy(R.prop('a'));
  7832. * const l1 = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
  7833. * const l2 = [{a: 3}, {a: 4}, {a: 5}, {a: 6}];
  7834. * R.symmetricDifferenceWith(eqA, l1, l2); //=> [{a: 1}, {a: 2}, {a: 5}, {a: 6}]
  7835. */
  7836. var symmetricDifferenceWith = _curry3(function symmetricDifferenceWith(pred, list1, list2) {
  7837. return concat(differenceWith(pred, list1, list2), differenceWith(pred, list2, list1));
  7838. });
  7839. /**
  7840. * Returns a new list containing the last `n` elements of a given list, passing
  7841. * each value to the supplied predicate function, and terminating when the
  7842. * predicate function returns `false`. Excludes the element that caused the
  7843. * predicate function to fail. The predicate function is passed one argument:
  7844. * *(value)*.
  7845. *
  7846. * @func
  7847. * @memberOf R
  7848. * @since v0.16.0
  7849. * @category List
  7850. * @sig (a -> Boolean) -> [a] -> [a]
  7851. * @sig (a -> Boolean) -> String -> String
  7852. * @param {Function} fn The function called per iteration.
  7853. * @param {Array} xs The collection to iterate over.
  7854. * @return {Array} A new array.
  7855. * @see R.dropLastWhile, R.addIndex
  7856. * @example
  7857. *
  7858. * const isNotOne = x => x !== 1;
  7859. *
  7860. * R.takeLastWhile(isNotOne, [1, 2, 3, 4]); //=> [2, 3, 4]
  7861. *
  7862. * R.takeLastWhile(x => x !== 'R' , 'Ramda'); //=> 'amda'
  7863. */
  7864. var takeLastWhile = _curry2(function takeLastWhile(fn, xs) {
  7865. var idx = xs.length - 1;
  7866. while (idx >= 0 && fn(xs[idx])) {
  7867. idx -= 1;
  7868. }
  7869. return slice(idx + 1, Infinity, xs);
  7870. });
  7871. function XTakeWhile(f, xf) {
  7872. this.xf = xf;
  7873. this.f = f;
  7874. }
  7875. XTakeWhile.prototype['@@transducer/init'] = _xfBase.init;
  7876. XTakeWhile.prototype['@@transducer/result'] = _xfBase.result;
  7877. XTakeWhile.prototype['@@transducer/step'] = function(result, input) {
  7878. return this.f(input) ? this.xf['@@transducer/step'](result, input) : _reduced(result);
  7879. };
  7880. var _xtakeWhile = _curry2(function _xtakeWhile(f, xf) { return new XTakeWhile(f, xf); });
  7881. /**
  7882. * Returns a new list containing the first `n` elements of a given list,
  7883. * passing each value to the supplied predicate function, and terminating when
  7884. * the predicate function returns `false`. Excludes the element that caused the
  7885. * predicate function to fail. The predicate function is passed one argument:
  7886. * *(value)*.
  7887. *
  7888. * Dispatches to the `takeWhile` method of the second argument, if present.
  7889. *
  7890. * Acts as a transducer if a transformer is given in list position.
  7891. *
  7892. * @func
  7893. * @memberOf R
  7894. * @since v0.1.0
  7895. * @category List
  7896. * @sig (a -> Boolean) -> [a] -> [a]
  7897. * @sig (a -> Boolean) -> String -> String
  7898. * @param {Function} fn The function called per iteration.
  7899. * @param {Array} xs The collection to iterate over.
  7900. * @return {Array} A new array.
  7901. * @see R.dropWhile, R.transduce, R.addIndex
  7902. * @example
  7903. *
  7904. * const isNotFour = x => x !== 4;
  7905. *
  7906. * R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3]
  7907. *
  7908. * R.takeWhile(x => x !== 'd' , 'Ramda'); //=> 'Ram'
  7909. */
  7910. var takeWhile = _curry2(_dispatchable(['takeWhile'], _xtakeWhile, function takeWhile(fn, xs) {
  7911. var idx = 0;
  7912. var len = xs.length;
  7913. while (idx < len && fn(xs[idx])) {
  7914. idx += 1;
  7915. }
  7916. return slice(0, idx, xs);
  7917. }));
  7918. function XTap(f, xf) {
  7919. this.xf = xf;
  7920. this.f = f;
  7921. }
  7922. XTap.prototype['@@transducer/init'] = _xfBase.init;
  7923. XTap.prototype['@@transducer/result'] = _xfBase.result;
  7924. XTap.prototype['@@transducer/step'] = function(result, input) {
  7925. this.f(input);
  7926. return this.xf['@@transducer/step'](result, input);
  7927. };
  7928. var _xtap = _curry2(function _xtap(f, xf) { return new XTap(f, xf); });
  7929. /**
  7930. * Runs the given function with the supplied object, then returns the object.
  7931. *
  7932. * Acts as a transducer if a transformer is given as second parameter.
  7933. *
  7934. * @func
  7935. * @memberOf R
  7936. * @since v0.1.0
  7937. * @category Function
  7938. * @sig (a -> *) -> a -> a
  7939. * @param {Function} fn The function to call with `x`. The return value of `fn` will be thrown away.
  7940. * @param {*} x
  7941. * @return {*} `x`.
  7942. * @example
  7943. *
  7944. * const sayX = x => console.log('x is ' + x);
  7945. * R.tap(sayX, 100); //=> 100
  7946. * // logs 'x is 100'
  7947. * @symb R.tap(f, a) = a
  7948. */
  7949. var tap = _curry2(_dispatchable([], _xtap, function tap(fn, x) {
  7950. fn(x);
  7951. return x;
  7952. }));
  7953. function _isRegExp(x) {
  7954. return Object.prototype.toString.call(x) === '[object RegExp]';
  7955. }
  7956. /**
  7957. * Determines whether a given string matches a given regular expression.
  7958. *
  7959. * @func
  7960. * @memberOf R
  7961. * @since v0.12.0
  7962. * @category String
  7963. * @sig RegExp -> String -> Boolean
  7964. * @param {RegExp} pattern
  7965. * @param {String} str
  7966. * @return {Boolean}
  7967. * @see R.match
  7968. * @example
  7969. *
  7970. * R.test(/^x/, 'xyz'); //=> true
  7971. * R.test(/^y/, 'xyz'); //=> false
  7972. */
  7973. var test = _curry2(function test(pattern, str) {
  7974. if (!_isRegExp(pattern)) {
  7975. throw new TypeError('‘test’ requires a value of type RegExp as its first argument; received ' + toString$1(pattern));
  7976. }
  7977. return _cloneRegExp(pattern).test(str);
  7978. });
  7979. /**
  7980. * Returns the result of applying the onSuccess function to the value inside
  7981. * a successfully resolved promise. This is useful for working with promises
  7982. * inside function compositions.
  7983. *
  7984. * @func
  7985. * @memberOf R
  7986. * @category Function
  7987. * @sig (a -> b) -> (Promise e a) -> (Promise e b)
  7988. * @sig (a -> (Promise e b)) -> (Promise e a) -> (Promise e b)
  7989. * @param {Function} onSuccess The function to apply. Can return a value or a promise of a value.
  7990. * @param {Promise} p
  7991. * @return {Promise} The result of calling `p.then(onSuccess)`
  7992. * @see R.otherwise
  7993. * @example
  7994. *
  7995. * var makeQuery = (email) => ({ query: { email }});
  7996. *
  7997. * //getMemberName :: String -> Promise ({firstName, lastName})
  7998. * var getMemberName = R.pipe(
  7999. * makeQuery,
  8000. * fetchMember,
  8001. * R.then(R.pick(['firstName', 'lastName']))
  8002. * );
  8003. */
  8004. const then = _curry2(function then(f, p) {
  8005. _assertPromise('then', p);
  8006. return p.then(f);
  8007. });
  8008. /**
  8009. * The lower case version of a string.
  8010. *
  8011. * @func
  8012. * @memberOf R
  8013. * @since v0.9.0
  8014. * @category String
  8015. * @sig String -> String
  8016. * @param {String} str The string to lower case.
  8017. * @return {String} The lower case version of `str`.
  8018. * @see R.toUpper
  8019. * @example
  8020. *
  8021. * R.toLower('XYZ'); //=> 'xyz'
  8022. */
  8023. var toLower = invoker(0, 'toLowerCase');
  8024. /**
  8025. * Converts an object into an array of key, value arrays. Only the object's
  8026. * own properties are used.
  8027. * Note that the order of the output array is not guaranteed to be consistent
  8028. * across different JS platforms.
  8029. *
  8030. * @func
  8031. * @memberOf R
  8032. * @since v0.4.0
  8033. * @category Object
  8034. * @sig {String: *} -> [[String,*]]
  8035. * @param {Object} obj The object to extract from
  8036. * @return {Array} An array of key, value arrays from the object's own properties.
  8037. * @see R.fromPairs
  8038. * @example
  8039. *
  8040. * R.toPairs({a: 1, b: 2, c: 3}); //=> [['a', 1], ['b', 2], ['c', 3]]
  8041. */
  8042. var toPairs = _curry1(function toPairs(obj) {
  8043. var pairs = [];
  8044. for (var prop in obj) {
  8045. if (_has(prop, obj)) {
  8046. pairs[pairs.length] = [prop, obj[prop]];
  8047. }
  8048. }
  8049. return pairs;
  8050. });
  8051. /**
  8052. * Converts an object into an array of key, value arrays. The object's own
  8053. * properties and prototype properties are used. Note that the order of the
  8054. * output array is not guaranteed to be consistent across different JS
  8055. * platforms.
  8056. *
  8057. * @func
  8058. * @memberOf R
  8059. * @since v0.4.0
  8060. * @category Object
  8061. * @sig {String: *} -> [[String,*]]
  8062. * @param {Object} obj The object to extract from
  8063. * @return {Array} An array of key, value arrays from the object's own
  8064. * and prototype properties.
  8065. * @example
  8066. *
  8067. * const F = function() { this.x = 'X'; };
  8068. * F.prototype.y = 'Y';
  8069. * const f = new F();
  8070. * R.toPairsIn(f); //=> [['x','X'], ['y','Y']]
  8071. */
  8072. var toPairsIn = _curry1(function toPairsIn(obj) {
  8073. var pairs = [];
  8074. for (var prop in obj) {
  8075. pairs[pairs.length] = [prop, obj[prop]];
  8076. }
  8077. return pairs;
  8078. });
  8079. /**
  8080. * The upper case version of a string.
  8081. *
  8082. * @func
  8083. * @memberOf R
  8084. * @since v0.9.0
  8085. * @category String
  8086. * @sig String -> String
  8087. * @param {String} str The string to upper case.
  8088. * @return {String} The upper case version of `str`.
  8089. * @see R.toLower
  8090. * @example
  8091. *
  8092. * R.toUpper('abc'); //=> 'ABC'
  8093. */
  8094. var toUpper = invoker(0, 'toUpperCase');
  8095. /**
  8096. * Initializes a transducer using supplied iterator function. Returns a single
  8097. * item by iterating through the list, successively calling the transformed
  8098. * iterator function and passing it an accumulator value and the current value
  8099. * from the array, and then passing the result to the next call.
  8100. *
  8101. * The iterator function receives two values: *(acc, value)*. It will be
  8102. * wrapped as a transformer to initialize the transducer. A transformer can be
  8103. * passed directly in place of an iterator function. In both cases, iteration
  8104. * may be stopped early with the [`R.reduced`](#reduced) function.
  8105. *
  8106. * A transducer is a function that accepts a transformer and returns a
  8107. * transformer and can be composed directly.
  8108. *
  8109. * A transformer is an an object that provides a 2-arity reducing iterator
  8110. * function, step, 0-arity initial value function, init, and 1-arity result
  8111. * extraction function, result. The step function is used as the iterator
  8112. * function in reduce. The result function is used to convert the final
  8113. * accumulator into the return type and in most cases is
  8114. * [`R.identity`](#identity). The init function can be used to provide an
  8115. * initial accumulator, but is ignored by transduce.
  8116. *
  8117. * The iteration is performed with [`R.reduce`](#reduce) after initializing the transducer.
  8118. *
  8119. * @func
  8120. * @memberOf R
  8121. * @since v0.12.0
  8122. * @category List
  8123. * @sig (c -> c) -> ((a, b) -> a) -> a -> [b] -> a
  8124. * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
  8125. * @param {Function} fn The iterator function. Receives two values, the accumulator and the
  8126. * current element from the array. Wrapped as transformer, if necessary, and used to
  8127. * initialize the transducer
  8128. * @param {*} acc The initial accumulator value.
  8129. * @param {Array} list The list to iterate over.
  8130. * @return {*} The final, accumulated value.
  8131. * @see R.reduce, R.reduced, R.into
  8132. * @example
  8133. *
  8134. * const numbers = [1, 2, 3, 4];
  8135. * const transducer = R.compose(R.map(R.add(1)), R.take(2));
  8136. * R.transduce(transducer, R.flip(R.append), [], numbers); //=> [2, 3]
  8137. *
  8138. * const isOdd = (x) => x % 2 === 1;
  8139. * const firstOddTransducer = R.compose(R.filter(isOdd), R.take(1));
  8140. * R.transduce(firstOddTransducer, R.flip(R.append), [], R.range(0, 100)); //=> [1]
  8141. */
  8142. var transduce = curryN(4, function transduce(xf, fn, acc, list) {
  8143. return _reduce(xf(typeof fn === 'function' ? _xwrap(fn) : fn), acc, list);
  8144. });
  8145. /**
  8146. * Transposes the rows and columns of a 2D list.
  8147. * When passed a list of `n` lists of length `x`,
  8148. * returns a list of `x` lists of length `n`.
  8149. *
  8150. *
  8151. * @func
  8152. * @memberOf R
  8153. * @since v0.19.0
  8154. * @category List
  8155. * @sig [[a]] -> [[a]]
  8156. * @param {Array} list A 2D list
  8157. * @return {Array} A 2D list
  8158. * @example
  8159. *
  8160. * R.transpose([[1, 'a'], [2, 'b'], [3, 'c']]) //=> [[1, 2, 3], ['a', 'b', 'c']]
  8161. * R.transpose([[1, 2, 3], ['a', 'b', 'c']]) //=> [[1, 'a'], [2, 'b'], [3, 'c']]
  8162. *
  8163. * // If some of the rows are shorter than the following rows, their elements are skipped:
  8164. * R.transpose([[10, 11], [20], [], [30, 31, 32]]) //=> [[10, 20, 30], [11, 31], [32]]
  8165. * @symb R.transpose([[a], [b], [c]]) = [a, b, c]
  8166. * @symb R.transpose([[a, b], [c, d]]) = [[a, c], [b, d]]
  8167. * @symb R.transpose([[a, b], [c]]) = [[a, c], [b]]
  8168. */
  8169. var transpose = _curry1(function transpose(outerlist) {
  8170. var i = 0;
  8171. var result = [];
  8172. while (i < outerlist.length) {
  8173. var innerlist = outerlist[i];
  8174. var j = 0;
  8175. while (j < innerlist.length) {
  8176. if (typeof result[j] === 'undefined') {
  8177. result[j] = [];
  8178. }
  8179. result[j].push(innerlist[j]);
  8180. j += 1;
  8181. }
  8182. i += 1;
  8183. }
  8184. return result;
  8185. });
  8186. /**
  8187. * Maps an [Applicative](https://github.com/fantasyland/fantasy-land#applicative)-returning
  8188. * function over a [Traversable](https://github.com/fantasyland/fantasy-land#traversable),
  8189. * then uses [`sequence`](#sequence) to transform the resulting Traversable of Applicative
  8190. * into an Applicative of Traversable.
  8191. *
  8192. * Dispatches to the `traverse` method of the third argument, if present.
  8193. *
  8194. * @func
  8195. * @memberOf R
  8196. * @since v0.19.0
  8197. * @category List
  8198. * @sig (Applicative f, Traversable t) => (a -> f a) -> (a -> f b) -> t a -> f (t b)
  8199. * @param {Function} of
  8200. * @param {Function} f
  8201. * @param {*} traversable
  8202. * @return {*}
  8203. * @see R.sequence
  8204. * @example
  8205. *
  8206. * // Returns `Maybe.Nothing` if the given divisor is `0`
  8207. * const safeDiv = n => d => d === 0 ? Maybe.Nothing() : Maybe.Just(n / d)
  8208. *
  8209. * R.traverse(Maybe.of, safeDiv(10), [2, 4, 5]); //=> Maybe.Just([5, 2.5, 2])
  8210. * R.traverse(Maybe.of, safeDiv(10), [2, 0, 5]); //=> Maybe.Nothing
  8211. */
  8212. var traverse = _curry3(function traverse(of, f, traversable) {
  8213. return typeof traversable['fantasy-land/traverse'] === 'function' ?
  8214. traversable['fantasy-land/traverse'](f, of) :
  8215. sequence(of, map(f, traversable));
  8216. });
  8217. var ws = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003' +
  8218. '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028' +
  8219. '\u2029\uFEFF';
  8220. var zeroWidth = '\u200b';
  8221. var hasProtoTrim = (typeof String.prototype.trim === 'function');
  8222. /**
  8223. * Removes (strips) whitespace from both ends of the string.
  8224. *
  8225. * @func
  8226. * @memberOf R
  8227. * @since v0.6.0
  8228. * @category String
  8229. * @sig String -> String
  8230. * @param {String} str The string to trim.
  8231. * @return {String} Trimmed version of `str`.
  8232. * @example
  8233. *
  8234. * R.trim(' xyz '); //=> 'xyz'
  8235. * R.map(R.trim, R.split(',', 'x, y, z')); //=> ['x', 'y', 'z']
  8236. */
  8237. var trim = !hasProtoTrim || (ws.trim() || !zeroWidth.trim()) ?
  8238. _curry1(function trim(str) {
  8239. var beginRx = new RegExp('^[' + ws + '][' + ws + ']*');
  8240. var endRx = new RegExp('[' + ws + '][' + ws + ']*$');
  8241. return str.replace(beginRx, '').replace(endRx, '');
  8242. }) :
  8243. _curry1(function trim(str) {
  8244. return str.trim();
  8245. });
  8246. /**
  8247. * `tryCatch` takes two functions, a `tryer` and a `catcher`. The returned
  8248. * function evaluates the `tryer`; if it does not throw, it simply returns the
  8249. * result. If the `tryer` *does* throw, the returned function evaluates the
  8250. * `catcher` function and returns its result. Note that for effective
  8251. * composition with this function, both the `tryer` and `catcher` functions
  8252. * must return the same type of results.
  8253. *
  8254. * @func
  8255. * @memberOf R
  8256. * @since v0.20.0
  8257. * @category Function
  8258. * @sig (...x -> a) -> ((e, ...x) -> a) -> (...x -> a)
  8259. * @param {Function} tryer The function that may throw.
  8260. * @param {Function} catcher The function that will be evaluated if `tryer` throws.
  8261. * @return {Function} A new function that will catch exceptions and send then to the catcher.
  8262. * @example
  8263. *
  8264. * R.tryCatch(R.prop('x'), R.F)({x: true}); //=> true
  8265. * R.tryCatch(() => { throw 'foo'}, R.always('catched'))('bar') // => 'catched'
  8266. * R.tryCatch(R.times(R.identity), R.always([]))('s') // => []
  8267. `` */
  8268. var tryCatch = _curry2(function _tryCatch(tryer, catcher) {
  8269. return _arity(tryer.length, function() {
  8270. try {
  8271. return tryer.apply(this, arguments);
  8272. } catch (e) {
  8273. return catcher.apply(this, _concat([e], arguments));
  8274. }
  8275. });
  8276. });
  8277. /**
  8278. * Takes a function `fn`, which takes a single array argument, and returns a
  8279. * function which:
  8280. *
  8281. * - takes any number of positional arguments;
  8282. * - passes these arguments to `fn` as an array; and
  8283. * - returns the result.
  8284. *
  8285. * In other words, `R.unapply` derives a variadic function from a function which
  8286. * takes an array. `R.unapply` is the inverse of [`R.apply`](#apply).
  8287. *
  8288. * @func
  8289. * @memberOf R
  8290. * @since v0.8.0
  8291. * @category Function
  8292. * @sig ([*...] -> a) -> (*... -> a)
  8293. * @param {Function} fn
  8294. * @return {Function}
  8295. * @see R.apply
  8296. * @example
  8297. *
  8298. * R.unapply(JSON.stringify)(1, 2, 3); //=> '[1,2,3]'
  8299. * @symb R.unapply(f)(a, b) = f([a, b])
  8300. */
  8301. var unapply = _curry1(function unapply(fn) {
  8302. return function() {
  8303. return fn(Array.prototype.slice.call(arguments, 0));
  8304. };
  8305. });
  8306. /**
  8307. * Wraps a function of any arity (including nullary) in a function that accepts
  8308. * exactly 1 parameter. Any extraneous parameters will not be passed to the
  8309. * supplied function.
  8310. *
  8311. * @func
  8312. * @memberOf R
  8313. * @since v0.2.0
  8314. * @category Function
  8315. * @sig (* -> b) -> (a -> b)
  8316. * @param {Function} fn The function to wrap.
  8317. * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
  8318. * arity 1.
  8319. * @see R.binary, R.nAry
  8320. * @example
  8321. *
  8322. * const takesTwoArgs = function(a, b) {
  8323. * return [a, b];
  8324. * };
  8325. * takesTwoArgs.length; //=> 2
  8326. * takesTwoArgs(1, 2); //=> [1, 2]
  8327. *
  8328. * const takesOneArg = R.unary(takesTwoArgs);
  8329. * takesOneArg.length; //=> 1
  8330. * // Only 1 argument is passed to the wrapped function
  8331. * takesOneArg(1, 2); //=> [1, undefined]
  8332. * @symb R.unary(f)(a, b, c) = f(a)
  8333. */
  8334. var unary = _curry1(function unary(fn) {
  8335. return nAry(1, fn);
  8336. });
  8337. /**
  8338. * Returns a function of arity `n` from a (manually) curried function.
  8339. *
  8340. * @func
  8341. * @memberOf R
  8342. * @since v0.14.0
  8343. * @category Function
  8344. * @sig Number -> (a -> b) -> (a -> c)
  8345. * @param {Number} length The arity for the returned function.
  8346. * @param {Function} fn The function to uncurry.
  8347. * @return {Function} A new function.
  8348. * @see R.curry
  8349. * @example
  8350. *
  8351. * const addFour = a => b => c => d => a + b + c + d;
  8352. *
  8353. * const uncurriedAddFour = R.uncurryN(4, addFour);
  8354. * uncurriedAddFour(1, 2, 3, 4); //=> 10
  8355. */
  8356. var uncurryN = _curry2(function uncurryN(depth, fn) {
  8357. return curryN(depth, function() {
  8358. var currentDepth = 1;
  8359. var value = fn;
  8360. var idx = 0;
  8361. var endIdx;
  8362. while (currentDepth <= depth && typeof value === 'function') {
  8363. endIdx = currentDepth === depth ? arguments.length : idx + value.length;
  8364. value = value.apply(this, Array.prototype.slice.call(arguments, idx, endIdx));
  8365. currentDepth += 1;
  8366. idx = endIdx;
  8367. }
  8368. return value;
  8369. });
  8370. });
  8371. /**
  8372. * Builds a list from a seed value. Accepts an iterator function, which returns
  8373. * either false to stop iteration or an array of length 2 containing the value
  8374. * to add to the resulting list and the seed to be used in the next call to the
  8375. * iterator function.
  8376. *
  8377. * The iterator function receives one argument: *(seed)*.
  8378. *
  8379. * @func
  8380. * @memberOf R
  8381. * @since v0.10.0
  8382. * @category List
  8383. * @sig (a -> [b]) -> * -> [b]
  8384. * @param {Function} fn The iterator function. receives one argument, `seed`, and returns
  8385. * either false to quit iteration or an array of length two to proceed. The element
  8386. * at index 0 of this array will be added to the resulting array, and the element
  8387. * at index 1 will be passed to the next call to `fn`.
  8388. * @param {*} seed The seed value.
  8389. * @return {Array} The final list.
  8390. * @example
  8391. *
  8392. * const f = n => n > 50 ? false : [-n, n + 10];
  8393. * R.unfold(f, 10); //=> [-10, -20, -30, -40, -50]
  8394. * @symb R.unfold(f, x) = [f(x)[0], f(f(x)[1])[0], f(f(f(x)[1])[1])[0], ...]
  8395. */
  8396. var unfold = _curry2(function unfold(fn, seed) {
  8397. var pair = fn(seed);
  8398. var result = [];
  8399. while (pair && pair.length) {
  8400. result[result.length] = pair[0];
  8401. pair = fn(pair[1]);
  8402. }
  8403. return result;
  8404. });
  8405. /**
  8406. * Combines two lists into a set (i.e. no duplicates) composed of the elements
  8407. * of each list.
  8408. *
  8409. * @func
  8410. * @memberOf R
  8411. * @since v0.1.0
  8412. * @category Relation
  8413. * @sig [*] -> [*] -> [*]
  8414. * @param {Array} as The first list.
  8415. * @param {Array} bs The second list.
  8416. * @return {Array} The first and second lists concatenated, with
  8417. * duplicates removed.
  8418. * @example
  8419. *
  8420. * R.union([1, 2, 3], [2, 3, 4]); //=> [1, 2, 3, 4]
  8421. */
  8422. var union = _curry2(compose(uniq, _concat));
  8423. /**
  8424. * Returns a new list containing only one copy of each element in the original
  8425. * list, based upon the value returned by applying the supplied predicate to
  8426. * two list elements. Prefers the first item if two items compare equal based
  8427. * on the predicate.
  8428. *
  8429. * @func
  8430. * @memberOf R
  8431. * @since v0.2.0
  8432. * @category List
  8433. * @sig ((a, a) -> Boolean) -> [a] -> [a]
  8434. * @param {Function} pred A predicate used to test whether two items are equal.
  8435. * @param {Array} list The array to consider.
  8436. * @return {Array} The list of unique items.
  8437. * @example
  8438. *
  8439. * const strEq = R.eqBy(String);
  8440. * R.uniqWith(strEq)([1, '1', 2, 1]); //=> [1, 2]
  8441. * R.uniqWith(strEq)([{}, {}]); //=> [{}]
  8442. * R.uniqWith(strEq)([1, '1', 1]); //=> [1]
  8443. * R.uniqWith(strEq)(['1', 1, 1]); //=> ['1']
  8444. */
  8445. var uniqWith = _curry2(function uniqWith(pred, list) {
  8446. var idx = 0;
  8447. var len = list.length;
  8448. var result = [];
  8449. var item;
  8450. while (idx < len) {
  8451. item = list[idx];
  8452. if (!_includesWith(pred, item, result)) {
  8453. result[result.length] = item;
  8454. }
  8455. idx += 1;
  8456. }
  8457. return result;
  8458. });
  8459. /**
  8460. * Combines two lists into a set (i.e. no duplicates) composed of the elements
  8461. * of each list. Duplication is determined according to the value returned by
  8462. * applying the supplied predicate to two list elements.
  8463. *
  8464. * @func
  8465. * @memberOf R
  8466. * @since v0.1.0
  8467. * @category Relation
  8468. * @sig ((a, a) -> Boolean) -> [*] -> [*] -> [*]
  8469. * @param {Function} pred A predicate used to test whether two items are equal.
  8470. * @param {Array} list1 The first list.
  8471. * @param {Array} list2 The second list.
  8472. * @return {Array} The first and second lists concatenated, with
  8473. * duplicates removed.
  8474. * @see R.union
  8475. * @example
  8476. *
  8477. * const l1 = [{a: 1}, {a: 2}];
  8478. * const l2 = [{a: 1}, {a: 4}];
  8479. * R.unionWith(R.eqBy(R.prop('a')), l1, l2); //=> [{a: 1}, {a: 2}, {a: 4}]
  8480. */
  8481. var unionWith = _curry3(function unionWith(pred, list1, list2) {
  8482. return uniqWith(pred, _concat(list1, list2));
  8483. });
  8484. /**
  8485. * Tests the final argument by passing it to the given predicate function. If
  8486. * the predicate is not satisfied, the function will return the result of
  8487. * calling the `whenFalseFn` function with the same argument. If the predicate
  8488. * is satisfied, the argument is returned as is.
  8489. *
  8490. * @func
  8491. * @memberOf R
  8492. * @since v0.18.0
  8493. * @category Logic
  8494. * @sig (a -> Boolean) -> (a -> a) -> a -> a
  8495. * @param {Function} pred A predicate function
  8496. * @param {Function} whenFalseFn A function to invoke when the `pred` evaluates
  8497. * to a falsy value.
  8498. * @param {*} x An object to test with the `pred` function and
  8499. * pass to `whenFalseFn` if necessary.
  8500. * @return {*} Either `x` or the result of applying `x` to `whenFalseFn`.
  8501. * @see R.ifElse, R.when, R.cond
  8502. * @example
  8503. *
  8504. * let safeInc = R.unless(R.isNil, R.inc);
  8505. * safeInc(null); //=> null
  8506. * safeInc(1); //=> 2
  8507. */
  8508. var unless = _curry3(function unless(pred, whenFalseFn, x) {
  8509. return pred(x) ? x : whenFalseFn(x);
  8510. });
  8511. /**
  8512. * Shorthand for `R.chain(R.identity)`, which removes one level of nesting from
  8513. * any [Chain](https://github.com/fantasyland/fantasy-land#chain).
  8514. *
  8515. * @func
  8516. * @memberOf R
  8517. * @since v0.3.0
  8518. * @category List
  8519. * @sig Chain c => c (c a) -> c a
  8520. * @param {*} list
  8521. * @return {*}
  8522. * @see R.flatten, R.chain
  8523. * @example
  8524. *
  8525. * R.unnest([1, [2], [[3]]]); //=> [1, 2, [3]]
  8526. * R.unnest([[1, 2], [3, 4], [5, 6]]); //=> [1, 2, 3, 4, 5, 6]
  8527. */
  8528. var unnest = chain(_identity);
  8529. /**
  8530. * Takes a predicate, a transformation function, and an initial value,
  8531. * and returns a value of the same type as the initial value.
  8532. * It does so by applying the transformation until the predicate is satisfied,
  8533. * at which point it returns the satisfactory value.
  8534. *
  8535. * @func
  8536. * @memberOf R
  8537. * @since v0.20.0
  8538. * @category Logic
  8539. * @sig (a -> Boolean) -> (a -> a) -> a -> a
  8540. * @param {Function} pred A predicate function
  8541. * @param {Function} fn The iterator function
  8542. * @param {*} init Initial value
  8543. * @return {*} Final value that satisfies predicate
  8544. * @example
  8545. *
  8546. * R.until(R.gt(R.__, 100), R.multiply(2))(1) // => 128
  8547. */
  8548. var until = _curry3(function until(pred, fn, init) {
  8549. var val = init;
  8550. while (!pred(val)) {
  8551. val = fn(val);
  8552. }
  8553. return val;
  8554. });
  8555. /**
  8556. * Returns a list of all the properties, including prototype properties, of the
  8557. * supplied object.
  8558. * Note that the order of the output array is not guaranteed to be consistent
  8559. * across different JS platforms.
  8560. *
  8561. * @func
  8562. * @memberOf R
  8563. * @since v0.2.0
  8564. * @category Object
  8565. * @sig {k: v} -> [v]
  8566. * @param {Object} obj The object to extract values from
  8567. * @return {Array} An array of the values of the object's own and prototype properties.
  8568. * @see R.values, R.keysIn
  8569. * @example
  8570. *
  8571. * const F = function() { this.x = 'X'; };
  8572. * F.prototype.y = 'Y';
  8573. * const f = new F();
  8574. * R.valuesIn(f); //=> ['X', 'Y']
  8575. */
  8576. var valuesIn = _curry1(function valuesIn(obj) {
  8577. var prop;
  8578. var vs = [];
  8579. for (prop in obj) {
  8580. vs[vs.length] = obj[prop];
  8581. }
  8582. return vs;
  8583. });
  8584. // `Const` is a functor that effectively ignores the function given to `map`.
  8585. var Const = function(x) {
  8586. return {value: x, 'fantasy-land/map': function() { return this; }};
  8587. };
  8588. /**
  8589. * Returns a "view" of the given data structure, determined by the given lens.
  8590. * The lens's focus determines which portion of the data structure is visible.
  8591. *
  8592. * @func
  8593. * @memberOf R
  8594. * @since v0.16.0
  8595. * @category Object
  8596. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  8597. * @sig Lens s a -> s -> a
  8598. * @param {Lens} lens
  8599. * @param {*} x
  8600. * @return {*}
  8601. * @see R.prop, R.lensIndex, R.lensProp
  8602. * @example
  8603. *
  8604. * const xLens = R.lensProp('x');
  8605. *
  8606. * R.view(xLens, {x: 1, y: 2}); //=> 1
  8607. * R.view(xLens, {x: 4, y: 2}); //=> 4
  8608. */
  8609. var view = _curry2(function view(lens, x) {
  8610. // Using `Const` effectively ignores the setter function of the `lens`,
  8611. // leaving the value returned by the getter function unmodified.
  8612. return lens(Const)(x).value;
  8613. });
  8614. /**
  8615. * Tests the final argument by passing it to the given predicate function. If
  8616. * the predicate is satisfied, the function will return the result of calling
  8617. * the `whenTrueFn` function with the same argument. If the predicate is not
  8618. * satisfied, the argument is returned as is.
  8619. *
  8620. * @func
  8621. * @memberOf R
  8622. * @since v0.18.0
  8623. * @category Logic
  8624. * @sig (a -> Boolean) -> (a -> a) -> a -> a
  8625. * @param {Function} pred A predicate function
  8626. * @param {Function} whenTrueFn A function to invoke when the `condition`
  8627. * evaluates to a truthy value.
  8628. * @param {*} x An object to test with the `pred` function and
  8629. * pass to `whenTrueFn` if necessary.
  8630. * @return {*} Either `x` or the result of applying `x` to `whenTrueFn`.
  8631. * @see R.ifElse, R.unless, R.cond
  8632. * @example
  8633. *
  8634. * // truncate :: String -> String
  8635. * const truncate = R.when(
  8636. * R.propSatisfies(R.gt(R.__, 10), 'length'),
  8637. * R.pipe(R.take(10), R.append('…'), R.join(''))
  8638. * );
  8639. * truncate('12345'); //=> '12345'
  8640. * truncate('0123456789ABC'); //=> '0123456789…'
  8641. */
  8642. var when = _curry3(function when(pred, whenTrueFn, x) {
  8643. return pred(x) ? whenTrueFn(x) : x;
  8644. });
  8645. /**
  8646. * Takes a spec object and a test object; returns true if the test satisfies
  8647. * the spec. Each of the spec's own properties must be a predicate function.
  8648. * Each predicate is applied to the value of the corresponding property of the
  8649. * test object. `where` returns true if all the predicates return true, false
  8650. * otherwise.
  8651. *
  8652. * `where` is well suited to declaratively expressing constraints for other
  8653. * functions such as [`filter`](#filter) and [`find`](#find).
  8654. *
  8655. * @func
  8656. * @memberOf R
  8657. * @since v0.1.1
  8658. * @category Object
  8659. * @sig {String: (* -> Boolean)} -> {String: *} -> Boolean
  8660. * @param {Object} spec
  8661. * @param {Object} testObj
  8662. * @return {Boolean}
  8663. * @see R.propSatisfies, R.whereEq
  8664. * @example
  8665. *
  8666. * // pred :: Object -> Boolean
  8667. * const pred = R.where({
  8668. * a: R.equals('foo'),
  8669. * b: R.complement(R.equals('bar')),
  8670. * x: R.gt(R.__, 10),
  8671. * y: R.lt(R.__, 20)
  8672. * });
  8673. *
  8674. * pred({a: 'foo', b: 'xxx', x: 11, y: 19}); //=> true
  8675. * pred({a: 'xxx', b: 'xxx', x: 11, y: 19}); //=> false
  8676. * pred({a: 'foo', b: 'bar', x: 11, y: 19}); //=> false
  8677. * pred({a: 'foo', b: 'xxx', x: 10, y: 19}); //=> false
  8678. * pred({a: 'foo', b: 'xxx', x: 11, y: 20}); //=> false
  8679. */
  8680. var where = _curry2(function where(spec, testObj) {
  8681. for (var prop in spec) {
  8682. if (_has(prop, spec) && !spec[prop](testObj[prop])) {
  8683. return false;
  8684. }
  8685. }
  8686. return true;
  8687. });
  8688. /**
  8689. * Takes a spec object and a test object; returns true if the test satisfies
  8690. * the spec, false otherwise. An object satisfies the spec if, for each of the
  8691. * spec's own properties, accessing that property of the object gives the same
  8692. * value (in [`R.equals`](#equals) terms) as accessing that property of the
  8693. * spec.
  8694. *
  8695. * `whereEq` is a specialization of [`where`](#where).
  8696. *
  8697. * @func
  8698. * @memberOf R
  8699. * @since v0.14.0
  8700. * @category Object
  8701. * @sig {String: *} -> {String: *} -> Boolean
  8702. * @param {Object} spec
  8703. * @param {Object} testObj
  8704. * @return {Boolean}
  8705. * @see R.propEq, R.where
  8706. * @example
  8707. *
  8708. * // pred :: Object -> Boolean
  8709. * const pred = R.whereEq({a: 1, b: 2});
  8710. *
  8711. * pred({a: 1}); //=> false
  8712. * pred({a: 1, b: 2}); //=> true
  8713. * pred({a: 1, b: 2, c: 3}); //=> true
  8714. * pred({a: 1, b: 1}); //=> false
  8715. */
  8716. var whereEq = _curry2(function whereEq(spec, testObj) {
  8717. return where(map(equals, spec), testObj);
  8718. });
  8719. /**
  8720. * Returns a new list without values in the first argument.
  8721. * [`R.equals`](#equals) is used to determine equality.
  8722. *
  8723. * Acts as a transducer if a transformer is given in list position.
  8724. *
  8725. * @func
  8726. * @memberOf R
  8727. * @since v0.19.0
  8728. * @category List
  8729. * @sig [a] -> [a] -> [a]
  8730. * @param {Array} list1 The values to be removed from `list2`.
  8731. * @param {Array} list2 The array to remove values from.
  8732. * @return {Array} The new array without values in `list1`.
  8733. * @see R.transduce, R.difference, R.remove
  8734. * @example
  8735. *
  8736. * R.without([1, 2], [1, 2, 1, 3, 4]); //=> [3, 4]
  8737. */
  8738. var without = _curry2(function(xs, list) {
  8739. return reject(flip(_includes)(xs), list);
  8740. });
  8741. /**
  8742. * Creates a new list out of the two supplied by creating each possible pair
  8743. * from the lists.
  8744. *
  8745. * @func
  8746. * @memberOf R
  8747. * @since v0.1.0
  8748. * @category List
  8749. * @sig [a] -> [b] -> [[a,b]]
  8750. * @param {Array} as The first list.
  8751. * @param {Array} bs The second list.
  8752. * @return {Array} The list made by combining each possible pair from
  8753. * `as` and `bs` into pairs (`[a, b]`).
  8754. * @example
  8755. *
  8756. * R.xprod([1, 2], ['a', 'b']); //=> [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
  8757. * @symb R.xprod([a, b], [c, d]) = [[a, c], [a, d], [b, c], [b, d]]
  8758. */
  8759. var xprod = _curry2(function xprod(a, b) { // = xprodWith(prepend); (takes about 3 times as long...)
  8760. var idx = 0;
  8761. var ilen = a.length;
  8762. var j;
  8763. var jlen = b.length;
  8764. var result = [];
  8765. while (idx < ilen) {
  8766. j = 0;
  8767. while (j < jlen) {
  8768. result[result.length] = [a[idx], b[j]];
  8769. j += 1;
  8770. }
  8771. idx += 1;
  8772. }
  8773. return result;
  8774. });
  8775. /**
  8776. * Creates a new list out of the two supplied by pairing up equally-positioned
  8777. * items from both lists. The returned list is truncated to the length of the
  8778. * shorter of the two input lists.
  8779. * Note: `zip` is equivalent to `zipWith(function(a, b) { return [a, b] })`.
  8780. *
  8781. * @func
  8782. * @memberOf R
  8783. * @since v0.1.0
  8784. * @category List
  8785. * @sig [a] -> [b] -> [[a,b]]
  8786. * @param {Array} list1 The first array to consider.
  8787. * @param {Array} list2 The second array to consider.
  8788. * @return {Array} The list made by pairing up same-indexed elements of `list1` and `list2`.
  8789. * @example
  8790. *
  8791. * R.zip([1, 2, 3], ['a', 'b', 'c']); //=> [[1, 'a'], [2, 'b'], [3, 'c']]
  8792. * @symb R.zip([a, b, c], [d, e, f]) = [[a, d], [b, e], [c, f]]
  8793. */
  8794. var zip = _curry2(function zip(a, b) {
  8795. var rv = [];
  8796. var idx = 0;
  8797. var len = Math.min(a.length, b.length);
  8798. while (idx < len) {
  8799. rv[idx] = [a[idx], b[idx]];
  8800. idx += 1;
  8801. }
  8802. return rv;
  8803. });
  8804. /**
  8805. * Creates a new object out of a list of keys and a list of values.
  8806. * Key/value pairing is truncated to the length of the shorter of the two lists.
  8807. * Note: `zipObj` is equivalent to `pipe(zip, fromPairs)`.
  8808. *
  8809. * @func
  8810. * @memberOf R
  8811. * @since v0.3.0
  8812. * @category List
  8813. * @sig [String] -> [*] -> {String: *}
  8814. * @param {Array} keys The array that will be properties on the output object.
  8815. * @param {Array} values The list of values on the output object.
  8816. * @return {Object} The object made by pairing up same-indexed elements of `keys` and `values`.
  8817. * @example
  8818. *
  8819. * R.zipObj(['a', 'b', 'c'], [1, 2, 3]); //=> {a: 1, b: 2, c: 3}
  8820. */
  8821. var zipObj = _curry2(function zipObj(keys, values) {
  8822. var idx = 0;
  8823. var len = Math.min(keys.length, values.length);
  8824. var out = {};
  8825. while (idx < len) {
  8826. out[keys[idx]] = values[idx];
  8827. idx += 1;
  8828. }
  8829. return out;
  8830. });
  8831. /**
  8832. * Creates a new list out of the two supplied by applying the function to each
  8833. * equally-positioned pair in the lists. The returned list is truncated to the
  8834. * length of the shorter of the two input lists.
  8835. *
  8836. * @function
  8837. * @memberOf R
  8838. * @since v0.1.0
  8839. * @category List
  8840. * @sig ((a, b) -> c) -> [a] -> [b] -> [c]
  8841. * @param {Function} fn The function used to combine the two elements into one value.
  8842. * @param {Array} list1 The first array to consider.
  8843. * @param {Array} list2 The second array to consider.
  8844. * @return {Array} The list made by combining same-indexed elements of `list1` and `list2`
  8845. * using `fn`.
  8846. * @example
  8847. *
  8848. * const f = (x, y) => {
  8849. * // ...
  8850. * };
  8851. * R.zipWith(f, [1, 2, 3], ['a', 'b', 'c']);
  8852. * //=> [f(1, 'a'), f(2, 'b'), f(3, 'c')]
  8853. * @symb R.zipWith(fn, [a, b, c], [d, e, f]) = [fn(a, d), fn(b, e), fn(c, f)]
  8854. */
  8855. var zipWith = _curry3(function zipWith(fn, a, b) {
  8856. var rv = [];
  8857. var idx = 0;
  8858. var len = Math.min(a.length, b.length);
  8859. while (idx < len) {
  8860. rv[idx] = fn(a[idx], b[idx]);
  8861. idx += 1;
  8862. }
  8863. return rv;
  8864. });
  8865. /**
  8866. * Creates a thunk out of a function. A thunk delays a calculation until
  8867. * its result is needed, providing lazy evaluation of arguments.
  8868. *
  8869. * @func
  8870. * @memberOf R
  8871. * @category Function
  8872. * @sig ((a, b, ..., j) -> k) -> (a, b, ..., j) -> (() -> k)
  8873. * @param {Function} fn A function to wrap in a thunk
  8874. * @return {Function} Expects arguments for `fn` and returns a new function
  8875. * that, when called, applies those arguments to `fn`.
  8876. * @see R.partial, R.partialRight
  8877. * @example
  8878. *
  8879. * R.thunkify(R.identity)(42)(); //=> 42
  8880. * R.thunkify((a, b) => a + b)(25, 17)(); //=> 42
  8881. */
  8882. var thunkify = _curry1(function thunkify(fn) {
  8883. return curryN(fn.length, function createThunk() {
  8884. const fnArgs = arguments;
  8885. return function invokeThunk() {
  8886. return fn.apply(this, fnArgs);
  8887. };
  8888. });
  8889. });
  8890. exports.F = F;
  8891. exports.T = T;
  8892. exports.__ = __;
  8893. exports.add = add;
  8894. exports.addIndex = addIndex;
  8895. exports.adjust = adjust;
  8896. exports.all = all;
  8897. exports.allPass = allPass;
  8898. exports.always = always;
  8899. exports.and = and;
  8900. exports.any = any;
  8901. exports.anyPass = anyPass;
  8902. exports.ap = ap;
  8903. exports.aperture = aperture;
  8904. exports.append = append;
  8905. exports.apply = apply;
  8906. exports.applySpec = applySpec;
  8907. exports.applyTo = applyTo;
  8908. exports.ascend = ascend;
  8909. exports.assoc = assoc;
  8910. exports.assocPath = assocPath;
  8911. exports.binary = binary;
  8912. exports.bind = bind;
  8913. exports.both = both;
  8914. exports.call = call;
  8915. exports.chain = chain;
  8916. exports.clamp = clamp;
  8917. exports.clone = clone;
  8918. exports.comparator = comparator;
  8919. exports.complement = complement;
  8920. exports.compose = compose;
  8921. exports.composeK = composeK;
  8922. exports.composeP = composeP;
  8923. exports.composeWith = composeWith;
  8924. exports.concat = concat;
  8925. exports.cond = cond;
  8926. exports.construct = construct;
  8927. exports.constructN = constructN;
  8928. exports.contains = contains$1;
  8929. exports.converge = converge;
  8930. exports.countBy = countBy;
  8931. exports.curry = curry;
  8932. exports.curryN = curryN;
  8933. exports.dec = dec;
  8934. exports.defaultTo = defaultTo;
  8935. exports.descend = descend;
  8936. exports.difference = difference;
  8937. exports.differenceWith = differenceWith;
  8938. exports.dissoc = dissoc;
  8939. exports.dissocPath = dissocPath;
  8940. exports.divide = divide;
  8941. exports.drop = drop;
  8942. exports.dropLast = dropLast;
  8943. exports.dropLastWhile = dropLastWhile;
  8944. exports.dropRepeats = dropRepeats;
  8945. exports.dropRepeatsWith = dropRepeatsWith;
  8946. exports.dropWhile = dropWhile;
  8947. exports.either = either;
  8948. exports.empty = empty;
  8949. exports.endsWith = endsWith;
  8950. exports.eqBy = eqBy;
  8951. exports.eqProps = eqProps;
  8952. exports.equals = equals;
  8953. exports.evolve = evolve;
  8954. exports.filter = filter;
  8955. exports.find = find;
  8956. exports.findIndex = findIndex;
  8957. exports.findLast = findLast;
  8958. exports.findLastIndex = findLastIndex;
  8959. exports.flatten = flatten;
  8960. exports.flip = flip;
  8961. exports.forEach = forEach;
  8962. exports.forEachObjIndexed = forEachObjIndexed;
  8963. exports.fromPairs = fromPairs;
  8964. exports.groupBy = groupBy;
  8965. exports.groupWith = groupWith;
  8966. exports.gt = gt;
  8967. exports.gte = gte;
  8968. exports.has = has;
  8969. exports.hasIn = hasIn;
  8970. exports.hasPath = hasPath;
  8971. exports.head = head;
  8972. exports.identical = identical;
  8973. exports.identity = identity;
  8974. exports.ifElse = ifElse;
  8975. exports.inc = inc;
  8976. exports.includes = includes;
  8977. exports.indexBy = indexBy;
  8978. exports.indexOf = indexOf;
  8979. exports.init = init;
  8980. exports.innerJoin = innerJoin;
  8981. exports.insert = insert;
  8982. exports.insertAll = insertAll;
  8983. exports.intersection = intersection;
  8984. exports.intersperse = intersperse;
  8985. exports.into = into;
  8986. exports.invert = invert;
  8987. exports.invertObj = invertObj;
  8988. exports.invoker = invoker;
  8989. exports.is = is;
  8990. exports.isEmpty = isEmpty;
  8991. exports.isNil = isNil;
  8992. exports.join = join;
  8993. exports.juxt = juxt;
  8994. exports.keys = keys;
  8995. exports.keysIn = keysIn;
  8996. exports.last = last;
  8997. exports.lastIndexOf = lastIndexOf;
  8998. exports.length = length;
  8999. exports.lens = lens;
  9000. exports.lensIndex = lensIndex;
  9001. exports.lensPath = lensPath;
  9002. exports.lensProp = lensProp;
  9003. exports.lift = lift;
  9004. exports.liftN = liftN;
  9005. exports.lt = lt;
  9006. exports.lte = lte;
  9007. exports.map = map;
  9008. exports.mapAccum = mapAccum;
  9009. exports.mapAccumRight = mapAccumRight;
  9010. exports.mapObjIndexed = mapObjIndexed;
  9011. exports.match = match;
  9012. exports.mathMod = mathMod;
  9013. exports.max = max;
  9014. exports.maxBy = maxBy;
  9015. exports.mean = mean;
  9016. exports.median = median;
  9017. exports.memoizeWith = memoizeWith;
  9018. exports.merge = merge;
  9019. exports.mergeAll = mergeAll;
  9020. exports.mergeDeepLeft = mergeDeepLeft;
  9021. exports.mergeDeepRight = mergeDeepRight;
  9022. exports.mergeDeepWith = mergeDeepWith;
  9023. exports.mergeDeepWithKey = mergeDeepWithKey;
  9024. exports.mergeLeft = mergeLeft;
  9025. exports.mergeRight = mergeRight;
  9026. exports.mergeWith = mergeWith;
  9027. exports.mergeWithKey = mergeWithKey;
  9028. exports.min = min;
  9029. exports.minBy = minBy;
  9030. exports.modulo = modulo;
  9031. exports.multiply = multiply;
  9032. exports.nAry = nAry;
  9033. exports.negate = negate;
  9034. exports.none = none;
  9035. exports.not = not;
  9036. exports.nth = nth;
  9037. exports.nthArg = nthArg;
  9038. exports.o = o;
  9039. exports.objOf = objOf;
  9040. exports.of = of;
  9041. exports.omit = omit;
  9042. exports.once = once;
  9043. exports.or = or;
  9044. exports.otherwise = otherwise;
  9045. exports.over = over;
  9046. exports.pair = pair;
  9047. exports.partial = partial;
  9048. exports.partialRight = partialRight;
  9049. exports.partition = partition;
  9050. exports.path = path;
  9051. exports.pathEq = pathEq;
  9052. exports.pathOr = pathOr;
  9053. exports.pathSatisfies = pathSatisfies;
  9054. exports.pick = pick;
  9055. exports.pickAll = pickAll;
  9056. exports.pickBy = pickBy;
  9057. exports.pipe = pipe;
  9058. exports.pipeK = pipeK;
  9059. exports.pipeP = pipeP;
  9060. exports.pipeWith = pipeWith;
  9061. exports.pluck = pluck;
  9062. exports.prepend = prepend;
  9063. exports.product = product;
  9064. exports.project = project;
  9065. exports.prop = prop;
  9066. exports.propEq = propEq;
  9067. exports.propIs = propIs;
  9068. exports.propOr = propOr;
  9069. exports.propSatisfies = propSatisfies;
  9070. exports.props = props;
  9071. exports.range = range;
  9072. exports.reduce = reduce;
  9073. exports.reduceBy = reduceBy;
  9074. exports.reduceRight = reduceRight;
  9075. exports.reduceWhile = reduceWhile;
  9076. exports.reduced = reduced;
  9077. exports.reject = reject;
  9078. exports.remove = remove;
  9079. exports.repeat = repeat;
  9080. exports.replace = replace;
  9081. exports.reverse = reverse;
  9082. exports.scan = scan;
  9083. exports.sequence = sequence;
  9084. exports.set = set;
  9085. exports.slice = slice;
  9086. exports.sort = sort;
  9087. exports.sortBy = sortBy;
  9088. exports.sortWith = sortWith;
  9089. exports.split = split;
  9090. exports.splitAt = splitAt;
  9091. exports.splitEvery = splitEvery;
  9092. exports.splitWhen = splitWhen;
  9093. exports.startsWith = startsWith;
  9094. exports.subtract = subtract;
  9095. exports.sum = sum;
  9096. exports.symmetricDifference = symmetricDifference;
  9097. exports.symmetricDifferenceWith = symmetricDifferenceWith;
  9098. exports.tail = tail;
  9099. exports.take = take;
  9100. exports.takeLast = takeLast;
  9101. exports.takeLastWhile = takeLastWhile;
  9102. exports.takeWhile = takeWhile;
  9103. exports.tap = tap;
  9104. exports.test = test;
  9105. exports.then = then;
  9106. exports.times = times;
  9107. exports.toLower = toLower;
  9108. exports.toPairs = toPairs;
  9109. exports.toPairsIn = toPairsIn;
  9110. exports.toString = toString$1;
  9111. exports.toUpper = toUpper;
  9112. exports.transduce = transduce;
  9113. exports.transpose = transpose;
  9114. exports.traverse = traverse;
  9115. exports.trim = trim;
  9116. exports.tryCatch = tryCatch;
  9117. exports.type = type;
  9118. exports.unapply = unapply;
  9119. exports.unary = unary;
  9120. exports.uncurryN = uncurryN;
  9121. exports.unfold = unfold;
  9122. exports.union = union;
  9123. exports.unionWith = unionWith;
  9124. exports.uniq = uniq;
  9125. exports.uniqBy = uniqBy;
  9126. exports.uniqWith = uniqWith;
  9127. exports.unless = unless;
  9128. exports.unnest = unnest;
  9129. exports.until = until;
  9130. exports.update = update;
  9131. exports.useWith = useWith;
  9132. exports.values = values;
  9133. exports.valuesIn = valuesIn;
  9134. exports.view = view;
  9135. exports.when = when;
  9136. exports.where = where;
  9137. exports.whereEq = whereEq;
  9138. exports.without = without;
  9139. exports.xprod = xprod;
  9140. exports.zip = zip;
  9141. exports.zipObj = zipObj;
  9142. exports.zipWith = zipWith;
  9143. exports.thunkify = thunkify;
  9144. Object.defineProperty(exports, '__esModule', { value: true });
  9145. })));