{"id":16613168,"url":"https://github.com/c-cube/lwt-pipe","last_synced_at":"2025-03-21T14:31:16.328Z","repository":{"id":7940750,"uuid":"56882550","full_name":"c-cube/lwt-pipe","owner":"c-cube","description":"[beta] A multi-consumer, multi-producers blocking queue and stream for Lwt","archived":false,"fork":false,"pushed_at":"2022-02-18T21:10:17.000Z","size":71,"stargazers_count":30,"open_issues_count":0,"forks_count":9,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-16T00:14:43.260Z","etag":null,"topics":["lwt","ocaml","pipe","stream"],"latest_commit_sha":null,"homepage":"https://c-cube.github.io/lwt-pipe/","language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/c-cube.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-22T20:13:38.000Z","updated_at":"2024-11-04T23:40:31.000Z","dependencies_parsed_at":"2022-08-06T20:15:22.565Z","dependency_job_id":null,"html_url":"https://github.com/c-cube/lwt-pipe","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-cube%2Flwt-pipe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-cube%2Flwt-pipe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-cube%2Flwt-pipe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-cube%2Flwt-pipe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/c-cube","download_url":"https://codeload.github.com/c-cube/lwt-pipe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244141444,"owners_count":20404837,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lwt","ocaml","pipe","stream"],"created_at":"2024-10-12T01:46:17.368Z","updated_at":"2025-03-21T14:31:16.042Z","avatar_url":"https://github.com/c-cube.png","language":"OCaml","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Lwt Pipe [![build](https://github.com/c-cube/lwt-pipe/actions/workflows/main.yml/badge.svg)](https://github.com/c-cube/lwt-pipe/actions/workflows/main.yml)\n\nAn alternative to `Lwt_stream` with interfaces for producers and consumers\nand a bounded internal buffer.\n\n[Online Documentation](https://c-cube.github.io/lwt-pipe/)\n\n## Build\n\n```\nopam install lwt-pipe\n```\n\nor:\n\n```\nopam pin https://github.com/c-cube/lwt-pipe.git\n```\n\n## License\n\npermissive free software (BSD-2)\n\n## Use\n\nA pipe can be used as a regular iterator:\n\n```ocaml\n# #require \"lwt\";;\n# #require \"lwt-pipe\";;\n\n# open Lwt.Infix;;\n\n# let l = [1;2;3;4];;\nval l : int list = [1; 2; 3; 4]\n\n# Lwt_pipe.of_list l\n  |\u003e Lwt_pipe.Reader.map ~f:(fun x-\u003ex+1)\n  |\u003e Lwt_pipe.to_list;;\n- : int list = [2; 3; 4; 5]\n```\n\nBut also as a streaming queue (here with two producers `push_ints` that will\nput `1, 2, … 5` into the pipe, and one reader that consumes the whole pipe):\n\n```ocaml\n# let rec push_ints p i : unit Lwt.t =\n  if i \u003c= 0 then Lwt.return ()\n  else Lwt_pipe.write_exn p i \u003e\u003e= fun () -\u003e push_ints p (i-1) ;;\nval push_ints : (int, [\u003c `r | `w \u003e `w ]) Lwt_pipe.t -\u003e int -\u003e unit Lwt.t =\n  \u003cfun\u003e\n\n# let reader =\n    let p = Lwt_pipe.create ~max_size:3 () in\n    let t1 = push_ints p 5\n    and t2 = push_ints p 5\n    and t_read = Lwt_pipe.to_list p in\n    Lwt.join [t1;t2] \u003e\u003e= fun () -\u003e\n    Lwt_pipe.close p \u003e\u003e= fun () -\u003e\n    t_read\n  in\n  List.sort compare @@ Lwt_main.run reader\n  ;;\n- : int list = [1; 1; 2; 2; 3; 3; 4; 4; 5; 5]\n```\n\nThis can be expressed with higher level constructs:\n\n\n```ocaml\n# let rec list_range i = if i\u003c=0 then [] else i :: list_range (i-1);;\nval list_range : int -\u003e int list = \u003cfun\u003e\n# let int_range n = Lwt_pipe.of_list @@ list_range n ;;\nval int_range : int -\u003e int Lwt_pipe.Reader.t = \u003cfun\u003e\n\n# Lwt_main.run @@ Lwt_pipe.to_list (int_range 5);;\n- : int list = [5; 4; 3; 2; 1]\n\n# let reader =\n    let p1 = int_range 6\n    and p2 = int_range 6\n    and p3 = int_range 6 in\n    Lwt_pipe.to_list (Lwt_pipe.Reader.merge_all [p1;p2;p3])\n  in\n  List.sort compare @@ Lwt_main.run reader\n  ;;\n- : int list = [1; 1; 1; 2; 2; 2; 3; 3; 3; 4; 4; 4; 5; 5; 5; 6; 6; 6]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc-cube%2Flwt-pipe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fc-cube%2Flwt-pipe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc-cube%2Flwt-pipe/lists"}