Samuel Montgomery-Blinn's blog

List comprehension using select and collect

In trying to figure out the "Smalltalk way" to do things like list comprehensions I stumbled across some very neat functionality:

Smalltalk does not have list comprehensions, but it does have higher-order map and filter methods, called collect and select respectively.

coll := ((1 to: 100) select: [:x| ( x * x ) > 3]) collect: [:x | (x * 2)].

You can try this out in gst pretty easily to see the result:

st> | coll |

Simple scripts without "Smalltalk at: #b put: ..."

Going back and thinking about "what I've learned so far" in the few days I've been typing around on GNU Smalltalk, I noticed that I put something in my previous "example snippets" which wasn't immediately intuitive when I got started.

Motivation: when writing a simple script, or playing around with `gst`, you want to execute some block of code that requires various intermediate results, and get a final result you can inspect, without the pinkie-mangling jibber-jabber that is:

Smalltalk at: #z put: 'some value'!
Smalltalk at: #y put: 'another value'!

Some (very) simple networking examples

Tagged:  •  

While I'm still quite the Smalltalk beginner, in trying to learn how a few things are done in GNU Smalltalk I have tried to jot a few things down.

First I wrote a small and very simple (1 file listing) echo server using the TCP package directly:

Second I wrote a small and very simple chat server, which adds to the above by using a few classes, a few different file listings, and (of course) a bit of "state" on the server side to allow named connections and multi-person chat:

Fun with MD5 and Random

Tagged:  •  

GNU Smalltalk comes with a few fairly nice libraries if you want to generate alphanumeric passwords or salts, and compute MD5 digests, both fairly standard things you might want to do when dealing with storing sensitive data such as passwords.

You can generate some random strings fairly easily using built-in libraries without requiring any packages:

| d i l s |
  i := 20.
  d := '0123456789abcdefghijklmnopqrstuvwxyz'.
  l := d size.
  s := String new: i.
  1 to: i do: [ :x |
    s at: x put: (d at: (Random between: 1 and: l))
  s printNl!

Syndicate content

User login