Skip to content

Instantly share code, notes, and snippets.

@GiuseppeChillemi
Last active January 22, 2024 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GiuseppeChillemi/2d9f3339eb2e96b1847e90d0ab0f1a98 to your computer and use it in GitHub Desktop.
Save GiuseppeChillemi/2d9f3339eb2e96b1847e90d0ab0f1a98 to your computer and use it in GitHub Desktop.
For-Skip attemp
Red [
title: "For-Record"
description: "A For-skip alternative"
notes: {
Use BREAK-HERE let the function return the series at current position
Use RETURN-VALUE to let the function return anything you want
If the cycle will end naturally, the series will be at head position
}
]
encontext: func [
"Crate a context with the passed words"
word [word! block!]
/local
values
out-data
] [
case [
block? word [word: copy word]
word? word [word: to-block word]
]
values: copy word
forall values [
case [
word? :values/1 [change values to-get-word :values/1]
true [make error! "encontext-a-block-element-is-not-word"]
]
]
;--- TBD: Functions will eat words. Redo this part of code
;
values: reduce values
forall word [
case [
word? :word/1 [change word to-set-word :word/1]
true [make error! "encontext-a-block-element-is-not-word"]
]
]
append word none
out-data: make object! word
set out-data values
out-data
]
for-record: func [
"Iterates the series treating it as a record of RECORD-SIZE elements with code in body [REBINDS!]"
'target [word!] "The word containing the series"
record-size [integer!] "The size of the record"
code [block! function!] "The code to executes"
;/reverse "TBD: go backward"
/local
return-value
out-data
returned
series
ctx-target
ctx-return
] [
[STATUS: WORKNG TO-MOVE MAIN HERE V1.2]
;TBD: Check in encontext the problem why N is not set on exit
;TBD: add options on return...
;TBD: test if you can bend build in return
;TBD: understand how return on the inner code affects the upper function
;TBD: Analyze what to do on insertions and deletions
series: get target
case/all [
record-size < 1 [make error! "Negative or zero record size"]
not series? :series [make error! "target word does not contain a series"]
record-size > length? series [make error! "record-size greater than length of series"]
]
ctx-target: encontext target
set/any 'returned ()
set/any 'out-data ()
ctx-return: make object! [
return-value: func [arg] [
set/any 'out-data arg
returned: 'return-value
]
break-here: func [] [
returned: 'break-here
]
]
bind bind code ctx-target ctx-return
times: (length? series) / record-size
if float? times [times: to-integer (times + 1)]
loop times [
do code
either not unset? get/any 'returned [
if returned = 'break-here [out-data: :ctx-target/:target]
break
] [
ctx-target/:target: at ctx-target/:target (record-size + 1)
set target ctx-target/:target
]
]
:out-data
]
;n: [1 2 3 4 5 6 7 8 9 10 11 12 13]
;for-record n 3 [
; probe first n
;]
;1
;4
;7
;10
;13
;Break returns the series at break index
;probe for-record n 3 [
; if 10 <= index? n [break-here]
;]
;[10 11 12 13]
;n: [1 2 3 4 5 6 7 8 9 10 11 12 13]
;probe for-record n 3 [
; if 10 < index? n [return-value 999]
;]
;999
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment