https://github.com/Gozala/reducers
Raw File
Tip revision: 4b5432a40fc01ed434431fbd874bcc259106e497 authored by Irakli Gozalishvili on 07 November 2012, 06:34:53 UTC
Version 1.0.1
Tip revision: 4b5432a
take.js
"use strict";

var reducible = require("./reducible")
var accumulate = require("./accumulate")
var end = require("./end")

function take(source, n) {
  /**
  Returns sequence of first `n` items of the given `source`. If `source`
  contains less items than `n` then that's how  much items return sequence
  will contain.

  ## Example

  print(take([ 1, 2, 3, 4, 5 ], 2))   // => < 1 2 >
  print(take([ 1, 2, 3 ], 5))         // => < 1 2 3 >
  **/

  // If take `0` then optimize by returning an empty if less then `0`
  // then just return `source` back.
  if (n === 0) return void(0)
  if (n < 0) return source
  return reducible(function accumulateTake(next, initial) {
    // Capture `n` into count, since modifying `n` directly will have side
    // effects on subsequent calls.
    var count = n
    accumulate(source, function accumulateTakeSource(value, result) {
      count = count - 1
      result = next(value, result)

      // If we have not taken `n` items yet just pass result back. Otherwise
      // pass `end` of stream to a consumer. Note `reducible` will return
      // `reduced(result)` back signaling source it should stop.
      return count > 0 ? result :
             next(end)
    }, initial)
  })
}

module.exports = take
back to top