Path: utzoo!mnetor!uunet!mcvax!ukc!cam-cl!scc From: scc@cl.cam.ac.uk (Stephen Crawley) Newsgroups: comp.lang.misc Subject: Re: iterators (was Re: From Modula to Oberon) Message-ID: <1151@jenny.cl.cam.ac.uk> Date: 26 Mar 88 00:30:16 GMT References: <2827@enea.se> <1557@pasteur.Berkeley.Edu> <3764@bloom-beacon.MIT.EDU> <3864@bloom-beacon.MIT.EDU> <616@mcrware.UUCP> Reply-To: scc@cl.cam.ac.uk (Stephen Crawley) Organization: U of Cambridge Comp Lab, UK Lines: 157 Keywords: lockstep iteration Summary: Lock step iteration in Clu is easy In article <616@mcrware.UUCP> jejones@mcrware.UUCP (James Jones) writes: >There's one thing that bugs me about CLU iterators, which on the whole are >pretty neat: if I have two things I want to iterate through in lockstep, >how do I do it? (That's a non-rhetorical question--it would be great to >hear about a way to do it.) > > James Jones If I understand your problem correctly, there's a simple answer. In CLU, an iterator may be declared to yield a tuple each time it iterates. So you can iterate over 2 lists (say) in lock-step as follows. lock_step = iter (list1, list2: my_l_type) yields (my_v_type, my_v_type) my_nil: my_l_type := my_l_type$get_nil () % erm ... while list1 ~= my_nil & list2 ~= my_nil & yield (list1.value, list2.value) list1 := list1.next list2 := list2.next end end lock_step [The mussing around with my_nil is for real. There is no universal nil value in Clu, so you have to somehow construct one for each recursive abstract data type. It is usually has to be done using a oneof type ... sigh] And just for the hell of it, here's a generic 2 list iterator!! lock_step2 = iter [lt, t: type] (list1, list2: lt) yields (t, t) where lt has is_empty: proctype (lt) returns (bool), next: proctype (lt) returns (lt), value: proctype (lt) returns (t) while ~lt$is_empty (list1) & ~lt$is_empty (list2) do yield (lt$value (list1), lt$value (list2)) list1 := lt$list1.next list2 := lt$list2.next end end lock_step2 It strikes me that the above solution is too obvious. Perhaps James had a more tricky case in mind? -- Steve Newsgroups: comp.lang.misc Subject: Re: iterators (was Re: From Modula to Oberon) References: <2827@enea.se> <1557@pasteur.Berkeley.Edu> <3764@bloom-beacon.MIT.EDU> <3864@bloom-beacon.MIT.EDU> <616@mcrware.UUCP> From: scc@cl.cam.ac.uk (Stephen Crawley) Reply-To: scc@cl.cam.ac.uk (Stephen Crawley) Organization: U of Cambridge Comp Lab, UK Keywords: lockstep iteration In article <616@mcrware.UUCP> jejones@mcrware.UUCP (James Jones) writes: >There's one thing that bugs me about CLU iterators, which on the whole are >pretty neat: if I have two things I want to iterate through in lockstep, >how do I do it? (That's a non-rhetorical question--it would be great to >hear about a way to do it.) > > James Jones If I understand your problem correctly, there's a simple answer. In CLU, an iterator may be declared to yield a tuple each time it iterates. So you can iterate over 2 lists (say) in lock-step as follows. lock_step = iter (list1, list2: my_l_type) yields (my_v_type, my_v_type) my_nil: my_l_type := my_l_type$get_nil () % erm ... while list1 ~= my_nil & list2 ~= my_nil & yield (list1.value, list2.value) list1 := list1.next list2 := list2.next end end lock_step [The mussing around with my_nil is for real. There is no universal nil value in Clu, so you have to somehow construct one for each recursive abstract data type. It is usually has to be done using a oneof type ... sigh] And just for the hell of it, here's a generic 2 list iterator!! lock_step2 = iter [lt, t: type] (list1, list2: lt) yields (t, t) where lt has is_empty: proctype (lt) returns (bool), next: proctype (lt) returns (lt), value: proctype (lt) returns (t) while ~lt$is_empty (list1) & ~lt$is_empty (list2) do yield (lt$value (list1), lt$value (list2)) list1 := lt$list1.next list2 := lt$list2.next end end lock_step2 It strikes me that the above solution is too obvious. Perhaps James had a more tricky case in mind? -- Steve Newsgroups: comp.lang.misc Subject: Re: iterators (was Re: From Modula to Oberon) References: <2827@enea.se> <1557@pasteur.Berkeley.Edu> <3764@bloom-beacon.MIT.EDU> <3864@bloom-beacon.MIT.EDU> <616@mcrware.UUCP> From: scc@cl.cam.ac.uk (Stephen Crawley) Reply-To: scc@cl.cam.ac.uk (Stephen Crawley) Organization: U of Cambridge Comp Lab, UK Keywords: lockstep iteration In article <616@mcrware.UUCP> jejones@mcrware.UUCP (James Jones) writes: >There's one thing that bugs me about CLU iterators, which on the whole are >pretty neat: if I have two things I want to iterate through in lockstep, >how do I do it? (That's a non-rhetorical question--it would be great to >hear about a way to do it.) > > James Jones If I understand your problem correctly, there's a simple answer. In CLU, an iterator may be declared to yield a tuple each time it iterates. So you can iterate over 2 lists (say) in lock-step as follows. lock_step = iter (list1, list2: my_l_type) yields (my_v_type, my_v_type) my_nil: my_l_type := my_l_type$get_nil () % erm ... while list1 ~= my_nil & list2 ~= my_nil & yield (list1.value, list2.value) list1 := list1.next list2 := list2.next end end lock_step [The mussing around with my_nil is for real. There is no universal nil value in Clu, so you have to somehow construct one for each recursive abstract data type. It is usually has to be done using a oneof type ... sigh] And just for the hell of it, here's a generic 2 list iterator!! lock_step2 = iter [lt, t: type] (list1, list2: lt) yields (t, t) where lt has is_empty: proctype (lt) returns (bool), next: proctype (lt) returns (lt), value: proctype (lt) returns (t) while ~lt$is_empty (list1) & ~lt$is_empty (list2) do yield (lt$value (list1), lt$value (list2)) list1 := lt$list1.next list2 := lt$list2.next end end lock_step2 It strikes me that the above solution is too obvious. Perhaps James had a more tricky case in mind? -- Steve