123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384(* Copyright (C) 2018--2019 Petter A. Urkedal <paurkedal@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version, with the LGPL-3.0 Linking Exception.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* and the LGPL-3.0 Linking Exception along with this library. If not, see
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
*)openCaqti_stream_sigmoduletypeFIBER=sigtype+'atmoduleInfix:sigval(>>=):'at->('a->'bt)->'btval(>|=):'at->('a->'b)->'btendvalreturn:'a->'atendmoduleMake(Fiber:FIBER):Swithtype'afiber:='aFiber.t=structopenFiber.Infixlet(>>=?)res_futuref=res_future>>=function|Oka->fa|Error_asr->Fiber.returnrlet(>|=?)res_futuref=res_future>>=function|Oka->Fiber.return@@Ok(fa)|Error_asr->Fiber.returnrtype('a,'err)t=unit->('a,'err)nodeFiber.tand('a,'err)node=|Nil|Errorof'err|Consof'a*('a,'err)tletrecfold~ftstate=t()>>=function|Nil->Fiber.return(Okstate)|Errorerr->Fiber.return(Errorerr:('a,'err)result)|Cons(a,t')->fold~ft'(fastate)letrecfold_s~ftstate=t()>>=function|Nil->Fiber.return(Okstate)|Errorerr->Fiber.return(Error(`Congestederr):('a,'err)result)|Cons(a,t')->fastate>>=?fold_s~ft'letreciter_s~ft=t()>>=function|Nil->Fiber.return(Ok())|Errorerr->Fiber.return(Error(`Congestederr):('a,'err)result)|Cons(a,t')->fa>>=?fun()->iter_s~ft'letto_rev_listt=fold~f:List.const[]letto_listt=to_rev_listt>|=?List.revletrecof_listl=fun()->matchlwith|[]->Fiber.returnNil|hd::tl->Fiber.return(Cons(hd,(of_listtl)))letrecmap_result~fxs()=xs()>|=function|Cons(x,xs')->(matchfxwith|Result.Oky->Cons(y,map_result~fxs')|Result.Errore->Errore)|Nil|Error_asr->rend