Can't seem to get FJ's List.group() to work as expected (Scala's groupBy)

34 views
Skip to first unread message

andreak

unread,
Feb 28, 2012, 3:27:00 AM2/28/12
to functio...@googlegroups.com
In Scala you can do:

scala> List(1, 9, 9, 4, 4, 4, 9, 1, 9, 1, 1, 1, 9).groupBy(v => v);
res6: scala.collection.immutable.Map[Int,List[Int]] = Map(1 -> List(1, 1, 1, 1, 1), 4 -> List(4, 4, 4), 9 -> List(9, 9, 9, 9, 9))

And to do the same using FJ I'm trying this:

List<fj.data.List<Integer>> groupedList = array(1, 9, 9, 4, 4, 4, 9, 1, 9, 1, 1, 1, 9).toList().group(Equal.intEqual);

But that gives me:
for (List<Integer> integers : groupedList) {
    System.out.println(integers.index(0) + "\tcount: " + integers.length());
}

1    count: 1
9    count: 2
4    count: 3
9    count: 1
1    count: 1
9    count: 1
1    count: 3
9    count: 1

Im I wrong assuming FJ's group() working like Scala's groupBy?

Thanks to anyone shedding some light on this.

--
Andreas

Runar Bjarnason

unread,
Mar 3, 2012, 10:49:18 AM3/3/12
to functio...@googlegroups.com
FJ's group just groups together consecutive elements while Scala's
groupBy does the same thing, but sorts first.

Runar

> --
> You received this message because you are subscribed to the Google Groups
> "Functional Java" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/functionaljava/-/Vq5nSNNKKbwJ.
> To post to this group, send email to functio...@googlegroups.com.
> To unsubscribe from this group, send email to
> functionaljav...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/functionaljava?hl=en.

Reply all
Reply to author
Forward
0 new messages