-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.html
528 lines (473 loc) · 15.4 KB
/
README.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<html>
<head>
<title>Propose Changes to clojure.contrib.str-utils</title>
<style>
.code-block{
background-color:#FFFFCC;
border-color:#CCCCCC;
border-width:2px;
border-style:solid;
width:35em;
margin-left:auto;
margin-right:auto;
margin-top:10px;
margin-bottom:10px;
padding:5px;
}
body{
width:40em;
margin-left:auto;
margin-right:auto;
background-color:#3333FF;
margin-top:0px;
margin-bottom:0px;
}
#page-content{
border-color:#990000;
border-width:0px 2px 0px 2px;
border-style:solid;
background-color:#FFFFFF;
padding:5px;
margin:0px;
}
.ns{
color:green;
font-weight:bold;
}
</style>
</head>
<body>
<div id="page-content">
<h1 style="text-align:center">My Proposed changes to str-utils </h1>
<h4 style="text-align:center">Sean Devlin</h4>
<h4 style="text-align:center">May 13, 2009</h4>
<p>
I've been reviewing the str-utils package, and I'd like to propose a few changes to the library. I've included the code at the bottom.
</p>
<h2>Use Multi-Methods</h2>
<p>
I'd like to propose re-writing the following methods to used multi-methods. Every single method will take an input called input-string,
and a variable set of inputs called remaining-inputs. The mutli-dispatch will make decide what to do based on the remaining inputs.
Specifically, I've used
</p>
<div class="code-block">
<code>
(class (first remaining-inputs))<br>
</code>
</div>
<p>
repeatedly. The two most interesting classes are <code class="ns">java.util.regex.Pattern</code>, and <code class="ns">clojure.lang.PersistentList</code>. I deliberately decided to <b>not</b>
use sequences, because I believed order was important. One method takes a map as an input, but this is so that a tuple could be passed as an options
hash.
</p>
<h2><code>re-partion[input-string & remaining-inputs](...)</code></h2>
<div class="function-description">
This methods behaves like the original re-partition method, with the remaining-inputs being able to a list or a pattern. It returns a lazy sequence, and
is used as a basis for for several other methods.
</div>
<h2><code>re-split[input-string & remaining-inputs](...)</code></h2>
<div class="function-description">
The remaining inputs can be dispatched based on a regex pattern, a list of patterns, or a map. The regex method is the basis, and does the actual work.<br>
<h4>Regex</h4>
This method splits a string into a list based on a regex. It depends on the re-partition method, and returns a lazy sequence.<br>
<div class="code-block">
<code>
(re-split "1 2 3\n4 5 6" #"\n") => ("1 2 3" "4 5 6")<br>
</code>
</div>
<h4>Map</h4>
This splits each element based on the inputs of the map. It is how options are passed to the method.<br>
<div class="code-block">
<code>
(re-split "1 2 3" {:pattern #"\s+" :offset 1}) => (2.0 3.0)<br>
(re-split "1 2 3" {:pattern #"\s+" :length 2}) => (1.0 2.0)<br>
(re-split "1 2 3" {:pattern #"\s+" :marshal-fn #(java.lang.Double/parseDouble %)}) => (1.0 2.0 3.0)<br>
</code>
</div>
The <code>:pattern</code>, <code>:offset</code>, and <code>:length</code> options are relatively straightforward. The :marshal-fn is mapped after the string is split.<br>
<h4>List</h4>
This splits each element either like a map (datatype) or a regex. The map operator is applied recursively to each element<br>
<div class="code-block">
<code>
(re-split "1 2 3\n4 5 6" (list #"\n" #"\s+")) => (("1" "2" "3") ("4" "5" "6"))<br>
</code>
</div>
<h4>Chaining</h4>
These items can be chained together, as the following example shows<br>
<div class="code-block">
<code>
(re-split "1 2 3\n4 5 6" <br>
(list #"\n" {:pattern #"\s+" <br>
:length 2 <br>
:marshal-fn #(java.lang.Double/parseDouble %)}))<br>
=> ((1.0 2.0) (4.0 5.0))<br>
</code>
</div>
In my opinion, the <code>:marshal-fn</code> is best used at the end of the list. However, it could be used earlier in the list, but a exception will most likely be thrown.
</div>
<h2><code>re-gsub[input-string & remaining-inputs](...)</code></h2>
<div class="function-description">
This method can take a list or two atoms as the remaining inputs.<br>
Two atoms<br>
<div class="code-block">
<code>
(re-gsub "1 2 3 4 5 6" #"\s" "") => "123456"<br>
</code>
</div>
A paired list<br>
<div class="code-block">
<code>
(re-gsub "1 2 3 4 5 6" '((#"\s" " ) (#"\d" "D"))) => "DDDDDD"<br>
</code>
</div>
</div>
<h2><code>re-sub[input-string & remaining-inputs](...)</code></h2>
<div class="function-description">
Again, this method can take a list or two atoms as the remaining inputs.<br>
Two atoms<br>
<div class="code-block">
<code>
(re-sub "1 2 3 4 5 6" #"\d" "D") => "D 2 3 4 5 6"<br>
</code>
</div>
A paired list<br>
<div class="code-block">
<code>
(re-sub "1 2 3 4 5 6" '((#"\d" "D") (#"\d" "E"))) => "D E 3 4 5 6"<br>
</code>
</div>
</div>
<h2>The <code>nearby</code> Function</h2>
<p>
The nearby function is designed to assist with a spell checker, inspired by the example from Peter Norvig.
<h3>Signatures</h3>
<code>
<ul>
<li><b>nearby</b> input-string</li>
<li><b>nearby</b> input-string seq</li>
</ul>
</code>
Here's an example.
<div class="code-block">
<code>
(nearby "cat" (seq "abc")) => <br>("act" "atc"<br>
"acat" "aat" "caat" "cat" "caat" "caa" "cata"<br>
"bcat" "bat" "cbat" "cbt" "cabt" "cab" "catb"<br>
"ccat" "cat" "ccat" "cct" "cact" "cac" "catc")
</code>
</div>
The resulting sequence is lazy. In order to use it in a spellchecker, try using it like this:
<div class="code-block">
<code>
(set (take <i>number</i> (nearby "cat" (seq "abc"))))
</code>
</div>
If the function is called with only one argument, it behaves like this.
<div class="code-block">
<code>
(nearby "cat") =><br> (nearby "cat" (cons "" "etaoinshrdlcumwfgypbvkjxqz"))
</code>
</div>
The strange order was chosen because that is the english alphabet sorted by frequency. This way the earliest entries will have the
highest chance of being a valid word.
</p>
<h2>String Seq Utils</h2>
<div class="function-description">
The contrib version of str-utils contains the <code>str-join</code> function. This is a string specific version of the more general <cod>interpose</code> function. It inspired the creation of
four other functions, <code>str-take, str-drop, str-rest & str-reverse</code>. The mimic the behavior of the regular sequence operations, with the exception that they return strings instead of
a sequence. Also, some of them can alternately take a regex as an input.<br>
<h2><code>str-take</code></h2>
<p>
This function is designed to be similar to the <code>take</code> function from the core. It specifically applies the <code>str</code> function to the resulting sequence. Also, it can take a regex
instead of an integer, and will take everything before the regex. Be careful not to combine a regex and a sequence, as this will cause an error. Finally, an optional <code>:include</code> parameter
can be passed to include the matched regex.
</p>
<div class="code-block">
<code>
<table>
<tr>
<td>(str-take 7 "Clojure Is Awesome")</td>
<td>=></td>
<td>"Clojure"</td>
</tr>
<tr>
<td>(str-take 2 ["Clojure" "Is" "Awesome"])</td>
<td>=></td>
<td>"ClojureIs"</td>
</tr>
<tr>
<td>(str-take #"\s+" "Clojure Is Awesome")</td>
<td>=></td>
<td>"Clojure"</td>
</tr>
<tr>
<td>(str-take #"\s+" "Clojure Is Awesome" <br>   {:include true})</td>
<td>=></td>
<td>"Clojure "</td>
</tr>
<tr>
<td>(str-take #"\s+" ["Clojure" "Is" "Awesome"])</td>
<td>=></td>
<td style="color:red;"><b>error</b></td>
</tr>
</table>
</code>
</div>
<h2><code>str-drop</code></h2>
<p>
This function is designed to be similar to the <code>drop</code> function from the core. It specifically applies the <code>str</code> function to the resulting sequence. Also, it can take a regex
instead of an integer, and will take everything after the regex. Be careful not to combine a regex and a sequence, as this will cause an error. Finally, an optional <code>:include</code> parameter
can be passed to include the matched regex.
</p>
<div class="code-block">
<code>
<table>
<tr>
<td>(str-drop 8 "Clojure Is Awesome")</td>
<td>=></td>
<td>"Is Awesome"</td>
</tr>
<tr>
<td>(str-drop 1 ["Clojure" "Is" "Awesome"])</td>
<td>=></td>
<td>"IsAwesome"</td>
</tr>
<tr>
<td>(str-drop #"\s+" "Clojure Is Awesome")</td>
<td>=></td>
<td>"Is Awesome"</td>
</tr>
<tr>
<td>(str-drop #"\s+" "Clojure Is Awesome" <br>   {:include true})</td>
<td>=></td>
<td>" Is Awesome"</td>
</tr>
<tr>
<td>(str-drop #"\s+" ["Clojure" "Is" "Awesome"])</td>
<td>=></td>
<td style="color:red;"><b>error</b></td>
</tr>
</table>
</code>
</div>
<h2><code>str-rest</code></h2>
<p>This function applies <code>str</code> to the <code>rest</code> of the input. It is equivalent to <code>(str-drop 1 <i>input</i>)</code></p>
<div class="code-block">
<code>
<table>
<tr>
<td>(str-rest (str :Clojure))</td>
<td>=></td>
<td>"Clojure"</td>
</tr>
</table>
</code>
</div>
<!--
<table>
<tr>
<td></td>
<td>=></td>
<td></td>
</tr>
</table> -->
<h2><code>str-reverse</code></h2>
<div class="brief-function-description">
This methods reverses a string<br>
<div class="code-block">
<code>
(str-reverse "Clojure") => "erujolC"
</code>
</div>
</div>
<h3>An Example</h3>
These methods can be used to help parse strings, such as below.<br>
<div class="code-block">
<code>
(str-take ">" (str-drop #"< h4" "< h4 ... >")) <br>=> ;the stuff in the middle
</code>
</div>
</div>
<h2>
New Inflectors
</h2>
I've added a few inflectors that I am familiar with from Rails. My apologies if their origin is anther language. I'd be interested in knowing where the method originated
<h4>trim</h4>
<div class="brief-function-description">
This is a convenience wrapper for the trim method java supplies<br>
<div class="code-block">
<code>
(trim " Clojure ") => "Clojure"
</code>
</div>
</div>
<h4>strip</h4>
<div class="brief-function-description">
This is an alias for trim. I accidently switch between <code>trim</code> and <code>strip</code> all the time.<br>
<div class="code-block">
<code>
(strip " Clojure ") => "Clojure"
</code>
</div>
</div>
<h4>ltrim</h4>
<div class="brief-function-description">
This method removes the leading whitespace<br>
<div class="code-block">
<code>
(ltrim " Cloure ") => "Clojure "
</code>
</div>
</div>
<h4>rtrim</h4>
<div class="brief-function-description">
This method removes the trailing whitespace<br>
<div class="code-block">
<code>
(ltrim " Cloure ") => " Clojure"
</code>
</div>
</div>
<h4>downcase</h4>
<div class="brief-function-description">
This is a convenience wrapper for the toLowerCase method java supplies<br>
<div class="code-block">
<code>
(downcase "Clojure") => "clojure"
</code>
</div>
</div>
<h4>upcase</h4>
<div class="brief-function-description">
This is a convenience wrapper for the toUpperCase method java supplies<br>
<div class="code-block">
<code>
(upcase "Clojure") => "CLOJURE"
</code>
</div>
</div>
<h4>capitalize</h4>
<div class="brief-function-description">
This method capitalizes a string<br>
<div class="code-block">
<code>
(capitalize "clojure") => "Clojure"
</code>
</div>
</div>
<h4>titleize, camelize, dasherize, underscore</h4>
<div class="brief-function-description">
These methods manipulate "sentences", producing a consistent output. Check the unit tests for more examples<br>
<div class="code-block">
<code>
<table>
<tr>
<td>(titleize "clojure iS Awesome")</td>
<td>=></td>
<td>"Clojure Is Awesome"</td>
</tr>
<tr>
<td>(camleize "clojure iS Awesome")</td>
<td>=></td>
<td>"clojureIsAwesome"</td>
</tr>
<tr>
<td>(dasherize "clojure iS Awesome")</td>
<td>=></td>
<td>"clojure-is-awesome"</td>
</tr>
<tr>
<td>(underscore "clojure iS Awesome")</td>
<td>=></td>
<td>"clojure_is_awesome"</td>
</tr>
</table>
</code>
</div>
</div>
<h4>pluralize</h4>
<div>
This is an early attempt at Rails' <code>pluralaize</code> function. The code for the <code>pluralize</code>
function was based on functions contributed by Brian Doyle.
<div class="code-block">
<code>
<table>
<tr>
<td>(pluralize "foo")</td>
<td>=></td>
<td>"foos"</td>
</tr>
<tr>
<td>(pluralize "beach")</td>
<td>=></td>
<td>"beaches"</td>
</tr>
<tr>
<td>(pluralize "baby")</td>
<td>=></td>
<td>"babies"</td>
</tr>
<tr>
<td>(pluralize "bus")</td>
<td>=></td>
<td>"buses"</td>
</tr>
</table>
</code>
</div>
</div>
<h4>singularize</h4>
<div>
This is an early attempt at Rails' <code>singularize</code> function. The code for the <code>singulaize</code>
function was based on functions contributed by Brian Doyle.
<div class="code-block">
<code>
<table>
<tr>
<td>(singularize "foos")</td>
<td>=></td>
<td>"foo"</td>
</tr>
<tr>
<td>(singularize "beaches")</td>
<td>=></td>
<td>"beach"</td>
</tr>
<tr>
<td>(singularize "babies")</td>
<td>=></td>
<td>"baby"</td>
</tr>
<tr>
<td>(singularize "stops")</td>
<td>=></td>
<td>"stop"</td>
</tr>
</table>
</code>
</div>
</div>
<h2>Closing thoughts</h2>
<p>
There are three more methods, <code>str-join, chop, & chomp</code> that were already in str-utils. I changed the implementation of the methods, but the behavior should be the same.
</p>
<p>
There is a big catch with my proposed change. The signature of re-split, re-partition, re-gsub and re-sub changes. They will not be backwards compatible, and will break code. However, I think the flexibility is worth it.
</p>
<h2>TO-DOs</h2>
There are a few more things I'd like to add, but that could done at a later date.
<ul>
<li>Add more inflectors</li>
</ul>
The following additions become pretty easy if the propsed re-gsub is included:
<ul>
<li>Add HTML-escape function (like Rails' <code>h</code> method)</li>
<li>Add Javascript-escape function (like Rails' <code>escape_javascript</code> method)</li>
<li>Add SQL-escape function</li>
</ul>
<p>
Okay, that's everything I can think of for now. I'd like to thank the Stuart Sierra, and all of the contributors to this library. This is possible because I'm standing on their shoulders.
</p>
</div>
</body>
</html>