An alternative to Array literal #(...), using a String instead

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

An alternative to Array literal #(...), using a String instead

John Trinder
The real drawback of Smalltalk's Array literal, #(), is that it only allows
the programmer to store very simple objects - for example, #(1 2 3), etc. If
one does something like #(1@2 3@4) then this is converted to anArray,  #(1
#@ 2 3 #@ 4) which is not what is wanted most times. What is wanted
generally by the user is to convert the literal "properly" into a collection
of, in this case, Points.

I have attached a package consisting of 3 String methods one of which is the
single user method - String>>asLiteral
Instead of using an Array literal to input data, a String is used and the
data is entered as per Array literal. This is parsed into it's component
Smalltalk expressions and then each expression evaluated by native Dolphin
base messages. The result is an OrderedCollection of correspondingly ordered
objects.

I trust that many people will find this simple method useful - I wonder that
no one thought of it years ago.

Any bugs and suggestions, please write me at [hidden email]

Regards all, John Trinder







begin 666 JT String method asLiteral.pac
M?"!P86-K86=E('P-"G!A8VMA9V4@.CT@4&%C:V%G92!N86UE.B G2E0@4W1R
M:6YG(&UE=&AO9"!A<TQI=&5R86PG+@T*<&%C:V%G92!P87A697)S:6]N.B P
M.PT*"6)A<VEC0V]M;65N=#H@)S(W4V5P,#,-"@T*4&%C:V%G92!C;VYS:7-T
M<R!O9B S(&UE=&AO9',@;VYL>2X-"@T*57-E<B!-971H;V1S.B @4W1R:6YG
M/CYA<TQI=&5R86P-"@T*57-A9V4Z(" \85-T<FEN9SX@87-,:71E<F%L#0H)
M=VAE<F4@85-T<FEN9R!C86X@8F4@82!S97)I97,@;V8@<')I;6ET:79E(%-M
M86QL=&%L:R!E>'!R97-S:6]N<R!S97!E<F%T960@8GD-"@ES<&%C97,-"@T*
M06YS=V5R.B!A;D]R9&5R961#;VQL96-T:6]N#0H)8V]N<VES=&EN9R!O9B!T
M:&4@86)O=F4@97AP<F5S<VEO;G,@979A;'5A=&5D#0H-"E9A;&ED($5X<')E
M<W-I;VYS.@T*"4UA>2!C;VYT86EN(&=L;V)A;"!V87)I86)L92!R969E<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R:79A=&4A("$-"@T*(D5N9"!O9B!P86-K86=E(&1E9FEN:71I;VXB(0T*
M#0HB4V]U<F-E($=L;V)A;',B(0T*#0HB0VQA<W-E<R(A#0H-"B)":6YA<GD@
=1VQO8F%L<R(A#0H-"B)297-O=7)C97,B(0T*#0H`
`
end


Reply | Threaded
Open this post in threaded view
|

Re: An alternative to Array literal #(...), using a String instead

Bill Schwab-2
John,

> I have attached a package consisting of 3 String methods one of which is
the
> single user method - String>>asLiteral
> Instead of using an Array literal to input data, a String is used and the
> data is entered as per Array literal. This is parsed into it's component
> Smalltalk expressions and then each expression evaluated by native Dolphin
> base messages. The result is an OrderedCollection of correspondingly
ordered
> objects.

That's an interesting idea, but I suspect that it would be much slower than

| out |
out := Array writeStream.
out
    nextPut:1@2;
    nextPut:3@4;
    nextPut;'etc.'.
out contents.

which is what I usually do under these circumstances.  Another approach is

OrderedCollection new
    add:2@3;
    add:'etc.';
    yourself.

Parsing and evaluating has uses of course.  There have been some questions
recently about web features for Dolphin.  You might combine this with
FastCGI and Apache to allow Smalltalk expressions to be embedded in web
pages.  I recently started doing something similar with replacing pieces of
LaTeX source based on Smalltalk expressions.

Have a good one,

Bill

--
Wilhelm K. Schwab, Ph.D.
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: An alternative to Array literal #(...), using a String instead

John Trinder
Hi Bill,

Thanks for your reply.
I'm sure your correct. However I wanted an easy way to enter data
programmatically which I first thought sometime ago Array literals would do.
Obviously this is going to be slower but the convenience afforded by having
an alternative array-like literal in most circumstances would far outweigh
the shortcomings.

For example, who'd want to use

OrderedCollection new add: #one->1; add: #two->2; add: #three->3; add:
#four->4; ......; add: #twenty->20.

When you could just use

'#one -> 1 #two -> 2 #three -> 3 #four -> 4 ..... #four -> 20' asLiteral

Regards, John


"Bill Schwab" <[hidden email]> wrote in message
news:[hidden email]...
> John,
>
> > I have attached a package consisting of 3 String methods one of which is
> the
> > single user method - String>>asLiteral
> > Instead of using an Array literal to input data, a String is used and
the
> > data is entered as per Array literal. This is parsed into it's component
> > Smalltalk expressions and then each expression evaluated by native
Dolphin
> > base messages. The result is an OrderedCollection of correspondingly
> ordered
> > objects.
>
> That's an interesting idea, but I suspect that it would be much slower
than

>
> | out |
> out := Array writeStream.
> out
>     nextPut:1@2;
>     nextPut:3@4;
>     nextPut;'etc.'.
> out contents.
>
> which is what I usually do under these circumstances.  Another approach is
>
> OrderedCollection new
>     add:2@3;
>     add:'etc.';
>     yourself.
>
> Parsing and evaluating has uses of course.  There have been some questions
> recently about web features for Dolphin.  You might combine this with
> FastCGI and Apache to allow Smalltalk expressions to be embedded in web
> pages.  I recently started doing something similar with replacing pieces
of

> LaTeX source based on Smalltalk expressions.
>
> Have a good one,
>
> Bill
>
> --
> Wilhelm K. Schwab, Ph.D.
> [hidden email]
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: An alternative to Array literal #(...), using a String instead

Dave Harris-3
[hidden email] (John Trinder) wrote (abridged):
> For example, who'd want to use
>
> OrderedCollection new add: #one->1; add: #two->2; add: #three->3; add:
> #four->4; ......; add: #twenty->20.
>
> When you could just use
>
> '#one -> 1 #two -> 2 #three -> 3 #four -> 4 ..... #four -> 20' asLiteral

I think I prefer the first version. The verbosity of specifying the
collection type is offset by the benefit of being able to choose the type.
Admittedly #add: is not very convenient, mainly because it returns the
element instead of the container and partly because as a keyword message
it tends to get itself glued up syntactically. If I had to do a lot of
literal arrays I would probably add a #<<: message to the collection
protocol. In simple cases it becomes:

    array := OrderedCollection << 1 << 2 << 3 << 4.

Some cases now need brackets:

    array := OrderedCollection
        << (#one->1) << (#two->2) << (#three->3).

but I don't mind that. It's at least clear what is going on. I dislike
your #asLiteral for two reasons. First, I think it's error prone and
harder to read because there's no separator between expressions. Secondly,
I dislike making up message selectors from expressions because it affects
optimisation and makes it harder to see where all the selectors are.

-- Dave Harris, Nottingham, UK


Reply | Threaded
Open this post in threaded view
|

Re: An alternative to Array literal #(...), using a String instead

Blair McGlashan-2
In reply to this post by John Trinder
"John Trinder" <[hidden email]> wrote in message
news:3f76e33d$[hidden email]...

> ...
> For example, who'd want to use
>
> OrderedCollection new add: #one->1; add: #two->2; add: #three->3; add:
> #four->4; ......; add: #twenty->20.
>
> When you could just use
>
> '#one -> 1 #two -> 2 #three -> 3 #four -> 4 ..... #four -> 20' asLiteral
>

Or you could use ##() expressions if compile time construction is
appropriate (and I guess it would be since construction of literal arrays
seems to be what you have in mind):

#(##(#one->1) ##(#two->2) ##(#three -> 3) ... ##(#twenty -> 20))

This is not so easy on the eye*, but it will carry no runtime overhead. A
further advantage is that the symbols and any object references in the array
will be visible to the reflection facilities of the language, and so the
browsers, automatic package dependency tracking, and image stripper will be
able to see them.

Regards

Blair

*We'd have preferred a different, "lighter", syntax (such as the use of
curly brackets) but at the time we introduced compile time expression
evaluation Visual Age had already settled on ##(), so we went along with
that. It's a shame the same attitude has not prevailed over the introduction
of namespace separators into the various Smalltalks.