Hylograph.Expr.Friendly
- Package
- purescript-hylograph-selection
- Repository
- afcondon/purescript-hylograph-selection
Friendly DSL for Data-Driven Attributes
A more approachable API for the finally-tagless expression system. Designed for D3.js developers learning PureScript.
Quick Start
import Hylograph.Expr.Friendly
-- Define field accessors for your data type
_x = field @"x"
_y = field @"y"
_name = field @"name"
-- Build expressions with readable arithmetic
xPosition = _x `times` 40.0 `plus` 50.0
yPosition = _y `times` 30.0
-- Create attributes
myAttrs =
[ computed "cx" xPosition
, computed "cy" yPosition
, staticStr "fill" "steelblue"
]
Value Constructors
num 42.0- numeric valuetext "hello"- string valuebool true- boolean value
Arithmetic (use with backticks for infix)
plus,minus,times,dividedBy- binary operationsnegated- unary negation
Attribute Constructors
computed- data-driven numeric/showable attributecomputedStr- data-driven string attributestatic- constant numeric/showable attributestaticStr- constant string attributefrom- escape hatch using plain PureScript function
#text Source
text :: forall repr. StringExpr repr => String -> repr StringCreate a text (string) value
fill = text "steelblue"
label = _name `append` text " (selected)"
#dividedByN Source
dividedByN :: forall repr. NumExpr repr => repr Number -> Number -> repr NumberDivide by a literal number
#lessThan Source
lessThan :: forall repr. CompareExpr repr => repr Number -> repr Number -> repr BooleanLess than comparison
#lessOrEqual Source
lessOrEqual :: forall repr. CompareExpr repr => repr Number -> repr Number -> repr BooleanLess than or equal
#greaterThan Source
greaterThan :: forall repr. CompareExpr repr => repr Number -> repr Number -> repr BooleanGreater than comparison
#greaterOrEqual Source
greaterOrEqual :: forall repr. CompareExpr repr => repr Number -> repr Number -> repr BooleanGreater than or equal
#equals Source
equals :: forall repr. CompareExpr repr => repr Number -> repr Number -> repr BooleanNumeric equality
#textEquals Source
textEquals :: forall repr. StringCompareExpr repr => repr String -> repr String -> repr BooleanString equality
#textNotEquals Source
textNotEquals :: forall repr. StringCompareExpr repr => repr String -> repr String -> repr BooleanString inequality
#append Source
append :: forall repr. StringExpr repr => repr String -> repr String -> repr StringConcatenate strings
label = _name `append` text ": " `append` _value
#field Source
field :: forall @sym repr datumRow a r. IsSymbol sym => Cons sym a r datumRow => DatumExpr repr datumRow => repr aAccess a field from the datum
Define field accessors for your data type:
type MyDataRow = (x :: Number, y :: Number, name :: String)
_x :: forall repr. DatumExpr repr MyDataRow => repr Number
_x = field @"x"
_y :: forall repr. DatumExpr repr MyDataRow => repr Number
_y = field @"y"
_name :: forall repr. DatumExpr repr MyDataRow => repr String
_name = field @"name"
#attr Source
attr :: forall datum a. ToAttributeValue a => String -> EvalD datum a -> Attribute datumCreate a data-driven attribute from an expression. Works for both numeric and string expressions - no need for separate functions!
attr "cx" $ _x `timesN` 20.0 `plusN` 50.0
attr "r" $ num 5.0
attr "fill" $ text "steelblue"
attr "fill" $ ifThen condition (text "red") (text "blue")
#attrWithIndex Source
attrWithIndex :: forall datum a. ToAttributeValue a => String -> EvalD datum a -> Attribute datumCreate a data-driven attribute that also uses the element index
attrWithIndex "x" $ index `timesN` 50.0
#fromWithIndex Source
fromWithIndex :: forall datum a. Show a => String -> (datum -> Int -> a) -> Attribute datumCreate an indexed attribute from a function taking datum and index
fromWithIndex "x" (\d i -> toNumber i * 50.0)
#fromStrWithIndex Source
fromStrWithIndex :: forall datum. String -> (datum -> Int -> String) -> Attribute datumCreate an indexed string attribute from a function
#strokeWidth Source
strokeWidth :: forall datum a. ToAttributeValue a => EvalD datum a -> Attribute datum#class_ Source
class_ :: forall datum a. ToAttributeValue a => EvalD datum a -> Attribute datumCSS class attribute (underscore suffix since class is a reserved word)
class_ $ text "my-element highlighted"
#textAnchor Source
textAnchor :: forall datum a. ToAttributeValue a => EvalD datum a -> Attribute datum#dominantBaseline Source
dominantBaseline :: forall datum a. ToAttributeValue a => EvalD datum a -> Attribute datum#fontFamily Source
fontFamily :: forall datum a. ToAttributeValue a => EvalD datum a -> Attribute datum#textContent Source
textContent :: forall datum a. ToAttributeValue a => EvalD datum a -> Attribute datum#transform Source
transform :: forall datum a. ToAttributeValue a => EvalD datum a -> Attribute datum#color Source
color :: forall repr. StringExpr repr => Color -> repr StringCreate a color expression from a Color value
fill $ color steelblue
#hex Source
hex :: forall repr. StringExpr repr => String -> repr StringParse a hex color string
fill $ hex "#2c3e50"
#black Source
black :: forall repr. StringExpr repr => repr StringPure black color
#white Source
white :: forall repr. StringExpr repr => repr StringPure white color
#em_ Source
em_ :: forall repr. StringExpr repr => Number -> repr StringCreate an em value (note: em_ to avoid conflict with Prelude's em)
fontSize $ em_ 1.5
#pct Source
pct :: forall repr. StringExpr repr => Number -> repr StringCreate a percentage value
width $ pct 100.0
#computedStr Source
computedStr :: forall datum. String -> EvalD datum String -> Attribute datumCreate a data-driven string attribute (no quotes added)
computedStr "fill" (ifThen condition (text "red") (text "blue"))
computedStr "class" _category
#computedWithIndex Source
computedWithIndex :: forall datum a. Show a => String -> EvalD datum a -> Attribute datumCreate a data-driven attribute that also uses the element index
computedWithIndex "x" (index `timesN` 50.0)
#computedStrWithIndex Source
computedStrWithIndex :: forall datum. String -> EvalD datum String -> Attribute datumCreate a data-driven string attribute that also uses the element index
#ToAttributeValue Source
class ToAttributeValue a whereTypeclass for converting values to attribute strings.
String values pass through unchanged, other types use Show.
This allows attr to work for both numeric and string expressions.
Members
toAttrValue :: a -> String
Instances
ToAttributeValue StringToAttributeValue NumberToAttributeValue IntToAttributeValue BooleanToAttributeValue ColorColors convert to hex strings for SVG attributes
ToAttributeValue PxToAttributeValue PtToAttributeValue EmToAttributeValue RemToAttributeValue Percent
Re-exports from Hylograph.Expr.Animation
#AnimatedBuilder Source
type AnimatedBuilder datum = { config :: AnimationConfig, fromValue :: Maybe (AnimatedValue datum), name :: String, toValue :: AnimatedValue datum }Builder for animated attributes
Accumulates the animation specification before converting to Attribute.
Use animatedTo to create, config modifiers to adjust, and animated to finalize.
#withStagger Source
withStagger :: forall datum. Number -> AnimatedBuilder datum -> AnimatedBuilder datumSet staggered delay based on element index
Creates an indexed animation where each element's delay is: baseDelay + (index * staggerDelay)
animated $ animatedTo (num 1.0)
# withStagger 50.0 -- Each element starts 50ms after the previous
#withEasing Source
withEasing :: forall datum. EasingType -> AnimatedBuilder datum -> AnimatedBuilder datumSet the easing function
animated $ animatedTo (num 1.0)
# withEasing ElasticOut
#withDuration Source
withDuration :: forall datum. Number -> AnimatedBuilder datum -> AnimatedBuilder datumSet the animation duration in milliseconds
animated $ animatedTo (num 1.0) # withDuration 500.0
#withDelay Source
withDelay :: forall datum. Number -> AnimatedBuilder datum -> AnimatedBuilder datumSet the delay before animation starts (in milliseconds)
animated $ animatedTo (num 1.0)
# withDelay 200.0 -- Wait 200ms before starting
#animatedToIndexed Source
animatedToIndexed :: forall datum. String -> (datum -> Int -> Number) -> AnimatedBuilder datumCreate an indexed animated attribute builder (uses datum and element index for target)
animated $ animatedToIndexed "cx" (\d i -> d.x + toNumber i * spacing)
#animatedTo Source
animatedTo :: forall datum. String -> EvalD datum Number -> AnimatedBuilder datumCreate an animated attribute builder targeting a value
The attribute name is required. The target value can be:
- A constant:
animatedTo "opacity" (num 1.0) - A field:
animatedTo "cx" (field @"x") - An expression:
animatedTo "cx" (field @"x"timesN20.0)
opacity $ animated $ animatedTo (num 1.0)
# withDuration 500.0
#animatedFromStatic Source
animatedFromStatic :: forall datum. Number -> AnimatedBuilder datum -> AnimatedBuilder datumSet a static starting value for an animation
animated $ animatedFromStatic 0.0 $ animatedTo "opacity" (num 1.0)
#animatedFromIndexed Source
animatedFromIndexed :: forall datum. (datum -> Int -> Number) -> AnimatedBuilder datum -> AnimatedBuilder datumSet an indexed starting value for an animation (uses datum and element index)
animated $ animatedFromIndexed (\d i -> d.baseX + toNumber i * 10.0) $ animatedTo "cx" (field @"x")
#animatedFrom Source
animatedFrom :: forall datum. EvalD datum Number -> AnimatedBuilder datum -> AnimatedBuilder datumSet the starting value for an animation
Without animatedFrom, the animation reads the current DOM value as start.
With animatedFrom, the animation uses the specified value.
-- Animate from 0 to 1
opacity $ animated $ animatedFrom (num 0.0) $ animatedTo (num 1.0)
-- Animate from current DOM value to target (no animatedFrom)
opacity $ animated $ animatedTo (field @"targetOpacity")
#animatedAttr Source
animatedAttr :: forall datum. String -> AnimatedBuilder datum -> Attribute datumCreate an animated attribute with explicit name (alternative syntax)
animatedAttr "opacity" $ animatedFrom (num 0.0) $ animatedTo (num 1.0)
#animated Source
animated :: forall datum. AnimatedBuilder datum -> Attribute datumConvert an AnimatedBuilder to an Attribute
This is the final step after building the animation specification.
myAttr = animated $ animatedFrom (num 0.0) $ animatedTo (num 1.0)
# withDuration 500.0
# withEasing QuadOut
Re-exports from Hylograph.Expr.Datum
#DatumExpr Source
class DatumExpr :: (Type -> Type) -> Row Type -> Constraintclass DatumExpr (repr :: Type -> Type) (datumRow :: Row Type) | repr -> datumRow
Datum field access with compile-time safety
The datumRow parameter is a Row kind (not Type), representing the
fields available in the datum record. The constraint Row.Cons sym a r datumRow
ensures:
datumRowcontains a field namedsym- The field has type
a - Compiler error if field doesn't exist
The interpreter works with Record datumRow at runtime.
Re-exports from Hylograph.Expr.Expr
#BoolExpr Source
class BoolExpr :: (Type -> Type) -> Constraintclass BoolExpr repr
Boolean expressions
#StringExpr Source
class StringExpr :: (Type -> Type) -> Constraintclass StringExpr repr
String expressions
Re-exports from Hylograph.Expr.Interpreter.Eval
#EvalD Source
newtype EvalD datum aEvaluator with datum access - a function from datum (and index) to value
Instances
NumExpr (EvalD datum)StringExpr (EvalD datum)BoolExpr (EvalD datum)CompareExpr (EvalD datum)StringCompareExpr (EvalD datum)TrigExpr (EvalD datum)UnitExpr (EvalD datum)UnitArith (EvalD datum)PathExpr (EvalD datum)DatumExpr (EvalD (Record datumRow)) datumRowDatum field access - looks up field from datum at runtime The
datumRowis a Row kind, and we work withRecord datumRowDataDSL (EvalD datum)DataDSL instance for EvalD (with datum and index access)
TrigDSL (EvalD datum)TrigDSL instance for EvalD (with datum and index access)
- 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
Colors convert to hex strings for SVG attributes