FOREACH

9 views
Skip to first unread message

The Beez

unread,
Dec 16, 2023, 4:23:05 AM12/16/23
to 4tH-compiler
Hi 4tH-ers!

I recently ended up in a c.l.f discussion concerning high order iterators. Now, we got one in 4tH, but it only works with arrays. Can't say I used it very often, because the implementation is a bit murky IMHO.

Well, someone made an extension with some ANS carnal knowledge. He had listed his requirements:

"After some experimentation I ended up with this statement:
FOR-EACH <iterator> DO[ <pipeline operations> ]NEXT

The <iterator> is completely under the programmers control to provide
the next item in any type of data structure".

Now - could I implement this one? One that works with more data structures? Well, I gave it a try (code in SVN).

IMHO, the beauty is you just get an address to the element in question. What you do with this address is your thing. Consequence is - there is no difference between FOREACH or MAP, because due to this requirement MAP cannot foresee how an element is to be stored.

But this is a small demonstration of it's capabilities:

struct
field: one
field: two
end-struct /bla

6 constant #bla

#bla /bla * array bla
[: dup bla - swap -> two ! ;] bla #bla /bla foreach
[: -> two ? ;] bla #bla /bla foreach depth . cr
[: -> two dup @ dup * swap ! ;] bla #bla /bla map depth . cr
[: -> two ? ;] bla #bla /bla foreach depth . cr
0 [: -> two @ + ;] bla #bla /bla reduce . depth .


You just need to feed an execution token handling the address, the address of the array of things, the number of things and the size of the thing and things get done. Note I still included MAP just to indicate what the purpose of the xt is.

Lets see if this gets more usage in my programs.

Hans Bezemer
Reply all
Reply to author
Forward
0 new messages