Last active
August 29, 2015 14:13
-
-
Save Shchvova/83313d746af8e229873f to your computer and use it in GitHub Desktop.
momen I realized that I just reinvented LINQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
if __name__ == '__main__': | |
with open(sys.argv[1]) as f: | |
lines = f.readlines() | |
lines = map(str.strip, lines) #stripping endlines | |
lines = filter(bool, lines) #stripping empty lines | |
lines = filter(lambda l: ':' in l, lines) #filter out strings without : | |
lines = map(lambda l: l.split(':', 1), lines) | |
lines = map(lambda l: "=".join(l), lines) | |
print "\n".join(lines) | |
#So I wanted to chain this in monadic style. Pattern is obvious. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
class Monadinize(): | |
def __init__(self, l): | |
self.l = l | |
def map(self, f, *args): | |
self.l = map(f, self.l, *args) | |
return self | |
def filter(self, f): | |
self.l = filter(f, self.l) | |
return self | |
def done(self): | |
return self.l | |
if __name__ == '__main__': | |
with open(sys.argv[1]) as f: | |
out = Monadinize(f.readlines()) \ | |
.map(str.strip) \ | |
.filter(bool) \ | |
.filter(lambda l: ':' in l) \ | |
.map(lambda l: l.split(':', 1)) \ | |
.map(lambda l: "=".join(l)) \ | |
.done() | |
print "\n".join(out) | |
# and then I found https://code.google.com/p/asq/ :D | |
# also, honourable mention to https://github.com/JulienPalard/Pipe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment