[ planet-factor ]

Joe Groff: Type-erased generic functions for C: A modest non-proposal

Earlier this year, I read Martin Uecker's proposal N3212 to add parametric polymorphism to C. It's easy to scoff at the idea of adding generic programming to plain C, when C++ already has templates, a…

Mon, 30 Sep 2024 00:40:00

Doug Coleman: Magic Combinator

Magic Combinator

In response to a blog post about Stack Complexity in Factor, I figured I would add one way that I structure programs when possible.

Suppose you want to make a server that runs sql queries where a route name is a filename. Some requirements of this are connecting to a database, opening a directory of sql templates, loading them all into a hashtable to query by filename, handling incoming connections, doing the query, writing the results back out–you obviously can’t reason about all of this state on the stack at the same time. It would be ten items deep, and we want to keep the stack depth to 3-4 things at once in the same stack effect. (Of course the stack keeps growing deeper, but we need to reason about only a few things at any time.)

You can imagine a magic combinator, composed of other combinators, that will do most of this for you. Suppose you start with a with-database combinator that will connect and tear down the connection and handle exceptions. Next, you need to read the templates–with-template-directory to the rescue! To handle all the incoming requests you will need a with-server combinator. We should also set up some routes that map to the filenames, maybe call it with-routes.

Now that we have all the parts, let’s combine them into a combinator that works with dynamic variables and you have your magic combinator!

! some setup variables
SYMBOL: db-settings   ! initial settings for db
SYMBOL: server-port   ! port

! state within the combinators
SYMBOL: db-connection
SYMBOL: templates
SYMBOL: routes
SYMBOL: client-connection

: with-networked-database-template-directory ( template-directory quot: ( args template -- ) -- )
    [ db-settings get ] 2dip '[
        _ [
             [
                 _ with-routes
            ] with-server
        ] with-template-directory
    ] with-database ; inline

Now your quot magically has the db connection, the templates, the routes, the client connection, and you can just concentrate on templating the query and getting the results, confident that this combinator takes care of the setup. You can access any of the state with code like db-connection get. If you need more state, just add another combinator. If there are problems, you can debug each combinator individually.

Hopefully this will help you handle state in your Factor programs, or even in python using the with keyword.

Thu, 11 Jul 2024 08:17:00

Doug Coleman: It's 2024 Already

We survived covid and we’re foolishly gearing up for WW3. What a great time to be a Factor blogger!

I got a job doing cmake and build systems in 2016 and since 2017 I’ve been doing JS/python/React/SOLR. I’m still working on Factor, so there should be a lot to post about.

Thanks to John for the sweet blogging setup!

Tue, 25 Jun 2024 08:17:00

Joe Groff: How the FM TOWNS boots from CD-ROM

The FM TOWNS was the first computer to come with a CD-ROM drive, and it well predates attempts to standardize bootable CD-ROM behavior on the IBM PC and other platforms. The FM TOWNS' boot process for…

Wed, 21 Feb 2024 01:52:00

Joe Groff: Use the COPY key to take screenshots in FM TOWNS Windows

The FM TOWNS keyboard doesn't have a PrintScreen key, so how do you get Windows to take a screenshot when Alt+PrintScreen isn't available? After failing to find an answer on the Internet, I randomly p…

Tue, 19 Dec 2023 18:03:00

Joe Groff: Imaging PC98, FM TOWNS, or X68000 floppy disks with KryoFlux

I got a KryoFlux so that I could image the original disks from some FM TOWNS software packages I recently got a hold of. The FM TOWNS, along with the NEC PC98 and Sharp X68000, all use a different phy…

Sat, 16 Dec 2023 03:25:00

Joe Groff: The lost language extensions of MetaWare's High C Compiler

This book I got in a pile of FM TOWNS books turns out to be a lot more interesting that I was expecting an '80s C compiler manual to be. For as long as C and its relatives have been in mainstream use,…

Mon, 25 Sep 2023 04:18:00

Joe Groff: Making a new CMOS battery for the FM TOWNS UX20

The previous owner of my FM TOWNS UX20 thoughtfully removed the CMOS battery from the computer to keep it from exploding and damaging the electronics inside, but unfortunately, TOWNS OS uses the CMOS …

Fri, 2 Jun 2023 04:04:00

Blogroll


planet-factor is an Atom/RSS aggregator that collects the contents of Factor-related blogs. It is inspired by Planet Lisp.

Syndicate