Tagged: folds

Till functions

Just wanted to share a couple little functions that I was playing with since it made my code terse and readable. At first I needed a way to fold a function until a predicate. This way I could stop and didn’t have to continue through the whole list. Then I needed to be able to do the same kind of thing but choosing all elements up until a predicate.

Folding

First, folding. I wanted to be able to get all the characters up until white space. For example:

let (++) a b = a.ToString() + b.ToString()

let upToSpaces str = foldTill Char.IsWhiteSpace (++) "" str

Which led me to write the following fold function. Granted it’s not lazy evaluated, but for me that was OK.

let foldTill check predicate seed list= 
    let rec foldTill' acc = function
        | [] -> acc
        | (h::t) -> match check h with 
                        | false 
Read more