Hylograph.Expr.Integration
- Package
- purescript-hylograph-selection
- Repository
- afcondon/purescript-hylograph-selection
Integration with Selection System
This module bridges the "finally tagless" attribute DSL with the selection/update pattern. Expressions compile to attributes, enabling:
- Polymorphic attribute definitions (same expr → different interpreters)
- Type-safe datum field access
- Full enter/update/exit pattern support
Usage:
-- Define expression (polymorphic)
nodeRadius :: forall repr. NumExpr repr => BoolExpr repr => DatumExpr repr NodeRow => repr Number
nodeRadius = ifThenElse hasChildren (n 8.0) (n 5.0)
-- Convert to attribute
radiusAttr :: Attribute Node
radiusAttr = evalAttr "r" nodeRadius
-- Use in selection
append Circle [radiusAttr, evalAttr "fill" nodeFill] enterSelection
#evalAttr Source
evalAttr :: forall datum a. Show a => String -> EvalD datum a -> Attribute datumConvert a datum expression to a data-driven attribute.
The expression is interpreted using EvalD, which produces a function from (datum, index) to the attribute value.
-- expression
scaleX :: forall repr. NumExpr repr => DatumExpr repr PointRow => repr Number
scaleX = xField *: 20.0 +: 200.0
-- attribute
cxAttr :: Attribute Point
cxAttr = evalAttr "cx" scaleX
#evalAttrStr Source
evalAttrStr :: forall datum. String -> EvalD datum String -> Attribute datumConvert a string expression to an attribute (no quoting)
Use this for string-valued attributes like fill, stroke, class, d, etc.
#evalAttrIndexed Source
evalAttrIndexed :: forall datum a. Show a => String -> EvalD datum a -> Attribute datumConvert an expression to an indexed attribute.
Use this when your expression uses the datum index (e.g., for staggered positioning or animation delays).
-- expression using index
xFromIndex :: forall repr. NumExpr repr => DatumExpr repr PointRow => repr Number
xFromIndex = indexNum *: 50.0 +: 25.0
where indexNum = E.mul (n 1.0) (unsafeCoerce index) -- Convert Int to Number
-- indexed attribute
xAttr :: Attribute Point
xAttr = evalAttrIndexed "x" xFromIndex
#evalAttrIndexedStr Source
evalAttrIndexedStr :: forall datum. String -> EvalD datum String -> Attribute datumConvert an indexed string expression to an attribute (no quoting)
#liftFn Source
liftFn :: forall datum a. (datum -> a) -> EvalD datum aLift a datum-dependent PureScript function to an EvalD expression
This is an escape hatch for complex computations that can't easily be expressed in the DSL.
linePath :: Series -> String
linePath s = ...complex path computation...
d (liftFn linePath)
#fnAttrIStr Source
fnAttrIStr :: forall datum. String -> (datum -> Int -> String) -> Attribute datumCreate an indexed string attribute from a plain PureScript function
#evalAttrs Source
evalAttrs :: forall datum a. Show a => Array { expr :: EvalD datum a, name :: String } -> Array (Attribute datum)Convert multiple expressions to attributes at once.
This is a convenience for defining attribute sets:
circleAttrs :: Array (Attribute Point)
circleAttrs = evalAttrs
[ { name: "cx", expr: scaleX }
, { name: "cy", expr: scaleY }
, { name: "r", expr: radius }
]
Re-exports from Hylograph.Internal.Attribute
#AttributeValue Source
data AttributeValueAttribute values
We support the most common value types. The ADT ensures type safety when setting attributes.
Constructors
Instances
#AttributeName Source
newtype AttributeNameAttribute names (SVG/HTML properties)
We use a newtype to prevent typos and enable IDE autocomplete. The String inside is the actual DOM attribute name.
Constructors
Instances
#Attribute Source
data Attribute datumType-safe attribute with datum phantom type
Attributes can be:
- Static: Same value for all elements
- Data-driven: Value computed from datum (with source metadata)
- Indexed: Value computed from datum and index (with source metadata)
The phantom type datum ensures attributes are only applied
to selections with matching data types.
The AttrSource field enables interpreters to inspect attribute origins
without evaluating the functions.
Constructors
StaticAttr AttributeName AttributeValueDataAttr AttributeName AttrSource (datum -> AttributeValue)IndexedAttr AttributeName AttrSource (datum -> Int -> AttributeValue)AnimatedAttr { config :: AnimationConfig, fromValue :: Maybe (AnimatedValue datum), name :: AttributeName, toValue :: AnimatedValue datum }AnimatedCompound { config :: AnimationConfig, fromValues :: Array (AnimatedValue datum), generator :: Array Number -> String, name :: AttributeName, toValues :: Array (AnimatedValue datum) }
Instances
Show (Attribute datum)Contravariant AttributeContravariant instance for Attribute
Attributes consume data (they're data sinks), making them naturally contravariant. This enables attribute reuse via
cmap:-- Define attribute for specific type radiusAttr :: Attribute Number radiusAttr = DataAttr (AttributeName "r") NumberValue -- Adapt to work with richer type type Circle = { radius :: Number, x :: Number, y :: Number } circleRadiusAttr :: Attribute Circle circleRadiusAttr = cmap _.radius radiusAttrThe key insight:
cmapcomposes the projection function with the attribute's data accessor, allowing attributes written for simple types to work with complex types via field selection.
#AttrSource Source
data AttrSourceSource metadata for attributes
Describes where the attribute value comes from, enabling interpreters like MetaAST to show meaningful information about attribute bindings.
This is automatically captured when using the DSL (field, num, etc.)
but is UnknownSource for raw PureScript functions (escape hatches).
Constructors
Instances
- 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
Animated compound attribute for paths and other generated strings
Animates multiple numeric values and calls a generator function to produce the final string value. Used for:
The generator receives interpolated values in the same order as fromValues/toValues.