5条回答
  •  难免孤独
    2020-12-19 04:36

    Probably no the most efficient solution, but this works:

    let takeAndSkipWhile f s = Seq.takeWhile f s, Seq.skipWhile f s
    
    let takeAndSkipUntil f = takeAndSkipWhile (f >> not)
    
    let rec splitOn f s =
        if Seq.isEmpty s then
            Seq.empty
        else
            let pre, post =
                if f (Seq.head s) then
                    takeAndSkipUntil f (Seq.skip 1 s)
                    |> fun (a, b) ->
                        Seq.append [Seq.head s] a, b
                else
                    takeAndSkipUntil f s
            if Seq.isEmpty pre then
                Seq.singleton post
            else
                Seq.append [pre] (splitOn f post)
    
    splitOn ((=) 1) [1;2;3;4;1;5;6;7;1;9] // int list is compatible with seq
    

    The type of splitOn is ('a -> bool) -> seq<'a> -> seq>. I haven't tested it on many inputs, but it seems to work.

提交回复
热议问题