Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented constantly in functoolz #318

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Functoolz
.. autosummary::
complement
compose
constantly
curry
do
excepts
Expand Down
19 changes: 16 additions & 3 deletions toolz/functoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,23 @@ def flip(func, a, b):
return func(b, a)


def return_none(exc):
""" Returns None.
def constantly(v):
"""Create function that always returns ``v``, independent of its input

>>> f = constantly(3)
>>> f(10)
3
>>> f({'any': 'obj'}, 'and', any='amt')
3
"""
def inner(*args, **kwargs):
return v
return inner


return_none = constantly(None)
return_none.__doc__ = """Returns None.
"""
return None


class excepts(object):
Expand Down
12 changes: 10 additions & 2 deletions toolz/tests/test_functoolz.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import platform

from toolz.functoolz import (thread_first, thread_last, memoize, curry,
compose, pipe, complement, do, juxt, flip, excepts)
from toolz.functoolz import (thread_first, thread_last, memoize, curry, compose,
pipe, complement, do, juxt, flip, excepts,
constantly)
from operator import add, mul, itemgetter
from toolz.utils import raises
from functools import partial
Expand Down Expand Up @@ -576,6 +577,13 @@ def f(a, b):
assert flip(f, 'a', 'b') == ('b', 'a')


def test_constantly():
f = constantly(42)

assert f() == 42
assert f(2) == 42


def test_excepts():
# These are descriptors, make sure this works correctly.
assert excepts.__name__ == 'excepts'
Expand Down