Giter Site home page Giter Site logo

lue-bird / elm-bits Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 331 KB

safe, typed bit arrays

Home Page: https://package.elm-lang.org/packages/lue-bird/elm-bits/latest/

License: MIT License

Elm 100.00%
bit elm safe type-safe typed vector array-sized typesafe-array

elm-bits's Introduction

0, 1 and bit lists of sizes that don't have to be multiples of 8.

import Bit exposing (Bit(..))
import Bits

-6 |> Bits.fromIntSigned 5
--> [ I, I, O, I, O ]

Use when correctness/precision matters more than raw speed.

example use case: id

Most id types use an opaque type to hold information. Example similar to danyx23's Uuid to skim through โ†“

module OpaqueId exposing (OpaqueId, generate, toString)

type OpaqueId
    = OpaqueId String

toString : OpaqueId -> String
toString =
    \(OpaqueId string) -> string

generate : Random.Generator OpaqueId
generate =
    Random.map
        (\fifteenHexDigits ->
            OpaqueId
                ([ fifteenHexDigits |> List.take 4 |> List.map mapToHex |> String.fromList
                 , "-"
                 , fifteenHexDigits |> List.drop 8 |> List.take 4 |> List.map mapToHex |> String.fromList
                 , "-"
                 , "4"
                 , fifteenHexDigits |> List.drop 12 |> List.take 3 |> List.map mapToHex |> String.fromList
                 , "-"
                 , fifteenHexDigits |> List.drop 15 |> List.take 1 |> List.map limitDigitRange8ToB |> List.map mapToHex |> String.fromList
                 ]
                    |> String.concat
                )
        )
        (Random.list 15 (Random.int 0 15))

with bits:

module MyId exposing (MyId(..))

import Bit exposing (Bit)
import Vector60

type MyId
    = MyId (Vector60 Bit) -- depending on necessary bits

Notice how extracting information is easy and to creating a new id can be done safely (without e.g. requiring going through decoders, parsers, validations, opaque random generators etc.).

๐Ÿงฉ Vector60 is from Chadtech/elm-vector but anything will do the job, like a record, custom codegen or lue-bird/elm-typesafe-array. Hell even if you just use an opaque List Bit you'll still have it easier than with a String.

conversions

Bits as a universal way of representing information can be converted from and to basically any shape โ†’ example

where elm-bits is used

  • elm-morph can create a parser-builder that can even read non-byte-multiple bit counts like 7
  • maybe you built something? Tell me about it โœฟ

Confused? Hyped? Hit @lue up on anything on slack

elm-bits's People

Contributors

indique avatar lue-bird avatar

Watchers

 avatar

Forkers

elm-review-bot

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.