 
 if typeof(require) != 'undefined'
   require.paths.unshift('#{__dirname}/../lib')
-  { Sequence } = require('sequence')
-  { suspend }  = require('functional')
+  { Sequence }                = require('sequence')
+  { recur, resolve, suspend } = require('functional')
 else
-  { Sequence, suspend } = pazy
+  { Sequence, recur, resolve, suspend } = pazy
 
 
 class FingerTreeType

class SortedExtensions

SortedSeq = (->
  Tree = new FingerTreeType OrderMeasure, SortedExtensions

  class Instance
    constructor: (@data) ->


      [l, r] = split @data, x
      new Instance l.concat r.dropUntil (m) -> m > x

    merge = (s, t1, t2) ->
      if t2.isEmpty()
        s.concat t1
      else
        k = t2.first()
        [l, x, r] = t1.split (m) -> m > k
        recur -> merge s.concat(l).before(k), t2.rest(), r.after(x)

    merge: (other) ->
      new Instance resolve merge Tree.build(), this.data, other.data

    toString: -> @data.toString()


  Tree = new FingerTreeType OrderMeasure, SortedExtensions

  Empty = new Instance Tree.build()

  { build: -> Sequence.reduce arguments, Empty, (s, a) -> s.insert a }
)()
