1

I know this can be done using list comprehension instead of recursion, but I wanted to experiment with recursion and found out strange behaviour in data values that I cannot explain.

So I have a list of lists of lists, like [[[x11, x12, x13, x14], ..., [xn1, xn2, xn3, xn4]], ...], where each 2-D list corresponds to a "sentence" consisting of 1x4 numeric vectors ("words"), sentences can be empty (empty list). And there is another list, in which "sentences" consist of single "words". The goal is to perform map-like function on the first list, where the transform and filter functions are given to the higher order map-like function. Here is an example:

import numpy as  np

class List:
    @staticmethod
    def map(rects, anchor_rect, f, filter_f, i=0, results=[]):
        if i == len(rects):
            return results
        rs = rects[i]
        anc_r = anchor_rect[i]
        for r in rs:
            if filter_f(r, anc_r):
                results.append(f(r, anc_r))
        return List.map(rects, anchor_rect, f, filter_f, i+1, results)

def my_f(r, anc_r):
    return r[0] + anc_r[0]

def filter_f(r, anc_r):
    return not np.all(r == 0)

rects = np.array([[[1, 2, 3, 5], [0, 0, 0, 0], [6, 7, 8, 9]]], dtype='int')
anchor_rect = np.array([[7, 7, 7, 7]], dtype='int')

print 'First call:',  List.map(rects, anchor_rect, my_f, filter_f)
print 'Parameters not changed:\n', rects, '\n', anchor_rect
print 'Second call:', List.map(rects, anchor_rect, my_f, filter_f)

The problem here is that for some reason I get different results when the List.map function is run given the same arguments, as if the List class is somehow changed. But I do not understand what kind of change that could be.

Student4K
  • 916
  • 2
  • 9
  • 20

0 Answers0