Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeating items #52

Open
njordhov opened this issue Mar 30, 2022 · 1 comment
Open

Repeating items #52

njordhov opened this issue Mar 30, 2022 · 1 comment

Comments

@njordhov
Copy link

A repeat function can generate a list of identical items repeated a given number of times.

Lists of explicitly repeated items are used in many Clarity contracts, typically with map and fold. For illustration, here is a function to increment integers in a list with an integer incremental `n':

(define-read-only (inc-many (items (list 20 int)) (n int)) 
  (map + items (list n n n n n n n n n n n n n n n n n n n n)))

Using repeat simplifies this to:

(define-read-only (inc-many (items (list 20 int)) (n int)) 
  (map + items (repeat 20 n)))

The cost of using repeat should be the same or less than having explicitly repeated items in the contract.

@njordhov njordhov changed the title Generating repeated items Repeating items Mar 30, 2022
@bgok
Copy link

bgok commented Mar 31, 2022

As an alternative, the map function could accept non-iterable parameters and pass them to each iteration.

(define-read-only (inc-many (items (list 20 uint))) 
  (map + items u1)
)

In this example, each time + is called, u1 is passed as the second parameter.

This change should also be applied to fold and filter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants