From a98a1aeb3f31912c39ec84713e5d2c07d8e7875a Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 4 Dec 2025 21:11:22 -0500 Subject: [PATCH] feat emitter: add `flat_map` --- src/emitter/emitter.ml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/emitter/emitter.ml b/src/emitter/emitter.ml index fb2f9285..7b8ffbfb 100644 --- a/src/emitter/emitter.ml +++ b/src/emitter/emitter.ml @@ -33,11 +33,21 @@ let[@inline] closed self : bool = self.closed () let[@inline] flush_and_close (self : _ t) : unit = self.flush_and_close () -(** [map f emitter] returns a new emitter that applies [f] to signals before - passing them to [emitter] *) +(** [map f emitter] returns a new emitter that applies [f] to signals item-wise + before passing them to [emitter] *) let map (f : 'a -> 'b) (self : 'b t) : 'a t = { self with emit = (fun l -> self.emit (List.map f l)) } +(** [map_l f emitter] applies [f] to incoming lists of signals, and emits the + resulting list (if non empty) *) +let flat_map (f : 'a list -> 'b list) (self : 'b t) : 'a t = + let emit l = + match f l with + | [] -> () + | fl -> self.emit fl + in + { self with emit } + (** [tap f e] is like [e], but every signal is passed to [f] *) let tap (f : 'a -> unit) (self : 'a t) : 'a t = let emit l =