[HN Gopher] Elixir-style pipelines in Ruby
       ___________________________________________________________________
        
       Elixir-style pipelines in Ruby
        
       Author : gregnavis
       Score  : 54 points
       Date   : 2022-12-10 14:47 UTC (8 hours ago)
        
 (HTM) web link (www.gregnavis.com)
 (TXT) w3m dump (www.gregnavis.com)
        
       | zelphirkalt wrote:
       | Do the steps of the pipeline implementation in Ruby run
       | concurrently?
       | 
       | I once did an Elixir course and really liked the pipelines. I
       | continued implementing pipelines with a Scheme macro, but not
       | concurrently.
        
         | Fire-Dragon-DoL wrote:
         | Elixir pipelines are not concurrent, unless you use something
         | that allows that (flow and probably stream behind the scenes)
        
         | woodruffw wrote:
         | Not naively: this kind of pipelining is partial function
         | application, not coordinated parallelism (the way Unix
         | pipeliens are).
        
           | AtlasBarfed wrote:
           | Does anyone know any documentation on how UNIX achieves
           | parallelism in pipelines, especially modern implementations?
           | Is each stage of a piped command just another spun off
           | process?
        
             | masklinn wrote:
             | > Is each stage of a piped command just another spun off
             | process?
             | 
             | Yes. With, as the name says, a pipe inbetween.
        
             | woodruffw wrote:
             | Yes, it's just that: each process in the pipeline is
             | spawned, and then blocks as it waits for input from the
             | previous step in the pipeline.
             | 
             | I think a better way to think about it is that the UNIX
             | model _requires_ parallelism: without parallelism, each
             | stage in the pipeline would need to fully buffer its
             | intermediate state before forwarding it to the next stage.
        
               | superdisk wrote:
               | > without parallelism, each stage in the pipeline would
               | need to fully buffer its intermediate state before
               | forwarding it to the next stage.
               | 
               | That's how DOS implemented the pipe syntax I believe.
        
       | bonquesha99 wrote:
       | Check out this proof of concept gem to perform pipe operations in
       | Ruby using block expressions:
       | 
       | https://github.com/lendinghome/pipe_operator#-pipe_operator
       | "https://api.github.com/repos/ruby/ruby".pipe do
       | URI.parse         Net::HTTP.get
       | JSON.parse.fetch("stargazers_count")         yield_self { |n|
       | "Ruby has #{n} stars" }         Kernel.puts       end       #=>
       | Ruby has 15120 stars
        
       | joeman1000 wrote:
       | I've had a look at threading/piping operators in a few languages
       | (list below). I'd say that Racket has the best one I've used. I
       | love that you can specify a '_' for the hole which the result
       | from the previous operation will fill. Julia's threading macro is
       | surprisingly brittle, only letting you chain single-argument
       | functions unless you want to use anonymous functions with one
       | bound variable and the rest free.
       | 
       | + Haskell ::
       | https://www.schoolofhaskell.com/user/Gabriel439/Pipes%20tuto...
       | 
       | + Racket :: https://docs.racket-lang.org/threading/index.html
       | 
       | + Clojure :: https://clojure.org/guides/threading_macros
       | 
       | + Julia :: https://syl1.gitbook.io/julia-language-a-concise-
       | tutorial/us...
       | 
       | + R :: https://r4ds.had.co.nz/pipes.html
        
         | nine_k wrote:
         | + F# :: https://fsharp.github.io/fsharp-core-
         | docs/reference/fsharp-c... -- only works with unary functions.
         | 
         | + Hack :: https://docs.hhvm.com/hack/expressions-and-
         | operators/pipe -- works with any functions, uses $$ for the
         | pipelined value.
         | 
         | OCaml also has a similar thing (@@) but I can't find a good
         | reference page.
        
       | bwilliams wrote:
       | The beauty (and horror) of Ruby is that you can do almost
       | anything with it. I think this is a really interesting and clever
       | use of the "can do anything" aspect of Ruby, although I think I'd
       | prefer not to run into it in a production app.
       | 
       | Still, it's really cool to see how far we can push/mold the
       | language to accomplish different tasks and patterns.
        
       | quechimba wrote:
       | I like using .then for chaining stuff, like this:
       | sig { params(obj: T.untyped).void }         def write(obj)
       | obj             .then { Array(_1) }             .then {
       | @wrapper.pack(_1) }             .then { @deflate.deflate(_1,
       | Zlib::SYNC_FLUSH) }             .then { @body.write(_1) }
       | end
        
       ___________________________________________________________________
       (page generated 2022-12-10 23:01 UTC)