Wikifunctions:Come creare implementazioni

From Wikifunctions
This page is a translated version of the page Wikifunctions:How to create implementations and the translation is 33% complete.
Outdated translations are marked like this.

Questa pagina fornisce una guida più dettagliata al creare implementazioni, oltre alla panoramica di Wikifunctions:Introduction.

Codice in Python

Di seguito diamo un esempio concreto di come creare una implementazione nella forma di Codice in Python. Mettiamo di voler creare una implementazione di una Funzione che accetta due stringhe in input, le combina mettendo uno spazio fra di loro e restituisce il risultato dell'operazione. Assumiamo che lo ZID di quella funzione sia Z11057.

Questa descrizione è valida nel caso che l'implementazione utiizzi Stringhe e Booleani come tipi di input o output. Nel caso venga usato un altro tipo, consulta la documentazione di quel tipo su come trasformarne il valore per essere usato in Python. Pianifichiamo di rendere questo più facile in futuro.

L'implementazione è definita utilizzando lo ZID della funzione. Lo sono anche gli argomenti della funzione. Quindi nel caso della funzione Z11057 e dei suoi due argomenti, la prima riga risulterà così:

def Z11057(Z11057K1, Z11057K2):

Nota: Pianifichiamo di nascondere gli ZID in futuro.

La prima riga dovrebbe venir creata automaticamente, dopo che hai selezionato Python come linguaggio di programmazione. Devi solo aggiungere il corpo della funzione.

Dopo di questa, devi scrivere il codice in Python come fai di solito. Solamente le variabili sembreranno un po' strane. In questo caso, per esempio:

  return Z11057K1 + " " + Z11057K2

Visto che è Python, ricordati di indentare correttamente la riga.

Se hai già dei Test (è buona abitudine scrivere i test per prima cosa), clicca sulla freccia circolare nei casi di Test e vedi se la tua implementazione passa i test.

Ricorda che l'ambiente di esecuzione non ha stato. Non dare per certi i valori per variabili globali o statiche. Se hai ulteriori domande su cosa puoi e non puoi fare in Python, chiedi su Wikifunctions:Python implementations.

Codice in JavaScript

Di seguito forniamo un esempio concreto su come creare una implementazione nella forma di Codice Javascript. Mettiamo di voler creare una implementazione per una Funzione che combina due stringhe di input mettendo uno spazio fra di esse, restituendo il risultato dell'operazione. Supponiamo che lo ZID di quella funzione sia Z11057.

Questa descrizione è solamente valida nel caso in cui le implementazioni usino Stringhe e Booleani come tipi di input o output. Nel caso venga usato un altro tipo, consulta la documentazione di quel tipo su come trasformare i valori in modo che possano essere usati in JavaScript. Pianifichiamo di renderlo più semplice in futuro.

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.

Composizione

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.