joakin / elm-grid / Grid

Functions to operate on a set of coordinates and do something with them.

You can think of them as kind of like list comprehension helpers for 2d and 3d lists.

Very useful where you would write nested for loops in imperative languages. For 1 dimensional comprehensions use List.range.

2d grid

fold2d : { rows : Basics.Int, cols : Basics.Int } -> (( Basics.Int, Basics.Int ) -> result -> result) -> result -> result

Fold over a 2 dimensional grid.

Getting just coordinates:

(fold2d
    {rows = 2, cols = 3 }
    (\(x, y) result -> (x, y) :: result)
    []
)
    |> List.reverse
{-
    [(0,0),(1,0),(2,0),(0,1),(1,1),(2,1)]
-}

foldr2d : { rows : Basics.Int, cols : Basics.Int } -> (( Basics.Int, Basics.Int ) -> result -> result) -> result -> result

Fold over a 2 dimensional grid from the right. Starts at the end of the dimensions space, and goes backwards.

Getting just coordinates:

foldr2d
    {rows = 2, cols = 3 }
    (::)
    []
{-
    [(0,0),(1,0),(2,0),(0,1),(1,1),(2,1)]
-}

3d grid

fold3d : { rows : Basics.Int, cols : Basics.Int, depth : Basics.Int } -> (( Basics.Int, Basics.Int, Basics.Int ) -> result -> result) -> result -> result

Fold over a 3 dimensional grid of coordinates

(fold3d
    { rows = 2, cols = 3, depth = 2 }
    (\( x, y, z ) result -> ( x, y, z ) :: result)
    []
)
    |> List.reverse
{-
    -- First plane depth=0
    [(0,0,0),(1,0,0),(2,0,0)
    ,(0,1,0),(1,1,0),(2,1,0)
    -- Second plane depth=1
    ,(0,0,1),(1,0,1),(2,0,1)
    ,(0,1,1),(1,1,1),(2,1,1)]
-}

foldr3d : { rows : Basics.Int, cols : Basics.Int, depth : Basics.Int } -> (( Basics.Int, Basics.Int, Basics.Int ) -> result -> result) -> result -> result

Fold over a 3 dimensional grid of coordinates from the right. Starts at the end of the dimensions space, and goes backwards.

foldr3d
    { rows = 2, cols = 3, depth = 2 }
    (\( x, y, z ) result -> ( x, y, z ) :: result)
    []
{-
    -- First plane depth=0
    [(0,0,0),(1,0,0),(2,0,0)
    ,(0,1,0),(1,1,0),(2,1,0)
    -- Second plane depth=1
    ,(0,0,1),(1,0,1),(2,0,1)
    ,(0,1,1),(1,1,1),(2,1,1)]
-}