I am trying to implement the concurrent version of List.map function in Ocaml. I know there is a similar function in module Async.Std.Deferred.List, but here I am just trying to attack this myself.
Here is what I have:
let deferred_listmap (f : 'a -> 'b Deferred.t) (l : 'a list) =
let f' acc elem =
(f elem) >>= fun s ->
return (acc@[s]) in
List.fold_left f' [] l
And obviously there is a type matching error here, since fold_left requires a function f with type ('b list -> 'a -> 'b list), while the f' I have here is of type ('b list -> 'a -> 'b list Deferred.t). But for this I cannot help since I am using a bind(>>=) here which expects the return type of its function argument to be a deferred type. So I am getting kind of a contradiction here, and I cannot think of other uses on bind or folds or other functions in List module to solve this. Any suggestions?
Asyncutilities to reimplement this function, I suppose thatAsync.Std.Deferred.allwould be handy (and is most certainly what the real function uses). That function in turn may be implemented using a fold ofboth(in the same module) on the list. - didierc