1. Unobvious (?) technique
- Posted by JEFF ZEITLIN <jeff.zeitlin at EXECNET.COM> Jul 12, 1997
- 749 views
- Last edited Jul 13, 1997
In working on one of my "concept code" projects, I discovered that I needed to go through a sequence and replace all instances of a certain value with a different value. My first attempt at this was the fairly obvious for i = 1 to length(s) if s[i] = keyval then s[i] = newval end if end for There's another way which seems to be somewhat faster: s = (newval * (s = keyval)) + (s * (s != keyval)) I know _why_ it works, but when I tried to write out an explanation, it ran to several pages. If there are any mathematicians here, they're welcome to try to explain it in language that we can all understand... ========================================================================= Jeff Zeitlin jeff.zeitlin at earth.execnet.com --- ~ OLXWin 1.00b ~ "Illigitimati non se carborundum"
2. Unobvious (?) technique
- Posted by Byron Hinkle <byron.hinkle at TSBBS.COM> Jul 13, 1997
- 758 views
Jeff, J> for i = 1 to length(s) J> if s[i] = keyval then J> s[i] = newval J> end if J> end for J> J> There's another way which seems to be somewhat faster: J> J> s = (newval * (s = keyval)) + (s * (s != keyval)) J> J> I know _why_ it works, but when I tried to write out an J> explanation, it ran to several pages. If there are any J> mathematicians here, they're welcome to try to explain it in J> language that we can all understand... Interesting, yes? Let me give my interpretation. Only (s = keyval) or (s != keyval) may be true at any given time. True is a one, not true is a zero. So it will equate to either s = (newval * 1) + (s * 0) which is: s = newval + 0 or s = (newval * 0) + (s * 1) which is: s = 0 + s since 0 times anything is zero and 1 times anything is itself. Now what you have here is a Boolean logic equation. To use it more easily, you can equate '*' to mean IF and '+' to mean OR. There is more to it, but just reread and it says: s = newval IF s = keyval, OR s = s IF s != keyval. --- * SRP 2.00 #2663 *
3. Unobvious (?) technique
- Posted by Colin Taylor <71630.1776 at COMPUSERVE.COM> Jul 14, 1997
- 725 views
>There's another way which seems to be somewhat faster: > s =3D (newval * (s =3D keyval)) + (s * (s !=3D keyval)) > I know _why_ it works, but when I tried to write out an > explanation, it ran to several pages. If there are any > mathematicians here, they're welcome to try to explain it in > language that we can all understand... Using Euphoria, you can do two types of sequence arithmetic (ie. arithmet= ic operations on entire sequences): 1. sequence, value: eg. {1,2,3}*5 result =3D {5,10,15} 2. sequence, sequence: eg. {1,2,3}*{1,2,3} result =3D {1,4,9} Note that the result is a sequence of the same form as the first sequence= =2E = Also note that in a "sequence, sequence" operation, both sequences must have the same form. In your example you have created what could be called a "boolean sequence= ", since it is filled with boolean values (0 or 1). To me, boolean sequence= s open up a world of "unobvious" operations. What you did was create two boolean sequences; one with 1's where your keyval was located and the res= t 0's, and another sequence just the opposite. You then multiplied the "true" sequence by newval, creating a new sequence with newval's and zero= s. You created a second new sequence with zeros instead of keyval, then finished by adding the two sequences together. Nicely done. The following code makes it a little easier to understand my explanation = (I hope): true =3D (s =3D keyval) false =3D not true s =3D (newval * true) + (s * false) Colin Taylor 71630.1776 at compuserve.com
4. Re: Unobvious (?) technique
- Posted by Larry D Poos <ldpoos at JUNO.COM> Jul 14, 1997
- 783 views
On Sun, 13 Jul 1997 19:33:00 GMT Byron Hinkle <byron.hinkle at TSBBS.COM> writes: ====================8<------- SNIP ------->8==================== >J> s = (newval * (s = keyval)) + (s * (s != keyval)) >it says: s = newval IF s = keyval, OR s = s IF s != keyval. ====================8<------- SNIP ------->8==================== Looks like you cut through all the math theory foodarah and got to the meat of the subject. <BG> Larry D. Poos -[USMC (Retar{bks}{bks}ired) Havelock, NC]- -(Just Downsized) Programming and System Consultant, LTAD Enterprises - e-mail: ldpoos at juno.com Fido: 1:3629/101.6
5. Re: Unobvious (?) technique
- Posted by "Carl R. White" Jul 25, 1997
- 721 views
On Sun, 13 Jul 1997, Byron Hinkle wrote: > Jeff, > > J> for i = 1 to length(s) > J> if s[i] = keyval then > J> s[i] = newval > J> end if > J> end for > J> > J> There's another way which seems to be somewhat faster: > J> > J> s = (newval * (s = keyval)) + (s * (s != keyval)) > J> > J> I know _why_ it works, but when I tried to write out an > J> explanation, it ran to several pages. If there are any > J> mathematicians here, they're welcome to try to explain it in > J> language that we can all understand... > > Interesting, yes? Let me give my interpretation. > Only (s = keyval) or (s != keyval) may be true at any given time. > True is a one, not true is a zero. So it will equate to either > s = (newval * 1) + (s * 0) which is: s = newval + 0 or > s = (newval * 0) + (s * 1) which is: s = 0 + s > since 0 times anything is zero and 1 times anything is itself. > Now what you have here is a Boolean logic equation. > To use it more easily, you can equate '*' to > mean IF and '+' to mean OR. There is more to it, but just reread > and it says: s = newval IF s = keyval, OR s = s IF s != keyval. I thought this function (or similar) was the euphoria norm: global function replace(char from, char to_, sequence in) return in + (in = from) * (to_ - from) end function -- Carl R White | e-mail...: crwhite- at -comp.brad.ac.uk | finger...: crwhite- at -dcsun1.comp.brad.ac.uk | web......: http://www.student.comp.brad.ac.uk/~crwhite/ Anti-Spam and Uncle-Pek measures in place & .sig fixed too...