Data.ArrayBuffer.Builder  
- Package
- purescript-arraybuffer-builder
- Repository
- jamesdbrock/purescript-arraybuffer-builder
This module provides a Builder monoid and a PutM monad
for serializing Data.ArrayBuffer.Types.ArrayBuffers.
See the package README for usage examples.
Writing to an ArrayBuffer is an Effectful activity, so most
functions in this module must be run in a MonadEffect context.
For operations for working with ArrayBuffer, see
module
Data.ArrayBuffer.ArrayBuffer
in package purescript-arraybuffer.
#PutM Source
type PutM :: (Type -> Type) -> Type -> Typetype PutM = WriterT Builder
The PutM monad is a WriterT Builder transformer monad which
gives us do-notation for the Builder monoid. The base monad must be
a MonadEffect.
To append Builders in this monad call tell, or any of the put*
functions in this module.
#execPutM Source
execPutM :: forall m. MonadEffect m => PutM m Unit -> m ArrayBufferBuild an ArrayBuffer with do-notation in any MonadEffect. O(n)
#execPut Source
execPut :: Put Unit -> Effect ArrayBufferBuild an ArrayBuffer with do-notation in Effect. O(n)
#subBuilder Source
subBuilder :: forall m. MonadEffect m => PutM m Unit -> PutM m BuilderBuild up a sub-Builder without telling it to the Writer yet.
One case where we might want to call a subBuilder is when
serializing length-prefixed messages in some protocol. In that case,
we must serialize the message first, calculate the message length,
append the message length, and then append the message.
In a PutM monad do-block, we can
do
  messageBuilder <- subBuilder $ do
    putField1
    putField2
  putInt32be $ length messageBuilder
  tell messageBuilder
#putArrayBuffer Source
putArrayBuffer :: forall m. MonadEffect m => ArrayBuffer -> PutM m UnitAppend an ArrayBuffer to the builder.
#putDataView Source
putDataView :: forall m. MonadEffect m => DataView -> PutM m UnitAppend a DataView to the builder.
#putDataBuff Source
putDataBuff :: forall m. MonadEffect m => DataBuff -> PutM m UnitAppend either an ArrayBuffer or a DataView to the builder.
#putUint8 Source
putUint8 :: forall m. MonadEffect m => UInt -> PutM m UnitAppend an 8-bit unsigned integer (byte) to the builder.
#putInt8 Source
putInt8 :: forall m. MonadEffect m => Int -> PutM m UnitAppend an 8-bit two’s-complement signed integer (char) to the builder.
#putUint16be Source
putUint16be :: forall m. MonadEffect m => UInt -> PutM m UnitAppend a 16-bit big-endian unsigned integer to the builder.
#putUint16le Source
putUint16le :: forall m. MonadEffect m => UInt -> PutM m UnitAppend a 16-bit little-endian unsigned integer to the builder.
#putInt16be Source
putInt16be :: forall m. MonadEffect m => Int -> PutM m UnitAppend a 16-bit big-endian two’s-complement signed integer to the builder.
#putInt16le Source
putInt16le :: forall m. MonadEffect m => Int -> PutM m UnitAppend a 16-bit little-endian two’s-complement signed integer to the builder.
#putUint32be Source
putUint32be :: forall m. MonadEffect m => UInt -> PutM m UnitAppend a 32-bit big-endian unsigned integer to the builder.
#putUint32le Source
putUint32le :: forall m. MonadEffect m => UInt -> PutM m UnitAppend a 32-bit little-endian unsigned integer to the builder.
#putInt32be Source
putInt32be :: forall m. MonadEffect m => Int -> PutM m UnitAppend a 32-bit big-endian two’s-complement signed integer to the builder.
#putInt32le Source
putInt32le :: forall m. MonadEffect m => Int -> PutM m UnitAppend a 32-bit little-endian two’s-complement signed integer to the builder.
#putFloat32be Source
putFloat32be :: forall m. MonadEffect m => Float32 -> PutM m UnitAppend a 32-bit big-endian IEEE single-precision float to the builder.
#putFloat32le Source
putFloat32le :: forall m. MonadEffect m => Float32 -> PutM m UnitAppend a 32-bit little-endian IEEE single-precision float to the builder.
#putFloat64be Source
putFloat64be :: forall m. MonadEffect m => Number -> PutM m UnitAppend a 64-bit big-endian IEEE double-precision float to the builder.
#putFloat64le Source
putFloat64le :: forall m. MonadEffect m => Number -> PutM m UnitAppend a 64-bit little-endian IEEE double-precision float to the builder.
Re-exports from Data.ArrayBuffer.Builder.Internal   
#Builder Source
data BuilderMonoidal builder for ArrayBuffers.
We can add two types of things to the Builder:
- ArrayBuffer
- DataView
We might prefer
to add a DataView to a Builder when we’re adding a large slice of data
from some other ArrayBuffer, so that we don’t
need an extra intermediate copy of the slice.
Instances
#length Source
length :: Builder -> ByteLengthCalculate the total byte length of the Builder, without actually
building it yet. O(n)
Left-associative
<>>append operatorTL;DR You probably don't want to use the
Buildermonoid directly in your code, it’s better to use thePutMmonad with do-notation instead.The
Buildermonoid in this library is efficient when wesnocsingle items onto the end of it, or when we onlyconssingle items to the beginning, but it can be less efficient when we are mixingconsandsnoc. Most of the time we want tosnoc, but theSemigroupappend operator<>is right-associative, which means it chains likecons.To solve this, we provide an operator
<>>for appendingBuilders.<>>is exactly the same as<>, but left-associative, which means it chains likesnoc.This only matters when we're chaining together three or more
Builders in a single associative expression. Instead ofwe should always prefer to write
so that we get the efficient
snocing ofBuilders.If we build our
ArrayBuffers with thePutMmonad instead of appending by using theSemigroupinstance ofBuilder, then we always get the efficientsnoccase.