Skip to content

Commit e9f6c84

Browse files
authored
Simplify word count (#102)
1 parent 63a642b commit e9f6c84

File tree

1 file changed

+34
-72
lines changed

1 file changed

+34
-72
lines changed

app/examples/word-count.recho.js

Lines changed: 34 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @created 2025-09-13
55
* @pull_request 96
66
* @github pearmini
7-
* @thumbnail_start 152
7+
* @thumbnail_start 119
88
*/
99

1010
/**
@@ -44,7 +44,7 @@ const clean = echo(text.toLowerCase().replace(/[.,!?;]/g, ""));
4444
* Step 2. Split into words
4545
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4646
*
47-
* The we need to split the text into words by whitespace. Here we use
47+
* Then we need to split the text into words by whitespace. Here we use
4848
*
4949
* - `string.split(splitter)`: splits the text into words by the splitter.
5050
* - `/\s+/`: a regular expression that matches one or more whitespace
@@ -83,9 +83,9 @@ const filtered = echo(words.filter((w) => !ignoredWords.includes(w)));
8383
*
8484
* Next, we count the frequencies of the words. We use the following method:
8585
*
86-
* - `array.reduce(callback, initial)`: reduces the array to a single value.
86+
* - `array.reduce(callback, initialValue)`: reduces the array to a single value.
8787
*
88-
* The result should an associative array (or object in JavaScript) that maps
88+
* The result should be an associative array (or object in JavaScript) that maps
8989
* each word to its frequency.
9090
*/
9191

@@ -102,76 +102,43 @@ const frequencies = echo(
102102
* Step 5. Visualize it
103103
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104104
*
105-
* The final step is to visualize the result. We first get the keys of the
106-
* object by `Object.keys(object)`, which is an array of the keys.
107-
*/
108-
109-
//➜ [ "dog", "cat", "mouse", "playing", "yard", "barked", "loudly", "ran", "quickly", "hid", "under", "bench", "kept", "looking", "jumped", "small", "fence", "followed", "bird", "watched", "silently", "mo…
110-
const keys = echo(Object.keys(frequencies));
111-
112-
/**
113-
* Then we get the maximum length of the keys by the following method:
105+
* The final step is to visualize the result. Since Recho is light, you don't
106+
* need some sophisticated visualizations. Some basic string manipulation is
107+
* enough to create something simple that provides insights. The following
108+
* methods are used:
114109
*
115-
* - `Math.max(...array)`: returns the maximum value in the array.
110+
* - `Object.entries(object)`: returns an array of key-value pairs.
116111
* - `array.map(callback)`: maps the array to a new array by the callback.
117-
* - `string.length`: the character length of the string.
118-
*/
119-
120-
//➜ 8
121-
const maxLength = echo(Math.max(...keys.map((d) => d.length)));
122-
123-
/**
124-
* Next, we get the entries of the object by `Object.entries(object)`, which
125-
* is an array of pairs of the key and the value.
112+
* - `string.repeat(value)`: repeats the string `value` times.
113+
* - `string.join(separator)`: joins the array to a string with the separator.
126114
*/
127115

128116
//➜ [ [ "dog", 5 ], [ "cat", 4 ], [ "mouse", 3 ], [ "playing", 1 ], [ "yard", 1 ], [ "barked", 1 ], [ "loudly", 1 ], [ "ran", 1 ], [ "quickly", 1 ], [ "hid", 1 ], [ "under", 1 ], [ "bench", 1 ], [ "kept",…
129117
const entries = echo(Object.entries(frequencies));
130118

131-
/**
132-
* Then we map the entries to the rows of the chart by the following method:
133-
*
134-
* - `string.padStart(length, padString)`: pads the string to the length with
135-
* the padString.
136-
* - `string.repeat(value)`: repeats the string value times.
137-
*/
138-
139-
//➜ [ " dog-|🟩🟩🟩🟩🟩", " cat-|🟩🟩🟩🟩", " mouse-|🟩🟩🟩", " playing-|🟩", " yard-|🟩", " barked-|🟩", " loudly-|🟩", " ran-|🟩", " quickly-|🟩", " hid-|🟩", " under-|🟩", " …
140-
const rows = echo(
141-
entries.map(([key, value]) => {
142-
return key.padStart(maxLength) + "-|" + "🟩".repeat(value);
143-
}),
144-
);
145-
146-
/**
147-
* Finally, we join the rows with a newline character to get the final output.
148-
*
149-
* - `array.join(separator)`: joins the array with the separator.
150-
*/
151-
152-
//➜ dog-|🟩🟩🟩🟩🟩
153-
//➜ cat-|🟩🟩🟩🟩
154-
//➜ mouse-|🟩🟩🟩
155-
//➜ playing-|🟩
156-
//➜ yard-|🟩
157-
//➜ barked-|🟩
158-
//➜ loudly-|🟩
159-
//➜ ran-|🟩
160-
//➜ quickly-|🟩
161-
//➜ hid-|🟩
162-
//➜ under-|🟩
163-
//➜ bench-|🟩
164-
//➜ kept-|🟩
165-
//➜ looking-|🟩
166-
//➜ jumped-|🟩
167-
//➜ small-|🟩
168-
//➜ fence-|🟩
169-
//➜ followed-|🟩
170-
//➜ bird-|🟩
171-
//➜ watched-|🟩
172-
//➜ silently-|🟩
173-
//➜ moved-|🟩
174-
echo(rows.join("\n"));
119+
//➜ 🟩🟩🟩🟩🟩 dog
120+
//➜ 🟩🟩🟩🟩 cat
121+
//➜ 🟩🟩🟩 mouse
122+
//➜ 🟩 playing
123+
//➜ 🟩 yard
124+
//➜ 🟩 barked
125+
//➜ 🟩 loudly
126+
//➜ 🟩 ran
127+
//➜ 🟩 quickly
128+
//➜ 🟩 hid
129+
//➜ 🟩 under
130+
//➜ 🟩 bench
131+
//➜ 🟩 kept
132+
//➜ 🟩 looking
133+
//➜ 🟩 jumped
134+
//➜ 🟩 small
135+
//➜ 🟩 fence
136+
//➜ 🟩 followed
137+
//➜ 🟩 bird
138+
//➜ 🟩 watched
139+
//➜ 🟩 silently
140+
//➜ 🟩 moved
141+
echo(entries.map(([word, count]) => "🟩".repeat(count) + " " + word).join("\n"));
175142

176143
/**
177144
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -186,12 +153,7 @@ echo(rows.join("\n"));
186153
* - `array.filter(callback)`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
187154
* - `array.includes(value)`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
188155
* - `array.reduce(callback, initialValue)`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
189-
* - `Object.keys(object)`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
190156
* - `Object.entries(object)`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
191-
* - `Math.max(...array)`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
192-
* - `array.map(callback)`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
193-
* - `string.padStart(length, padString)`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
194-
* the padString. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
195157
* - `string.repeat(value)`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
196158
* - `array.join(separator)`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
197159
*/

0 commit comments

Comments
 (0)