What is currying?

This question was asked at chia_network.public on Keybase.

2 Likes

Currying is passing solution arguments into a puzzle before it’s run on the network. It allows you to partially solve it beforehand to commit to specific values. A common use of this is to make a template puzzle that requires information to be passed in when it’s instantiated and used in a coin.

2 Likes

More generically currying is a technique commonly used in functional programming. If you have a function that takes multiple arguments you can convert it into a function that accepts only the first argument and returns a function that accepts the remaining arguments. This is especially useful when you have parameters that you would like to preset. Or, as is commonly the case in Chia, if you have parameters that you would like to be fixed in the puzzle hash.

For example, imagine you have a simple JavaScript function called add.

function add(a, b) {
  return a + b
}

What if you wanted to ensure that a specific number was added? You could curry in a like this:

function curry(a) {
  return function(b) {
    return add(a, b)
  }
}

This curry function takes the first a parameter and returns a new function that accepts the b parameter.

Now we could use this to make a new function that adds a specific value.

const add1 = curry(1)
const five = add1(4)

So the curry function takes the first parameter and returns a new function which we name add1. This new function can now be used to always call add with a first parameter of 1.

7 Likes

There’s also some documentation on this here: 5 - Common Functions in Chialisp | Chialisp

2 Likes