No cons function in Base.Sequence?

I have been looking at the Sequence module in the Janestreet’s Base library and I was surprised to see that there is no cons: 'a -> 'a t -> 'a t function. In contrast, such a function is present in the Seq module of the standard library.

Is there a specific reason for omitting cons from Sequence (e.g. its abuse may lead to performance problems) or am I missing an alternative?

Sequence.shift_right is the function you are looking for.

See here for documentation. Also see shift_right_with_list for some explanation of the performance costs associated with shift_right.

1 Like

I wonder if anyone has found much of a use for Seq.cons? In order to provide delayed (lazy) evaluation 'a Seq.t is a thunk. But Seq.cons is a function and not a macro, so for an eager language such as ocaml its first argument is evaluated immediately, which seems somewhat to defeat the purpose.

Given a lazy sequence s, I think in most cases I would cons onto it by hand with fun () -> Cons ([expr], s), so retaining lazy evaluation of [expr]. I guess Seq.cons is mostly used to prepend a literal value to a sequence?