3 ms·
Never mind the colors. This snippet demonstrates how a number of small tools, each doing its narrow job, strung together via the most trivial interface, produc
by nine_k 4y ago
Never mind the colors.
This snippet demonstrates how a number of small tools, each doing its narrow job, strung together via the most trivial interface, produces a non-trivial result.
This composability is still unreachable to the vast majority of GUI tools.
- kupopuffs 4y agoAh yes, the Unix Way
- deleted 4y ago[deleted]
- throwing_away 4y agoSaaS companies hate this one weird trick!
- vesinisa 4y agoThe non-trivial part here is actually the source data (the dict file.) It is also its pitfall - after adding 5 for S you should see a lithany of plurals. Most dict files (for English anyway) however seem to omit plural nouns. I guess the logic is that in English most plurals are regular, and the naive algorithm for deriving them from the singular forms (correctly most of the time) is quite trivial.
- miohtama 4y agoWhile it is a neat trick as one liner, I would recommend against doing anything like this in any software that requires maintenance. The code is hard, or impossible to follow, no comments. Brittle and only few people can understand what it really does. Better option would be 10 lines of Python or JavaScript with some comments.
- lrvick 4y agoI understood it instantly on first read. Probably depends on how much shell you write.
- rascul 4y agoComments can be added. Understanding it requires learning the tools. Just like understanding python or javascript requires learning python or javascript. It's not impossible to follow.
- kragen 4y agoI thought it was trivial to understand, though the comment above it helps a lot, and it's maybe an unfair advantage that I'd done the same thing in pretty much the same way four years ago. It probably depends on your background; I wouldn't write it that way for people who didn't know shell, just like I wouldn't write this comment in English for people who speak only Spanish. I'm not convinced that it's easier to understand in Python (even though I simplified it a bit, in part because one piece of the Python 3 braindamage was moving string.maketrans to bytes): import re def main(words): for word in words: word = word.strip().upper() if re.compile(r'[A-FOI]{6}$').match(word): print('#' + word.replace('I', '1').replace('O', '0')) if __name__ == '__main__': main(open('/usr/share/dict/words')) I think the shell version is clearly better for interactive improvisation, though.
- js2 4y agoI prefer search with an explicit '^' in the pattern to using match. For a throw-away script I'd probably do this: import re is_hex_like = re.compile(r"^[a-foi]{6}$", re.I).search for word in filter(is_hex_like, open("/usr/share/dict/words")): hexword = word.upper().replace("O", "0").replace("I", "1").rstrip() print(f"#{hexword}")
- kragen 4y agoThat's nicer than my version! I'm curious why you prefer search(), though.
- js2 4y ago1. I don't have to remember which implicitly anchors to the start of the string and which doesn't. 2. I prefer the explicitness of '^' (maybe that's just another way of stating (1). 3. I can use re.M to modify '^' to match at the start of each line on multiline strings, whereas match will still keep searching from the front. 4. The asymmetry of anchoring the front but not the end is weird. Python now has fullmatch, but ugh, just use the pattern for that if you need it. 5. Off the top of my head, I can't think of another language that has a regex function that implicitly anchors the front.