www.digitalmars.com         C & C++   DMDScript  

D - Example code

reply "Jon Frechette" <jonf4 mindspring.com> writes:
Hi folks,

I've just started trying to learn the D language and I really like it so
far. It seems that the example code included with the compiler is pretty
short on language features and uses no OOP at all. So, I created another
version of the wordcount program that uses every D feature I could learn.

I have attached the source code in case it might be of some value to
Walter's effort.

Ps:  I may have recreated the wheel in places, so any comments would be
welcomed by this newbie.

Thanks
Jul 29 2003
parent "Jon Frechette" <jonf4 mindspring.com> writes:
Oops, I forgot to attach the source code. Got it this time.


begin 666 wc.d
M#0II;7!O<G0 8RYS=&1I;SL-"FEM<&]R="!F:6QE.PT*:6UP;W)T(%-O<G0[
M#0H-"G-T<G5C="!#;W5N=', >PT*("!I;G0 ;&EN97,[#0H (&EN="!W;W)D
M<SL-"B  :6YT(&-H87)S.PT*("!].PT*#0IA;&EA<R!C:&%R(%M=('-T<FEN
M9SL-"F%L:6%S(&)I="!B;V]L96%N.PT*#0HO+R!A<W-O8VEA=&EV92!A<G)A
M>7, ;V8 :6YT<RP :6YD97AE9"!B>2!S=')I;F=S#0IT>7!E9&5F(&EN=%L 
M<W1R:6YG(%T =V]R9$-O=6YT3&ES=#L-" T*#0II;G0 ;6%I;B  <W1R:6YG
M(%M=(&%R9W, *0T*>PT*("!I9B H(&%R9W,N;&5N9W1H(#T](#$ *0T*("  
M('L <')I;G1F*" B57-A9V4Z('=C(&9I;&4 6V9I;&5S+BXN75QN(B I.R!R
M971U<FX ,3L ?0T*#0H ('=O<F1#;W5N=$QI<W0 =V]R9$QI<W0[#0H (&EN
M="!T;W1A;$QI;F5S(#T ,"P =&]T86Q7;W)D<R ](# L('1O=&%L0VAA<G, 
M/2 P.PT*#0H ('!R:6YT9B  (EQN(B I.PT*("!P<FEN=&8H(")L:6YE<R  
M('=O<F1S("  8VAA<G-<;B( *3L-"B  <')I;G1F*" B+2TM+2T (" M+2TM
M+2  ("TM+2TM7&XB("D[#0H-"B  9F]R("  :6YT(&D /2 Q.R!I(#P 87)G
M<RYL96YG=& [("LK:2 I#0H ("  >PT*("  ('-T<FEN9R!F:6QE;F%M92 ]
M(&%R9W-;(&D 73L-" T*("  ('-T<FEN9R!S='( /2 H<W1R:6YG*2!F:6QE
M+G)E860H(&9I;&5N86UE("D[#0H-"B  ("!#;W5N=', 8V]U;G1S(#T 9FEN
M9%=O<F1S*"!S='(L('=O<F1,:7-T("D[#0H-"B  ("!P<FEN=&8H("(E-60E
M.&0E.&0 (&EN("5S7&XB+"!C;W5N=',N;&EN97,L(&-O=6YT<RYW;W)D<RP 
M8V]U;G1S+F-H87)S+" H8VAA<B J*2!F:6QE;F%M92 I.PT*#0H ("  =&]T
M86Q,:6YE<R K/2!C;W5N=',N;&EN97,[#0H ("  =&]T86Q7;W)D<R K/2!C
M;W5N=',N=V]R9',[#0H ("  =&]T86Q#:&%R<R K/2!C;W5N=',N8VAA<G,[
M#0H ("  ?0T*#0H (&EF("  87)G<RYL96YG=&  /B R("D-"B  ("![(" -
M"B  ("!P<FEN=&8H("(M+2TM+2  ("TM+2TM("  +2TM+2U<;B( *3L-"B  
M("!P<FEN=&8H(B4U9"4X9"4X9"  =&]T86QS7&XB+"!T;W1A;$QI;F5S+"!T
M;W1A;%=O<F1S+"!T;W1A;$-H87)S("D[#0H ("  ?0T*#0H ('!R:6YT5V]R
M9$QI<W0H('=O<F1,:7-T("D[#0H-"B  <F5T=7)N(# [#0I]#0H-" T*0V]U
M;G1S(&9I;F17;W)D<R  <W1R:6YG(&EN<'5T+"!O=70 =V]R9$-O=6YT3&ES
M="!W;W)D3&ES=" I#0I[#0H ('-T<FEN9R!C=7)R96YT5V]R9" ]("(B.PT*
M("!B;V]L96%N(&EN5V]R9" ](&9A;'-E.PT*("!#;W5N=', 8V]U;G1S.PT*
M("!F;W( *"!I;G0 :2 ](# [(&D /"!I;G!U="YL96YG=& [(&DK*R I#0H 
M("  >PT*("  (&-H87( 8VAR(#T :6YP=71;(&D 73L-"B  ("!I9B H(&ES
M5V]R9$-H87(H(&-H<B I("D-"B  ("  ('L-"B  ("  (&-U<G)E;G17;W)D
M('X](&-H<CL-"B  ("  (&EF("  :6Y7;W)D(#T](&9A;'-E("D-"B  ("  
M("  :6Y7;W)D(#T =')U93L-"B  ("  ('T-"B  ("!E;'-E("\O(&YO="!A
M('=O<F0 8VAA< T*("  ("  >PT*("  ("  :68 *"!C:'( /3T (EQN(B I
M#0H ("  ("  (&-O=6YT<RYL:6YE<RLK.PT*#0H ("  ("!I9B H(&EN5V]R
M9" ]/2!T<G5E("D-"B  ("  ("  >PT*("  ("  ("!I9B H(&-H<B ]/2 G
M+2< *2 O+R!C:&5C:R!F;W( :'EP:&5N871E9"!W;W)D#0H ("  ("  ("  
M>PT*("  ("  ("  (&EF("  :2 \(&EN<'5T+FQE;F=T:" M(#$ )B8 :7-7
M;W)D0VAA<B  :6YP=71;(&D *R Q(%T *2 I#0H ("  ("  ("  ("!C=7)R
M96YT5V]R9"!^/2!C:'([( T*("  ("  ("  ('T-"B  ("  ("  96QS90T*
M("  ("  ("  ('L-"B  ("  ("  ("!A9&17;W)D*"!C=7)R96YT5V]R9"P 
M8V]U;G1S+"!W;W)D3&ES=" I.PT*("  ("  ("  (&EN5V]R9" ](&9A;'-E
M.PT*("  ("  ("  (&-U<G)E;G17;W)D(#T (B([#0H ("  ("  ("  ?0T*
M("  ("  ("!]#0H ("  ("!]#0H ("  ?0T*("!I9B H(&EN5V]R9" ]/2!T
M<G5E("D +R\ :&%P<&5N<R!I9B!W;W)D(&-H87( 9F]U;F0 :G5S="!B969O
M<F4 14],#0H ("  >PT*("  (&%D9%=O<F0H(&-U<G)E;G17;W)D+"!C;W5N
M=',L('=O<F1,:7-T("D[#0H ("  8V]U;G1S+FQI;F5S*RL[#0H ("  ?0T*
M("!R971U<FX 8V]U;G1S.PT*?0T*#0H-"F)O;VQE86X :7-7;W)D0VAA<B  
M8VAA<B!C("D-"GL-"B  <F5T=7)N("AC(#X]("=A)R F)B!C(#P]("=Z)RD 
M?'P *&, /CT )T$G("8F(&, /#T )UHG*3L-"GT-" T*#0IV;VED(&%D9%=O
M<F0H('-T<FEN9R!W;W)D+"!O=70 0V]U;G1S(&-O=6YT<RP ;W5T('=O<F1#
M;W5N=$QI<W0 =V]R9$QI<W0 *0T*>PT*(" O+W-T<FEN9R!;72!K97ES(#T 
M=V]R9$QI<W0N:V5Y<SL-"B  +R]S=')I;F< =R ]('1O;&]W97(H('=O<F0 
M*3L-"B  +R]I9B H("$ =R!I;B!W;W)D3&ES=" I#0H ("  >PT*("  ('=O
M<F1,:7-T6R!W;W)D(%TK*SL-"B  ("!C;W5N=',N=V]R9', *ST ,3L-"B  
M("!C;W5N=',N8VAA<G, *ST =V]R9"YL96YG=& [#0H ("  ?0T*?0T*#0H-
M"G9O:60 <')I;G17;W)D3&ES="  =V]R9$-O=6YT3&ES="!W;W)D3&ES=" I
M#0I[#0H ('-T<FEN9R!;72!K97ES(#T =V]R9$QI<W0N:V5Y<SL-"B  :6YT
M(%M=('9A;'5E<R ]('=O<F1,:7-T+G9A;'5E<SL-" T*("!I;G0 8V]M<&%R
M92  =6EN="!I+"!U:6YT(&H *0T*("![#0H ("  :6YT('9I(#T =F%L=65S
M6R!I(%T[#0H ("  :6YT('9J(#T =F%L=65S6R!J(%T[#0H ("  <F5T=7)N
M("AV:2 \('9J*2 _("TQ(#H *'9I(#T]('9J*2 _(#  .B Q.PT*("!]#0H-
M"B  =F]I9"!S=V%P*"!U:6YT(&DL('5I;G0 :B I#0H ('L-"B  ("!I;G0 
M=&UP26YT(#T =F%L=65S6R!I(%T[#0H ("  =F%L=65S6R!I(%T /2!V86QU
M97-;(&H 73L-"B  ("!V86QU97-;(&H 72 ]('1M<$EN=#L-"B  ("!S=')I
M;F< =&UP4W1R(#T :V5Y<UL :2!=.PT*("  (&ME>7-;(&D 72 ](&ME>7-;
M(&H 73L-"B  ("!K97ES6R!J(%T /2!T;7!3='([#0H ('T-" T*(" H;F5W
M(%%3;W)T*"DI+G-O<G0H(&ME>7,N;&5N9W1H+"!C;VUP87)E+"!S=V%P("D[
M#0H-"B  <')I;G1F*")<;F-O=6YT("!W;W)D7&XB("D[#0H ('!R:6YT9B B
M+2TM+2T ("TM+2U<;B( *3L-"B  9F]R("  :6YT(&D /2 P.R!I(#P :V5Y
M<RYL96YG=& [(&DK*R I#0H ("  >PT*("  ('!R:6YT9B B)35D(" E+BIS
M7&XB+"!V86QU97-;(&D 72P :V5Y<UL :2!=("D[#0H ("  ?0T*#0H (')E
.='5R;CL-"GT-" T*#0H`
`
end

begin 666 Sort.d
M#0IM;V1U;&4 4V]R=#L-" T*<')I=F%T92!I;7!O<G0 8RYS=&1I;SL-" T*
M#0H ("\O("TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM
M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+0T*(" O+R!A8G-T<F%C="!B
M87-E(&-L87-S#0H ("\O("TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM
M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+0T*8VQA<W, 
M45-O<G1"87-E#0I[#0H-"B  <'5B;&EC('9O:60 <V]R=" I#0H ('L-"B  
M("!U:6YT('-I>F4 /2!T:&ES+G-I>F4H*3L-"B  ("!I9B H('-I>F4 /B Q
M("D-"B  ("  (%-O<G0N<7-O<G0H(# L('-I>F4 +2 Q+"!T:&ES+F-O;7!A
M<F4L('1H:7,N<W=A<" I.PT*("!]#0H-"B  <'5B;&EC(&%B<W1R86-T('5I
M;G0 <VEZ92 I.PT*#0H ('!U8FQI8R!A8G-T<F%C="!V;VED(')E=F5R<V4H
M*3L-" T*("!P<F]T96-T960 86)S=')A8W0 :6YT(&-O;7!A<F4H('5I;G0 
M:2P =6EN="!J("D[#0H #0H ('!R;W1E8W1E9"!A8G-T<F%C="!V;VED('-W
M87 H('5I;G0 :2P =6EN="!J("D[#0H-"GT-" T*#0H ("\O("TM+2TM+2TM
M+2TM+2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[#0H ("  4V]R="YQ<V]R="  <"P <2P 8V]M<&%R92P <W=A
M<" I.PT*("  (%-O<G0N<7-O<G0H('$ *R Q+"!R+"!C;VUP87)E+"!S=V%P
M("D[#0H ("  ?0T*?0T*#0H-"G!R:79A=&4 <W1A=&EC('5I;G0 <&%R=&ET
M:6]N*"!U:6YT(' L('5I;G0 <BP-"B  :6YT("!D96QE9V%T92  =6EN="P 
M=6EN=" I(&-O;7!A<F4L#0H ('9O:60 9&5L96=A=&4H('5I;G0L('5I;G0 
M*2!S=V%P("D-"GL-"B  =6EN="!I(#T <" M(#$[#0H ('5I;G0 :B ]('( 
M*R Q.PT*("!W:&EL92 H(#$ *0T*("  ('L-"B  ("!D;R![#0H ("  ("!J
M("T](#$[#0H ("  ("!]('=H:6QE*"!C;VUP87)E*"!J+"!P("D /3T +3$ 
M*3L-"B  ("!D;R![#0H ("  ("!I("L](#$[#0H ("  ("!]('=H:6QE("  
M8V]M<&%R92  :2P <" I(#T](#$ *3L-"B  ("!I9B H(&D /"!J("D-"B  
M("  ('-W87 H(&DL(&H *3L-"B  ("!E;'-E#0H ("  ("!R971U<FX :CL-
M"B  ("!]#0I]#0H-" T*+R\ +2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM
M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+0T*#0IU
M;FET=&5S= T*>PT*("!P<FEN=&8H(")<;E1E<VEN9R!C;&%S<R!14V]R=$)A
M<V5<;BTM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM7&XB("D[#0H (&-L87-S
M(%0 .B!14V]R=$)A<V4-"B  ("![#0H ("  <')I=F%T92!C:&%R6UU;-ET 
M9&%T83L-"B  ("!T:&ES*"D-"B  ("  ('L-"B  ("  ('1H:7,N9&%T85L 
M,"!=(#T (D9O>'1R;W0B.R!T:&ES+F1A=&%;(#$ 72 ](")!;'!H82([#0H 
M("  ("!T:&ES+F1A=&%;(#( 72 ](")$;V<B.R!T:&ES+F1A=&%;(#, 72 ]
M(")"86ME<B([#0H ("  ("!T:&ES+F1A=&%;(#0 72 ](")%8VAO(CL =&AI
M<RYD871A6R U(%T /2 B0VAA<FQI92([#0H ("  ("!]#0H ("  <'5B;&EC
M(&-H87( 6UU;72!G971$871A*"D >R!R971U<FX =&AI<RYD871A.R!]#0H 
M("  <'5B;&EC(&]V97)R:61E('9O:60 <F5V97)S92 I('L =&AI<RYD871A
M+G)E=F5R<V4[('T-"B  ("!P=6)L:6, ;W9E<G)I9&4 =6EN="!S:7IE*"D 
M>R!R971U<FX =&AI<RYD871A+FQE;F=T:#L ?0T*("  ('!R;W1E8W1E9"!O
M=F5R<FED92!I;G0 8V]M<&%R92  =6EN="!I+"!U:6YT(&H *0T*("  ("  
M>PT*("  ("  8VAA<B!;72!V:2 ]('1H:7,N9&%T85L :2!=.PT*("  ("  
M8VAA<B!;72!V:B ]('1H:7,N9&%T85L :B!=.PT*("  ("  <F5T=7)N("AV
M:2 \('9J*2 _(#$ .B H=FD /3T =FHI(#\ ," Z("TQ.PT*("  ("  ?0T*
M("  ('!R;W1E8W1E9"!O=F5R<FED92!V;VED('-W87 H('5I;G0 :2P =6EN
M="!J("D-"B  ("  ('L-"B  ("  (&-H87( 6UT =&UP26YT(#T =&AI<RYD
M871A6R!I(%T[#0H ("  ("!T:&ES+F1A=&%;(&D 72 ]('1H:7,N9&%T85L 
M:B!=.PT*("  ("  =&AI<RYD871A6R!J(%T /2!T;7!);G0[#0H ("  ("!]
M#0H ("  ?0T*("!4('0 /2!N97< 5" I.PT*("!T+G-O<G0H*3L-"B  ="YR
M979E<G-E*"D[#0H (&-H87( 6UU;72!D(#T ="YG971$871A*"D[#0H (&9O
M<B H(&EN="!I(#T ,#L :2 \('0N<VEZ92 I.R!I*RL *0T*("  ('!R:6YT
M9B  9%L :2!=('X (EQN(B I.PT*?0T*#0IU;FET=&5S= T*>PT*(" O+VEM
M<&]R="!C+G-T9&EO.PT*("!P<FEN=&8H(")<;E1E<VEN9R!C;&%S<R!14V]R
M=%QN+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2U<;B( *3L-"B  <W1A=&EC
M(&-H87);75LV72!D871A(#T 6R B1F]X=')O="(L(")!;'!H82(L(")$;V<B
M+" B0F%K97(B+" B16-H;R(L(")#:&%R;&EE(B!=.PT*("!I;G0 8V]M<&%R
M92  =6EN="!I+"!U:6YT(&H *0T*("  ('L-"B  ("!C:&%R(%M=(&1I(#T 
M9&%T85L :2!=.PT*("  (&-H87( 6UT 9&H /2!D871A6R!J(%T[#0H ("  
M<F5T=7)N("AD:2 \(&1J*2 _(#$ .B H9&D /3T 9&HI(#\ ," Z("TQ.PT*
M("  ('T-"B  =F]I9"!S=V%P*"!U:6YT(&DL('5I;G0 :B I#0H ("  >PT*
M("  (&-H87( 6UT =&UP26YT(#T 9&%T85L :2!=.PT*("  (&1A=&%;(&D 
M72 ](&1A=&%;(&H 73L-"B  ("!D871A6R!J(%T /2!T;7!);G0[#0H ("  
M?0T*("!14V]R="!Q<R ](&YE=R!14V]R=" I.PT*("!Q<RYS;W)T*"!D871A
M+FQE;F=T:"P 8V]M<&%R92P <W=A<" I.PT*("!F;W( *"!I;G0 :2 ](# [
M(&D /"!D871A+FQE;F=T:#L :2LK("D-"B  ("!P<FEN=&8H(&1A=&%;(&D 
272!^(")<;B( *3L-"GT-" T*
`
end
Jul 29 2003