-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Array(1, 2, 3, 26, 20, 64, 5, 6, 7) filter (!Set(26, 20, 64).contains(_))
Prefer combinators to pattern-matching. Pattern-matching is a last resort.
The problem is that the code above eliminates all occurrences of 20, 26 and 64 numbers in
the array, not checking it is actually a sequence of 26, 20, 64.
The first sentence of the request was ambiguous but the second part clarified the
requirement...
--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --
Shouldn't "i == 0" be "i == -1"? And "3" should probably be slice.size
Also, it looks like this solution has complexity O(m * n), where m is the number of occurrences of the seq to eliminate. I'd hesitate to use such an algorithm unless I know m will be small.
Best regards
DanielOn Wed, Jul 13, 2011 at 12:28 PM, Kevin Wright <kev.lee...@gmail.com> wrote:
Actually, no it isn't...This should do the trick though:def eliminate[T](xs: Array[T], slice: Seq[T]): Array[T] = {val i = xs indexOfSlice sliceif(i == 0) xs else eliminate(xs.take(i) ++ xs.drop(i+3), slice)}
def eliminate[@specialized(Byte) T : ClassManifest](xs: Array[T],
slice: Seq[T]): Array[T] = {
val i = xs indexOfSlice slice
if(i == -1) xs else xs.take(i) ++ eliminate(xs.drop(i + slice.length), slice)
}
2011/7/13 Mark Nelson <mark.x...@gmail.com>:
On 13/07/11 21:10, Kevin Wright wrote:
> On 13 July 2011 12:02, Tony Morris <tonym...@gmail.com> wrote:
>
>> **
>>
>> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
>>
>> Array(1, 2, 3, 26, 20, 64, 5, 6, 7) filter (!Set(26, 20,
>> 64).contains(_))
>>
>> Prefer combinators to pattern-matching. Pattern-matching is a
>> last resort.
>>
>>
> That won't work, as the goal is to match the entire sequence, not
> just any occurrence of 26,20 or 64
>
Use tails.
- --
Tony Morris
http://tmorris.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk4dg7YACgkQmnpgrYe6r61+mACfRlrsRTbHznGmuMsDR36KwYoG
EgsAn2kGVkO0aaokDOyCp+v9VQjetu05
=XOZg
-----END PGP SIGNATURE-----
def eliminate(xs: Array[Byte], slice: Seq[Byte]): Array[Byte] = {
val i = xs indexOfSlice slice
if(i == -1) xs else xs.take(i) ++ eliminate(xs.drop(i + slice.length), slice)
}
Although, as others have noted before, this isn't very efficient.
Regards,
Rüdiger
2011/7/13 Mark Nelson <mark.x...@gmail.com>:
Thank you all, with your help, I got it working :-)
For the record, the code I ended up with was:
val b = Array[Byte](size)
// get data into b
val pattern = Array(26.toByte, 20.toByte, 64.toByte)
val c = eliminate[Byte](b, pattern)
c.foreach(byte => print(byte.toChar))
}
def eliminate[@specialized(Byte) T : ClassManifest](xs: Array[T], slice: Seq[T]): Array[T] = {
val i = xs indexOfSlice slice
if(i == -1) xs else xs.take(i) ++ eliminate(xs.drop(i + slice.length), slice)
}
Best regards,
If I'm not mistaken Arrays are not the problem here. Actually, as they
provide O(1) index access, they're a pretty good structure for searching.
To improve from O(m*n) to O(n) in the worst case, I'd recommend using
the Boyer-Moore algorithm
(<http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm>)
instead. That's unfortunately in imperative style, but I don't know any
other efficient method.
x.map(_.toChar).mkString.replaceAll(Array(26,20,64).map(_.toChar).mkString,
"").map(_.toByte)
If you have something that won't map into chars, you can use Scala's
generic automata/regex stuff (scala.util.regexp, the package, not to
be confused with scala.util.matching.Regex, the class).
--
Daniel C. Sobral
I travel to the future all the time.
To avoid complexity multiplication one should scan once.
-Vlad