Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Add pluck function #32

Closed
wants to merge 26 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,21 @@ cons :: a -> [a] -> [a]
contains :: a -> Predicate [a]
contains_with :: Relation a -> a -> Predicate [a]
drop :: Number -> [a] -> [a]
drop_last :: Number -> [a] -> [a]
filter :: Predicate a -> [a] -> [a]
find :: Predicate a -> Reduction a
map :: (a -> b) -> [a] -> [b]
reduce :: (a -> b -> b) -> a -> [b] -> a
take :: Number -> [a] -> [a]
pluck :: k -> [a] -> [b]
uniq :: [a] -> [b]
zip_obj :: [a] -> [b] -> [c]
zip_with :: f -> [a] -> [b] -> [c]
times :: Number -> [a]
xprod :: [a] -> [b] -> [c]
without :: [a] -> [b] -> [c]
update :: Number -> Number -> [a] -> [b]
join :: [a] -> [b] -> c
```

Logic
Expand All @@ -79,6 +89,7 @@ either :: Operation (Predicate a)
if_else :: Predicate a -> Operation (a -> a)
not_func :: Boolean -> Boolean
or_func :: Operation Boolean
empty :: a -> Boolean
```

Math
Expand Down Expand Up @@ -110,6 +121,7 @@ lt :: Ord a => Relation a
lte :: Ord a => Relation a
max :: Ord a => Reduction a
min :: Ord a => Reduction a
identity :: a -> a
```

Other
Expand Down
10 changes: 10 additions & 0 deletions pyramda/iterable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@
from .for_each import for_each
from .flatten import flatten
from .reject import reject
from .pluck import pluck
from .times import times
from .adjust import adjust
from .zip_obj import zip_obj
from .zip_with import zip_with
from .uniq import uniq
from .xprod import xprod
from .without import without
from .drop_last import drop_last
from .join import join
4 changes: 4 additions & 0 deletions pyramda/iterable/adjust.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pyramda.function.curry import curry

"""This functions applies function at given index"""
adjust = curry(lambda f, i, xs: [f(x) if i == ind else x for ind, x in enumerate(xs)])
14 changes: 14 additions & 0 deletions pyramda/iterable/adjust_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from .adjust import adjust
from pyramda.private.asserts import assert_iterables_equal


def add_one(x):
return x + 1


def cons_nocurry_test():
assert_iterables_equal(adjust(add_one, 1, [2, 3]), [2, 4])


def cons_curry_test():
assert_iterables_equal(adjust(add_one, 1)([2, 3]), [2, 4])
6 changes: 6 additions & 0 deletions pyramda/iterable/drop_last.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pyramda.function.curry import curry


@curry
def drop_last(n, xs):
return xs[:-n or None]
10 changes: 10 additions & 0 deletions pyramda/iterable/drop_last_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .drop_last import drop_last
from pyramda.private.asserts import assert_iterables_equal


def drop_nocurry_test():
assert_iterables_equal(drop_last(2, [1, 2, 3, 4]), [1, 2])


def drop_curry_test():
assert_iterables_equal(drop_last(2)([1, 2, 3, 4]), [1, 2])
7 changes: 7 additions & 0 deletions pyramda/iterable/join.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pyramda.function.curry import curry
from .reduce import reduce


@curry
def join(sep, xs):
return reduce(lambda x, y: x + sep + y, "", xs)
10 changes: 10 additions & 0 deletions pyramda/iterable/join_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .join import join
from pyramda.private.asserts import assert_equal


def reduce_nocurry_test():
assert_equal(join(" ", ["aa", "bb", "cc"]), "aa bb cc")


def reduce_curry_test():
assert_equal(join(" ")(["aa", "bb", "cc"]), "aa bb cc")
14 changes: 14 additions & 0 deletions pyramda/iterable/pluck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pyramda.function.curry import curry


@curry
def prop(key, x):
try:
return x[key]
except KeyError:
return None


@curry
def pluck(key, xs):
return list(map(prop(key), xs))
25 changes: 25 additions & 0 deletions pyramda/iterable/pluck_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from .pluck import pluck


data = [{
'a': 'a1',
'b': 'b1'
}, {
1: 1,
2: 2
}, {
'a': 'a2',
'b': 'b2'
}]


def a_test():
assert pluck('a', data) == ['a1', None, 'a2']


def one_test():
assert pluck(1, data) == [None, 1, None]


def iterator_test():
assert pluck(None, map(lambda x: x, [{None: 1}])) == [1]
5 changes: 5 additions & 0 deletions pyramda/iterable/times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""This functions returns list from 1 to n"""


def times(n):
return list(range(1, n))
6 changes: 6 additions & 0 deletions pyramda/iterable/times_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .times import times
from pyramda.private.asserts import assert_iterables_equal


def uniq_nocurry_test():
assert_iterables_equal(times(5), [1, 2, 3, 4])
5 changes: 5 additions & 0 deletions pyramda/iterable/uniq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""This functions removed duplicated from given list"""


def uniq(xs):
return list(set(xs))
6 changes: 6 additions & 0 deletions pyramda/iterable/uniq_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .uniq import uniq
from pyramda.private.asserts import assert_iterables_equal


def uniq_nocurry_test():
assert_iterables_equal(uniq([1, 2, 3, 3, 4]), [1, 2, 3, 4])
5 changes: 5 additions & 0 deletions pyramda/iterable/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pyramda.function.curry import curry

"""This functions applies function at given index"""
update = curry(lambda i, v, xs: [
v if i == ind else x for ind, x in enumerate(xs)])
10 changes: 10 additions & 0 deletions pyramda/iterable/update_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .update import update
from pyramda.private.asserts import assert_iterables_equal


def cons_nocurry_test():
assert_iterables_equal(update(1, 1, [2, 3]), [2, 1])


def cons_curry_test():
assert_iterables_equal(update(1, 1)([2, 3]), [2, 1])
8 changes: 8 additions & 0 deletions pyramda/iterable/without.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pyramda.function.curry import curry

"""Returns a new list without values in the first argument"""

a = [1, 2]
b = [1, 2, 1, 3, 4]

without = curry(lambda xs1, xs2: [x for x in xs2 if x not in xs1])
10 changes: 10 additions & 0 deletions pyramda/iterable/without_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pyramda.private.asserts import assert_iterables_equal
from .without import without


def uniq_nocurry_test():
assert_iterables_equal(without([1, 2], [1, 2, 1, 3, 4]), [3, 4])


def take_curry_test():
assert_iterables_equal(without([1, 2])([1, 2, 1, 3, 4]), [3, 4])
6 changes: 6 additions & 0 deletions pyramda/iterable/xprod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pyramda.function.curry import curry

"""This functions takes cross product of two list"""


xprod = curry(lambda xs1, xs2: [[x, y] for y in xs2 for x in xs1])
12 changes: 12 additions & 0 deletions pyramda/iterable/xprod_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pyramda.private.asserts import assert_iterables_equal
from .xprod import xprod


def uniq_nocurry_test():
assert_iterables_equal(xprod([1, 2], ['a', 'b']), [
[1, 'a'], [2, 'a'], [1, 'b'], [2, 'b']])


def take_curry_test():
assert_iterables_equal(xprod([1, 2])(['a', 'b']), [
[1, 'a'], [2, 'a'], [1, 'b'], [2, 'b']])
4 changes: 4 additions & 0 deletions pyramda/iterable/zip_obj.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pyramda.function.curry import curry

"""This functions takes two list and creates a dictionary with it"""
zip_obj = curry(lambda key, val: dict(zip(key, val)))
10 changes: 10 additions & 0 deletions pyramda/iterable/zip_obj_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .zip_obj import zip_obj
from pyramda.private.asserts import assert_iterables_equal


def uniq_nocurry_test():
assert_iterables_equal(zip_obj(['a', 'b', 'c'], [1, 2, 3]), {'a': 1, 'b': 2, 'c': 3})


def take_curry_test():
assert_iterables_equal(zip_obj(['a', 'b', 'c'])([1, 2, 3]), {'a': 1, 'b': 2, 'c': 3})
4 changes: 4 additions & 0 deletions pyramda/iterable/zip_with.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pyramda.function.curry import curry

"""This functions takes two list and applies given function to it"""
zip_with = curry(lambda f, xs1, xs2: [f(x, y) for x, y in zip(xs1, xs2)])
14 changes: 14 additions & 0 deletions pyramda/iterable/zip_with_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from .zip_with import zip_with
from pyramda.private.asserts import assert_iterables_equal


def add(a, b):
return a + b


def uniq_nocurry_test():
assert_iterables_equal(zip_with(add, [1, 1, 1], [1, 2, 3]), [2, 3, 4])


def take_curry_test():
assert_iterables_equal(zip_with(add)([1, 1, 1], [1, 2, 3]), [2, 3, 4])
1 change: 1 addition & 0 deletions pyramda/logic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .if_else import if_else
from .not_func import not_func
from .or_func import or_func
from .empty import empty
5 changes: 5 additions & 0 deletions pyramda/logic/empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def empty(x):
if x:
return False
else:
return True
8 changes: 8 additions & 0 deletions pyramda/logic/empty_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .empty import empty


def complement_nocurry_test():
assert empty('')
assert empty([])
assert empty({})
assert empty(())
1 change: 1 addition & 0 deletions pyramda/relation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from .max import max
from .min import min
from .ord import lt, gt, lte, gte
from .identity import identity
5 changes: 5 additions & 0 deletions pyramda/relation/identity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""A function that does nothing but return the parameter supplied to it"""


def identity(x):
return x
6 changes: 6 additions & 0 deletions pyramda/relation/identity_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .identity import identity
from pyramda.private.asserts import assert_iterables_equal


def uniq_nocurry_test():
assert_iterables_equal(identity(1), 1)