Вікіфункції:Як створювати реалізації
Ця сторінка надає більш детальні інструкції щодо того як створювати реалізації, ніж короткий огляд на сторінці Wikifunctions:Introduction.
Код на Python
Далі ми надаємо конкретний приклад того, як створювати Реалізації у вигляді Коду на Python. Скажімо, ми хочемо створити реалізацію Функції, яка комбінує два рядки вводу вставивши між ними пробіл, і повертає результат цього. Давайте припустимо, що ZID такої функції Z11057.
This description is only valid in case the implementation uses Strings and Booleans as the input or output types. In case another type is used, please consult the documentation on the given type on how to transform the values so they can be used in Python. We plan to make this easier in the future.
The implementation is defined using the ZID of the function. So are the arguments of the function. So in the case of the function Z11057 with its two arguments, the first line should look like this:
def Z11057(Z11057K1, Z11057K2):
Note: We plan to hide the ZIDs in the future.
The first line should be automatically created for you, once you have selected Python as the programming language. You only need to add the function body.
Below that, you write the code in Python as normal. It is just that the variables look a bit funny. In this case it could be, for example:
return Z11057K1 + " " + Z11057K2
Since this is Python, do not forget to indent this line.
If you already have Tests (and it’s a good habit to create the tests first), click on the circular arrow in the Тести panel and see if your implementation passes the tests.
Remember that the runtime has no state. Don’t assume values for global or static variables. If you have further questions about what you can and cannot do in Python, ask on Wikifunctions:Python implementations.
Код на JavaScript
In the following we give a concrete example on how to create an Implementation in the form of Code in JavaScript. Say we want to create an implementation for a Function which combines two input strings by putting a space between them, returning the result of that. Let’s assume the ZID of that function is Z11057.
This description is only valid in case the implementation uses Strings and Booleans as the input or output types. In case another type is used, please consult the documentation on the given type on how to transform the values so they can be used in JavaScript. We plan to make this easier in the future.
The implementation is defined using the ZID of the function. So are the arguments of the function. So in the case of the function Z11057 with its two arguments, the first line should look like this:
function Z11057( Z11057K1, Z11057K2 ){
Note: We plan to hide the ZIDs in the future.
The implementation should end with a line closing the opening parenthesis from the first line.
This should be automatically created for you, once you have selected JavaScript as the programming language. You only need to add the function body.
Inside of the braces, you write the code in JavaScript as normal. It is just that the variables look a bit funny. In this case it could be, for example:
return Z11057K1 + " " + Z11057K2;
If you already have Tests (and it’s a good habit to create the tests first), click on the circular arrow in the Тести panel and see if your implementation passes the tests.
Remember that the runtime has no state. Don’t assume values for global or static variables. If you have further questions about what you can and cannot do in JavaScript, ask on Wikifunctions:JavaScript implementations.
Композиція
In the following we give a concrete example on how to create an Implementation in the form of a composition. Say we want to create an implementation for a Function which combines two input strings by putting a space between them, returning the result of that. Let’s assume the ZID of that function is Z11057.
It is usually a good idea to first think about how to combine existing functions in order to create the desired output. Sometimes this might be trivial and you can just go ahead with composing functions, but in many cases it is worthwhile to note the desired composition down. The Wikifunctions composition interface is not very good yet at editing compositions, so it is a good idea to first figure out what exactly you want to create.
For example, for the given Function, we could use the existing Function to concatenate strings. That Function takes two strings and makes one string out of them. But we need to add a space in between. So we first concatenate a space to the end of the first string, and then concatenate the second string to the result of that first concatenation. So our composition could be noted down like this:
concatenate(concatenate(Z11057K1, " "), Z11057K2).
In order to create this:
- Click on the red Select Function.
- Enter the name of the Function we want to select, i.e. “concatenate” and select that.
- The first argument of that outer concatenation is a function call itself. In order to do that, click on the > next to First.
- Now click on the value of the Type of First. It currently says String.
- You will see four options. Select Function call.
- Now, select the inner Function. That is, again, concatenate.
- The argument First of the inner concatenate needs to be our first argument. So click again on the > next to First.
- Click on the dropdown box showing String.
- Select Argument reference.
- Enter the Key ID of the first argument, which in this case is Z11057K1 (you can look it up on the function page). In the future, this will become a drop down box.
- In the inner field Second (not the outer one), type in a single space character. This will be basically invisible.
- If you already have Tests (and it’s a good habit to create the tests first), click on the circular arrow in the Тести panel and see if your implementation passes the tests.
- Now add the second argument. For the outer Second argument, select the > next to it.
- Click on the drop down box saying String.
- Select Argument reference.
- Enter the Key ID of the second argument, which in this case is Z11057K2 (you can look it up on the function page). In the future, this will become a drop down box.
- If you already have Tests (and it’s a good habit to create the tests first), click on the circular arrow ⎋ in the Тести panel and see if your implementation passes the tests.
If the composition passes the test, you can publish it.