Skip to content

Add Gen.traverse combinatorΒ #294

@n-osborne

Description

@n-osborne

Signature is:

val traverse : ('a -> 'b Gen.t) -> 'a list -> 'b list Gen.t

Implementation proposition:

let rec traverse gen = function
  | [] -> pure []
  | x :: xs ->
      let* x = gen x and* xs = traverse gen xs in
      pure (x :: xs)

Or in short:

let traverse gen xs =
  let cons_f x xs = List.cons <$> gen x <*> xs in
  List.fold_right cons_f xs (pure [])

I had been in need for this particular combinator in a relatively specific case: when generating well-typed terms of a language with n-ary functions. The term generator takes a type from the object language, and when I wanted to generate a function call, I needed to generate a list of terms from a list of types from the object language.

But maybe there are other use-cases?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions