From 203143b97aa454248593ab64c097f6bd70a4e4ab Mon Sep 17 00:00:00 2001 From: Jakub Kramarz Date: Sun, 1 Jan 2017 13:04:55 +0100 Subject: [PATCH] reimplemented unhash using Horner's method --- hashids-scala/src/main/scala/org/hashids/impl/package.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hashids-scala/src/main/scala/org/hashids/impl/package.scala b/hashids-scala/src/main/scala/org/hashids/impl/package.scala index 82f660f..7b6a5c3 100644 --- a/hashids-scala/src/main/scala/org/hashids/impl/package.scala +++ b/hashids-scala/src/main/scala/org/hashids/impl/package.scala @@ -43,8 +43,8 @@ package object impl { } def unhash(input: String, alphabet: String): Long = { - input.zipWithIndex.foldLeft[Long](0L) { case (acc, (in, i)) => - acc + (alphabet.indexOf(in) * Math.pow(alphabet.length, input.length - 1 - i)).toLong + input.foldLeft[Long](0L) { + (carry, item) => carry * alphabet.length + alphabet.indexOf(item) } }