On this page:
Qi:   A Functional, Flow-Oriented DSL
8.3

Qi: A Functional, Flow-Oriented DSL

Siddhartha Kasivajhula

 (require qi) package: qi-lib

An embeddable, general-purpose language to allow convenient framing of programming logic in terms of functional flows.

One way to structure computations – the one we typically employ when writing functions in Racket or another programming language – is as a flowchart, with arrows representing transitions of control, indicating the sequence in which actions are performed. Aside from the implied ordering, the actions are independent of one another and could be anything at all. Another way – provided by the present module – is to structure computations as a fluid flow, like a flow of energy, electricity passing through a circuit, streams flowing around rocks. Here, arrows represent that actions feed into one another.

The former way is often necessary when writing functions at a low level, where the devil is in the details. But once these functional building blocks are available, the latter model is often more appropriate, allowing us to compose functions at a high level to derive complex and robust functional pipelines from simple components with a minimum of repetition and boilerplate, engendering effortless clarity. The facilities in the present module allow you to employ this flow-oriented model in any source program.

> (( (~> sqr add1)) 3)

10

> (map ( (~> sqr add1)) (list 1 2 3 4))

'(2 5 10 17)

> (filter ( (< 5 _ 10)) (list 3 7 9 12))

'(7 9)

> (~> (2 3) (>< ->string) string-append)

"23"

> (define-flow ( m n)
    (~> - abs (< 1)))
> ( 5 7)

#f

> ( 5 5.4)

#t

> (define-flow root-mean-square
    (~>> (map sqr) (-< sum length) / sqrt))
> (root-mean-square (range 10))

5.338539126015656

    1 Using These Docs

    2 Introduction and Usage

      2.1 What is a Flow?

      2.2 Usage

      2.3 Flowing with the Flow

        2.3.1 DrRacket

        2.3.2 Vim/Emacs

          2.3.2.1 Keybindings

          2.3.2.2 Indentation

      2.4 Relationship to the Threading Macro

    3 Tutorial

      3.1 Interactive Tutorial

      3.2 Online Tutorial

    4 Language Interface

      4.1 Core

      4.2 Threading

      4.3 Conditionals

      4.4 Lambdas

      4.5 Definitions

      4.6 Interoperating with the Host Language

    5 Qi Forms

      5.1 Basic

      5.2 Predicates

      5.3 Boolean Algebra

      5.4 Routing

      5.5 Conditionals

      5.6 Higher-order Flows

      5.7 Literals

      5.8 Templates and Partial Application

    6 When Should I Use Qi?

      6.1 Hadouken!

        6.1.1 Super Smush Numbers

        6.1.2 Root-Mean-Square

      6.2 The Science of Deduction

        6.2.1 Compound Predicates

        6.2.2 abs

      6.3 The Structure and Interpretation of Flows

      6.4 Using the Right Tool for the Job