Scala List.filter with two conditions, applied only once

前端 未结 4 362
旧时难觅i
旧时难觅i 2021-02-04 13:18

Don\'t know if this is possible, but I have some code like this:

val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
val evens = list.filter { e => e % 2 =         


        
相关标签:
4条回答
  • 2021-02-04 13:57

    You just write two conditions in the filter:

    val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    
    var result = List(0)
    val someCondition = true
    
    result = if (someCondition) list.filter { e => e % 2 == 0 && e % 3 == 0 }
             else               list.filter { e => e % 2 == 0 && e % 5 == 0 }
    
    0 讨论(0)
  • 2021-02-04 13:59

    You could use function composition. someCondition here is only called once, when deciding which function to compose with:

    def modN(n: Int)(xs: List[Int]) = xs filter (_ % n == 0)
    
    val f = modN(2) _ andThen (if (someCondition) modN(3) else modN(5))
    
    val result = f(list)
    

    (This doesn't do what you want - it still traverses the list twice)

    Just do this:

    val f: Int => Boolean = if (someCondition) { _ % 3 == 0 } else { _ % 5 == 0 }
    val result = list filter (x => x % 2 == 0 && f(x))
    

    or maybe better:

    val n = if (someCondition) 3 else 5
    val result = list filter (x => x % 2 == 0 && x % n == 0)
    
    0 讨论(0)
  • 2021-02-04 14:05

    Wouldn't this work:

    list.filter{e => e % 2 == 0 && (if (someCondition) e % 3 == 0 else e % 5 == 0)}
    

    also FYI e % 2 == 0 is going to give you all the even numbers, unless you're naming the val odds for another reason.

    0 讨论(0)
  • 2021-02-04 14:17

    If you turn list into a lazy collection, such as an Iterator, then you can apply all the filter operations (or other things like map etc) in one pass:

    val list = (1 to 12).toList
    val doubleFiltered: List[Int] =
      list.iterator
        .filter(_ % 2 == 0)
        .filter(_ % 3 == 0)
        .toList
    println(doubleFiltered)
    

    When you convert the collection to an Iterator with .iterator, Scala will keep track of the operations to be performed (here, two filters), but will wait to perform them until the result is actually accessed (here, via the call to .toList).

    So I might rewrite your code like this:

    val list = (1 to 12).toList
    val evens = list.iterator.filter(_ % 2 == 0)
    
    val result = 
      if(someCondition)
        evens.filter(_ % 3 == 0)
      else
        evens.filter(_ % 5 == 0)
    
    result foreach println
    

    Depending on exactly what you want to do, you might want an Iterator, a Stream, or a View. They are all lazily computed (so the one-pass aspect will apply), but they differ on things like whether they can be iterated over multiple times (Stream and View) or whether they keep the computed value around for later access (Stream).

    To really see these different lazy behaviors, try running this bit of code and set <OPERATION> to either toList, iterator, view, or toStream:

    val result =
      (1 to 12).<OPERATION>
        .filter { e => println("filter 1: " + e); e % 2 == 0 }
        .filter { e => println("filter 2: " + e); e % 3 == 0 }
    result foreach println
    result foreach println
    

    Here's the behavior you will see:

    • List (or any other non-lazy collection): Each filter is requires a separate iteration through the collection. The resulting filtered collection is stored in memory so that each foreach can just display it.
    • Iterator: Both filters and the first foreach are done in a single iteration. The second foreach does nothing since the Iterator has been consumed. Results are not stored in memory.
    • View: Both foreach calls result in their own single-pass iteration over the collection to perform the filters. Results are not stored in memory.
    • Stream: Both filters and the first foreach are done in a single iteration. The resulting filtered collection is stored in memory so that each foreach can just display it.
    0 讨论(0)
提交回复
热议问题