Wikifunctions:How to create implementations
This page provides a more detailed guide to creating implementations, beyond the overview at Wikifunctions:Introduction.
Code in Python
In this section, we give a concrete example on how to create an Implementation in the form of Code in Python. 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 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 Tests 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.
Code in JavaScript
In this section, 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’s code must end with a line closing the opening curly brace 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 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 Tests 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.
Composition
In this section, 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:
- Start adding an implementation.
- Under “Implementation”, select “composition”.
- Click the red “Select Function” button.
- Enter the name of the Function, i.e. “join two strings” and select that when it appears in the autocompletion list.
- The first argument of that outer concatenation is a function call itself. In order to do that, click on the ... next to “first string”.
- You will see four options. Select “Function call”.
- Now, select the inner Function. That is, again, “join two strings”.
- The argument “first string” of the inner “join two strings” needs to be our first argument. So click again on the ... next to “first string”.
- Select “Argument reference”.
- Select the name of the second argument, “second string”.
- In the inner field “second string” (not the outer one), type in a single space character.
- If you already have Tests (and it’s a good habit to create the tests first), click on the circular arrow in the Tests panel and see if your implementation passes the tests.
- Now add the second argument. For the outer “second string” argument, select the ... next to it.
- Select “Argument reference”.
- Select the name of the second argument, “second string”.
- If you already have Tests (and it’s a good habit to create the tests first), click on the circular arrow in the Tests panel and see if your implementation passes the tests.
If the composition passes the test, you can publish it.