Collecting attributes for bulk application...

3 views
Skip to first unread message

Stuart Hungerford

unread,
May 17, 2012, 5:58:03 PM5/17/12
to diagrams...@googlegroups.com
Hi,

Newbie time again. I'm planning to load a collection of Diagrams
attributes from a JSON file and parsing those values into some kind of
Haskell data structure. I'd like to be able to pass that data
structure to a function that can apply all the attributes it contains
to a Diagram.

In other languages the data structure would be a heterogeneous map
with string keys, but in Haskell I feel there must be a better way of
approaching this? Are there aspects of Diagrams that would make bulk
application of attributes easier? I've looked at the
Diagrams.Attributes module but not expert enough yet to see the
opportunities there.

Any advice much appreciated,

Stu

Brent Yorgey

unread,
May 17, 2012, 7:47:42 PM5/17/12
to diagrams...@googlegroups.com
On Fri, May 18, 2012 at 07:58:03AM +1000, Stuart Hungerford wrote:
> Hi,
>
> Newbie time again. I'm planning to load a collection of Diagrams
> attributes from a JSON file and parsing those values into some kind of
> Haskell data structure. I'd like to be able to pass that data
> structure to a function that can apply all the attributes it contains
> to a Diagram.
>
> In other languages the data structure would be a heterogeneous map
> with string keys,

That's (essentially) what is already provided by the Style type --
except all wrapped up in a nice type-safe way. See

http://hackage.haskell.org/packages/archive/diagrams-core/0.5.0.1/doc/html/Graphics-Rendering-Diagrams-Style.html

> I've looked at the
> Diagrams.Attributes module but not expert enough yet to see the
> opportunities there.

Don't worry, it not your expertise at issue -- Diagrams.Attributes
just defines some standard attributes, but the framework for
attributes and styles itself is defined in the diagrams-core package
(in the module I linked above).

I expect you should be able to use the Style type to do exactly what
you're talking about doing. If you have any trouble figuring out how
to do it don't hesitate to ask. Perhaps this is also an area in which
the user manual could be improved/extended?

-Brent

Stuart Hungerford

unread,
May 18, 2012, 4:14:27 AM5/18/12
to diagrams...@googlegroups.com
> Brent Yorgey <byo...@seas.upenn.edu> May 17 07:47PM -0400
>
> [...]
>> In other languages the data structure would be a heterogeneous map
>> with string keys,
>
> That's (essentially) what is already provided by the Style type --
> except all wrapped up in a nice type-safe way. See
>
> http://hackage.haskell.org/packages/archive/diagrams-core/0.5.0.1/doc/html/Graphics-Rendering-Diagrams-Style.html

Thanks Brent. This is very neat--I can see now that building up
styles with something like:

my_style = mempty # fontSize 10 # lw 0 # fc green...

For later application with:

diagram # applyStyle my_style # defaults...

Is an excellent way to use and replace collections of attributes. I
notice that the Style type itself seems to have the heterogeneity
ability that I think I will need to accept parsed JSON attribute names
and values. Do you have an example of constructing a Style instance
by attribute? I'm guessing something like:

import qualified Data.Map as M

my_styles = M.fromList [()]
updated = addAttr ??? my_styles

Thanks,

Stu

Brent Yorgey

unread,
May 18, 2012, 8:05:39 AM5/18/12
to diagrams...@googlegroups.com
On Fri, May 18, 2012 at 06:14:27PM +1000, Stuart Hungerford wrote:
> > Brent Yorgey <byo...@seas.upenn.edu> May 17 07:47PM -0400
> >
> > [...]
> >> In other languages the data structure would be a heterogeneous map
> >> with string keys,
> >
> > That's (essentially) what is already provided by the Style type --
> > except all wrapped up in a nice type-safe way. See
> >
> > http://hackage.haskell.org/packages/archive/diagrams-core/0.5.0.1/doc/html/Graphics-Rendering-Diagrams-Style.html
>
> Thanks Brent. This is very neat--I can see now that building up
> styles with something like:
>
> my_style = mempty # fontSize 10 # lw 0 # fc green...
>
> For later application with:
>
> diagram # applyStyle my_style # defaults...

Right, exactly. And you can construct a Style directly from
attributes using the 'addAttr' or 'setAttr' functions (from the same
module).

> import qualified Data.Map as M
>
> my_styles = M.fromList [()]
> updated = addAttr ??? my_styles

I don't think I understand what you intend with this code. What is
my_styles? A Map from what to what?

-Brent

Stuart Hungerford

unread,
May 19, 2012, 2:18:15 AM5/19/12
to diagrams...@googlegroups.com
> Brent Yorgey <byo...@seas.upenn.edu> May 18 08:05AM -0400
> [...]
>> import qualified Data.Map as M
>
>> my_styles = M.fromList [()]
>> updated = addAttr ??? my_styles
>
> I don't think I understand what you intend with this code. What is
> my_styles? A Map from what to what?

Apologies -- I've been assuming I'd need some intermediate data
structure between JSON attribute parsing and Style building. I can
see now that I can do (mempty # lc ....) expressions directly as
needed and pass those around for later use.

Thanks,

Stu
Reply all
Reply to author
Forward
0 new messages