Home | History | Annotate | Download | only in codewalk

Lines Matching full:code

3 Use of this source code is governed by a BSD-style
19 To model this data, we use a <code>map[string][]string</code>.
20 Each map key is a prefix (a <code>string</code>) and its values are
21 lists of suffixes (a slice of strings, <code>[]string</code>).
38 store prefixes in the map as a single <code>string</code>.
40 <code>[]string</code>, but we can't do this with a map because the
43 Therefore, in most of our code we will model prefixes as a
44 <code>[]string</code> and join the strings together with a space
57 the word length of the prefixes. The <code>Chain</code> struct stores
62 The <code>Chain</code> struct has two unexported fields (those that
64 <code>NewChain</code> constructor function that initializes the
65 <code>chain</code> map with <code>make</code> and sets the
66 <code>prefixLen</code> field.
69 program is within a single package (<code>main</code>) and therefore
75 those fields. Also, structuring <code>Chain</code> like this means we
81 <code>Prefix</code> type with the concrete type <code>[]string</code>.
83 working with a prefix instead of just a <code>[]string</code>.
85 so we can add methods that operate on <code>Prefix</code> if we need to.
89 The first method we define on <code>Prefix</code> is
90 <code>String</code>. It returns a <code>string</code> representation
91 of a <code>Prefix</code> by joining the slice elements together with
97 The <code>Build</code> method reads text from an <code>io.Reader</code>
99 <code>Chain</code>.
101 The <code><a href="/pkg/io/#Reader">io.Reader</a></code> is an
103 other Go code. Our code uses the
104 <code><a href="/pkg/fmt/#Fscan">fmt.Fscan</a></code> function, which
105 reads space-separated values from an <code>io.Reader</code>.
107 The <code>Build</code> method returns once the <code>Reader</code>'s
108 <code>Read</code> method returns <code>io.EOF</code> (end of file)
114 <code>Readers</code>. For efficiency we wrap the provided
115 <code>io.Reader</code> with
116 <code><a href="/pkg/bufio/">bufio.NewReader</a></code> to create a
117 new <code>io.Reader</code> that provides buffering.
121 At the top of the function we make a <code>Prefix</code> slice
122 <code>p</code> using the <code>Chain</code>'s <code>prefixLen</code>
129 In our loop we read words from the <code>Reader</code> into a
130 <code>string</code> variable <code>s</code> using
131 <code>fmt.Fscan</code>. Since <code>Fscan</code> uses space to
135 <code>Fscan</code> returns an error if it encounters a read error
136 (<code>io.EOF</code>, for example) or if it can't scan the requested
138 stop scanning, so we <code>break</code> out of the loop.
142 The word stored in <code>s</code> is a new suffix. We add the new
143 prefix/suffix combination to the <code>chain</code> map by computing
144 the map key with <code>p.String</code> and appending the suffix
147 The built-in <code>append</code> function appends elements to a slice
149 <code>nil</code>, <code>append</code> allocates a new slice.
152 the zero value of <code>[]string</code> is <code>nil</code>.
153 When our program encounters a new prefix (yielding a <code>nil</code>
154 value in the map) <code>append</code> will allocate a new slice.
156 For more information about the <code>append</code> function and slices
169 the new value for <code>p</code> would be
173 the code to perform this mutation of the slice inside a method on
174 <code>Prefix</code> named <code>Shift</code>.
178 The <code>Shift</code> method uses the built-in <code>copy</code>
179 function to copy the last len(p)-1 elements of <code>p</code> to
186 We then assign the provided <code>word</code> to the last index
195 The <code>Generate</code> method is similar to <code>Build</code>
196 except that instead of reading words from a <code>Reader</code>
198 appends them to a slice (<code>words</code>).
200 <code>Generate</code> uses a conditional for loop to generate
201 up to <code>n</code> words.
206 for the current prefix. We access the <code>chain</code> map at key
207 <code>p.String()</code> and assign its contents to <code>choices</code>.
209 If <code>len(choices)</code> is zero we break out of the loop as there
212 in that case, <code>choices</code> will be <code>nil</code> and the
213 length of a <code>nil</code> slice is zero.
218 <code><a href="/pkg/math/rand/#Intn">rand.Intn</a></code> function.
220 value. Passing in <code>len(choices)</code> gives us a random index
224 <code>next</code> and append it to the <code>words</code> slice.
226 Next, we <code>Shift</code> the new suffix onto the prefix just as
227 we did in the <code>Build</code> method.
232 <code>strings.Join</code> function to join the elements of
233 the <code>words</code> slice together, separated by spaces.
238 use the <code><a href="/pkg/flag/">flag</a></code> package to parse
241 These calls to <code>flag.Int</code> register new flags with the
242 <code>flag</code> package. The arguments to <code>Int</code> are the
243 flag name, its default value, and a description. The <code>Int</code>
250 The <code>main</code> function begins by parsing the command-line
251 flags with <code>flag.Parse</code> and seeding the <code>rand</code>
255 <code>flag.Parse</code> function will print an informative usage
260 To create the new <code>Chain</code> we call <code>NewChain</code>
261 with the value of the <code>prefix</code> flag.
263 To build the chain we call <code>Build</code> with
264 <code>os.Stdin</code> (which implements <code>io.Reader</code>) so
269 Finally, to generate text we call <code>Generate</code> with
270 the value of the <code>words</code> flag and assigning the result
271 to the variable <code>text</code>.
273 Then we call <code>fmt.Println</code> to write the text to standard
291 This is the source code repository for the Go source
299 The <code>Generate</code> function does a lot of allocations when it
300 builds the <code>words</code> slice. As an exercise, modify it to
301 take an <code>io.Writer</code> to which it incrementally writes the
302 generated text with <code>Fprint</code>.
303 Aside from being more efficient this makes <code>Generate</code>
304 more symmetrical to <code>Build</code>.