def test_flip_sebastienfilion(): # add :: Number → [Number] → Number add = reduce(lambda a, b: a + b) # multiply :: Number → [Number] → Number multiply = reduce(lambda a, b: a * b) total = compose(flip(multiply)([1, 2, 3]), flip(add)([1, 2, 3]))(0) assert total == 36
def edges_to_functions(edges): result = [] for edge in edges: # z założenia są monotoniczne min_e = R.reduce(R.min_by(lambda x: x[0]), [1e9, 0], edge) max_e = R.reduce(R.max_by(lambda x: x[0]), [0, 0], edge) a = (max_e[1] - min_e[1]) / (max_e[0] - min_e[0]) b = min_e[1] - a * min_e[0] result.append([a, b]) return result
def stats_to_matrix(pair_stats, authors={}): result_stats = [] pairs = ramda.to_pairs(pair_stats) _pair_list = ramda.reduce(stats_to_matrix_prep, [], pairs) pair_list = ramda.reduce(lambda acc, cur: [*acc, *cur], [], _pair_list) authors_emails = sorted(ramda.uniq(ramda.pluck(0, pair_list))) for author_a in [*authors_emails]: author_a_pair_stats = [] for author_b in [*authors_emails]: if author_a == author_b: author_a_pair_stats.append(None) else: stat = ramda.find( lambda pair: pair[0] == author_a and pair[1] == author_b, pair_list) author_a_pair_stats.append(stat[2]) result_stats.append({ 'author': authors.get(author_a, {}), 'matrix': author_a_pair_stats, }) return result_stats
def apply_to_resource(self, coll, fn): return r.reduce(fn, self, r.to_pairs(coll))
# im2 = copy.deepcopy(im) edges1 = feature.canny(im) edges = break_edges(edges1) functions = edges_to_functions(edges) errors = blad_srednikwadratowy(edges, functions) print(errors) # angles_between_functions(functions) neighbour_angles = angles_with_neighbouring_functions(functions) print(neighbour_angles) neighbour_angles_sum = R.map(sum, neighbour_angles) print(neighbour_angles_sum) max_index = lambda my_list: R.reduce( lambda acc, x: acc if acc[1] >= x[1] else x, [0, 0], R.zip(range(len(my_list)), my_list)) e = max_index(errors) print(e) a = max_index(neighbour_angles_sum) print(a) #wg błędów index_podstawy = (e[0] + 2) % 4 print('wg błędów', index_podstawy, functions[index_podstawy]) [a1, _] = functions[index_podstawy] #wg kątów index_podstawy2 = a[0] print('wg kątów', index_podstawy2, functions[index_podstawy2]) [a2, _] = functions[index_podstawy2]
def accumulate_pair_stats(self): self.pair_stats = ramda.reduce( lambda a, b: ramda.merge_with(lambda _a, _b: _a + _b, a, b), {}, self._pair_stats, )