Unobvious (?) technique

new topic     » goto parent     » topic index » view thread      » older message » newer message

>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

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu