Skip to content

Instantly share code, notes, and snippets.

@chrismckelt
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismckelt/d9d113edc3d3bf4afe91 to your computer and use it in GitHub Desktop.
Save chrismckelt/d9d113edc3d3bf4afe91 to your computer and use it in GitHub Desktop.
F# AsyncSeq anagram checker -- async run through a list pulling out found results
namespace CodeDojo
module CodeDojo_Anagram =
open System
open System.ServiceModel
open System.Collections.Generic
open Microsoft.FSharp.Linq
open FSharp.Control
[<Literal>]
let testWord = "table"
let testWords = new List<string>()
testWords.Add("bleat")
testWords.Add("blate")
testWords.Add("junk")
let hasWord (word:string) =
let mutable res = true
let a = testWord.ToCharArray() |> Set.ofArray
let b = word.ToCharArray() |> Set.ofArray
let difference = Set.intersect a b
match difference.Count with
| 0 -> false
| _ -> true
let test2 (words:List<string>, (word:string)) : AsyncSeq<string> =
asyncSeq {
let res =
(words)
|> Seq.filter(fun x-> (hasWord(x)) )
|> AsyncSeq.ofSeq
yield! res
}
let runTest = test2(testWords,testWord)
|> AsyncSeq.toObservable
|> fun a -> a.Subscribe(fun x -> printfn "Found '%A' " x)
|> ignore
()
@chrismckelt
Copy link
Author

usage:
let results = CodeDojo.CodeDojo_Anagram.runTest
Console.WriteLine("Press any key to continue...");

@burlistic
Copy link

Can I run in Linqpad?

@chrismckelt
Copy link
Author

yes mate - f sharp program

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment