Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seq.fold and boolean accumulator

Tags:

f#

I can never find the source code of the F# core libraries. I know it is supposedly open but google is not kind to me in helping me locate it, if so I would have looked up the impl of Seq.fold - but here goes the question.

Does anybody see any issue with the following snippet:

let success = myList |>
                    Seq.fold 
                        (fun acc item -> evaluation item)
                        false 

Logically it doesn't seem to hold water and I can and will experiment to test it. But I wanted to ask the community. If any single evaluation inside of myList retruns false, I want the success variable to be false...


So the test:

let myList = [true; true]
let success = List.fold (fun acc item -> acc && item) true myList

and

let myList = [true; false; true]
let success = List.fold (fun acc item -> acc && item) true myList

do return the proper results - I just would be more comfy seeing the source...

like image 923
akaphenom Avatar asked Oct 16 '25 03:10

akaphenom


1 Answers

I think what you're looking for is something like this:

let success = myList |>
                    Seq.fold
                        (fun acc item -> acc && evaluation item)
                        true

This also offers "short-circut" evaluation so that if acc is false from a previous evaluation, evaluation item won't run and the expression will simply return false.

MSDN documentation for fold operator

like image 142
Wesley Wiser Avatar answered Oct 19 '25 00:10

Wesley Wiser