是的,绝对有可能做到这一点。我已经用 Python 编写了一个完整的工作解决方案。
首先,我用不同的编程语言创建了一组对应的函数:
correspondingFunctionDatabase = [
[
["Java", "System.out.println"], ["JavaScript", "console.log"], ["Python", "print"],
],
[
["Java", "s1.replaceAll(str1, str2);"], ["JavaScript", "str.replace(str1, str2)"], ["Python", "str.replace(str1, str2)"], ["Haxe", "replace(str, str1, str2)"]
],
[
["JavaScript", "str.split(separator)"], ["Java", "str.split(separator)"], ["Python", "re.split(separator, string)"]
],
[
["JavaScript", "eval(statement)"], ["Python", "eval(statement)"]
]
]
接下来,我编写了一个名为 的函数getCorrespondingValue
,它将返回一种语言的函数,该函数对应于另一种语言的函数:
def containsAll(containingArray, containsAllOfThese):
i = 0
while(i < len(containsAllOfThese)):
if((containsAllOfThese[i] in containingArray) == False):
return False
i += 1
return True
def getCorrespondingValue(valueToRetrieve, valueCorrespondsTo, mainArray=correspondingFunctionDatabase, printMessages=False):
i = 0
correspondingValues = []
while(i < len(mainArray)):
if(containsAll(containsAllOfThese=valueCorrespondsTo, containingArray = mainArray[i])):
if(printMessages == True):
print("The elements of the array:\n", valueCorrespondsTo)
print("Are contained in the array:\n", str(mainArray[i])+"\n")
j = 0
while(j < len(mainArray[i])):
if(mainArray[i][j][0] == valueToRetrieve):
print(mainArray[i][j])
correspondingValues += [mainArray[i][j][1]]
j += 1
i += 1
print("The retrieved values for", valueToRetrieve, "from", valueCorrespondsTo, "are", correspondingValues)
return correspondingValues
以下是使用此功能的几个示例:
'''
This function call returns the Python function that corresponds to System.out.println in Java.
'''
print(getCorrespondingValue("Python", [["Java", "System.out.println"]]))
'''
This function call returns the Java function that corresponds to console.log in JavaScript.
'''
print(getCorrespondingValue("Java", [["JavaScript", "console.log"]]))