On this page:
Pict.rebuild
Pict.replace
Pict.configure
Pict.find_  rebuilt
rebuildable
1.0.0.2+9.2.900

6 Pict Rebuilds🔗ℹ

method

method (pict :: Pict).rebuild(

  ~pre: pre_adjust :: Function.of_arity(1),

  ~as_rebuilt: as_rebuilt :: Any.to_boolean = #true,

  ~post: post_adjust :: Function.of_arity(1),

  ~config: config_adjust :: Function.of_arity(1)

             = fun(config): config

) :: Pict

Returns a pict that is like pict, but replays pict’s construction with pre_adjust and post_adjust applied to each component pict, and with config_adjust applied to each configuration map of a pict created by rebuildable.

To support Pict.rebuild, the representation of a pict effectively records all primitive operations used to construct the pict. This recording is limited to Pict and Find objects. If, for example, you use Find.in to obtain a number and then construct a pict using that number, the number itself cannot record its derivation from the picts used with Find.in. In such cases, use rebuildable or the ~children argument of animate to establish a connection between the input picts and the result.

When traversing the children of pict to rebuild it, the pre_adjust function is applied to each pict before its own children. When pre_adjust returns a value other than the pict that it is given, rebuilding does not recur to its children, and the result of pre_adjust is used immediately as the rebuilt replacement for the given pict. When rebuilding does recur, when a pict’s descendants are unchanged and when config_adjust (if applicable) returns a configuration unchanged, then the original construction of the pict is kept. Finally, post_adjust is applied to either the rebuilt pict or so-far-preserved original pict to obtain the rebuilt replacement for the original pict. The replacement of a given pict is cached, so pre_adjust and post_adjust are each applied at most once to a pict within a call to Pict.rebuild.

If as_rebuilt is true, then any replacement provided by pre_adjust is wrapped to share an identify with the pict that it replaces. Any other rebuilt pict—either pict itself or a rebuilt dependency—always shares the identity of the original pict, so the rebuilt pict can be located via Find objects, replaced via Pict.replace, or extracted using Pict.find_rebuilt. See also Pict Findable and Replaceable Identity.

> def s = square(~size: 20, ~fill: "blue")

> def p = beside(~sep: 10, s, Pict.launder(s))

> p

image

> p.rebuild(~pre: fun (q): if q == s | s.scale(2) | q)

image

> p.rebuild(~pre: fun (q): if q == s | s.scale(2) | q,

            ~post: fun (q :~ Pict):

                     rectangle(~around: q.pad(2), ~line: "red"))

image

Changed in version 1.2 of package rhombus-pict-lib: Added the as_rebuilt argument, keeping the default behavior as #true.

method

method (pict :: Pict).replace(

  orig :: Pict,

  replacement :: Pict,

  ~as_rebuilt: as_rebuilt :: Any.to_boolean = #true

) :: Pict

Returns a pict that is like pict, but replays pict’s construction to replace each use of orig with replacement.

This operation is equivalent to a use of Pict.rebuild:

pict.rebuild(~pre: fun (p):

                     if p == orig

                     | replacement

                     | p,

             ~as_rebuilt: as_rebuilt)

> def s = square(~size: 20, ~fill: "blue")

> def c = circle(~size: 15, ~fill: "red")

> def p = beside(~sep: 10, s, s)

> p

image

> p.replace(s, c)

image

Changed in version 1.2 of package rhombus-pict-lib: Added the as_rebuilt argument and changed the default behavior to #true.

method

method (pict :: Pict).configure(key :: Any, val :: Any) :: Pict

Returns a pict that is like pict, but replays pict’s construction to replace any configuration map entry for key with val.

This operation is equivalent to a use of Pict.rebuild:

pict.rebuild(~config:

               fun (config :: Map):

                 if config.has_key(key)

                 | config ++ { key: val }

                 | config)

method

method (pict :: Pict).find_rebuilt(orig :: Pict) :: maybe(Pict)

Returns a descendant of pict (by recursively checking findable children) that has the same identity as orig, or returns #false no such pict can be found.

See Pict Findable and Replaceable Identity for more information about pict identity and rebuilt picts.

function

fun rebuildable(

  rebuild

    :: (~deps: List.of(Pict)) -> Pict

    || (~config: maybe(Map)) -> Pict

    || (~deps: List.of(Pict), ~config: maybe(Map)) -> Pict,

  ~deps: deps :: List.of(Pict) = [],

  ~config: config :: maybe(Map) = #false

) :: Pict

Creates a pict that is the same as the result of rebuild(~deps: deps), rebuild(~config: config) or rebuild(~deps: deps, ~config: config), but where deps contains picts that can be adjusted via Pict.rebuild or Pict.replace, and where config can be updated by Pict.rebuild or Pict.configure. When rebuild is called, its result is wrapped as the sole child of a new pict whose identity is the same as the result of rebuildable itself; see also Pict Findable and Replaceable Identity.

The given deps list determines the replaceable dependencies of the result pict, but the result from proc determines the findable children.