Copper

Copper Cheat Sheet

Cheat sheet to understand most Copper code I've published without knowing Copper.

Refs

Macros

Defining a macro : like a function or method, but with do:

// A macro that takes
//  a parameter 'n',
//  a block parameter that takes an Int and must return an Int
// and returns and Int

func sum(n: Int) do (Int)[Int]: Int
    var sum = Int: 0
    var i = Int: 1
    while i <= n
        sum += yield(i)
        i += 1
    end
    return sum
end

Invoking the macro:

// Will get the sum 2 + 4 + 6 + ... + 20
var result =
    sum(10) do x
        ...
        // 'pass' returns a value to the 'yield' that has invoked this block
        pass x * 2
    end