Re: Make many insertions in long string

"Ashish Agarwal [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAMu2m2+NgCV-nfVitRJ-dTmPfHeqAAbjcE0VQqwHc_SLB0cuag@mail.gmail.com>
Note, that the Rope data structure is also available in Core:
https://github.com/janestreet/core_kernel/blob/master/src/rope.mli

On Sun, Nov 29, 2015 at 11:18 AM, [email protected]
[ocaml_beginners] <[email protected]> wrote:

>
>
> Thank you for your feedback. It helped me a lot ! In reality, using
> String.concat is not that hard (see the relatively short&simple code
> below), you just have to be careful a little bit about the order of the
> operations.
>  I was amazed by the efficiency of that solution : it executes my initial
> computation in less than a second!
>
>
>   let adjacent_elements_pair_list l=
>         let rec sub_f=
>             (function (accu,a,rl)->match rl with
>             []->List.rev(accu)
>             |b::x->sub_f((a,b)::accu,b,x)
>             ) in
>         match l with
>         []->[]
>         |u::v->sub_f([],u,v);;
>
>   let interval s a b=String.sub s (a-1) (b-a+1);;
>
>
>   let insert_at_points s l=
>      let n=String.length s in
>      let temp1=(n,"")::(List.rev_append l [0,""]) in
>      let temp2=adjacent_elements_pair_list temp1 in
>      let temp3=List.rev_map (fun ((j2,w2),(j1,w1))->(interval s (j1+1)
> j2)^w2) temp2 in
>      String.concat "" temp3;;
>
>   let example= insert_at_points "123456789" [3,"aaa";6,"bb"];;
>
>
> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.