Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gen.traverse combinator #294

Open
n-osborne opened this issue Sep 5, 2024 · 0 comments
Open

Add Gen.traverse combinator #294

n-osborne opened this issue Sep 5, 2024 · 0 comments

Comments

@n-osborne
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant