[WIP] add support for TypedArrays/arrayBuffer/nodejs' Buffer#716
[WIP] add support for TypedArrays/arrayBuffer/nodejs' Buffer#716dotnetCarpenter wants to merge 1 commit intosanctuary-js:mainfrom
Conversation
Currently failing to implement tests for TypedArray. In `assert.strictEqual (equals (actual) (expected), true);` the `Setoid.test` test fails unconditionaly for a TypedArray (`Uint8Array`). Need help ;)
|
I don't think this makes sense. Here is the type of take :: (Applicative f, Foldable f, Monoid (f a)) => Integer -> f a -> Maybe (f a)We could specialize this by replacing take :: Integer -> Array String -> Maybe (Array String)Doing the same with take :: Integer -> Uint8Array String -> Maybe (Uint8Array String)
One could define a specialized function: takeUint8 :: Integer -> Uint8Array -> Maybe Uint8Array |
|
@davidchambers How would I go about implementing specialised functions for Sanctuary? Would takeTypedArray :: Integer -> TypedArray -> Maybe TypedArray |
|
The hypothetical The simplest solution would be to define a specialized function for each type. |
|
So that would be 11 specialized functions, just for takeInt8Array :: Integer -> Int8Array -> Maybe Int8ArraytakeUint8Array :: NonNegativeInteger -> Uint8Array -> Maybe Uint8ArraytakeUint8ClampedArray :: NonNegativeInteger -> Uint8ClampedArray -> Maybe Uint8ClampedArraytakeInt16Array :: Integer -> Int16Array -> Maybe Int16ArraytakeUint16Array :: NonNegativeInteger -> Uint16Array -> Maybe Uint16ArraytakeInt32Array :: Integer -> Int32Array -> Maybe Int32ArraytakeUint32Array :: NonNegativeInteger -> Uint32Array -> Maybe Uint32ArraytakeFloat32Array :: ValidNumber -> Float32Array -> Maybe Float32ArraytakeFloat64Array :: ValidNumber -> Float64Array -> Maybe Float64ArrayDoes Sanctuary support BigInt? takeBigInt64Array :: BigInt -> BigInt64Array -> Maybe BigInt64ArraytakeBigUint64Array :: NonNegativeBigInt -> BigUint64Array -> Maybe BigUint64ArrayBut actually, before going down this rabbit hole. I'm much more interested in support for Nodejs' Buffer since I deal with a database that returns Buffers. takeBuffer :: NonNegativeInteger -> Buffer -> Maybe Buffer
dropBuffer :: NonNegativeInteger -> Buffer -> Maybe Buffer
takeLastBuffer :: NonNegativeInteger -> Buffer -> Maybe Buffer
dropLastBuffer :: NonNegativeInteger -> Buffer -> Maybe Buffer
takeWhileBuffer :: (a -> Boolean) -> Buffer -> Buffer
dropWhileBuffer :: (a -> Boolean) -> Buffer -> BufferRef: NonNegativeInteger |
|
I tried to look through some code a few months back to find context for this. I did not find anything. But as I recall, I got a Buffer from the database and wanted to use Changing that one line, made it all work in Sanctuary but .... here I am. |
|
If I'd want to // firstFiveBytes :: Buffer -> Maybe Buffer
const firstFiveBytes = S.pipe ([
Array.from,
S.take (5),
S.map (Buffer.from),
]) |
|
@Avaq I'm imagining that working with Buffers without converting them to Array would be faster overall. But I should write some code to compare with. There is only one line that need to change in Sanctuary in order to support Buffers, so it should be fairly easy to compare. But it won't be today :) |
Currently failing to implement tests for TypedArray.
In
assert.strictEqual (equals (actual) (expected), true);theSetoid.testtest fails unconditionaly for a TypedArray (Uint8Array).Need help ;)
Reproduce:
PS. For now my goal is to support various Buffers for
uncheckedfunctions, since it seems like this should be opt-in and I don't know how to implement support in https://github.com/sanctuary-js/sanctuary-def yet.