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

Fix reduce builtin #2925

Merged
merged 1 commit into from
Nov 1, 2023
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
4 changes: 2 additions & 2 deletions docs/lexicon.html
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ <h2 class="title-2" id="python-builtins">
<span>
<code class="code"
><span class="fn-name">reduce</span><span class="fn-p">(</span
><span class="fn-arg">function</span>, <span class="fn-arg">seq</span
><span class="fn-arg">reducer</span>, <span class="fn-arg">seq</span
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was copied from here:
https://docs.python.org/3/library/functools.html#functools.reduce

Looks like you've changed the arg name as well which makes it inconsistent with Python if I understand correctly. Should we switch this back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now it is in line with its neighbours in please code but yes we can make it in line with python instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please/docs/lexicon.html

Lines 177 to 181 in 8d9ffe8

<code class="code"
><span class="fn-name">map</span><span class="fn-p">(</span
><span class="fn-arg">mapper</span>, <span class="fn-arg">seq</span
><span class="fn-p">)</span></code
>

>, <span class="fn-arg">initializer=None</span
><span class="fn-p">)</span></code
>
- apply function of two arguments cumulatively to the items of seq from left to right reducing to a single value returning that value. eg. reduce(lambda x, y: x+y, [0, 1, 2, 3]) would return (((0+1)+2)+3)=6. If initializer is present it is placed before the first item in the sequence.
- returns a single value by applying function cumulatively to the list items, from left to right.
</span>
</li>
<li>
Expand Down
2 changes: 1 addition & 1 deletion rules/builtins.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def filter(filter:function, seq:list):
def map(mapper:function, seq:list):
pass

def reduce(function:function, seq:list, initializer=None):
def reduce(reducer:function, seq:list, initializer=None):
pass

def bool(b) -> bool:
Expand Down
31 changes: 14 additions & 17 deletions src/parse/asp/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,32 +751,29 @@ func mapFunc(s *scope, args []pyObject) pyObject {
}

func reduce(s *scope, args []pyObject) pyObject {
function, isFunc := args[0].(*pyFunc)
reducer, isFunc := args[0].(*pyFunc)
l, isList := args[1].(pyList)
init := args[2]
s.Assert(isFunc, "Argument function must be callable, not %s", args[0].Type())
s.Assert(isFunc, "Argument reducer must be callable, not %s", args[0].Type())
s.Assert(isList, "Argument seq must be a list, not %s", args[1].Type())

ret := init
start := 0
if init == None {
start = 1
if len(l) > 0 {
ret = l[0]
}
if len(l) <= 1 {
return ret
}
if len(l) == 0 {
return args[2]
}
for _, li := range l[start:] {

var ret pyObject
if ret = args[2]; ret == None {
ret, l = l[0], l[1:]
}

for _, li := range l {
c := &Call{
Arguments: []CallArgument{{
Value: Expression{Optimised: &OptimisedExpression{Constant: li}},
}, {
Value: Expression{Optimised: &OptimisedExpression{Constant: ret}},
}, {
Value: Expression{Optimised: &OptimisedExpression{Constant: li}},
}},
}
ret = function.Call(s, c)
ret = reducer.Call(s, c)
}
return ret
}
Expand Down
1 change: 1 addition & 0 deletions src/parse/asp/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func TestReduce(t *testing.T) {
assert.Equal(t, pyInt(5), s.Lookup("r5"))
assert.Equal(t, pyInt(6), s.Lookup("r6"))
assert.Equal(t, pyInt(7), s.Lookup("r7"))
assert.EqualValues(t, "abcde", s.Lookup("r8"))
}

func TestInterpreterUnpacking(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion src/parse/asp/test_data/interpreter/reduce.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ r1 = reduce(lambda x, y: x + y, [0, 1, 2, 3])

# with init
r2 = reduce(
function=lambda x, y: x + y,
reducer=lambda x, y: x + y,
seq=[0, 1, 2, 3],
initializer=10,
)
Expand All @@ -15,3 +15,6 @@ r4 = reduce(lambda x, y: x + y, [])
r5 = reduce(lambda x, y: x + y, [], initializer=5)
r6 = reduce(lambda x, y: x + y, [1], initializer=5)
r7 = reduce(lambda x, y: x + y, [7])

# string (non-commutative)
r8 = reduce(lambda x, y: ''.join((x, y)), ["a", "b", "c", "d", "e"])
Loading