I feel the same way about Haskell. Every program I’ve used is either a “Look at what else Haskell can do!” example, or an endorsement of universal packages whenever I have to update 200 haskell modules.
↑ This. Haskell makes it super easy to get good CLI filters. All you need to do is interact and process the string it gives you. You’ll automatically get streaming behavior because of laziness without lifting a finger.
interact is (String → String) → IO (), a function that takes a String → String (a function that takes a string and returns a string) and returns an I/O operation (which is a separate type since Haskell doesn’t have side-effects). The function you give it will receive all of stdin as a string and its output will be stdout. The magic comes because Haskell uses cons-lists that are lazy in their spine — the list doesn’t actually exist until you look at it. This means that, from your perspective (probably not how this is actually implemented), the list you return is iterated character-by-character, and each character that gets printed only waits for the characters it needs, allowing the rest of the stdin list to remain unevaluated.
Let me guess: Haskell has become that language where you can write anything you want with only three lines of code, provided you use that definitions provided by someone else who did the same thing last month from a third person’s code, etc, back to 1990.
I feel the same way about Haskell. Every program I’ve used is either a “Look at what else Haskell can do!” example, or an endorsement of universal packages whenever I have to update 200 haskell modules.
↑ This. Haskell makes it super easy to get good CLI filters. All you need to do is
interact
and process the string it gives you. You’ll automatically get streaming behavior because of laziness without lifting a finger.Huh. My brain is on fire right now.
interact
is(String → String) → IO ()
, a function that takes aString → String
(a function that takes a string and returns a string) and returns an I/O operation (which is a separate type since Haskell doesn’t have side-effects). The function you give it will receive all of stdin as a string and its output will be stdout. The magic comes because Haskell uses cons-lists that are lazy in their spine — the list doesn’t actually exist until you look at it. This means that, from your perspective (probably not how this is actually implemented), the list you return is iterated character-by-character, and each character that gets printed only waits for the characters it needs, allowing the rest of the stdin list to remain unevaluated.Huh. My brain is on fire right now.
Let me guess: Haskell has become that language where you can write anything you want with only three lines of code, provided you use that definitions provided by someone else who did the same thing last month from a third person’s code, etc, back to 1990.