[MLton] Monadic MLton.Vector.create with update

Daniel C. Wang danwang@CS.Princeton.EDU
Thu, 30 Mar 2006 09:47:09 -0800


Another generalization that I think would require runtime support would be

val unfoldi_s: 'b * (int * 'b -> ('a * 'b) option) -> 'a vector

where the length of the resulting vector is not known, but unfolding 
terminates by returning NONE.
I suspect you'd want special runtime support to make this efficient. I 
can see this being very useful for taking a large tree and serializing 
it to a vector of bytes whose size is not  known up front. If you run 
into the situation of needing to generate a vector whose size is not 
easily determined up front, you either have to do two passes over your 
data structure or do some buffering and play  games to get it right.

Stephen Weeks wrote:
>> You can write unfold_vec using the (non-monadic) create, but you need a ref
>> cell to carry along the state (if you do it in the tabulator) or an initial
>> value (if you do it in the final thunk).
>>     
>
> Hmm, I wonder if create's tabulator function should also do a fold
> :-).
>
>   
>> Note, your unfold_vec throws away the final state (the 'b returned in the
>> tuple by the last call to the 'b -> 'b * 'a unfolder).
>>     
>
> MLton.Vector.unfoldi does that as well.  Do you think its type should
> be changed to
>
>  val unfoldi: int * 'b * (int * 'b -> 'a * 'b) -> 'a vector * 'b
>
> _______________________________________________
> MLton mailing list
> MLton@mlton.org
> http://mlton.org/mailman/listinfo/mlton
>