Skip to content
Merged
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
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "Python: Current File",
"type": "python",
"request": "test",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ isinstance(clone, Obj) # True
- [ ] composeWith
- [x] 0.1.2 concat
- [ ] cond
- [ ] construct
- [ ] constructN
- [x] construct
- [x] constructN
- [x] 0.1.4 converge
- [ ] count
- [x] 0.1.2 countBy
Expand Down
2 changes: 2 additions & 0 deletions ramda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from .comparator import comparator
from .compose import compose
from .concat import concat
from .construct import construct
from .constructN import constructN
from .converge import converge
from .countBy import countBy
from .curry import curry
Expand Down
13 changes: 13 additions & 0 deletions ramda/construct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .constructN import constructN
from .Max import Max
from .private._curry1 import _curry1
from .private._inspect import funcArgsLength


def inner_construct(Fn):
# should ignore first argument
n = Max(0, funcArgsLength(Fn.__init__) - 1)
return constructN(n, Fn)


construct = _curry1(inner_construct)
35 changes: 35 additions & 0 deletions ramda/constructN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from .curry import curry
from .nAry import nAry
from .private._curry2 import _curry2


def inner_constructN(n, Fn):
if n > 10:
raise ValueError('Constructor with more than 10 arguments')
if n == 0:
return Fn

def wrapper(a0, a1=None, a2=None, a3=None, a4=None, a5=None, a6=None, a7=None, a8=None, a9=None):
if n == 1:
return Fn(a0)
if n == 2:
return Fn(a0, a1)
if n == 3:
return Fn(a0, a1, a2)
if n == 4:
return Fn(a0, a1, a2, a3)
if n == 5:
return Fn(a0, a1, a2, a3, a4)
if n == 6:
return Fn(a0, a1, a2, a3, a4, a5)
if n == 7:
return Fn(a0, a1, a2, a3, a4, a5, a6)
if n == 8:
return Fn(a0, a1, a2, a3, a4, a5, a6, a7)
if n == 9:
return Fn(a0, a1, a2, a3, a4, a5, a6, a7, a8)
return Fn(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
return curry(nAry(n, wrapper))


constructN = _curry2(inner_constructN)
3 changes: 3 additions & 0 deletions ramda/private/_inspect.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

from inspect import getfullargspec

from ._has import _has
from ._isPlaceholder import _isPlaceholder


Expand All @@ -9,6 +10,8 @@ def funcArgsLength(fn):
Get the number of args for function fn
Not count *args and **kwargs
"""
if not _has(fn, '__code__'):
return 0
return fn.__code__.co_argcount


Expand Down
57 changes: 57 additions & 0 deletions test/test_construct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

import unittest

import ramda as R

"""
https://github.com/ramda/ramda/blob/master/test/construct.js
"""


class Rectangle:
def __init__(self, w, h):
self.w = w
self.h = h

def area(self):
return self.w * self.h


class TestConstruct(unittest.TestCase):
def test_returns_a_constructor_function_into_one_that_can_be_called_without_new(self):
rect = R.construct(Rectangle)
r1 = rect(3, 4)
self.assertEqual(3, r1.w)
self.assertEqual(12, r1.area())

def test_supports_constructors_with_no_arguments(self):
class Foo:
pass
foo = R.construct(Foo)
self.assertIsInstance(foo(), Foo)

def test_does_not_support_constructors_with_more_than_ten_arguments(self):
class Foo:
def __init__(self, a, b, c, d, e, f, g, h, i, j, k):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
self.g = g
self.h = h
self.i = i
self.j = j
self.k = k
with self.assertRaises(ValueError):
R.construct(Foo)

def test_returns_a_curried_function(self):
rect = R.construct(Rectangle)
r1 = rect(3)(4)
self.assertEqual(3, r1.w)
self.assertEqual(12, r1.area())

if __name__ == '__main__':
unittest.main()
96 changes: 96 additions & 0 deletions test/test_constructN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

import datetime
import unittest
from math import pi

import ramda as R

"""
https://github.com/ramda/ramda/blob/master/test/constructN.js
"""


class Circle:
def __init__(self, r, *args):
print(args)
self.r = r
self.colors = list(args)

def area(self):
return self.r ** 2 * pi


class TestConstructN(unittest.TestCase):
def test_turns_a_constructor_function_into_a_function_with_n_arguments(self):
circle = R.constructN(2, Circle)
c1 = circle(1, 'red')
self.assertIsInstance(c1, Circle)
self.assertEqual(1, c1.r)
self.assertEqual(['red'], c1.colors)
self.assertEqual(pi, c1.area())

def test_can_be_used_to_create_date_object(self):
date = R.constructN(3, datetime.date)
d1 = date(2016)(1)(1)
self.assertEqual(2016, d1.year)
self.assertEqual(1, d1.month)
self.assertEqual(1, d1.day)

def test_supports_constructors_with_no_arguments(self):
class Foo:
pass
foo = R.constructN(0, Foo)
self.assertIsInstance(foo(), Foo)

def test_does_not_support_constructor_with_more_than_ten_arguments(self):
with self.assertRaises(ValueError):
R.constructN(11, Circle)

def test_all_arity(self):
circle = R.constructN(1, Circle)
self.assertEqual([], circle(1).colors)

circle = R.constructN(2, Circle)
self.assertEqual(['red'], circle(1)('red').colors)

circle = R.constructN(3, Circle)
self.assertEqual(['red', 'blue'], circle(1)('red')('blue').colors)

circle = R.constructN(4, Circle)
self.assertEqual(
['red', 'blue', 'green'],
circle(1)('red')('blue')('green').colors)

circle = R.constructN(5, Circle)
self.assertEqual(
['red', 'blue', 'green', 'yellow'],
circle(1)('red')('blue')('green')('yellow').colors)

circle = R.constructN(6, Circle)
self.assertEqual(
['red', 'blue', 'green', 'yellow', 'orange'],
circle(1)('red')('blue')('green')('yellow')('orange').colors)

circle = R.constructN(7, Circle)
self.assertEqual(
['red', 'blue', 'green', 'yellow', 'orange', 'purple'],
circle(1)('red')('blue')('green')('yellow')('orange')('purple').colors)

circle = R.constructN(8, Circle)
self.assertEqual(
['red', 'blue', 'green', 'yellow', 'orange', 'purple', 'brown'],
circle(1)('red')('blue')('green')('yellow')('orange')('purple')('brown').colors)

circle = R.constructN(9, Circle)
self.assertEqual(
['red', 'blue', 'green', 'yellow', 'orange', 'purple', 'brown', 'black'],
circle(1)('red')('blue')('green')('yellow')('orange')('purple')('brown')('black').colors)

circle = R.constructN(10, Circle)
self.assertEqual(
['red', 'blue', 'green', 'yellow', 'orange', 'purple', 'brown', 'black', 'white'],
circle(1)('red')('blue')('green')('yellow')('orange')('purple')('brown')('black')('white').colors)


if __name__ == '__main__':
unittest.main()