Each cell of the grid will contain a character from the string.
The string will be converted to 2 dimensions by splitting on newline characters.
If the lines are not of equal length, the grid will be as wide as the longest line,
with missing characters filled with nil
.
(g/grid "hello\nworld")
| h | e | l | l | o |
| w | o | r | l | d |
If dimensions are specified, you can leave out the newline characters. In this case the length of the string must exactly match the total number of cells:
(g/grid 3 3 "abcdefghi")
| a | b | c |
| d | e | f |
| g | h | i |
You can instead supply a sequence of strings, where each string will represent one row in the grid,
even if it contains newline characters.
Note that newline will be printed normally, messing up the output,
and the second row is padded with a nil
which prints as the empty string.
(g/grid ["grid\nwith" "newlines"])
| g | r | i | d |
| w | i | t | h |
| n | e | w | l | i | n | e | s | |
You can specify the dimensions of the grid, and supply a sequence of any values, of length exactly equal to the total number of cells:
(g/grid 4 3 [1 2 3 4 2 4 6 8 3 6 9 12])
| 1 | 2 | 3 | 4 |
| 2 | 4 | 6 | 8 |
| 3 | 6 | 9 | 12 |
You can supply a map, where the keys are vectors of 2 non-negative integers representing x-y coordinates.
The specified coordinates will be filled with the given values, and unspecified cells will contain nil
.
(g/grid {[1 0] 1, [0 1] 4, [2 1] 2, [1 2] 3})
| `nil` | 1 | `nil` |
| 4 | `nil` | 2 |
| `nil` | 3 | `nil` |
The outer one maps y coordinate
You can initialise grid cells from another grid. Note however that both grids will have the same origin as there is no way to specify the starting point, only the size of the new grid.
(def board (g/grid [[1 2 3] [4 5 6]]))
(g/grid 2 2 board)
| 1 | 2 |
| 4 | 5 |
For a more flexible way to create a subgrid from another grid, you can use slice indexing (see ...).
If you want all cells to have the same value, you can use the everywhere
helper:
(g/grid 2 3 (everywhere 42))
| 42 | 42 |
| 42 | 42 |
| 42 | 42 |
(g/grid 4 4 *)
| 0 | 0 | 0 | 0 |
| 0 | 1 | 2 | 3 |
| 0 | 2 | 4 | 6 |
| 0 | 3 | 6 | 9 |
The grid constructor function is also availeble as a reader macro (#emlyn/grid
).
This can be useful if you already have a large data structure representing the contents of the grid;
then you only need to add #emlyn/grid
in front of it, instead of having to wrap parentheses around it:
#emlyn/grid
[[1 2 3 4 5]
[2 4 6 8 10]
[3 6 9 12 15]
[4 8 12 16 20]
[5 10 15 20 25]]
| 1 | 2 | 3 | 4 | 5 |
| 2 | 4 | 6 | 8 | 10 |
| 3 | 6 | 9 | 12 | 15 |
| 4 | 8 | 12 | 16 | 20 |
| 5 | 10 | 15 | 20 | 25 |
Note that it is not possible to specify the size in this case, since the macro only operates on the single form following it, so you have to use a structure for which the size can be inferred.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close