Re: F# fsharp
Jason Yandell <[email protected]>
| Newsgroups | gmane.games.devel.sweng |
|---|---|
| Message-ID | <[email protected]> |
On Sun, May 30, 2010 at 2:09 PM, Brandon Van Every <[email protected]> wrote: > > On Sun, May 30, 2010 at 1:19 PM, Jason Yandell <[email protected]> wrote: > > Brandon, this stance is quite unexpected coming from an F# proponent. > > > > Perhaps you have not seen async workflows (monads) yet? > > Monads are the biggest FP head scratch of all time. I never swallowed > the pure FP Kool-Aid. Yes, yes they are. You don't need to start off by implementing your own, though F# supports that and combinators and other wonderful FP "head-scratches". F# ships with several what-they-call workflows, most notably the async workflow. You don't need to know you are doing monads. I used the word monad because I thought any FP enthusiasts reading this would be enticed by the fact that F# supported them so cleanly. Here's a code snippet from a link I posted yesterday. Does this cause a head-scratch? It does not for me. It inspires me. The monads REMOVE the complexity. ( async { } is the monad. ) #light open System open System.IO open System.Text.RegularExpressions open Microsoft.FSharp.Control.CommonExtensions let path = @"C:\Users\robert\Documents\Fielding" let readFileAsync filePath = async { // open and read file let fileStream = File.OpenText(filePath) // JY: let! means kick this function off of the currently executing thread // JY: until this read is complete let! text = fileStream.ReadToEndAsync() // JY: continue executing. The monad and IO libs handle all of the complexity // find all the "words" using a regex let word = new Regex("\w+") let matches = word.Matches(text) let words = { for m in matches -> m.Value } // count unique words using a set let uniqueWords = Set.of_seq words // print the results let name = Path.GetFileNameWithoutExtension(filePath) do Console.WriteLine("{0} - Words: {1} Unique words: {2} ", name, matches.Count, uniqueWords.Count) } let main() = let filePaths = Directory.GetFiles(path) let tasks = [ for filePath in filePaths -> readFileAsync filePath ] Async.Run (Async.Parallel tasks) // JY: do all of that work above in the thread pool (5 threads or so) main() Not sure what your threading experience is, but if you've been doing much and you're not impressed, you're not paying attention. This does not justify a full-force shift to F#, but it is a strong language and I believe is as good or better than Google Go's in-language parallel support, but I'll let the audience be the judge. Brandon, you would do well to get behind this feature if you are going to espouse the language. > > You do not have to > > "rearrange everything" to get parallelism in light of those. Your code > > looks nearly identical to the serial version. > > Ok if you say so. Worry about it when I get there. I do say so and you need to get there as it is trivial. Do not fight a fellow F# enthusiast, join me. If you are going to write a game, you're going to be doing IO at least and you would do well to avoid blocking while that is happening, particularly when such a clean mode of programming is available to you in the language that you yourself are espousing. _______________________________________________ Sweng-Gamedev mailing list [email protected] http://lists.midnightryder.com/listinfo.cgi/sweng-gamedev-midnightryder.com