Skip to content

Instantly share code, notes, and snippets.

def sumMultiples3or5(list: List[Int]): Int =
list.foldLeft(0)((acc: Int, b: Int) => if (b % 3 == 0 | b % 5 == 0) acc + b else acc)
def sumMultiplesGeneric(list: List[Int], multiplesOf: List[Int]): Int = {
// Transform the list of multiples to a single function that check if a number is multiple of any in the list
val multipleOfAny: Int => Boolean = multiplesOf
.map(x => (target: Int) => target % x == 0) // list of functions
.reduce((f1, f2) => (target: Int) => f1(target) | f2(target)) // single function
list.foldLeft(0)((a: Int, b: Int) => if (multipleOfAny(b)) a + b else a)

Keybase proof

I hereby claim:

  • I am edsanav on github.
  • I am edsanav (https://keybase.io/edsanav) on keybase.
  • I have a public key ASBCZW5Znq5ha5O2k3amUXkr71nVkn58HnPAKRtXzBOsvgo

To claim this, I am signing this object: