Skip to content

lue-bird/elm-bits

Repository files navigation

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