Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Source cleanup of wc demo.

  • Loading branch information...
commit ee385afabaeb4bb539f2310dde06124804f084b6 1 parent f441e4d
@briantrice authored
Showing with 23 additions and 23 deletions.
  1. +23 −23 src/demo/wc
View
46 src/demo/wc
@@ -1,12 +1,12 @@
#!/usr/local/bin/slate
-define: #scriptArgs -> (StartupArguments allButFirst: 3).
+#scriptArgs ::= StartupArguments allButFirst: 3.
-define: #options -> (scriptArgs select: [| :arg | arg first = $-] collect: #allButFirst`er) join &separator: Nil.
+#options := (scriptArgs select: [| :arg | arg first = $-] collect: #allButFirst`er) join &separator: Nil.
(options detect: [| :each | ('wlcm' includes: each) not])
ifNotNilDo: [| :char | error: 'Illegal option - ' ; (char as: String)].
-options isEmpty ifTrue: [define: #options -> 'clw'].
+options isEmpty ifTrue: [options := 'clw'].
#rawInputs :=
(scriptArgs indexOf: [| :each | (each first = $-) not])
@@ -16,28 +16,31 @@ options isEmpty ifTrue: [define: #options -> 'clw'].
((index below: scriptArgs indexLast)
collect: [| :idx | File newNamed: (scriptArgs at: idx)])].
-#byteCounts := rawInputs collect: [| :_ | Counter newOn: 0].
-#wordCounts := rawInputs collect: [| :_ | Counter newOn: 0].
-#lineCounts := rawInputs collect: [| :_ | Counter newOn: 0].
+#countTypes ::= {#byte. #word. #line}.
+
+#countFlags := Dictionary new*,
+ #byte -> (options includes: $c),
+ #word -> (options includes: $w),
+ #line -> (options includes: $l).
+
+#counts := Dictionary new*,
+ #byte -> (rawInputs collect: [| :_ | Counter newOn: 0]),
+ #word -> (rawInputs collect: [| :_ | Counter newOn: 0]),
+ #line -> (rawInputs collect: [| :_ | Counter newOn: 0]).
rawInputs doWithIndex:
[| :in :idx |
in reader sessionDo:
[| :r pipe |
- pipe: r.
- (options includes: $c) ifTrue: [pipe: (pipe echoTo: (byteCounts at: idx))].
- (options includes: $l) ifTrue: [pipe: (pipe lines echoTo: (lineCounts at: idx))].
- (options includes: $w) ifTrue: [pipe: (pipe split echoTo: (wordCounts at: idx))].
- ]].
+ pipe := r.
+ countTypes do: [| :type |
+ (countFlags at: type) ifTrue: [pipe := pipe echoTo: ((counts at: type) at: idx)]]].
_@here printCountsAt: index
[
- (options includes: $l)
- ifTrue: [Console ; '\t' ; (lineCounts at: index) contents printString].
- (options includes: $w)
- ifTrue: [Console ; '\t' ; (wordCounts at: index) contents printString].
- (options includes: $c)
- ifTrue: [Console ; '\t' ; (byteCounts at: index) contents printString].
+ countTypes do: [| :type |
+ (countFlags at: type)
+ ifTrue: [Console ; '\t' ; ((counts at: type) at: index) contents printString].
].
rawInputs size > 1
@@ -46,11 +49,8 @@ rawInputs size > 1
[| :in :index |
printCountsAt: index.
Console ; ' ' ; ((in is: File) ifTrue: [in filename] ifFalse: ['']) ; '\n'].
- (options includes: $l)
- ifTrue: [Console ; '\t' ; ((lineCounts collect: #contents `er) reduce: #+`er) printString].
- (options includes: $w)
- ifTrue: [Console ; '\t' ; ((wordCounts collect: #contents `er) reduce: #+`er) printString].
- (options includes: $c)
- ifTrue: [Console ; '\t' ; ((byteCounts collect: #contents `er) reduce: #+`er) printString].
+ countTypes do: [| :type |
+ (countFlags at: type)
+ ifTrue: [Console ; '\t' ; (((counts at: type) collect: #contents `er) reduce: #+`er) printString]].
Console ; ' total\n']
ifFalse: [printCountsAt: 0. Console ; '\n'].
Please sign in to comment.
Something went wrong with that request. Please try again.