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

Simplify equality reducer #7

Open
njordhov opened this issue May 26, 2020 · 0 comments
Open

Simplify equality reducer #7

njordhov opened this issue May 26, 2020 · 0 comments

Comments

@njordhov
Copy link

njordhov commented May 26, 2020

The value-tuple state in flip-coin.clar can be reused and just passed along until and after a match, simplifying the implementation and making it more efficient:

(define-private (is-even (item (buff 1)) (value-tuple {value: (buff 1), result: (buff 1)}))
(let ((val (get value value-tuple)))
(if (is-eq item val )
{value: val, result: 0x00}
{value: val, result: (get result value-tuple)}
)
)
)

=>

(define-private (is-even (item (buff 1)) (state {value: (buff 1), result: (buff 1)})) 
   (let ((val (get value state))) 
     (if (is-eq item val) 
       {value: val, result: 0x00} 
       state))) 

Also, consider turning result into a boolean initially set to false to eliminate the test on the result of the reduction:

(define-read-only (even (value (buff 1)))
  (get result (fold is-even even-buffer {value: value, result: false})))
@njordhov njordhov changed the title Reuse value-tuple Simplify equality reducer May 26, 2020
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

1 participant