Groovy addAll makes a list of lists - list

this might not be the best question but I am curious about what might be going on.
I have this piece of code in groovy
def markets = []
for (int i = 0; i < marketIds.size(); i += 200) {
int from = i
int to = (i + 200) > marketIds.size() ? marketIds.size() : (i + 200)
markets.addAll(service.getMarketCatalog(args...))
}
markets
service.getMarketCatalog() returns a list of objects/maps (it's a call to 3rd part API that returns a list of some objects defined in their API. We get them as maps).
For some reason the result of this method is markets being a list of lists.
So if the for loop runs 3 times and the API responds each time with a list, the list is added to the markets as an object.
Anyone have any ideas why? Maybe I have to define that the result is a list (i.e. service.getMarketCatalog(args...) as List) otherwise it is treated as an object and just gets added as an object to the list?

Judging by my own test...
Object x() { return [1, 2] }
def y = []
y.addAll(x())
y.addAll(x())
println y
Result: [1, 2, 1, 2]
... no matter what the return type of service.getMarketCatalog() is, even if it's just Object, if the value is truly a List or other Collection, then addAll has the expected behavior. I get a warning in the IDE, but Groovy evidently tries casting the Object to a Collection at runtime and succeeds.
Thus I can only conclude that service.getMarketCatalog() is not actually returning a single-level list as you believe it is. It must be a list of lists, itself. You'll have to investigate whether that's truly the case. If it is, then of course you can spread the top-level list to addAll each of the sublists: markets.addAll(*service.getMarketCatalog(args...))

Related

Difference between List.subList and slice in Kotlin

I recently realised there are two very similar functions in Kotlin for getting a portion of a List, but I'm unsure of the difference:
The documentation for List.subList says:
Returns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive). The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
Structural changes in the base list make the behavior of the view undefined.
whereas the documentation for slice says:
Returns a list containing elements at indices in the specified indices range.
Or
Returns a list containing elements at specified indices.
It seems that the key differences are that the first one returns a "view of the portion" of the list, and whether non-structural changes are reflected? However I'm not quite sure what this means.
I looked at the source code for the slice function:
public fun <T> List<T>.slice(indices: IntRange): List<T> {
if (indices.isEmpty()) return listOf()
return this.subList(indices.start, indices.endInclusive + 1).toList()
}
But it returns a list from the subList function.
Could someone explain the differences between these two functions and when you might want to use one over the other?
The key in List<T>.slice function is the .toList() at the end.
The call to toList() will create a new List with all the elements, like a copy.
For summary:
.slice() will create a new List with the subset of elements
.subList() is only a view of the original List that will change with it.
You can see differences here: https://pl.kotl.in/-JU8BDNZN
fun main() {
val myList = mutableListOf(1, 2, 3, 4)
val subList = myList.subList(1, 3)
val sliceList = myList.slice(1..2)
println(subList) // [2, 3]
println(sliceList) // [2, 3]
myList[1] = 5
println(subList) // [5, 3]
println(sliceList) // [2, 3]
}

OCaml return value in if statement nested in loop

I am trying to return a value if something occurs when iterating through a list. Is it possible to return a string if X happens when iterating through the list, otherwise return another string if it never happens?
let f elem =
if not String.contains str elem then "false" in
List.iter f alphlist;
"true";
This is not working in my implemented method sadly.
OCaml is a functional language, so you pretty much need to concentrate on the values returned by functions. There are ways to return different values in exceptional cases, but (IMHO) the best way to learn is to start just with ordinary old nested function calls.
List.iter always returns the same value: (), which is known as unit.
For this reason, the expression List.iter f alphlist will also always return () no matter what f does.
There is another kind of list-handling function that works by maintaining a value across all the calls and returning that value at the end. It's called a fold.
So, if you want to compute some value that's a kind of summary of what it saw in all of the string lists in alphlist, you should probably be using a fold, say List.fold_left.
Here is a function any_has_7 that determines whether any one of the specified lists contains the integer 7:
let any_has_7 lists =
let has_7 sofar list =
sofar || List.mem 7 list
in
List.fold_left has_7 false lists
Here's how it looks when you run it:
# any_has_7 [[1;2]; [3;4]];;
- : bool = false
# any_has_7 [[1;2]; [5;7]; [8;9]];;
- : bool = true
In other words, this function does something a lot like what you're asking for. It returns true when one or more of the lists contains a certain value, and false when none of them contains the value.
I hope this helps.

Scala - using a list of ints as an index to populate a new data structure

I have a list of Objects (Items, in this case) which have category ids and properties (which itself is a list of custom types).
I am trying to def a function that takes a list of integers e.g. List(101, 102, 102, 103, 104) that correspond to the category ids for the Items and creates a list of tuples that include the category type (which is an Option) and each property type from a list of properties that go along with each category. So far I have the below, but I am getting an error that value _2 is not a member of Product with Serializable.
def idxToData(index: List[Int], items: Seq[Item]): List[(Option[Category], Property[_])] = {
def getId(ic: Option[Category]): Int => {
ic match {
case Some(e) => e._id
case None => 0
}
}
index.flatMap(t => items.map(i => if(t == getId(i.category)){
(i.category, i.properties.list.map(_.property).toList.sortWith(_._id < _._id))
} else {
None
}.filter(_ != None )
))
.map(x => x._2.map(d => (x._1, d)))
.toList
}
I am not sure how it is assigning that type (I am assuming at that point that I should have a list of tuples that I am trying to map).
Overall, is there a better way in scala to achieve the desired result from taking in a list of indices and using that to access the specific items in a list where a tuple of two parts of each corresponding item would "replace" the index to create the new list structure?
You should split your code, give names to things (add some vals and some defs), and when the compiler does not agree with you, write types, so that the compiler will tell you early where it disagrees (don't worry, we all did that when starting with FP)
Also, when posting such a question, you might want to give (relevant parts of) the interface of elements that are referenced but not defined. What are "is" (is that items?), Item, category, properties...., or simplify your code so that they do not appear.
Now, to the problem :
if(t == (i.category match { case Some(e) => e._id})){
(i.category, i.properties.list.map(_.property).toList.sortWith(_._id < _._id))
} else {
None
}
The first branch is the type Tuple2(Int, whatever) while the second branch is of the completely unrelated type None. Clearly, there is no common super type better than AnyRef, so that is the type of the if expression. Then the type of is.map (supposing is is some sort of Seq) will be Seq[AnyRef]. filter does not change the type, so still Seq[AnyRef], and in the map(x =>...), x is an AnyRef too, not a Tuple2, so it has no _2.
Of course, the list actually contains only tuples, because originally it had tuples and Nones and you have removed the Nones. But that was lost to the compiler when it typed that AnyRef.
(as the compiler error message tells and as noted by Imm, the compiler finds a slightly more precise type than AnyRef, Product with Serializable; however, that will not do you any good, all of the useful typing information is still lost there).
To preserve the type, in general you should do something such as
if(....) {
Some(stuff)
else
None
That would have been typed Option[type of stuff], where type of stuff is your Pair.
However, there is something simpler with routine collect.
It is a bit like match, except that it takes a partial function, and it discard elements for which the partial function is not defined.
So that would be
is.collect { case i if categoryId(i) == Some(t) =>
(i.catetory, i.properties....)
}
supposing you have defined
def categoryId(item: Item): Option[Int] = item.category.map(._id)
When you do this:
is.map(i => if(t == getId(i.category)){
(i.category, i.properties.list.map(_.property).toList.sortWith(_._id < _._id))
} else {
None
}
you get a List[Product with Serializable] (what you should probably get is a type error, but that could be a long digression), because that's the only supertype of None and (Category, List[Property[_]]) or whatever that tuple type is. The compiler isn't smart enough to carry the union type through and figure out that when you filter(_ != None) anything left in the list must be the tuple.
Try to rephrase this part. E.g. you could do is.filter(i => t == getId(i.category)) first, before the map, and then you wouldn't need to mess around with Nones in your list.

Recursive function that returns all values in list (In OCaml)

I need a function that recursively returns (not prints) all values in a list with each iteration. However, every time I try programming this my function returns a list instead.
let rec elements list = match list with
| [] -> []
| h::t -> h; elements t;;
I need to use each element each time it is returned in another function that I wrote, so I need these elements one at a time, but I can't figure this part out. Any help would be appreciated.
Your function is equivalent to :
let rec elements list =
match list with
| [] -> []
| h :: t -> elements t
This happens because a ; b evaluates a (and discards the result) and then evaluates and returns b. Obviously, this is in turn equivalent to:
let elements (list : 'a list) = []
This is not a very useful function.
Before you try solving this, however, please understand that Objective Caml functions can only return one value. Returning more than one value is impossible.
There are ways to work around this limitation. One solution is to pack all the values you wish to return into a single value: a tuple or a list, usually. So, if you need to return an arbitrary number of elements, you would pack them together into a list and have the calling code process that list:
let my_function () = [ 1 ; 2; 3; 4 ] in (* Return four values *)
List.iter print_int (my_function ()) (* Print four values *)
Another less frequent solution is to provide a function and call it on every result:
let my_function action =
action 1 ;
action 2 ;
action 3 ;
action 4
in
my_function print_int
This is less flexible, but arguably faster, than returning a list : lists can be filtered, sorted, stored...
Your question is kind of confusing - you want a function that returns all the values in a list. Well the easiest way of returning a variable number of values is using a list! Are you perhaps trying to emulate Python generators? OCaml doesn't have anything similar to yield, but instead usually accomplishes the same by "passing" a function to the value (using iter, fold or map).
What you have currently written is equivalent to this in Python:
def elements(list):
if(len(list) == 0):
return []
else:
list[0]
return elements(list[1:])
If you are trying to do this:
def elements(list):
if(len(list) > 0):
yield list[0]
# this part is pretty silly but elements returns a generator
for e in elements(list[1:]):
yield e
for x in elements([1,2,3,4,5]):
dosomething(x)
The equivalent in OCaml would be like this:
List.iter dosomething [1;2;3;4;5]
If you are trying to determine if list a is a subset of list b (as I've gathered from your comments), then you can take advantage of List.mem and List.for_all:
List.for_all (fun x -> List.mem x b) a
fun x -> List.mem x b defines a function that returns true if the value x is equal to any element in (is a member of) b. List.for_all takes a function that returns a bool (in our case, the membership function we just defined) and a list. It applies that function to each element in the list. If that function returns true for every value in the list, then for_all returns true.
So what we have done is: for all elements in a, check if they are a member of b. If you are interested in how to write these functions yourself, then I suggest reading the source of list.ml, which (assuming *nix) is probably located in /usr/local/lib/ocaml or /usr/lib/ocaml.

Scala: Remove duplicates in list of objects

I've got a list of objects List[Object] which are all instantiated from the same class. This class has a field which must be unique Object.property. What is the cleanest way to iterate the list of objects and remove all objects(but the first) with the same property?
list.groupBy(_.property).map(_._2.head)
Explanation: The groupBy method accepts a function that converts an element to a key for grouping. _.property is just shorthand for elem: Object => elem.property (the compiler generates a unique name, something like x$1). So now we have a map Map[Property, List[Object]]. A Map[K,V] extends Traversable[(K,V)]. So it can be traversed like a list, but elements are a tuple. This is similar to Java's Map#entrySet(). The map method creates a new collection by iterating each element and applying a function to it. In this case the function is _._2.head which is shorthand for elem: (Property, List[Object]) => elem._2.head. _2 is just a method of Tuple that returns the second element. The second element is List[Object] and head returns the first element
To get the result to be a type you want:
import collection.breakOut
val l2: List[Object] = list.groupBy(_.property).map(_._2.head)(breakOut)
To explain briefly, map actually expects two arguments, a function and an object that is used to construct the result. In the first code snippet you don't see the second value because it is marked as implicit and so provided by the compiler from a list of predefined values in scope. The result is usually obtained from the mapped container. This is usually a good thing. map on List will return List, map on Array will return Array etc. In this case however, we want to express the container we want as result. This is where the breakOut method is used. It constructs a builder (the thing that builds results) by only looking at the desired result type. It is a generic method and the compiler infers its generic types because we explicitly typed l2 to be List[Object] or, to preserve order (assuming Object#property is of type Property):
list.foldRight((List[Object](), Set[Property]())) {
case (o, cum#(objects, props)) =>
if (props(o.property)) cum else (o :: objects, props + o.property))
}._1
foldRight is a method that accepts an initial result and a function that accepts an element and returns an updated result. The method iterates each element, updating the result according to applying the function to each element and returning the final result. We go from right to left (rather than left to right with foldLeft) because we are prepending to objects - this is O(1), but appending is O(N). Also observe the good styling here, we are using a pattern match to extract the elements.
In this case, the initial result is a pair (tuple) of an empty list and a set. The list is the result we're interested in and the set is used to keep track of what properties we already encountered. In each iteration we check if the set props already contains the property (in Scala, obj(x) is translated to obj.apply(x). In Set, the method apply is def apply(a: A): Boolean. That is, accepts an element and returns true / false if it exists or not). If the property exists (already encountered), the result is returned as-is. Otherwise the result is updated to contain the object (o :: objects) and the property is recorded (props + o.property)
Update: #andreypopp wanted a generic method:
import scala.collection.IterableLike
import scala.collection.generic.CanBuildFrom
class RichCollection[A, Repr](xs: IterableLike[A, Repr]){
def distinctBy[B, That](f: A => B)(implicit cbf: CanBuildFrom[Repr, A, That]) = {
val builder = cbf(xs.repr)
val i = xs.iterator
var set = Set[B]()
while (i.hasNext) {
val o = i.next
val b = f(o)
if (!set(b)) {
set += b
builder += o
}
}
builder.result
}
}
implicit def toRich[A, Repr](xs: IterableLike[A, Repr]) = new RichCollection(xs)
to use:
scala> list.distinctBy(_.property)
res7: List[Obj] = List(Obj(1), Obj(2), Obj(3))
Also note that this is pretty efficient as we are using a builder. If you have really large lists, you may want to use a mutable HashSet instead of a regular set and benchmark the performance.
Starting Scala 2.13, most collections are now provided with a distinctBy method which returns all elements of the sequence ignoring the duplicates after applying a given transforming function:
list.distinctBy(_.property)
For instance:
List(("a", 2), ("b", 2), ("a", 5)).distinctBy(_._1) // List((a,2), (b,2))
List(("a", 2.7), ("b", 2.1), ("a", 5.4)).distinctBy(_._2.floor) // List((a,2.7), (a,5.4))
Here is a little bit sneaky but fast solution that preserves order:
list.filterNot{ var set = Set[Property]()
obj => val b = set(obj.property); set += obj.property; b}
Although it uses internally a var, I think it is easier to understand and to read than the foldLeft-solution.
A lot of good answers above. However, distinctBy is already in Scala, but in a not-so-obvious place. Perhaps you can use it like
def distinctBy[A, B](xs: List[A])(f: A => B): List[A] =
scala.reflect.internal.util.Collections.distinctBy(xs)(f)
With preserve order:
def distinctBy[L, E](list: List[L])(f: L => E): List[L] =
list.foldLeft((Vector.empty[L], Set.empty[E])) {
case ((acc, set), item) =>
val key = f(item)
if (set.contains(key)) (acc, set)
else (acc :+ item, set + key)
}._1.toList
distinctBy(list)(_.property)
One more solution
#tailrec
def collectUnique(l: List[Object], s: Set[Property], u: List[Object]): List[Object] = l match {
case Nil => u.reverse
case (h :: t) =>
if (s(h.property)) collectUnique(t, s, u) else collectUnique(t, s + h.prop, h :: u)
}
I found a way to make it work with groupBy, with one intermediary step:
def distinctBy[T, P, From[X] <: TraversableLike[X, From[X]]](collection: From[T])(property: T => P): From[T] = {
val uniqueValues: Set[T] = collection.groupBy(property).map(_._2.head)(breakOut)
collection.filter(uniqueValues)
}
Use it like this:
scala> distinctBy(List(redVolvo, bluePrius, redLeon))(_.color)
res0: List[Car] = List(redVolvo, bluePrius)
Similar to IttayD's first solution, but it filters the original collection based on the set of unique values. If my expectations are correct, this does three traversals: one for groupBy, one for map and one for filter. It maintains the ordering of the original collection, but does not necessarily take the first value for each property. For example, it could have returned List(bluePrius, redLeon) instead.
Of course, IttayD's solution is still faster since it does only one traversal.
My solution also has the disadvantage that, if the collection has Cars that are actually the same, both will be in the output list. This could be fixed by removing filter and returning uniqueValues directly, with type From[T]. However, it seems like CanBuildFrom[Map[P, From[T]], T, From[T]] does not exist... suggestions are welcome!
With a collection and a function from a record to a key this yields a list of records distinct by key. It's not clear whether groupBy will preserve the order in the original collection. It may even depend on the type of collection. I'm guessing either head or last will consistently yield the earliest element.
collection.groupBy(keyFunction).values.map(_.head)
When will Scala get a nubBy? It's been in Haskell for decades.
If you want to remove duplicates and preserve the order of the list you can try this two liner:
val tmpUniqueList = scala.collection.mutable.Set[String]()
val myUniqueObjects = for(o <- myObjects if tmpUniqueList.add(o.property)) yield o
this is entirely a rip of #IttayD 's answer, but unfortunately I don't have enough reputation to comment.
Rather than creating an implicit function to convert your iteratble, you can simply create an implicit class:
import scala.collection.IterableLike
import scala.collection.generic.CanBuildFrom
implicit class RichCollection[A, Repr](xs: IterableLike[A, Repr]){
def distinctBy[B, That](f: A => B)(implicit cbf: CanBuildFrom[Repr, A, That]) = {
val builder = cbf(xs.repr)
val i = xs.iterator
var set = Set[B]()
while (i.hasNext) {
val o = i.next
val b = f(o)
if (!set(b)) {
set += b
builder += o
}
}
builder.result
}
}