mirror of
https://github.com/rgleichman/glance.git
synced 2024-10-05 19:58:30 +03:00
Add a few more examples to advantages.hs. Add SVG for advantages.hs.
This commit is contained in:
parent
610ad9a4eb
commit
a396ec7ce3
@ -1,7 +1,7 @@
|
|||||||
{-Advantages of Glance:
|
{-Advantages of a Visual Representation of Code:
|
||||||
|
|
||||||
The following are several admittedly contrived examples demonstrating some
|
The following are several admittedly contrived examples demonstrating six
|
||||||
of the advantages of Glance over textual code.
|
of the advantages of displaying code visually instead of textually.
|
||||||
|
|
||||||
|
|
||||||
1. Display out of order arguments:
|
1. Display out of order arguments:
|
||||||
@ -20,7 +20,7 @@ been swapped.
|
|||||||
badSyntaxGraphToTuple (SyntaxGraph a b c d e) = (e, b, c, d, a)
|
badSyntaxGraphToTuple (SyntaxGraph a b c d e) = (e, b, c, d, a)
|
||||||
-}
|
-}
|
||||||
badSyntaxGraphToTuple (SyntaxGraph a b c d e) = (e, b, c, d, a)
|
badSyntaxGraphToTuple (SyntaxGraph a b c d e) = (e, b, c, d, a)
|
||||||
{- In the Glance image of badSyntaxGraphToTuple, it is immediately apparent that
|
{-In the Glance image of badSyntaxGraphToTuple, it is immediately apparent that
|
||||||
some of the tuple arguments are out of order.
|
some of the tuple arguments are out of order.
|
||||||
|
|
||||||
|
|
||||||
@ -37,6 +37,7 @@ y x = g where
|
|||||||
g = v
|
g = v
|
||||||
|
|
||||||
to just
|
to just
|
||||||
|
|
||||||
y x = 2 * x
|
y x = 2 * x
|
||||||
-}
|
-}
|
||||||
|
|
||||||
@ -128,4 +129,62 @@ foo x y
|
|||||||
in temp2
|
in temp2
|
||||||
|
|
||||||
{-In future version of Glance, nested guards/cases could be rendered using a 2D grid.
|
{-In future version of Glance, nested guards/cases could be rendered using a 2D grid.
|
||||||
|
|
||||||
|
|
||||||
|
5. Information at a Glance:
|
||||||
|
|
||||||
|
Let's look again at the factorial function from the README.
|
||||||
|
|
||||||
|
factorial x =
|
||||||
|
if x == 0
|
||||||
|
then 1
|
||||||
|
else x * factorial (x - 1)
|
||||||
-}
|
-}
|
||||||
|
factorial x =
|
||||||
|
if x == 0
|
||||||
|
then 1
|
||||||
|
else x * factorial (x - 1)
|
||||||
|
{-In the visual version, it is quick and easy to see that the parameter (x)
|
||||||
|
is used exactly three times in the body of the function. In the text,
|
||||||
|
figuring out how many times a parameter is used requires searching through
|
||||||
|
the entire function.
|
||||||
|
|
||||||
|
Similarly, in the visual image, it is easy to see that "factorial" is recursively
|
||||||
|
called exactly once.
|
||||||
|
|
||||||
|
|
||||||
|
6. See the Topology of Code:
|
||||||
|
|
||||||
|
Assume we have a function "mapFirst" that maps the first item in a tuple.
|
||||||
|
|
||||||
|
mapFirst :: (a -> a) -> (a, a) -> (a, a)
|
||||||
|
mapFirst f (x, y) = (x', y) where
|
||||||
|
x' = f x
|
||||||
|
-}
|
||||||
|
mapFirst f (x, y) = (x', y) where
|
||||||
|
x' = f x
|
||||||
|
|
||||||
|
{-Now, for whatever reason, we want to write a version of mapFirst where the items
|
||||||
|
in the result tuple are swapped like so:
|
||||||
|
mapFirstAndSwap f (x, y) = (y, x') where
|
||||||
|
x' = f x
|
||||||
|
|
||||||
|
But we make a mistake and accidentally write:
|
||||||
|
badMapFirstAndSwap f (x, y) = (x', x') where
|
||||||
|
x' = f x
|
||||||
|
|
||||||
|
When we visualize badMapFirstAndSwap we can immediately see that (f x)
|
||||||
|
is no longer nested, and has become an additional node in the graph.
|
||||||
|
|
||||||
|
Since swapping the values in a tuple should not change the topology of our code,
|
||||||
|
this extra node indicates there is an error in the code.
|
||||||
|
-}
|
||||||
|
badMapFirstAndSwap f (x, y) = (x', x') where
|
||||||
|
x' = f x
|
||||||
|
|
||||||
|
{-
|
||||||
|
If you were curious, here is what the good version of mapFirstAndSwap
|
||||||
|
looks like.
|
||||||
|
-}
|
||||||
|
mapFirstAndSwap f (x, y) = (y, x') where
|
||||||
|
x' = f x
|
||||||
|
3
examples/advantages.svg
Normal file
3
examples/advantages.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 366 KiB |
Loading…
Reference in New Issue
Block a user