Let me try to clarify it for you. lambda
is a shorter way of writing functions, and all of the following are equivalent ways of writing it. For example,
f = lambda x: 2*x
is similar to python’s def:
def f(x):
return 2*x
which is similar to java’s way of defining functions:
float f(float x) {
return 2*x;
}
So, the function lambda that you got here:
lambda (r,g,b): step(r,g,b,8)
is similar to:
def f(tuple):
return step(tuple[0],tuple[1],tuple[2],8)
Sort is a list method that usually modifies a list so that its elements are in increasing order, like from lowest to highest, or from A to Z. For instance, in python, if you tried to sort the following list:
names = ["Stephanie","Sarah","Abe","Al"]
names.sort()
you would get them back in alphabetical order:
['Abe', 'Al', 'Sarah', 'Stephanie']
But what if you wanted to sort them by their age instead? Let’s assume you have the following:
names = ["Stephanie","Sarah","Abe","Al"]
age = {"Stephanie":26,
"Sarah":28,
"Abe":25,
"Al":24}
Then I would want to sort the names based on their ages, which you can do by setting the key argument as follows:
names = ["Stephanie","Sarah","Abe","Al"]
age = {"Stephanie":26,
"Sarah":28,
"Abe":25,
"Al":24}
names.sort(key=lambda x: age[x])
print(names)
which returns:
['Al', 'Abe', 'Stephanie', 'Sarah']
key
argument kind of maps a function to each element in the list. In this case, I used a lambda function because I needed it for a simple purpose.
Going back to the original topic to translating it to Java, you essentially need to sort a list of tuples of (R,G,B) colors based on the order you would get if you had sorted a list of those same colors after performing step()
function on the elements of that list. I’m sure gotoloop has suggestions on how to do that in java.