Move design idea example for hierarchical graphs out of features directory
[shapes.git] / examples / features / fgraphs.shape
blobceab9bbcd0410f1b1bc6386b7cd9fb7655be062f
1 /** This file is part of Shapes.
2  **
3  ** Shapes is free software: you can redistribute it and/or modify
4  ** it under the terms of the GNU General Public License as published by
5  ** the Free Software Foundation, either version 3 of the License, or
6  ** any later version.
7  **
8  ** Shapes is distributed in the hope that it will be useful,
9  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
10  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  ** GNU General Public License for more details.
12  **
13  ** You should have received a copy of the GNU General Public License
14  ** along with Shapes.  If not, see <http://www.gnu.org/licenses/>.
15  **
16  ** Copyright 2013 Henrik Tidefelt
17  **/
19 /** This example is demonstrates a trick using functions to represent graphs.
20  **
21  ** The data of a node is made the result of a function of no arguments.
22  ** This makes it possible for nodes to have references to other nodes
23  ** with bindings in the current scope.
24  **
25  ** The ability to refer to bindings in this way is allowed in the
26  ** language to support the creation of mutually recursive functions.
27  **/
29 ##lookin ..Shapes
30 ##lookin ..Shapes..Control
31 ##lookin ..Shapes..Data
35   a: \ → (> value: `A´  out_neighbors: [vector b] <)
36   b: \ → (> value: `B´  out_neighbors: [vector c d] <)
37   c: \ → (> value: `C´  out_neighbors: [vector a] <)
38   d: \ → (> value: `D´  out_neighbors: [vector] <)
40   [vector a b c d]
43 /** Test the graph by traversing edges down to a given depth, and collect
44  ** all values.
45  **/
46 traverse: \ node depth →
47 [if depth > '0
48   [[node].out_neighbors.foldl
49     \ p e → ( p + [traverse e depth-'1] )
50     ( ` ´ + [node].value )
51   ]
52   `´
55 IO..•stdout << [traverse [g '0] '7] << "{n}
57 Graphics..@spot