Skip to content

Instantly share code, notes, and snippets.

View sma's full-sized avatar

Stefan Matthias Aust sma

  • I.C.N.H GmbH
  • Kiel
View GitHub Profile
@sma
sma / parser_combinator.md
Last active March 3, 2024 13:00
A toy parser combinator using Dart 3

Parser Combinator

I wrote a toy parser combinator using Dart 3.

[Warning, the current Dart compiler crashes when running this!]

To enable inline classes, add these lines to your analysis_options.yaml file:

enable-experiment:
  - inline-class
@sma
sma / roll.dart
Created December 30, 2022 10:51
Rolls some dice, sums up the results
import 'dart:math';
final _r = Random();
/// Rolls dice according to a dice formula that follows this EBNF grammar:
///
/// formula = term {op term}.
/// op = "+" | "-".
/// term = dice | integer.
/// dice = integer "d" integer.

Server-Sent Events

There's something called server-sent events which is an alternative to using web sockets if you want to break free from the strict request-response scheme.

I want to explore this technology using Dart.

First, let's create the simplest webserver that could possibly work:

Future main(List arguments) async {
@sma
sma / effects.dart
Created November 30, 2022 08:20
A reactive programming experiment
import 'dart:async';
/// A reactive variable which will automatically rerun an effect if
/// changed after using it within an effect function run by [createEffect].
class Signal<T> {
Signal(T initialValue) : _value = initialValue;
T call() {
final tracker = _Tracker.current;
if (tracker != null) _trackers.add(tracker);
@sma
sma / terminal-flutter.md
Last active October 3, 2024 19:12
For fun, I wrote a Flutter-like framework for command line applications

Terminal Flutter

For fun, I recreated a subset of Flutter that is sufficient to build a tiny Minesweeper application for the terminal.

Here is how it looks:

+----------------------+
|Minesweeper       3/12|
| |
@sma
sma / _gui.md
Created November 29, 2021 14:14
A tutorial for Dart on how to create a GUI from scratch that runs in a HTML canvas

Canvas GUI

So you want to create a GUI from scratch using HTML canvas?

Colors

Let's start with an abstraction for ARGB color values:

class Color {
@sma
sma / server.dart
Created November 17, 2021 11:05
A very simple key value storage engine
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math' show Random;
final _random = Random.secure();
/// Returns a unique id of length 20, based on ~120 bits of randomness.
String uid() {
const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
@sma
sma / dotenv.dart
Created November 12, 2021 14:22
Read an `.env` and provide easy access
import 'dart:io';
class DotEnv {
DotEnv(String path)
: _environment = Map.fromIterable(
File(path)
.readAsLinesSync()
.map((line) => line.trim())
.where((line) => line.isNotEmpty && !line.startsWith('#')),
key: (line) => line.substring(0, line.indexOf('=')).trim(),

Eine Datenbank in Dart

Ich habe aus Versehen eine SQL-Datenbank geschrieben.

Sie ist nicht produktiv einsetzbar, zeigt aber das Prinzip.

Eine Datenbank verwaltet Tabellen, die Zeilen und Spalten haben. Konzentrieren wir uns im folgenden auf eine Tabelle, denn Relationen zwischen Tabellen unterstütze ich nicht.

Spalten könnten auf einen Datentyp beschränkt sein, das unterstütze ich aber nicht. Da ich alles im Hauptspeicher halte, kann bei mir in jeder Spalte jedes Objekt stecken. Gedanklich beschränke ich mich aber auf null und Werte der Typen bool, int, double, String und DateTime.

@sma
sma / MainFlutterWindow.swift
Created October 31, 2021 09:45
Flutter: Observing whether the application window is active or not
import Cocoa
import FlutterMacOS
class MainFlutterWindow: NSWindow, NSWindowDelegate {
override func awakeFromNib() {
let flutterViewController = FlutterViewController.init()
let windowFrame = self.frame
self.contentViewController = flutterViewController
self.setFrame(windowFrame, display: true)