fantasyland / fantasy-land

Specification for interoperability of common algebraic structures in JavaScript
MIT License
10.08k stars 373 forks source link

Equivalent for Serial typeclass? #302

Open creatorrr opened 6 years ago

creatorrr commented 6 years ago

Wondering what ReadableStream and other streaming data structures are instances of.

https://hackage.haskell.org/package/lazysmallcheck-0.6/docs/Test-LazySmallCheck.html#t:Serial https://hackage.haskell.org/package/Stream-0.4.7.2/docs/Data-Stream.html

richytong commented 4 years ago

In rubico, ReadableStream is regarded as part of a Semigroup called Stream (for Node.js streams) in the context of extending a WritableStream.

/**
 * streamExtend(stream Writable)(
 *   chunk string|Buffer|Uint8Array|any,
 *   encoding string|undefined,
 *   callback function|undefined,
 * ) -> stream
 */
const streamExtend = function extend(
  stream, chunk, encoding, callback,
) {
  if (isBinary(chunk) || isString(chunk)) {
    const chunkLength = chunk.length
    let index = -1
    while (++index < chunkLength) {
      stream.write(chunk[index], encoding, callback)
    }
  } else { // objectMode
    stream.write(chunk, encoding, callback)
  }
  return stream
}

Above is an extend for performance reasons, but could be rewritten into a concat.