Difference between revisions of "Diagrams/Migrate0.5"

From HaskellWiki
Jump to navigation Jump to search
(note a few more migrations)
m (→‎Semantics of beside: small typo fix)
 
Line 55: Line 55:
 
== Semantics of beside ==
 
== Semantics of beside ==
   
The semantics of <code>beside</code>---and hence also terms <code>(|||)</code> and <code>(===)</code>, which are implemented in terms of beside---is now different. In particular, the local origin of <code>beside v d1 d2</code> is now the local origin of <code>d1</code> rather than the point of tangency between <code>d1</code> and <code>d2</code>. This has several advantages, namely
+
The semantics of <code>beside</code>---and hence also of <code>(|||)</code> and <code>(===)</code>, which are implemented in terms of beside---is now different. In particular, the local origin of <code>beside v d1 d2</code> is now the local origin of <code>d1</code> rather than the point of tangency between <code>d1</code> and <code>d2</code>. This has several advantages, namely
 
 
 
# <code>beside v</code>, <code>(|||)</code>, and <code>(===)</code> are now associative
 
# <code>beside v</code>, <code>(|||)</code>, and <code>(===)</code> are now associative

Latest revision as of 03:31, 6 March 2012

This page describes API changes between diagrams 0.4 and 0.5, along with explanations of how to migrate to the new 0.5 API.

R2 and P2 are now abstract

The biggest change to the API between versions 0.4 and 0.5 is that the R2 and P2 types (representing 2D vectors and points, respectively) are now abstract. R2 used to be a synonym for (Double, Double) but this is no longer the case. Also, the constructor P for points is no longer exported.

You can use the functions

r2 :: (Double, Double) -> R2
unr2 :: R2 -> (Double, Double)

p2 :: (Double, Double) -> P2
unp2 :: P2 -> (Double, Double)

to convert between pairs of Doubles and vectors or points. For example, if you had

beside (4,2) d1 d2

change it to

beside (r2 (4,2)) d1 d2

If you had

fromOffsets [(2,1), (3,5), (2,0)]

change it to

fromOffsets . map r2 $ [(2,1), (3,5), (2,0)]

Note you can still use unitX, unitY, unit_X, unit_Y :: R2.

If you had some code pattern-matching on vectors or points, the ViewPatterns extension comes in handy: change

foo :: R2 -> ...
foo (x,y) = ... x ... y ...

bar :: P2 -> ...
bar (P (x,y)) = ... x ... y ...

to

{-# LANGUAGE ViewPatterns #-}

foo :: R2 -> ...
foo (unr2 -> (x,y)) = ... x ... y ...

bar :: P2 -> ...
bar (unp2 -> (x,y)) = ... x ... y ...

To convert a vector v to a point, use origin .+^ v. To convert a point p to a vector, use p .-. origin.

roundedRect

The roundedRect function now takes two Double arguments instead of an R2 argument, to be more consistent with rect. If you had something like roundedRect (2,3) 0.2, just change it to roundedRect 2 3 0.2.

circlePath

The circlePath function has been removed; you should be able to simply replace it with circle.

Semantics of beside

The semantics of beside---and hence also of (|||) and (===), which are implemented in terms of beside---is now different. In particular, the local origin of beside v d1 d2 is now the local origin of d1 rather than the point of tangency between d1 and d2. This has several advantages, namely

  1. beside v, (|||), and (===) are now associative
  2. The new semantics is more consistent with the semantics of cat.

If you want the old behavior, simply perform an alignment on the first diagram before combining. That is, replace beside v d1 d2 by beside v (align v d1) d2. If d1 and d2 are the same width the same effect can be achieved by first combining and then centering.

AnnDiagram renamed QDiagram

This change was made to be consistent with referring to the monoidal parameter as a "query" rather than an "annotation". Simply change AnnDiagram to QDiagram everywhere it occurs.

Additional Semigroup constraints

In some places diagrams now requires a Semigroup constraint in addition to Monoid (ideally Monoid would have Semigroup as a superclass...). If you get an error about "could not deduce Semigroup constraint" you probably just need to add one.