>>> first(3,iter([1,2,3,4]))
    [1, 2, 3]
    >>> first(3,iter([1,2,3,4]), iter) #doctest: +ELLIPSIS
    <itertools.islice object at ...>
    >>> first(3,iter([1,2,3,4]), tuple)
    (1, 2, 3)
338073
def first(n, it, constructor=list):
    
    return constructor(itertools.islice(it,n))
306189
def iflatten(L):
    """"""
    for sublist in L:
        if hasattr(sublist, '__iter__'):
            for item in iflatten(sublist): yield item
        else: yield sublist
1163112
def window(iterable, size=2):
    '''  '''
    iterable = iter(iterable)
    d = deque(islice(iterable, size-1), maxlen=size)
    for _ in map(d.append, iterable):
        yield tuple(d)
117329
def iter_islast(iterable):
    
    it = iter(iterable)
    prev = next(it)
    for item in it:
        yield prev, False
        prev = item
    yield prev, True
1170329
def first(sequence, message=None):
    
    try:
        return next(iter(sequence))
    except StopIteration:
        raise ValueError(message or ('Sequence is empty: %s' % sequence))
1311672
private void findBestWithFirst(DatanodeDescriptor first,
      List<DatanodeDescriptor> listOfNodes, DatanodeDescriptor[] result) {
    for (int in2 = 0; in2 < listOfNodes.size(); in2++) {
      DatanodeDescriptor n2 = listOfNodes.get(in2);
      if (!first.equals(n2)) {
        if (result[1] == null && inWindow(first, n2)) {
          result[0] = first;
          result[1] = n2;
        }

        for (int in3 = in2 + 1; in3 < listOfNodes.size(); in3++) {
          DatanodeDescriptor n3 = listOfNodes.get(in3);
          if (!first.equals(n3)
              && inWindow(first, n3, n2)) {
            result[0] = first;
            result[1] = n2;
            result[2] = n3;
            return;
          }
        }
      }
    }
  }
406013
def joinpath(cls, first, *others):
        
        if not isinstance(first, cls):
            first = cls(first)
        return first._next_class(first.module.join(first, *others))
458182
def _mixed_join(iterable, sentinel):
    """"""
    iterator = iter(iterable)
    first_item = next(iterator, sentinel)
    if isinstance(first_item, bytes):
        return first_item + b''.join(iterator)
    return first_item + u''.join(iterator)
913887
def split_in_pairs(arg: Iterable) -> Iterable[Tuple]:
    
    # We are using zip_longest with one clever hack:
    # https://docs.python.org/3/library/itertools.html#itertools.zip_longest
    # We create an iterator out of the list and then pass the same iterator to
    # the function two times. Thus the function consumes a different element
    # from the iterator each time and produces the desired result.
    iterator = iter(arg)
    return zip_longest(iterator, iterator)
1158150
def first_true(iterable, func):
    # type: (Iterable[T], Callable) -> T
    "
    try:
        return next((x for x in iterable if func(x)))
    except StopIteration as e:
        # TODO: Find a better error message
        raise_from(IndexError('No match for %s' % func), e)
623813
def traverse_inorder(self, leaves=True, internal=True):
        '''
        '''
        c = self; s = deque(); done = False
        while not done:
            if c is None:
                if len(s) == 0:
                    done = True
                else:
                    c = s.pop()
                    if (leaves and c.is_leaf()) or (internal and not c.is_leaf()):
                        yield c
                    if len(c.children) == 0:
                        c = None
                    elif len(c.children) == 2:
                        c = c.children[1]
                    else:
                        raise RuntimeError(INORDER_NONBINARY)
            else:
                s.append(c)
                if len(c.children) == 0:
                    c = None
                elif len(c.children) == 2:
                    c = c.children[0]
                else:
                    raise RuntimeError(INORDER_NONBINARY)
610179
def matchfirst(self, event):
        '''
        
        '''
        # 1. matches(self.index[ind], event)
        # 2. matches(self.any, event)
        # 3. self.matches
        if self.depth < len(event.indices):
            ind = event.indices[self.depth]
            if ind in self.index:
                m = self.index[ind].matchfirst(event)
                if m is not None:
                    return m
            if hasattr(self, 'any'):
                m = self.any.matchfirst(event)
                if m is not None:
                    return m
        if self._use_dict:
            for o, m in self.matchers_dict:
                if m is None or m.judge(event):
                    return o
        else:
            for o, m in self.matchers_list:
                if m is None or m.judge(event):
                    return o
703701
def _pairwise(iterable):
    
    a, b = itertools.tee(iterable)
    next(b, None)
    return zip(a, b)
518678
def is_Tuple_ellipsis(tpl):
    """"""
    try:
        return tpl.__tuple_use_ellipsis__
    except AttributeError:
        try:
            if tpl.__args__ is None:
                return False
            # Python 3.6
            if tpl.__args__[-1] is Ellipsis:
                return True
        except AttributeError:
            pass
        return False
82865
def chunks(items, n):
    
    it = iter(items)
    chunk = tuple(itertools.islice(it, n))
    while chunk:
        yield chunk
        chunk = tuple(itertools.islice(it, n))
245405
def nth(iterable, n, default=None):
        

        if type(n) != int:
            raise TypeError("n is not an integer")

        return next(islice(iterable, n, None), default)
683471
def slice_before(predicate, iterable):
    
    if isinstance(predicate, string_type):
        predicate = re.compile(predicate)

    if hasattr(predicate, 'match'):
        predicate = predicate.match

    record = []

    for i, item in enumerate(iterable):
        if i == 0:
            record = [item]
            continue

        if predicate(item):
            yield tuple(record)
            record = [item]
        else:
            record.append(item)

    if record:
        yield tuple(record)