Hylograph.Unified.DataDSL
- Package
- purescript-hylograph-selection
- Repository
- afcondon/purescript-hylograph-selection
PSD3.Unified.DataDSL - Unified Data Operations
This module provides a unified finally-tagless DSL for data operations that works across both visualization (PSD3) and spreadsheet contexts.
Core Insight
D3's data joins and spreadsheet formulas are fundamentally the same:
selection.data(arr).join("rect")≈mapA template (source arr)=SUM(A1:A10)≈foldA (+) 0 (source range)
By abstracting these operations into a type class, we enable:
- Same computation definition for viz AND spreadsheet
- Multiple interpreters (Eval, Deps, Pretty, CodeGen)
- Type-safe composition across contexts
Relationship to Existing PSD3 Classes
DataDSL extends and unifies the existing expression classes:
NumExpr→DataDSL(num, add, sub, mul, div)BoolExpr→DataDSL(bool, and, or, not, ifThenElse)CompareExpr→DataDSL(lt, lte, gt, gte, eqNum)
Plus NEW array/data operations:
source,mapA,foldA,filterA,flatMapA
Usage
-- Define computation once
growthRates :: forall repr. DataDSL repr => repr (Array Number) -> repr (Array Number)
growthRates = mapA (\x -> x * 1.1)
-- Use in visualization
myViz = join (growthRates data) \rate -> elem Rect [height (rate * 10)]
-- Use in spreadsheet
myCell = format percentageD (avgA (growthRates data))
#DataDSL Source
class DataDSL :: (Type -> Type) -> Constraintclass DataDSL (repr :: Type -> Type) where
The unified data operations type class
This class captures the fundamental operations on data that are shared between visualization and spreadsheet contexts.
Key insight: These operations are exactly what D3 data joins do, and also what spreadsheet formulas do. They're the same thing!
Members
num :: Number -> repr Numberstr :: String -> repr StringString literal
bool :: Boolean -> repr BooleanBoolean literal
arr :: forall a. Array a -> repr (Array a)Array literal
source :: forall a. DataSource a -> repr (Array a)Data source - the "join point" where external data enters
In D3:
selection.data(arr)In spreadsheet: cell range reference likeA1:A10mapA :: forall a b. (a -> b) -> repr (Array a) -> repr (Array b)Map over array - the heart of D3 data joins
In D3: The template function in
.join("rect").attr("height", d => d.value)In spreadsheet:=MAP(A1:A10, x => x * 2)foldA :: forall a b. (b -> a -> b) -> b -> repr (Array a) -> repr bFold/reduce array to single value
In D3:
.data(arr).reduce((acc, d) => acc + d.value, 0)In spreadsheet:=SUM(A1:A10),=PRODUCT(B1:B5)filterA :: forall a. (a -> Boolean) -> repr (Array a) -> repr (Array a)Filter array by predicate
In D3:
.data(arr.filter(d => d.value > 0))In spreadsheet:=FILTER(A1:A10, x => x > 0)flatMapA :: forall a b. (a -> Array b) -> repr (Array a) -> repr (Array b)FlatMap - map then flatten (for nested data)
This is the
decomposein NestedJoin! In D3: Nested selections In spreadsheet:=FLATTEN(MAP(A1:A10, x => x.items))zipWithA :: forall a b c. (a -> b -> c) -> repr (Array a) -> repr (Array b) -> repr (Array c)Zip two arrays with combining function
headA :: forall a. repr (Array a) -> repr (Maybe a)Get first element
add :: repr Number -> repr Number -> repr NumberArithmetic operations (from NumExpr)
sub :: repr Number -> repr Number -> repr Numbermul :: repr Number -> repr Number -> repr Numberdiv :: repr Number -> repr Number -> repr Numbernegate :: repr Number -> repr Numberconcat :: repr String -> repr String -> repr StringString operations (from StringExpr)
lt :: repr Number -> repr Number -> repr BooleanComparison operations (from CompareExpr)
lte :: repr Number -> repr Number -> repr Booleangt :: repr Number -> repr Number -> repr Booleangte :: repr Number -> repr Number -> repr BooleaneqNum :: repr Number -> repr Number -> repr BooleanstrEq :: repr String -> repr String -> repr BooleanString comparison (from StringCompareExpr)
strNeq :: repr String -> repr String -> repr Booleanand :: repr Boolean -> repr Boolean -> repr BooleanBoolean operations (from BoolExpr)
or :: repr Boolean -> repr Boolean -> repr Booleannot :: repr Boolean -> repr BooleanifThenElse :: forall a. repr Boolean -> repr a -> repr a -> repr aConditional
#TrigDSL Source
class TrigDSL :: (Type -> Type) -> Constraintclass (DataDSL repr) <= TrigDSL repr where
Trigonometric operations - extension for visualization-specific math
Separated from DataDSL because spreadsheets rarely need trig, but polar/radial visualizations (chord diagrams, pie charts) do.
polarX :: forall repr. DataDSL repr => TrigDSL repr => repr Number -> repr Number -> repr Number
polarX r angle = r `mul` cos angle
Members
#DataSource Source
data DataSource aData source - where data enters the computation pipeline
This is the "join point" - analogous to D3's selection.data()
Constructors
Re-exports from Hylograph.Expr.Expr
#BoolExpr Source
class BoolExpr :: (Type -> Type) -> Constraintclass BoolExpr repr
Boolean expressions
#CompareExpr Source
class CompareExpr :: (Type -> Type) -> Constraintclass CompareExpr repr
Comparison expressions (produce Boolean from Numbers)
- Modules
- Data.
DependencyGraph - Hylograph.
AST - Hylograph.
Axis. Axis - Hylograph.
Brush - Hylograph.
Brush. FFI - Hylograph.
Brush. Types - Hylograph.
Classify - Hylograph.
Data. Graph - Hylograph.
Data. Graph. Algorithms - Hylograph.
Data. Node - Hylograph.
Data. Tree - Hylograph.
Expr. Animation - Hylograph.
Expr. Attr - Hylograph.
Expr. Datum - Hylograph.
Expr. Expr - Hylograph.
Expr. Friendly - Hylograph.
Expr. Integration - Hylograph.
Expr. Interpreter. CodeGen - Hylograph.
Expr. Interpreter. Eval - Hylograph.
Expr. Interpreter. Meta - Hylograph.
Expr. Interpreter. PureSVG - Hylograph.
Expr. Interpreter. SVG - Hylograph.
Expr. Path - Hylograph.
Expr. Path. Generators - Hylograph.
Expr. Sugar - Hylograph.
Expr. Units - Hylograph.
HATS - Hylograph.
HATS. Friendly - Hylograph.
HATS. InterpreterTick - Hylograph.
HATS. Transitions - Hylograph.
Interaction. Brush - Hylograph.
Interaction. Coordinated - Hylograph.
Interaction. Pointer - Hylograph.
Interaction. Zoom - Hylograph.
Internal. Attribute - Hylograph.
Internal. Behavior. FFI - Hylograph.
Internal. Behavior. Types - Hylograph.
Internal. Capabilities. Selection - Hylograph.
Internal. Capabilities. Transition - Hylograph.
Internal. FFI - Hylograph.
Internal. Selection. Join - Hylograph.
Internal. Selection. Operations - Hylograph.
Internal. Selection. Operations. Conversions - Hylograph.
Internal. Selection. Operations. Helpers - Hylograph.
Internal. Selection. Operations. Selection - Hylograph.
Internal. Selection. Query - Hylograph.
Internal. Selection. Types - Hylograph.
Internal. Transition. FFI - Hylograph.
Internal. Transition. Manager - Hylograph.
Internal. Transition. Scene - Hylograph.
Internal. Transition. Types - Hylograph.
Internal. Types - Hylograph.
Interpreter. D3 - Hylograph.
Interpreter. English - Hylograph.
Interpreter. Mermaid - Hylograph.
Interpreter. MetaAST - Hylograph.
Interpreter. SemiQuine - Hylograph.
Interpreter. SemiQuine. TreeToCode - Hylograph.
Interpreter. SemiQuine. Types - Hylograph.
Render - Hylograph.
Scale - Hylograph.
Scale. FP - Hylograph.
Shape. Arc - Hylograph.
Shape. Pie - Hylograph.
Shape. Polygon - Hylograph.
Tooltip - Hylograph.
Transform - Hylograph.
TreeDSL - Hylograph.
TreeDSL. ShapeTree - Hylograph.
Unified - Hylograph.
Unified. Attribute - Hylograph.
Unified. DataDSL - Hylograph.
Unified. Display - Hylograph.
Unified. Examples - Hylograph.
Unified. Join - Hylograph.
Unified. Sugar
Numeric literal