Paste: Stateful Predicate

Author: Capital
Mode: factor
Date: Thu, 7 Sep 2023 05:27:27
Plain Text |
SYMBOL: current-seq


PREDICATE: out-of-bounds < fixnum current-seq get length >= ;
PREDICATE: negative      < fixnum  0 < ;


GENERIC#: (wrapping-nth) 1 ( n seq -- elm )
M: out-of-bounds (wrapping-nth) dup [ length rem ] dip nth-unsafe ;
M: negative      (wrapping-nth) [ abs ] dip (wrapping-nth) ;
M: fixnum        (wrapping-nth) nth-unsafe ;


: wrapping-nth ( n seq -- elm )
    dup current-seq [ (wrapping-nth) ] with-variable ;


: main ( -- )
    1 { 1 2 3 4 } wrapping-nth >dec print 
    2 { 1 2 3 4 } wrapping-nth >dec print
    3 { 1 2 3 4 } wrapping-nth >dec print
    4 { 1 2 3 4 } wrapping-nth >dec print ;


MAIN: main

New Annotation

Summary:
Author:
Mode:
Body: