Wikifunctions:How to create implementations

From Wikifunctions

This page provides a more detailed guide to creating implementations, beyond the overview at Wikifunctions:Introduction.

Code in Python

In the following 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 Test cases 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 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 Test cases 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 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:

  1. Click on the red Select Function.
  2. Enter the name of the Function we want to select, i.e. “concatenate” and select that.
  3. The first argument of that outer concatenation is a function call itself. In order to do that, click on the > next to First.
  4. Now click on the value of the Type of First. It currently says String.
  5. You will see four options. Select Function call.
  6. Now, select the inner Function. That is, again, concatenate.
  7. The argument First of the inner concatenate needs to be our first argument. So click again on the > next to First.
  8. Click on the dropdown box showing String.
  9. Select Argument reference.
  10. 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.
  11. In the inner field Second (not the outer one), type in a single space character. This will be basically invisible.
  12. If you already have Tests (and it’s a good habit to create the tests first), click on the circular arrow in the Test cases and see if your implementation passes the tests.
  13. Now add the second argument. For the outer Second argument, select the > next to it.
  14. Click on the drop down box saying String.
  15. Select Argument reference.
  16. 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.
  17. If you already have Tests (and it’s a good habit to create the tests first), click on the circular arrow ⎋ in the Test cases and see if your implementation passes the tests.

If the composition passes the test, you can publish it.