Seq.fold and boolean drive

I can never find the source code of the main F # libraries. I know this is supposedly open, but google is not good for me, helping me find it, if so, I would look at img Seq.fold, but the question arises here.

Does anyone see any problems with the following snippet:

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

It seems that this is not water, and I can experiment to test it. But I wanted to ask about it. If any one evaluation inside myList returns 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

return the correct results - I will just be more convenient when I see the source ...

+3
source share
6 answers

I think you are looking for something like this:

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

" ", acc false , evaluation item , false.

MSDN fold

+4

Seq.exists :

let success = 
  [1;2;3;40;5;2] 
  |> Seq.exists (fun item->(item>30))
  |> not
+4

, Visual Studio F # , F #. , , Seq.fold :

let fold f seed items =
    let mutable res = seed
    for item in items do
        res <- f res item
    res

- myList false, , ...

, evaluation. false, - , Seq.forall.

+1

-

let l = [true; true; true; false; true]

let eval x = x

let x = (true, l) ||> Seq.fold(fun acc item -> acc && (eval item))

?

let l = [true; false; true]

l |> Seq.forall id
0

, 10 2010.

, , , .

// Seq module
let fold<'T,'State> f (x:'State) (source : seq<'T>)  = 
    checkNonNull "source" source
    use e = source.GetEnumerator() 
    let mutable state = x 
    while e.MoveNext() do
        state <- f state  e.Current;
    state


// Array module
let fold<'T,'State> (f : 'State -> 'T -> 'State) (acc: 'State) (array:'T[]) = //'
    checkNonNull "array" array
    let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
    let mutable state = acc 
    let len = array.Length
    for i = 0 to len - 1 do 
        state <- f.Invoke(state,array.[i])
    state


// List module
let fold<'T,'State> f (s:'State) (list: 'T list) = 
    match list with 
    | [] -> s
    | _ -> 
        let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
        let rec loop s xs = 
            match xs with 
            | [] -> s
            | h::t -> loop (f.Invoke(s,h)) t
        loop s list


// MapTree module (Used by Map module)
let rec fold (f:OptimizedClosures.FSharpFunc<_,_,_,_>) x m  = 
    match m with 
    | MapEmpty -> x
    | MapOne(k,v) -> f.Invoke(x,k,v)
    | MapNode(k,v,l,r,_) -> 
        let x = fold f x l
        let x = f.Invoke(x,k,v)
        fold f x r

// Map module
let fold<'Key,'T,'State when 'Key : comparison> f (z:'State) (m:Map<'Key,'T>) = //'
    let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
    MapTree.fold f z m.Tree


// SetTree module (Used by Set module)
let rec fold f x m = 
    match m with 
    | SetNode(k,l,r,_) -> 
        let x = fold f x l in 
        let x = f x k
        fold f x r
    | SetOne(k) -> f x k
    | SetEmpty -> x

// Set module
let fold<'T,'State  when 'T : comparison> f (z:'State) (s : Set<'T>) = //'
    SetTree.fold f z s.Tree
0

, , , .

, false, false: Seq.forAll.

, :

let success = Seq.forAll evaluation myList

, TechNeilogys ()

let success = not (Seq.exists evaluation myList)

, evaluation , . , , , . , Seq.forAll , . , Seq.exists, Seq.takeWhile,...

, . .

1: - , , .

2: Seq.scan fold. Seq.scan fold, , , . , : Seq.last (Seq.scan folder initialState mySequence) = Seq.fold folder initialState mySequence

3: Seq.scan. : Seq.takeWhile, Seq.forAll, Seq.exists,...

None , , .

let allDistinct mySequence =
    let folder state element =
        match state with
            | Some elementsSoFar when not (Set.contains element elementsSoFar) ->
                Some (Set.add element elementsSoFar)
            | _ ->
                None
    let initialState = Some Set.empty
    let scanning = Seq.scan folder initialState mySequence
    Seq.forall Option.isSome scanning
0

Source: https://habr.com/ru/post/1770167/


All Articles