Counter Examples
I love experimenting with programming languages, databases, frameworks, etc.
The standard example of a "Hello World" program is nice to get started, but usually way too simple. So I started to create a slightly more complex example. A counter example.
In each example we are going to create a counter. A simple counter will just increment a single number. A multi-counter will handle several counters. e.g. one per client, one per user, or one per some arbitrary string.
I think these counter example can show you several aspects of a programming environment and can help you get started writing your own applications.
In this project we are going to implement a counter with various front-ends and back-ends. Sometimes these two parts will be in one program. Some of the solution will be web based. Some will be command line based. Some might be GUI-based.
The counters might be stored in some kind of a "database". It can be a plain text file, some data serialization format, a relational database, a NoSQL database and who knows what else.
The simpler version of the counter handles a single counter. Every time we run the counter it will show a number one higher than the previous one.
An interaction with a command-line tool might look like this:
$ count 1 $ count 2 $ count 3
The multi-counter is slightly more complex. It will be able to maintain several counters at once. So an interaction with a command-line tool might look like this:
$ count foo foo: 1 $ count foo foo: 2 $ count bar bar: 1 $ count foo foo: 3
This is going to be a long project, but it might help understand various techniques for data serialization.
JavaScript
- Plain Vanilla JavaScript
- Improved Vanilla JavaScript
- On-load counter with plain Vanilla JavaScript and local storage
- Multiple counters with plain Vanialla JavaScript and local storage
Python
- Command-line counter in Python for a single counter.
- Python and MongoDB
- Flask counter with SQLite, SQLAlchemy, pytest
Go
Perl
- A command-line counter script in Perl that uses a plain file called counter.txt for a single counter.
- Multiple command line counters with plain TSV text file back-end in Perl.
- A command line counter with database back-end using Perl DBIx::Class
- Command line counter with JSON backend in Perl.
- Counter with Perl Dancer session
- Command line counter in Perl with MongoDB as storage
- Counter with Perl Mojolicious Lite
- Command line counter with Memcached using Perl
- Increasing numbers in a text file using Perl
- Counter example in Perl using YAML file to store the data
- A counter using Perl Dancer and an in-memory SQLite database
- A counter using Perl Dancer and Redis, both in a Docker container
R
Rust
- Command line counter with plain text file storage in Rust
- Command line multi-counter with JSON file storage in Rust
- Rocket web framework - Single counter in a plain text file
- Rocket web framework - Multi-counter using cookies
- Rocket web framework - Multi counter using encrypted cookies
- Multi-counter with embedded SurrealDB database using Rust
Angular JS
- Simple In-memory counter using AngularJS
- Automatic counter using AngularJS
- Several counters in MongoDB client
Published on 2019-03-30