Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fold inverts the order of a sequence

Tags:

arrays

f#

The order of my Sequence, array etc matters. I have tried converting between List, Seq and Array to see if there are differences and in each case it inverses the order.

I have a seq of [noun] [verb] [ajective] for example, which is transformed into strings and then folded together. A sample response given this template might be "bad run bandits" instead of "bandits run bad".

Any thoughts on why fold does this or how to get it to execute in the appropriate order?

let res = template |> Seq.map(fun pos -> 
                    let e = s |> PredictionEngine.GetRandom
                    pos |> PredictionEngine.GetBestPartOfSpeechWord e)
                    |> Seq.fold(fun acc w -> w.Text + " " + acc) ""
like image 637
David Crook Avatar asked Dec 24 '22 14:12

David Crook


2 Answers

I believe this is just a matter of changing the order of your last function to:

Seq.fold(fun acc w -> acc + " " + w.Text) ""

Like this the new itens get concatenated to the end of the old ones. A minimum example of this working can be seen in the following snippet:

["Bandits";"Run";"Bad"]
|> Seq.fold(fun acc w -> acc + " " + w) ""
|> printfn "%s"
like image 133
William Barbosa Avatar answered Jan 10 '23 01:01

William Barbosa


If you have such common job as concatenating strings I recommend using functions from library.

["noun"; "verb"; "adj"]
|> String.concat " "

On daily basis work, I think, there are not so many problems that require writing custom folding.

like image 29
Bartek Kobyłecki Avatar answered Jan 10 '23 02:01

Bartek Kobyłecki