allenwyma

allenwyma OP

I’m quite stuck at something I thought would be quite straight forward, but it seems maybe this topic hasn’t come up much in my career in Elixir.

I am trying to run a test to check STOUT and test STDIN for a function:

test "can read and output" do
    input =
    """
    3
    Hello
    Hello World
    Hello A B World
    """
    assert ExUnit.CaptureIO.capture_io(fn ->
             IO.write(input)
            task = Task.async(Example, :start, [])
            Task.await(task)
           end) == """
    3
    Hello
    Hello World
    Hello B A World
    """
  end

Within my code I’ve been trying to read from stdin, but I keep getting :eof when I used IO.read(:line) or ""if I tryIO.read(:all)`.

I’m pretty sure I must be close, but I am missing something somewhere. The reason I’m launching a task is because I’m pretty sure the code will block so I’m unable to write to :stdio.

here’s my code for the function:

def start(pid \\ :stdio) do
    number_of_lines =
      IO.read(pid, :read)
      |> String.trim()
      |> String.to_integer()
    Enum.each(0..number_of_lines, fn _i ->
      sentence =
        IO.read(pid, :line)
        |> IO.inspect()
        |> String.trim()
        |> String.split()
      sentence_to_write =
        if length(sentence) > 2 do
          [hd | tail] = sentence
          {last, rest} = List.pop_at(tail, length(tail))
          new_tail = Enum.reverse(rest) ++ [last]
          [hd | new_tail]
        else
          sentence
        end
      sentence_to_write
      |> Enum.reduce(fn word, acc ->
        acc <> " " <> word
      end)
      |> IO.write(pid)
    end)
  end

The there’s a pid there is I was also thinking I can create a pid that responds to the message protocol for writing to a device, but I kind of gave up around there, thinking there must be a way to do this without creating another process.

Does anyone have any idea about how I can achieve this?

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

WilBarrios
Who this resource is by Created by Wilfredo Barrios as part of the open-source Coding5s Framework. Short description I’m sharing two fre...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement