Groovy Collect List of Lists - list

I have a list of lists similar to this:
[[NAME:JFK, ENUMBER:E12365576], [NAME:Connor Moore, ENUMBER:E12365575]]
I know that if i do:
data.collect {s -> s.eNumber}
I get:
["E12365576", "E12365575"]
What I want to end up with though is something like:
["E12365576 JFK", "E12365575 Connor Moore"]
//Or, If possible something like below
["E12365576 (JFK)", "E12365575 (Connor Moore)"]
I've been looking and haven't found something similar to help me figure it out. Any help is appreciated, thanks

data.collect {s -> "$s.ENUMBER ($s.NAME)" }
or, more precisely
data.collect { "$it.ENUMBER ($it.NAME)" }
by using implicit it

Related

Two action after then in Ocaml

Is it possible to make two actions after a then in Ocaml ?
I try to search and I found that I could use a semicolon.
Should I use it like this ? :
let test (a:int)=
if a = 0
then print_int(1);print_int(2)
else()
;;
It's just an example. In my case I want to launch a function and give a tuple like that :
let move_square(x,y:int*int):int*int=
..
let direction : int = Random.int(5);
if direction = 0
then draw_square(x,y+1);x,y+1
else ..
Thanks for helping me
You can refer to §Séquence of https://caml.inria.fr/pub/old_caml_site/FAQ/qrg-fra.html.
Generally you have to group ocaml statement in an if-then-else structure,
either by using explicitly beginand end keywords, or by using parenthesis to group your sequence.

How do I transform this code into a list comprehension?

I have this code in Python2.7 and I wanna (if possible) transform this code into a list comprehension.
z=[]
if p==1:
z.append('a')
if m==2:
z.append('b')
print ''.join(z)
The problem is it gives me an error (syntax error) when I transformed the code like this:
z=['b' if m==2 'a' if p==1]
print ''.join(z)
Or
z=['a' if p==1 'b' if ==m]
print ''.join(z)
Please let me know if this question has a duplicate.
I would appreciate your advice.
This is a tricky one. I came up with a solution that uses enumerate and an inline if statement to tell the difference between the two if statements. Honestly, using a list comp for this will probably obfuscate the code and it'd be better to just stick with the simpler if statements you already have.
values = ['a', 'b'] # put the append arguments in here, you can also inline this but I put it apart to make the line shorter
z = [val for idx, val in enumerate(values) if (m==2 and p==1 if idx==1 else p==1)]

How to change/modify strings different in a list

I don't know if this question has been asked before, but it seems that I can't find the function that I am looking for. I want to add two different string to different strings in a list. Something like this:
old_list: ['spider','cat','iron','super','bat']
old_list: ['spiderman','catwoman','ironman','superwoman','batman']
So I want some kind of function that changes the strings by adding 'man' or 'woman' without making a new list. I think/know it can be done with some kind of for-loop but can't seem to find a easy way of doing it. And I am sorry if this question has been asked before, but I can't really find an answer to this specific function.
Slice-assign back into the list.
>>> ['{}{}'.format(pref, suff) for (pref, suff) in zip(old_list, itertools.cycle(('man', 'woman')))]
['spiderman', 'catwoman', 'ironman', 'superwoman', 'batman']
>>> id(old_list)
43518144
>>> old_list[:] = ['{}{}'.format(pref, suff) for (pref, suff) in zip(old_list, itertools.cycle(('man', 'woman')))]
>>> id(old_list)
43518144
>>> old_list
['spiderman', 'catwoman', 'ironman', 'superwoman', 'batman']
Just for the sake of a simpler answer, I will post this one:
for i in range(len(old_list)):
old_list[i] += suffixes[i%len(suffixes)]
Note you can have any number of suffixes.

convert List[Tuple2[A,B]] to Tuple2[Seq[A],Seq[B]]

Stuck here, trying to convert a List of case class tuples to a tuple of sequences and multi-assign the result.
val items = repo.foo.list // gives me a List[(A,B)]
I can pull off multi-assignment like so:
val(a,b) = (items.map(_._1).toSeq, items.map(_._2).toSeq)
but it would be nicer to do in 1 step, along the lines of:
val(a,b) = repo.foo.list.map{case(a,b) => (a,b)}
I am not sure if I understood the question correctly. Maybe unzip works for what you want?
Here is a link with some examples: http://daily-scala.blogspot.de/2010/03/unzip.html
For a more general case you can look at product-collections. A CollSeqN is both a Seq[TupleN[A1..An]] and a TupleN[Seq[A1..An]]
In your example you could extract the Seqs like so:
items._1
items._2
...

Sort a list of lists in Groovy

Sorry if this is a straight forward thing, I'm new to Groovy. I'm trying to figure out how to sort this list on the "uses" key in each sub list, but I can't seem to get it figured out:
[[name:foo, uses:2], [name:bar, uses:1], [name:baz, uses:4]]
I'm hoping to get the following result:
[[name:baz, uses:4], [name:foo, uses:2], [name:bar, uses:1]]
Does someone out there know how to best handle this? I checked similar questions but couldn't find anything pertaining to Groovy.
Thanks in advance.
The easiest way to do it would be to use the sort method
def sorted = lists.sort( {a, b -> b["uses"] <=> a["uses"] } )
sorted.each {
println it
}
// prints
// [name:baz, uses:4]
// [name:foo, uses:2]
// [name:bar, uses:1]
I think I figured it out...
sort{a,b -> b['uses'] <=> a['uses']}
...seems to do the trick.
just wanted to add a shorter version
[[name:'foo', uses:2], [name:'bar', uses:1], [name:'baz', uses:4]].sort{-it.uses}​​​​​