Skip to content

Commit

Permalink
Use reduce instead of loop in 'circular-buffer'
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed Oct 2, 2024
1 parent 4975157 commit f22f7e5
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions exercises/practice/circular-buffer/.meta/circular-buffer.ys
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

defn run(capacity operations):
buffer =: M(:B V(capacity * [nil]), :P 0)
loop [op *ops] operations, buffer buffer:
when op:
op item pass want =:
op.slice('operation' 'item'
'should_succeed' 'expected')
args =:
condp eq op:
-'clear': -[buffer]
-'read': L(buffer pass want)
-'write': L(buffer pass item)
-'overwrite': L(buffer pass item)
buffer =: (value("op-$op") args*)
recur: ops buffer

reduce _ buffer operations:
fn(buffer op):
when op:
op item pass want =:
op.slice('operation' 'item'
'should_succeed' 'expected')
args =:
condp eq op:
-'clear' : -[buffer]
-'read' : L(buffer pass want)
-'write' : L(buffer pass item)
-'overwrite' : L(buffer pass item)
value("op-$op"): args*
=>: true

defn op-clear(buffer):
Expand Down

0 comments on commit f22f7e5

Please sign in to comment.