Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 41 lines (29 sloc) 1.032 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import functools

class BFList(list):
  """
BFList is a extension of Python's standard list with some useful functional methods (map, filter, reduce, etc) on the instance objects.
"""
  
  # Override so that we return a BFList
  def __add__(self, other):
    return BFList(super(BFList, self).__add__(other))
  
  def foreach(self, f):
    for i in self:
      f(i)
      
  def filter(self, f):
    return BFList(i for i in self if f(i))
  
  def map(self, f):
    return BFList(f(i) for i in self)
    
  def reduceLeft(self, f):
    return functools.reduce(f, self)
    
  def reduce(self, f):
    return self.reduceLeft(f)
    
  def foldLeft(self, f, initial):
    return functools.reduce(f, self, initial)
    
  def sort(self, *args, **kwargs):
    return BFList(sorted(self, *args, **kwargs))
  
  def reverse(self):
    return BFList(self[::-1])
    
  def reduceRight(self, f):
    return functools.reduce(f, self.reverse())
  
  def foldRight(self, f, initial):
    return functools.reduce(f, self.reverse(), initial)
Something went wrong with that request. Please try again.