Commits

Maxim Moiseev committed 6315044

rearranged code slightly

  • Participants
  • Parent commits 76f8b1b

Comments (0)

Files changed (3)

File foonc/core.py

+import functools as f
+
+def ignore(*args, **kwargs):
+    '''Ignores all the arguments being passed in.
+    Might be usefule when you need a fucntion, but do not care of it's result'''
+    pass
+
+def const(x):
+    '''Creates a function that returns x regardless of arguments'''
+    def _const(*args, **kwargs):
+        return x
+    return _const
+
+def id_(x):
+    '''Identity function'''
+    return x
+
+def flip(f):
+    '''Creates a function taking a reversed list of arguments.
+    Keyword arguments remain unchanged.'''
+    def _flipped(*args, **kwargs):
+        return f(*reversed(args), **kwargs)
+    return _flipped
+
+def _compose2(f, g):
+    def _compose_inner(*args, **kwargs):
+        return f(g(*args, **kwargs))
+    return _compose_inner
+
+def compose(*fs):
+    '''Composition of several functions'''
+    if len(fs) == 1:
+        # a list of functions has been passed in
+        fs = fs[0]
+    return reduce(_compose2, fs)
+
+def wrap(f):
+    return _Foonc(f)
+
+class _Foonc(object):
+    '''Somewhat useful function wrapper'''
+    def __init__(self, f):
+        self._f = f
+
+    def __call__(self, *args, **kwargs):
+        return self._f(*args, **kwargs)
+
+    @staticmethod
+    def _unwrap(f):
+        if isinstance(f, _Foonc):
+            return f._f
+        return f
+
+    def __rshift__(self, other):
+        f = self._unwrap(other)
+        return _Foonc(compose(f, self._f))
+
+    def __lshift__(self, other):
+        f = self._unwrap(other)
+        return _Foonc(compose(self._f, f))
+

File foonc/foonc.py

-import functools as f
-
-def ignore(*args, **kwargs):
-    '''Ignores all the arguments being passed in.
-    Might be usefule when you need a fucntion, but do not care of it's result'''
-    pass
-
-def const(x):
-    '''Creates a function that returns x regardless of arguments'''
-    def _const(*args, **kwargs):
-        return x
-    return _const
-
-def id_(x):
-    '''Identity function'''
-    return x
-
-def flip(f):
-    '''Creates a function taking a reversed list of arguments.
-    Keyword arguments remain unchanged.'''
-    def _flipped(*args, **kwargs):
-        return f(*reversed(args), **kwargs)
-    return _flipped
-
-def _compose2(f, g):
-    def _compose_inner(*args, **kwargs):
-        return f(g(*args, **kwargs))
-    return _compose_inner
-
-def compose(*fs):
-    '''Composition of several functions'''
-    if len(fs) == 1:
-        # a list of functions has been passed in
-        fs = fs[0]
-    return reduce(_compose2, fs)
-
-def wrap(f):
-    return _Foonc(f)
-
-class _Foonc(object):
-    '''Somewhat useful function wrapper'''
-    def __init__(self, f):
-        self._f = f
-
-    def __call__(self, *args, **kwargs):
-        return self._f(*args, **kwargs)
-
-    @staticmethod
-    def _unwrap(f):
-        if isinstance(f, _Foonc):
-            return f._f
-        return f
-
-    def __rshift__(self, other):
-        f = self._unwrap(other)
-        return _Foonc(compose(f, self._f))
-
-    def __lshift__(self, other):
-        f = self._unwrap(other)
-        return _Foonc(compose(self._f, f))
-

File foonctest/foonctest.py

 import unittest
 
-from foonc import foonc
+from foonc.core import *
+from foonc.core import _compose2
 
 def inc(x):
     return x + 1
         super(FooncTests, self).setUp()
 
     def test_const(self):
-        self.assertEqual(42, foonc.const(42)('whatever', is_passed='in'))
+        self.assertEqual(42, const(42)('whatever', is_passed='in'))
 
     def test_compose2(self):
-        f = foonc._compose2(inc, int)
+        f = _compose2(inc, int)
         self.assertEqual(42, f('41'))
 
     def test_compose_args(self):
-        f = foonc.compose(str, inc, int)
+        f = compose(str, inc, int)
         self.assertEqual('42', f('41'))
 
     def test_compose_list(self):
-        f = foonc.compose([str, inc, int])
+        f = compose([str, inc, int])
         self.assertEqual('42', f('41'))
 
     def test_flip(self):
         def f(*args):
             return args
-        flipped = foonc.flip(f)
+        flipped = flip(f)
         self.assertEqual((3, 2, 1), flipped(1, 2, 3))
 
 
         super(FooncWrapperTests, self).setUp()
 
     def test_left_shift(self):
-        f = foonc.wrap(int) >> inc
+        f = wrap(int) >> inc
         self.assertEqual(42, f('41'))