Home
Insert
Update snippet 'Finding matching pairs in two sequences'
Title
Description
Here's a function which takes a comparator function and two sequences, and returns tuples consisting of an item from each sequence, where the comparator function returns true for those two items. This is a small part of my wider project to generate guitar chord shapes. One of the requirements there is to take a list of 'wanted' notes for a chord, and a list of 'available' notes within a range of frets and to combine them into an actual set of frettings. That's what led to tryFindFirstPair and hence to findPairs.
Source code
/// Given two sequences and a comparator function, find the pairs of items for /// which the comparator returns true let findPairs compare seqT seqU = seq { for t in seqT do for u in seqU do if (compare t u) then yield (t, u) } /// Given two sequences and a comparator function, find the first pair of items /// for which the comparator returns true let tryFindFirstPair compare seqT seqU = let matches = findPairs compare seqT seqU if not (Seq.isEmpty matches) then Some(Seq.nth 0 matches) else None //// A quick demo to generate lists of rhyming words: /// Reverse a string (from TomasP): let reverse (s:string) = let rec reverseAux idx acc = if (idx = s.Length) then acc else reverseAux (idx+1) ((s.[idx])::acc) new string(Array.ofList (reverseAux 0 [])) /// A crude way to work out the last syllable for a word: let lastSyllable word = let vowels = [|'a';'e';'i';'o';'u';'y'|] let wordRev = reverse word let vowelPos = wordRev.IndexOfAny(vowels, 1) let lastSylRev = wordRev.Substring(0, vowelPos+1) reverse lastSylRev /// Return true when words might rhyme based on their final syllables being /// the same let mightRhyme wordA wordB = (wordA <> wordB) && (lastSyllable wordA = lastSyllable wordB) /// Two steams of words, some of which might rhyme let words1 = ["orange"; "purple"; "hubble"; "indicative"; "mandatory"] let words2 = ["hurple"; "rhubarb"; "tory"; "bubble"] /// Find the rhymes findPairs mightRhyme words1 words2 // Output: seq [("purple", "hurple"); ("hubble", "bubble"); ("mandatory", "tory")] /// Find the rhymes in a single stream of words: let wordsAll = ["orange"; "purple"; "hubble"; "indicative"; "mandatory"; "hurple"; "rhubarb"; "tory"; "bubble"] findPairs mightRhyme wordsAll wordsAll // Output: seq // [("purple", "hurple"); ("hubble", "bubble"); ("mandatory", "tory"); // ("hurple", "purple"); ...]
Tags
learning f#
sequences
learning f#
sequences
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update