Module

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 datum

Convert 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 datum

Convert 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 datum

Convert 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 datum

Convert an indexed string expression to an attribute (no quoting)

#staticNum Source

staticNum :: forall datum a. Show a => String -> a -> Attribute datum

Create a static numeric attribute.

For expressions that don't depend on datum at all, this produces a StaticAttr which is more efficient (evaluated once, not per-element).

#staticStr Source

staticStr :: forall datum. String -> String -> Attribute datum

Create a static string attribute (no quoting)

#liftFn Source

liftFn :: forall datum a. (datum -> a) -> EvalD datum a

Lift 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)

#liftFnI Source

liftFnI :: forall datum a. (datum -> Int -> a) -> EvalD datum a

Lift an indexed PureScript function to an EvalD expression

#fnAttr Source

fnAttr :: forall datum a. Show a => String -> (datum -> a) -> Attribute datum

Create a data-driven attribute from a plain PureScript function

Shortcut for evalAttr name (liftFn f)

#fnAttrStr Source

fnAttrStr :: forall datum. String -> (datum -> String) -> Attribute datum

Create a string data-driven attribute from a plain PureScript function

Shortcut for evalAttrStr name (liftFn f)

#fnAttrI Source

fnAttrI :: forall datum a. Show a => String -> (datum -> Int -> a) -> Attribute datum

Create an indexed attribute from a plain PureScript function (datum -> Int -> a)

Use this when you need both datum and index access

#fnAttrIStr Source

fnAttrIStr :: forall datum. String -> (datum -> Int -> String) -> Attribute datum

Create 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 AttributeValue

Attribute values

We support the most common value types. The ADT ensures type safety when setting attributes.

Constructors

Instances

#AttributeName Source

newtype AttributeName

Attribute 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 datum

Type-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

Instances

  • Show (Attribute datum)
  • Contravariant Attribute

    Contravariant 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 radiusAttr
    

    The key insight: cmap composes 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 AttrSource

Source 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