2

I have a file, which is formatted like this:

header line 1
header line 2
header line 3

line 1
line 2
line 3
...

I need to get Iterable<String> with those lines (line 1, line 2, line 3 etc.), after the header. How to achieve that with the help of Cactoos? The header is separated by the blank line.

I was able to skip the header lines with this code:

new Skipped<>(
    new SplitText(
        new TextOf(this.path),
        "\n"
    ),
    4
);

Now how to map the Iterable<Text> with the skipped header to the Iterable<String>?

I'm trying to do it with this code, which doesn't work:

new Mapped<>(
    new FuncOf<>(
        input -> input.asString()
    ),
    new Skipped<>(
        new SplitText(
            new UncheckedText(
                new TextOf(
                    this.path
                )
            ),
            "\n"
        ),
        4
    )
);
Izbassar Tolegen
  • 1,990
  • 2
  • 20
  • 37

1 Answers1

1

When you call:

new FuncOf<>(
    input -> input.asString()
)

You're actually calling FuncOf<>(final Y result), which is not what you want.

Try providing a lambda as an implementation of Func instead. For example, the following works:

@Test
public void test() {
    final String text =
        "header line 1\n" +
        "header line 2\n" +
        "header line 3\n" +
        "\n" +
        "line 1\n" +
        "line 2\n" +
        "line 3";
    MatcherAssert.assertThat(
        new Mapped<>(
            txt -> txt.asString(),
            new Skipped<>(
                new SplitText(
                    text,
                    "\n"
                ),
                4
            )
        ),
        Matchers.contains("line 1", "line 2", "line 3")
    );
}
George Aristy
  • 1,373
  • 15
  • 17