Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Source.SplitAfter Akka example extra output #3222

Closed
vasily-kirichenko opened this issue Dec 22, 2017 · 5 comments · Fixed by #3242
Closed

A Source.SplitAfter Akka example extra output #3222

vasily-kirichenko opened this issue Dec 22, 2017 · 5 comments · Fixed by #3242

Comments

@vasily-kirichenko
Copy link
Contributor

vasily-kirichenko commented Dec 22, 2017

I'm trying to port an example from https://doc.akka.io/docs/akka/snapshot/stream/stream-substream.html

use system = ActorSystem.Create("test")
let mat = system.Materializer()

let text = "This is the first line.\nThe second line.\nThere is also the 3rd line\n"

Source.From(text.ToCharArray())
    .SplitAfter(fun x -> x = '\n')
    .Where(fun x -> x <> '\n')
    .Select(fun _ -> 1)
    .Aggregate(0, fun s x -> s + x)
    .To(Sink.ForEach(fun x -> Console.WriteLine (sprintf "%A" x)))
    .Run(mat)
    |> ignore
       
Console.ReadKey() |> ignore

As the docs says, it should output

23
16
26

but it outputs

26
0
16
23

Where does the 0 come from?

@vasily-kirichenko vasily-kirichenko changed the title A Source.SplitAfter Akka example does not work A Source.SplitAfter Akka example extra output Dec 22, 2017
@vasily-kirichenko
Copy link
Contributor Author

If I replace SplitAfter with SplitWhen, the result is not changed.

@vasily-kirichenko
Copy link
Contributor Author

A simpler example.

Akka

val xs =
    List(
      Some(1), Some(2), None,
      Some(3), Some(4), None,
      Some(5), None)

  Source(xs)
    .splitAfter(_.isEmpty)
    .filter(_.isDefined)
    .fold(List[Int]())((x, r) => r.get +: x)
    .to(Sink.foreach(println))
    .run()
List(4, 3)
List(5)
List(2, 1)

Akka.NET

use system = ActorSystem.Create("test")
    let mat = system.Materializer()
    
    let xs =
        [ Ok 1; Ok 2; Error 0
          Ok 3; Ok 4; Error 0
          Ok 5; Error 0 ]
    
    Source.From(xs)
        .SplitAfter(fun x -> match x with Ok _ -> false | _ -> true)
        .Where(fun x -> match x with Ok _ -> true | _ -> false)
        .Aggregate([], fun r (Ok x) -> x :: r)
        .To(Sink.ForEach(fun x -> printfn "%A" x))
        .Run(mat)
        |> ignore
[]
[5]
[4; 3]
[2; 1]

(here I use F# Result type because it's a struct whereas Option is a ref type and None is represented as null, which causes Akka.NET crashes at runtime, see #3223).

@marcpiechura
Copy link
Contributor

marcpiechura commented Jan 1, 2018

Problem ist that SplitAfter creates 4 substreams, the last one is empty an no element get's pushed but the Aggregate stage pushes the aggration when upstream finishes and since the aggregate function wasn't called for this substream it pushes the zero element downstream, i.e. the empty list. Need to investigate further why SplitAfter creates a stream too much.

@marcpiechura
Copy link
Contributor

@vasily-kirichenko should be fixed in the next release, thx for reporting.

@vasily-kirichenko
Copy link
Contributor Author

Thank you very much :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants