Remix.run Logo
veqq 5 hours ago

> Edgar Codd formalised relational algebra in 1970. SQL sits on top of it as a declarative interface. You describe what you want. The database engine decides how to get it. The engine improves every year. Your query stays the same.

Although SQL is of course not relational Algebra (and others like Datalog and D4M are better), it's still cool. It inspired kSQL like Lil uses https://beyondloom.com/decker/lil.html#lilthequerylanguage , which inspired the code I'm most proud of: https://codeberg.org/veqq/declarative-dsls A common query language, a common idiom, for many data structures (arrays, hashmaps, datafremas) is liberating, permitting you to e.g. solve sudoku, make mandelbrot sets or calculate primes directly:

    (def n 40) # to reach primes up to, left is sqr of n, right n/2, then multiply them for rows
    (def composites
    (df/select :from (range 2 (+ 1 (math/floor (math/sqrt n))))
               :cross (range 2 (+ 1 (/ n 2)))
               :where |(<= (* ($ :value_left) ($ :value_right)) n)
               [[:value_left :value_right] :value
                |(* ($ :value_left) ($ :value_right))]))
    (df/select :from (range 2 (+ 1 n)) :exclude composites)
Or e.g.

    (import declarative-dsls/dataframes :as df)
    (def people (df/dataframe :name :age :job))
    (df/dataframe? people)
    
    (df/insert! {:name "Bob" :age 30 :job "Developer"} :into people)
    (df/insert! {:name "Alice" :age 27 :job "Sales"} :into people)
    (df/update! :set {:job "Engineer"}
             :where |(= ($ :job) "Developer")
             :from people)
    
    (df/save-csv people "people.csv" :sep "\\t")
    (def people2 (df/load-csv "people.csv" :sep "\\t"))
    
    (-> people2
       df/dataframe->rows
       df/rows->dataframe
       df/print-as-table)
The tests file has many such things (like the sudoku solver) and even datalog and minikanren implemented on top of this!
andersmurphy 5 hours ago | parent [-]

Datalog is the dream. But SQL with a good query builder like Clojure's honeysql is not so bad.

That and SQLite seems to be able to scale to almost any problem, is disgustingly fast and with litestream incredibly resilient.