def tokens(code_str, xmap_str): lines = fp.go( code_str, F.curry(F.partition_by)(lambda s: s == '\n'), fp.lmap(''.join), # Ignore newlines in beginning of source code lambda xs: F.rest(xs) if '\n' in xs[0] else xs, # join ['source line', '\n\n\n'] F.curry(F.chunks)(2), fp.lmap(''.join), ) slice_idxs = fp.go( xmap_str.splitlines(), fp.map(fp.pipe( lambda s: s.strip(), lambda s: s.split(), fp.map(int), fp.lmap(fp.dec), # Make tokens from the beginning of the line lambda xs: [0] + xs[1:] if xs else xs, )), fp.remove(fp.is_empty) ) return fp.lmapcat( fp.lsplit_with, slice_idxs, lines )
함수형 파이썬