<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Extracting words from a string</title>
        <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx</link>
        <description>I still plan on writing more on scheduling algorithms, but I was at Strange Loop last week and listened to Guy Steele talking about parallel algorithms.  He’s working in a language he called Fortress, but certainly the algorithms could be implemented in C# as well.  The problem Steele was working on to demonstrate a parallel algorithm is splitting a string into words.  For simplicity, we’ll assume that words are only separated by spaces.  Here are some sample tests so you can see the pattern.             void Check(Splitter splitter)             {                Assert.Equal(new List&amp;lt;string&amp;gt; { "This", "is", "a", "sample" }, splitter.Split("This is a sample").ToList());                Assert.Equal(new List&amp;lt;string&amp;gt; { "Here", "is", "another", "sample" }, splitter.Split(" Here is another sample ").ToList());                Assert.Equal(new List&amp;lt;string&amp;gt; { "JustOneWord" }, splitter.Split("JustOneWord").ToList());                Assert.Empty(splitter.Split(" "));                Assert.Empty(splitter.Split(""));                     Assert.Equal(new List&amp;lt;string&amp;gt; { "Here", "is", "a", "sesquipedalian", "string", "of", "words" }, splitter.Split("Here is a sesquipedalian string of words").ToList());            }         In a typical implementation, it’s not too complicated to do this in a sequential way.  Start with an empty list.  Iterate over each character, building a word along the way, and when you hit a space, add the word to the list and start over.  Maybe something like this:                    public abstract class Splitter        {            public abstract IEnumerable&amp;lt;string&amp;gt; Split(string stringToSplit);        }             public class NormalSplitter : Splitter        {            public override IEnumerable&amp;lt;string&amp;gt; Split(string ss)            {                var result = new List&amp;lt;string&amp;gt;();                string word = "";                for (int i = 0; i &amp;lt; ss.Length; ++i)                {                    if (ss[i] == ' ')                    {                        if (!string.IsNullOrEmpty(word))                        {                            result.Add(word);                            word = "";                        }                    }                    else                        word += ss[i];                }                if (!string.IsNullOrEmpty(word))                    result.Add(word);                     return result;            }        }   Of course, I’m not big on For loops.  Linq’s Aggregate function is more or less the same as the Reduce side of the MapReduce algorithm, or some of the Folding methods you see in other languages.  How would we do this using Aggregate?  Instead of the For loop, we give the Aggregate algorithm the seed, or result it should give back for an empty string, and a lambda method that tells it how to combine a list of words, with an additional character, like this:                 string word = "";                var words = ss.AsEnumerable().Aggregate(new List&amp;lt;string&amp;gt;(), (List&amp;lt;string&amp;gt; result, char c) =&amp;gt;   The lambda method more or less is the same as the inside of the normal splitter, above:                     if (c == ' ')                    {                        if (!string.IsNullOrEmpty(word))                        {                            result.Add(word);                            word = "";                        }                    }                    else                        word += c;                         return result;   How do they compare?  They’re pretty close to each other.  I handed each one a string consisting of the text of Moby Dick and each one was able to process it in about 300 milliseconds.  Sometimes one is faster, sometimes the other.  Now, how would you break this problem down into chunks that multiple processors can work on?  It’s tricky.  We have a list of strings that we’re holding on to, we have a word that we’re holding on to, and as long as we have those two things, we’re stuck iterating over the characters one by one so we can update our stuff in the correct order.  If we try to process two characters simultaneously, the ordering of our words, or the words themselves, can be jumbled.  What do we do?  I’ll try to answer that question next time.</description>
        <language>en-US</language>
        <copyright>Ben Fulton</copyright>
        <generator>Subtext Version 2.1.2.2</generator>
        <image>
            <title>Extracting words from a string</title>
            <url>http://algorithmicalley.com/images/RSS2Image.gif</url>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Mr</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#34</link>
            <description>This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work &lt;br /&gt;</description>
            <dc:creator>aanrechtblad</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#34</guid>
            <pubDate>Fri, 31 Oct 2014 15:53:49 GMT</pubDate>
        </item>
        <item>
            <title>toko plastik</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#39</link>
            <description>I really appreciate the time and effort you spend to share this with us! I do hope to read more updates from you.=)and go on!</description>
            <dc:creator>toko plastik</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#39</guid>
            <pubDate>Sat, 08 Nov 2014 07:10:42 GMT</pubDate>
        </item>
        <item>
            <title>re: Extracting words from a string</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#40</link>
            <description>I like the post and i am surprised. the way of ex-planing is very good manner.&lt;br /&gt;</description>
            <dc:creator>keukenblad graniet</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#40</guid>
            <pubDate>Tue, 11 Nov 2014 16:56:48 GMT</pubDate>
        </item>
        <item>
            <title>Marks and Spencer</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#51</link>
            <description>The facts you have released is very useful. The sites you have known was outstanding. Thanks for talking about.</description>
            <dc:creator>Marks and Spencer</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#51</guid>
            <pubDate>Fri, 05 Dec 2014 04:45:41 GMT</pubDate>
        </item>
        <item>
            <title>douchevloer</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#55</link>
            <description>Great post. I used to be checking continuously this blog and I am inspired! Very useful information specially the remaining section :) I handle such information a lot. I used to be seeking this certain info for a very long time. Thanks and good luck.... &lt;br /&gt;</description>
            <dc:creator>douchevloer</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#55</guid>
            <pubDate>Tue, 09 Dec 2014 16:37:08 GMT</pubDate>
        </item>
        <item>
            <title>re: Extracting words from a string</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#56</link>
            <description>This is very nice blog because information provided here through the article and the pictures are very effective. Because sometimes words cannot explain the things that pictures can and here the words and pictures both are expressing the things in balance.&lt;br /&gt;</description>
            <dc:creator>Mindfulness</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#56</guid>
            <pubDate>Tue, 09 Dec 2014 16:38:21 GMT</pubDate>
        </item>
        <item>
            <title>re: Extracting words from a string</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#57</link>
            <description>Great blog, all posts have something to learn. Your work is very good and i appreciate you and hopping for some more informative posts. Keep writing.&lt;br /&gt;</description>
            <dc:creator>This website</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#57</guid>
            <pubDate>Tue, 09 Dec 2014 20:57:35 GMT</pubDate>
        </item>
        <item>
            <title>re: Extracting words from a string</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#58</link>
            <description>Thank you for the work you have put into your nice blog. We will bookmark to your blog because it is very informational. We love the site and will come back to see your new posts.&lt;br /&gt;</description>
            <dc:creator>Click Here</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#58</guid>
            <pubDate>Thu, 11 Dec 2014 18:26:17 GMT</pubDate>
        </item>
        <item>
            <title>Mr</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#59</link>
            <description>So well explained incredible article, any buddy can follow it, thanks and keep up the great work. All the contents you mentioned in post are too good and can be very useful, Thanks&lt;br /&gt;</description>
            <dc:creator>mindfulness exercises</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#59</guid>
            <pubDate>Tue, 16 Dec 2014 14:34:20 GMT</pubDate>
        </item>
        <item>
            <title>re: Extracting words from a string</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#69</link>
            <description>This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.&lt;br /&gt;</description>
            <dc:creator>closetspoeler</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#69</guid>
            <pubDate>Wed, 24 Dec 2014 17:16:37 GMT</pubDate>
        </item>
        <item>
            <title>Sommerfest Oslo</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#71</link>
            <description>Nice post. &lt;a href="http://www.highjump.no/event/sommerfest/"&gt;Sommerfest Oslo&lt;/a&gt;</description>
            <dc:creator>Sommerfest Oslo</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#71</guid>
            <pubDate>Sun, 28 Dec 2014 09:43:01 GMT</pubDate>
        </item>
        <item>
            <title>How apple pay works with passbook?</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#72</link>
            <description>This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog. &amp;quot; </description>
            <dc:creator>How apple pay works with passboo</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#72</guid>
            <pubDate>Mon, 29 Dec 2014 12:00:43 GMT</pubDate>
        </item>
        <item>
            <title>&amp;#160;verhuizen hoorn</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#2077</link>
            <description>Thanks for sharing so significative article with us. I agree with your idea competely.I am looking forward to another great article from you.&lt;br /&gt;</description>
            <dc:creator>verhuizen hoorn</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#2077</guid>
            <pubDate>Wed, 14 Jan 2015 14:47:55 GMT</pubDate>
        </item>
        <item>
            <title>fx pip rebates</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#2078</link>
            <description>This is also a very outstanding post which I really knowledgeable learning. It is not everyday that I have the opportunity to see something like this.</description>
            <dc:creator>fx pip rebates</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#2078</guid>
            <pubDate>Sat, 17 Jan 2015 05:23:21 GMT</pubDate>
        </item>
        <item>
            <title>Online GED</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#2081</link>
            <description>very good work thanks for sharing</description>
            <dc:creator>Online GED Program</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#2081</guid>
            <pubDate>Wed, 21 Jan 2015 13:28:50 GMT</pubDate>
        </item>
        <item>
            <title>re: Extracting words from a string</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#2083</link>
            <description>Looking at these lines of code is getting me back into the groove of programming, though I do enjoy troubleshooting a great deal. </description>
            <dc:creator>How I Prevent Phishing</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#2083</guid>
            <pubDate>Mon, 26 Jan 2015 23:12:25 GMT</pubDate>
        </item>
        <item>
            <title>re: Extracting words from a string</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3090</link>
            <description>A mind blowing article is provided here. And it is written with great skill and the words directly explain the thought of author. &lt;br /&gt;</description>
            <dc:creator>doucheslang</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3090</guid>
            <pubDate>Tue, 10 Feb 2015 17:33:35 GMT</pubDate>
        </item>
        <item>
            <title>re: Extracting words from a string</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3093</link>
            <description>Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles.Keep up the good work! &lt;br /&gt;</description>
            <dc:creator>plak bh</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3093</guid>
            <pubDate>Wed, 18 Feb 2015 13:42:29 GMT</pubDate>
        </item>
        <item>
            <title>holidays in india</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3094</link>
            <description>I am hoping the same best effort from you in the future as well. In fact your creative writing skills has inspired me.</description>
            <dc:creator>holidays in india</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3094</guid>
            <pubDate>Wed, 18 Feb 2015 16:32:42 GMT</pubDate>
        </item>
        <item>
            <title>re: Extracting words from a string</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3150</link>
            <description>Excellent and decent post. I found this much informative, as to what I was exactly searching for. Thanks for such post and please keep on sharing with us.&lt;br /&gt;</description>
            <dc:creator>Translation Services in Abu Dhab</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3150</guid>
            <pubDate>Tue, 24 Feb 2015 16:12:51 GMT</pubDate>
        </item>
        <item>
            <title>re: Extracting words from a string</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3151</link>
            <description>It is very nice and informative blog through which we gain a lot information for our some purpose. you did a great job and i appreciate your effort. Very nice shear and keep on sharing.&lt;br /&gt;</description>
            <dc:creator>Proofreading services Dubai</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3151</guid>
            <pubDate>Tue, 24 Feb 2015 16:13:43 GMT</pubDate>
        </item>
        <item>
            <title>re: Extracting words from a string</title>
            <link>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3152</link>
            <description>Good post. I was checking consistently this blog and I am impressed. Quite useful information specially the final component. I was looking for this distinct information for a pretty long time. And know i am very happy after visiting your blog. Very nice work and thanks for sharing.&lt;br /&gt;</description>
            <dc:creator>dessert safari discount</dc:creator>
            <guid>http://algorithmicalley.com/archive/2010/10/21/extracting-words-from-a-string.aspx#3152</guid>
            <pubDate>Tue, 24 Feb 2015 16:15:16 GMT</pubDate>
        </item>
    </channel>
</rss>