eucalypt

eucalypt · functional data language

A little language for YAML, JSON and TOML.

Generate, template and transform structured data with a lazy, functional language and a command-line tool that reads and writes the formats you already use.

$ brew install curvelogic/homebrew-tap/eucalypt

Every output in this editor was produced by the eu engine when the page was built.

layers.eu
service(overrides): {
  name: "ledger"
  replicas: 2
  db: { host: "db.internal" port: 5432 pool: 10 }
} << overrides

staging: service{ db: { host: "db.staging" } }
production: service{ replicas: 6 db: { pool: 40 } }
Output
---
staging:
  name: ledger
  replicas: 2
  db:
    host: db.staging
    port: 5432
    pool: 10
production:
  name: ledger
  replicas: 6
  db:
    host: db.internal
    port: 5432
    pool: 40

It speaks your formats

Reads what you have. Writes what you need.

YAML, JSON, JSON Lines, TOML, EDN, XML, CSV and text in; YAML, JSON, TOML, EDN, HTML, text and eucalypt itself out. Mix them on one command line.

Embedded in YAML

Tag a value with !eu and keep the rest of the file as it is.

config.yaml
region: eu-west-1
buckets: !eu '["logs", "assets"] map("{region}-{}")'
eu config.yaml
output
region: eu-west-1
buckets:
  - eu-west-1-logs
  - eu-west-1-assets

CSV in, totals out

Import a file as a named input and process it like any other data.

staff.csv
name,team,hours
ada,core,38
brendan,web,22
grace,core,41
hours.eu
{ import: "rows=staff.csv" }

hours: rows group-by(_.team) map-values(map(_.hours num) ; sum)
eu hours.eu
output
hours:
  core: 79
  web: 22

A one-liner in the shell

Query structured data without writing a file. Here, JSON out with -j.

terminal
$ eu -j rows=staff.csv -e 'rows filter(_.team = "core") map(.name)'
stdout
[
  "ada",
  "grace"
]

Why eucalypt

Three things it is, and what each one means.

Data-native

Blocks are mappings and lists are lists. You build the data structure itself, so there is no indentation to template and no quoting to escape.

output as YAML, JSON or TOML
[staging]
name = "ledger"
replicas = 2
What eucalypt is for →

Lazy and functional

Nothing is evaluated until the output needs it, so infinite lists and unused branches cost nothing.

powers.eu
` "An infinite list, evaluated only as far as it is needed."
powers: iterate(* 2, 1) take(8)
eu -x toml powers.eu
powers = [1, 2, 4, 8, 16, 32, 64, 128]
Lazy evaluation →

Gradually typed

Annotate where it helps. The checker warns without stopping evaluation, unless you pass --strict.

types.eu
` { type: "number -> string" }
port-label(n): "port {n}"

label: port-label("8080")
stderr
warning: type mismatch calling 'port-label'
  ┌─ types.eu:4:19
  │
4 │ label: port-label("8080")
  │                   ^^^^^^ expected number, found "8080"
Type checking →

A short tour

Six ideas, each a line or two long.

Every snippet runs. Open one in the playground to change it.

Pipelinescatenation

last: [1, 2, 3, 4, 5] reverse head
last: 5
Open in the playground →

Anaphora_0, {}

squares: [1, 2, 3, 4] map(_0 * _0)
labels: [:a, :b] map("item-{}")
squares:
  - 1
  - 4
  - 9
  - 16
labels:
  - item-a
  - item-b
Open in the playground →

Interpolation"{x:%03d}"

user: { name: "ada" id: 7 }
greeting: "Hello, {user.name}! You are user #{user.id:%03d}."
user:
  name: ada
  id: 7
greeting: "Hello, ada! You are user #007."
Open in the playground →

Deep merge<<

merged: { db: { host: "localhost" port: 5432 } }
  << { db: { host: "db.prod" } }
merged:
  db:
    host: db.prod
    port: 5432
Open in the playground →

Destructuringf{w h}

area{w h}: w * h
room: area{w: 4 h: 3}
room: 12
Open in the playground →

Inline tests//=>

slug(s): s str.to-lower str.replace(" +", "-")

title: "Hello Gum Trees" slug //=> "hello-gum-trees"
title: hello-gum-trees
Open in the playground →

Get started

Install it, then point your editor at it.

macOS · Homebrew
$ brew install curvelogic/homebrew-tap/eucalypt
Linux and macOS · install script
$ curl -sSf https://raw.githubusercontent.com/curvelogic/eucalypt/master/install.sh | sh
From source, in a clone of the repository
$ cargo install --path .
Quick start →

Editor support

VS
VS Code
The eucalypt extension starts the language server for you.
λ
Emacs
eucalypt-mode, with eglot for the language server.
eu
Any LSP client
eu lsp speaks the Language Server Protocol over stdio.
Language server →