DecentralChainDecentralChain
  • Technologysubmenu
    Why DecentralChainWhat sets this chain apartHow the chain worksPublic blocks, public validatorsBlockchain featuresWhat is on, what is being voted onFinalityThe rollback bound, and what is unverifiedRIDEContracts that cannot run foreverFor machinesUnsigned transactions and RIDE in processSolana bridgePhantom and Solflare, mainnet onlyNetwork statusHeight, sync and peers, read live
  • Ecosystemsubmenu
    DCC public saleOpens 26 SeptemberDecentralCoinSupply, allocation and tokenomicsDecentral.ExchangeexternalDecentralPropDecentralScansoonCubensis ConnectDecentralAmericasoon
  • Developerssubmenu
    QuickstartRead, compile and composeREST APIEvery endpoint, no key, no accountSDK packages19 on npm, 5 in the workspaceSigningAuthorising without custodyMCP & AgentsThe chain, and the tools we have builtDecentralChain NodeRun a validator, and what LPoS pays
  • Communitysubmenu
    ChannelsTelegram, X and the reposDCC AirdropAllocated, not yet arrangedGovernanceWho can actually decide anythingBrand kitThe mark, and how to use it
  • Documentation
XGitHub
Start buildingarrow
DecentralChainDecentralChain

An open Layer-1 with Leased Proof of Stake.

Native token: DecentralCoin (DCC)

Fixed supply 100,000,000 DCC
Minted at genesis, never inflated

  • X
  • GitHub
  • Telegram

Build

  • Quickstart
  • REST API
  • SDK packages
  • Signing
  • MCP & Agents
  • Documentation

Ecosystem

  • DCC public sale
  • DecentralCoin
  • Decentral.Exchange
  • DecentralProp
  • DecentralSwap
  • Cubensis Connect

Network

  • Blockchain features
  • Finality
  • RIDE
  • Solana bridge
  • Run a node
  • Network status

Project

  • Community
  • DCC Airdrop
  • Governance
  • Brand kit
  • GitHub
mainnetHeight—Node—read from your browser
© 2026 DecentralChainOperated by DecentralExchange · Cédula Jurídica 3-102-956858
Jacó, Garabito, Costa Rica
TermsPrivacySecurityLicensingdecentralchain.io
Documentation/RIDE language

Iterations with FOLD<N>

RIDE language

  • Syntax Basics
  • Data Types
  • Functions
  • Script Types
  • Structures
  • Iterations with FOLD<N>
  • dApp-to-App Invocation

FOLD<N> macro makes it possible to implement operations on a list of values such as sum, filter, map, zip, exists, etc. The macro behaves like the fold or reduce function in other programming languages.

FOLD<N>(list, start, foldFunc)
Iterations with FOLD
ParameterDescription
NMaximum number of iterations, up to 1000.
listList of values.
startInitial value.
foldFuncCombining function.

The combining function accepts two input parameters: the intermediate result and the next element of the list. Macro FOLD<N>(list, start, foldFunc) means:

  • Execute up to N iterations.
  • At each iteration: take the r-esult of the previous iteration (at the first iteration take the start value) and the next list item list, apply the foldFunc function to this pair.
  • Return the final result.

The value of N must be known in advance. If there are more elements in the list than specified in FOLD, the script fails. The complexity of FOLD<N> corresponds to the complexity of foldFunc multiplied by N plus extras. The FOLD<N> macro is a syntactic sugar; it is unwrapped by the compiler. Therefore, in particular, the size of the script increases linearly with N.

Sum

func sum(accum: Int, next: Int) = accum + next
let arr = [1,2,3,4,5]
FOLD<5>(arr, 0, sum)    # Result: 15

The expression

FOLD<5>(arr, 0, sum)

after compiling and decompiling will look like this:

let $list = arr
let $size = size($list)
let $acc0 = 0
if (($size == 0))
 then $acc0
 else {
   let $acc1 = sum($acc0, $list[0])
   if (($size == 1))
     then $acc1
     else {
       let $acc2 = sum($acc1, $list[1])
       if (($size == 2))
         then $acc2
         else {
           let $acc3 = sum($acc2, $list[2])
           if (($size == 3))
           then $acc3
             else {
               let $acc4 = sum($acc3, $list[3])
               if (($size == 4))
                 then $acc4
                 else {
                   let $acc5 = sum($acc4, $list[4])
                   if (($size == 5))
                     then $acc5
                     else {
                       let $acc6 = sum($acc5, $list[5])
                       throw("List size exceed 5")
                     }
                 }
             }
         }
     }
 }

Product

func mult(accum: Int, next: Int) = accum * next
let arr = [1,2,3,4,5]
FOLD<5>(arr, 1, mult)    # Result: 1204]

Filter

The following code composes an array consisting only of even elements of the original array:

func filterEven(accum: List[Int], next: Int) =
 if (next % 2 == 0) then accum :+ next else accum
let arr = [1,2,3,4,5]
FOLD<5>(arr, [], filterEven)    # Result: [2, 4]

Map

The following code inverts the array, reducing each element by 1:

func map(accum: List[Int], next: Int) = (next - 1) :: accum
let arr = [1, 2, 3, 4, 5]
FOLD<5>(arr, [], map)    # Result: [4, 3, 2, 1, 0]
PreviousStructuresNextdApp-to-App Invocationarrow

Elsewhere on this site

RIDE on DecentralChainarrowWhat the language guarantees, and why a script is priced before it deploys.SDK packagesarrowThe compiler and the SDKs, published to npm and runnable in a browser.