Git Product home page Git Product logo

elm-vector's Introduction

Elm Vector

A vector is a collection type, kind of similar to a List or an Array, except that it has a fixed length. This fixed length means you know with certainty and type safety whether or not there is an 4th element in the vector because you know if its at least 4 elements long. A direct implication of this is the VectorN.get function returns an a, instead of a Maybe a.

This package exposes 59 vector modules, for vector types of length 1 to 60. Each module has an assortment of types and functions that make these vectors useful.

Heres a demo

Background

I have written this exact code many times:

type Thing = A | B | C

things = [ A, B, C ]

I know other people who have written that exact code many times before too. That code definitely has a first, second, and third element inside things. We know this. But, the type system does not know this. The compiler makes us handle the case that things is empty whenever we work with it.

The other day in the Elm slack, someone was making a tic tac toe game. They came across this exact same thing, since their tic tac toe board was represented as a List (List a) where each List was three items long. "What do I do about the Nothing case? I know the element is there."

I recommended they just handle the Nothing case, since its harmless and fairly effortless, and the List being empty is probably more likely than either of us realize, such as if there really is some List manipulating error in the code.

I also suggested that if they really wanted to, they could model their tic tac toe board like this; avoiding the Nothing case:

type alias Board a =
    { upperLeft : a 
    , upper : a
    , upperRight : a
    , left : a 
    , center : a
    , right : a
    , lowerLeft : a
    , lower : a
    , lowerRight : a
    }

Using this type would be a lot more difficult, but it would be a complete representation of the board that does not include any of these weird Nothing cases that we know do not exist.

Thats when I realized

  • This type is really just a 2D vector.
  • If one person- just once- made this tedious Vector3 a type, it could be re-used by everyone.
  • With code generation, someone could generate as many vector types as they wanted, at any size.

So thats what I did. Thats what this repo is. ./builder-src/Main.hs generates a bunch of *.elm vector files, of any size.

elm-vector's People

Contributors

chadtech avatar

Stargazers

Chris avatar Benjamin Le Forestier avatar Andrejs Agejevs avatar  avatar abe haile avatar Samuel Lissner avatar Andy avatar

Watchers

James Cloos avatar

elm-vector's Issues

Suggestion for some additions

Hi!

Thanks for this lib, it makes creating a lot of game "type safe" ๐Ÿ˜„

By working with it I'd have some suggestions:

  • generator : Random.Generator a -> Random.Generator (VecX a)
  • shuffle : VecX a -> Random.Generator (VecX a)
  • all : (a -> Bool) -> VecX a -> Bool
  • any : (a -> Bool) -> VecX a -> Bool
  • update : VecX.Index -> (a -> a) -> VecX a -> VecX a
  • decoder : Decoder a -> Decoder (VecX a) (this might try to decode a list and fail if it is not the correct size)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.