Skip to content

Instantly share code, notes, and snippets.

@danneu
Created April 21, 2018 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danneu/82ca5f8bae80fe1dfa841e4ea369c84b to your computer and use it in GitHub Desktop.
Save danneu/82ca5f8bae80fe1dfa841e4ea369c84b to your computer and use it in GitHub Desktop.
module Quilt exposing (..)
import Array.Hamt as Array exposing (Array)
{-| A quilt is a datastructure for maintaining a Pinterest-like
grid of images. Each image is wrapped with a Cell which calculates
its width/height from the image, but scales the dimensions to
something that fits.
Example:
type alias Item =
{ id : String
, height : Int
, width : Int
-- Extra fields for my use
, name : String
, color : String
}
-- Error: The annotation says `Quilt Item` but the actual type is `Quilt {}`
myQuilt : Quilt Item
myQuilt = Quilt.empty 800 600 3
|> Quilt.insert (Item "a" 100 200 "item1" "yellow")
|> Quilt.insert (Item "b" 800 180 "item2" "red")
|> Quilt.insert (Item "c" 450 350 "item3" "purple")
-}
{-| Holds whatever the user wants to store in the Quilt.
Its properties are derived from the entry that it holds.
-}
type alias Cell a =
{ contents : a
, id : Int
, width : Int
, height : Int
, pruned : Bool
}
{-| Something with an id, width, and height can be sewn in to the quilt.
-}
type alias Quiltable a =
{ a
| id : String
, width : Int
, height : Int
}
type alias Quilt a =
{ columns : Array (Array (Cell (Quiltable a)))
, width : Int
, height : Int
}
empty : Int -> Int -> Int -> Quilt a
empty width height columns =
let
columns_ =
Basics.max 1 columns
in
{ width = width
, height = height
, columns = (Array.initialize columns (\_ -> Array.empty))
}
insert : Quiltable a -> Quilt a -> Quilt a
insert entry quilt =
let
cell =
{ id = entry.id
, contents = entry
, width = entry.width
, height = entry.height
, prune = False
}
in
-- Unimplemented
quilt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment