Skip to content

Instantly share code, notes, and snippets.

@RAtioNAn
Forked from leandrob13/OptionFilter.scala
Created February 20, 2021 18:06
Show Gist options
  • Save RAtioNAn/391a3c99ac71309a058aac591a02a0de to your computer and use it in GitHub Desktop.
Save RAtioNAn/391a3c99ac71309a058aac591a02a0de to your computer and use it in GitHub Desktop.
Based on MaybeFilter (https://gist.github.com/cvogt/9193220), it uses implicit class to add the methods to the Query objects. FilteredBy works for filtering lists and it returns the query if no filters were applied. FoundBy works for finding a single register, if no filter defined it returns None.
implicit class OptionFilter[ X, Y ]( query: Query[ X, Y, Seq ] ) {
def filteredBy[ T ]( op: Option[ T ] )( f: ( X, T ) => Column[ Option[ Boolean ] ] ): Query[ X, Y, Seq ] = {
op map { o => query.filter( f( _, o ) ) } getOrElse { query }
}
def foundBy[ T ]( op: Option[ T ] )( f: ( X, T ) => Column[ Option[ Boolean ] ] ): Query[ X, Y, Seq ] = {
op map { o => query.filter( f( _, o ) ) } getOrElse { query.take( 0 ) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment