def _clean_sentence(sentence): '''Clean a sentence and its corresponding links''' cleaned_sentence = _.assign({}, sentence, {'text': clean_page_content(sentence['text'])}) if 'links' in sentence: cleaned_sentence['links'] = reduce(_.curry(_sentence_clean_reducer)(cleaned_sentence['text']), sentence['links'], []) return cleaned_sentence
def test_curry(case, arglist, expected): curried = _.curry(*case) # Run test twice to verify curried can be reused for x in range(2): ret = curried for args in arglist: ret = ret(*args) assert ret == expected
def get_link_contexts(redirects_lookup, page): '''link contexts is a dictionary from entity to mention details''' sections = page['sections'] sentences = sum([section['sentences'] for section in sections if 'sentences' in section], []) sentences_from_tables = sum([[table['data'] for table in section['tables'][0] if table.get('data')] for section in sections if section.get('tables')], []) all_sentences = sentences + sentences_from_tables return reduce(_.curry(_sentence_to_link_contexts_reducer)(redirects_lookup, page), all_sentences, {})
def test_curry_arity_max_from_func(): def func(data, accum, id): accum[id] = _.reduce_(data, lambda total, n: total + n) return accum ids = [1] data = [1, 2] curried_func_with_data = _.curry(func)(data) result = _.reduce_(ids, curried_func_with_data, {}) assert result == {1: 3}
import path_helpers as ph import pydash as py_ _fp = py__() # **TODO**: Move `get_typedef_path` and `get_typedef_factory` into # `clang_helpers.clang_ast` module. def get_typedef_path(typedef_str): parts_i = typedef_str.split('::') return ('namespaces.' + '.namespaces.'.join(parts_i[:-1]) + '.' if parts_i[:-1] else '') + 'typedefs.' + parts_i[-1] get_typedef_factory = lambda ast: py_.pipe(get_typedef_path, py_.curry(py_.get, arity=2)(ast)) def get_definition_header(cpp_ast_json, type_): ''' Parameters ---------- cpp_ast_json : dict JSON-serializable C++ abstract syntax tree. type_ : str Name of C++ type defined within C++ abstract syntax tree. Returns ------- path_helpers.path Path to header where type of variable is defined.
def _link_title_exact_match_heuristic(page, link_contexts): '''look for an occurance of the link anchor text''' link_titles = list(link_contexts.keys()) return reduce(_.curry(_apply_exact_match_heuristic)(page), link_titles, link_contexts)
def get_class_factory(ast): return py_.pipe(get_class_path, py_.curry(py_.get, arity=2)(ast))
# currying_func = currying_func(3) # ---------------------------------------- def currying_func2(arg1, arg2, arg3): print('hello') def currying_func2(resolve, reject): print('arg1', arg1) print('arg2', arg2) print('arg3', arg3) resolve('Bye!') return Promise(currying_func2) currying_func2 = curry(currying_func2) currying_func2(1)(2)(3).then(print) # ----------------------------------------- def promise_curried_func(resolve, reject, arg3): print(resolve, reject, arg3) promise_curried_func = curry(promise_curried_func) promise_curried_func = promise_curried_func(Promise(lambda resolve, reject: promise_curried_func(resolve, reject))) # promise_curried_func = promise_curried_func(1)(2)(3) # -----------------------------------------