Open dotnetCarpenter opened 2 years ago
I don't think this makes sense.
Here is the type of take
:
take :: (Applicative f, Foldable f, Monoid (f a)) => Integer -> f a -> Maybe (f a)
We could specialize this by replacing f
with Array
and a
with String
:
take :: Integer -> Array String -> Maybe (Array String)
Doing the same with Uint8Array
results in a nonsensical type:
take :: Integer -> Uint8Array String -> Maybe (Uint8Array String)
Uint8Array
is a type rather than a type constructor: these arrays only ever contain 8-bit unsigned integers.
One could define a specialized function:
takeUint8 :: Integer -> Uint8Array -> Maybe Uint8Array
@davidchambers How would I go about implementing specialised functions for Sanctuary?
Would TypedArray
encompass all of Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, BigInt64Array and BigUint64Array?
takeTypedArray :: Integer -> TypedArray -> Maybe TypedArray
The hypothetical TypedArray
type is problematic because it would allow takeTypedArray
to take a Uint8Array
but return a Maybe Float64Array
.
The simplest solution would be to define a specialized function for each type.
So that would be 11 specialized functions, just for take
?
takeInt8Array :: Integer -> Int8Array -> Maybe Int8Array
takeUint8Array :: NonNegativeInteger -> Uint8Array -> Maybe Uint8Array
takeUint8ClampedArray :: NonNegativeInteger -> Uint8ClampedArray -> Maybe Uint8ClampedArray
takeInt16Array :: Integer -> Int16Array -> Maybe Int16Array
takeUint16Array :: NonNegativeInteger -> Uint16Array -> Maybe Uint16Array
takeInt32Array :: Integer -> Int32Array -> Maybe Int32Array
takeUint32Array :: NonNegativeInteger -> Uint32Array -> Maybe Uint32Array
takeFloat32Array :: ValidNumber -> Float32Array -> Maybe Float32Array
takeFloat64Array :: ValidNumber -> Float64Array -> Maybe Float64Array
Does Sanctuary support BigInt?
takeBigInt64Array :: BigInt -> BigInt64Array -> Maybe BigInt64Array
takeBigUint64Array :: NonNegativeBigInt -> BigUint64Array -> Maybe BigUint64Array
But 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 -> Buffer
Ref: 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 S.unchecked.drop
but the Array,isArray
check in _takeDrop
would not let me, so I used .slice
instead without a Maybe
.
Changing that one line, made it all work in Sanctuary but .... here I am.
If I'd want to take
from a Buffer, I'd probably do something like:
// 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.test
test fails unconditionaly for a TypedArray (Uint8Array
).Need help ;)
Reproduce:
PS. For now my goal is to support various Buffers for
unchecked
functions, 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.