• rtxn@lemmy.worldM
    link
    fedilink
    English
    arrow-up
    24
    ·
    8 months ago

    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.

    • PoolloverNathan
      link
      fedilink
      arrow-up
      6
      ·
      8 months ago

      ↑ 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.

        • PoolloverNathan
          link
          fedilink
          arrow-up
          3
          ·
          8 months ago

          interact is (StringString) → IO (), a function that takes a StringString (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.