Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file seq_s.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openMonad(* This module is about sequences mixed in with Lwt, the most common monad
here is the Lwt monad, we open its syntax module for the whole file (and
shadow it when needed. *)openLwt_syntaxtype+'anode='aLwt_seq.node=Nil|Consof'a*'atand'at=unit->'anodeLwt.tletprotectseq()=Lwt.applyseq()letnil_s=Lwt.returnNilletempty()=nil_s(* we define [return] at the end of the file to avoid shadowing the one from the
opened monad syntax *)letreturn_sp()=Lwt.map(funx->Cons(x,empty))pletconsitemt()=Lwt.return(Cons(item,t))letcons_sitemt()=let*item=iteminreturn(Cons(item,t))letrecappendtatb()=let*n=ta()inmatchnwith|Nil->tb()|Cons(item,ta)->return(Cons(item,appendtatb))letfirsts=let*n=s()inmatchnwithNil->return_none|Cons(x,_)->return_somexletrecfold_leftfaccseq=let*n=seq()inmatchnwith|Nil->returnacc|Cons(item,seq)->fold_leftf(faccitem)seqletfold_leftfaccseq=fold_leftfacc@@protectseqletrecfold_left_efaccseq=let*n=seq()inmatchnwith|Nil->return_okacc|Cons(item,seq)->(matchfaccitemwith|Error_ase->Lwt.returne|Okacc->fold_left_efaccseq)letfold_left_efaccseq=fold_left_efacc@@protectseqletrecfold_left_sfaccseq=let*n=seq()inmatchnwith|Nil->returnacc|Cons(item,seq)->let*acc=facciteminfold_left_sfaccseqletfold_left_sfaccseq=fold_left_sfacc@@protectseqletrecfold_left_esfaccseq=let*n=seq()inletopenLwt_result_syntaxinmatchnwith|Nil->returnacc|Cons(item,seq)->let*acc=facciteminfold_left_esfaccseqletfold_left_esfaccseq=fold_left_esfacc@@protectseqletreciterfseq=let*n=seq()inmatchnwith|Nil->return_unit|Cons(item,seq)->fitem;iterfseqletiterfseq=iterf@@protectseqletreciter_efseq=let*n=seq()inletopenLwt_result_syntaxinmatchnwith|Nil->return_unit|Cons(item,seq)->let*?()=fiteminiter_efseqletiter_efseq=iter_ef@@protectseqletreciter_sfseq=let*n=seq()inmatchnwith|Nil->return_unit|Cons(item,seq)->let*()=fiteminiter_sfseqletiter_sfseq=iter_sf@@protectseqletreciter_esfseq=let*n=seq()inletopenLwt_result_syntaxinmatchnwith|Nil->return_unit|Cons(item,seq)->let*()=fiteminiter_esfseqletiter_esfseq=iter_esf@@protectseqletiter_epfseq=let*ps=fold_left(funaccitem->Lwt.applyfitem::acc)[]seqinLwt_result_syntax.joinpsletiter_pfseq=let*ps=fold_left(funaccitem->Lwt.applyfitem::acc)[]seqinjoinpsletrecmapfseq()=let*n=seq()inmatchnwith|Nil->nil_s|Cons(item,seq)->return(Cons(fitem,mapfseq))letmapfseq=mapf@@protectseqletrecmap_sfseq()=let*n=seq()inmatchnwith|Nil->nil_s|Cons(item,seq)->let*item=fiteminreturn(Cons(item,map_sfseq))letmap_sfseq=map_sf@@protectseqletrecfilterfseq()=let*n=seq()inmatchnwith|Nil->nil_s|Cons(item,seq)->iffitemthenreturn(Cons(item,seq))elsefilterfseq()letfilterfseq=filterf@@protectseqletrecfilter_sfseq()=let*n=seq()inmatchnwith|Nil->nil_s|Cons(item,seq)->let*b=fiteminifbthenreturn(Cons(item,filter_sfseq))elsefilter_sfseq()letfilter_sfseq=filter_sf@@protectseqletrecfilter_mapfseq()=let*n=seq()inmatchnwith|Nil->nil_s|Cons(item,seq)->(matchfitemwith|None->filter_mapfseq()|Someitem->return(Cons(item,filter_mapfseq)))letfilter_mapfseq=filter_mapf@@protectseqletrecfilter_map_sfseq()=let*n=seq()inmatchnwith|Nil->nil_s|Cons(item,seq)->(let*item_o=fiteminmatchitem_owith|None->filter_map_sfseq()|Someitem->return(Cons(item,filter_map_sfseq)))letfilter_map_sfseq=filter_map_sf@@protectseqletrecunfoldfa()=matchfawith|None->nil_s|Some(item,a)->Lwt.return(Cons(item,unfoldfa))letrecunfold_sfa()=let*cont=fainmatchcontwith|None->nil_s|Some(item,a)->return(Cons(item,unfold_sfa))letrecof_seqseq()=matchseq()with|Stdlib.Seq.Nil->nil_s|Stdlib.Seq.Cons(e,seq)->return(Cons(e,of_seqseq))letrecof_seq_sseq()=matchseq()with|Stdlib.Seq.Nil->nil_s|Stdlib.Seq.Cons(p,seq)->let*e=pinreturn(Cons(e,of_seq_sseq))letreturnx()=Lwt.return(Cons(x,empty))