Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/20250701_105439_shane.obrien_elem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added

- Add `elem` and `elem1` to `Rel8.Array` for testing if an element is contained in `[]` and `NonEmpty` `Expr`s.
31 changes: 30 additions & 1 deletion src/Rel8/Array.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE OverloadedStrings #-}

module Rel8.Array
(
-- ** @ListTable@
Expand All @@ -6,13 +10,15 @@ module Rel8.Array
, index, indexExpr
, last, lastExpr
, length, lengthExpr
, elem

-- ** @NonEmptyTable@
, NonEmptyTable
, head1, head1Expr
, index1, index1Expr
, last1, last1Expr
, length1, length1Expr
, elem1

-- ** Unsafe
, unsafeSubscript
Expand All @@ -21,11 +27,34 @@ module Rel8.Array
where

-- base
import Prelude hiding (head, last, length)
import Data.List.NonEmpty (NonEmpty)
import Prelude hiding (elem, head, last, length)

-- rel8
import Rel8.Expr (Expr)
import Rel8.Expr.Array (listOf, nonEmptyOf)
import Rel8.Expr.Function (rawBinaryOperator)
import Rel8.Expr.List
import Rel8.Expr.NonEmpty
import Rel8.Expr.Subscript
import Rel8.Schema.Null (Sql)
import Rel8.Table.List
import Rel8.Table.NonEmpty
import Rel8.Type.Eq (DBEq)


-- | @'elem' a as@ tests whether @a@ is an element of the list @as@.
elem :: Sql DBEq a => Expr a -> Expr [a] -> Expr Bool
elem = (<@) . listOf . pure
where
(<@) = rawBinaryOperator "<@"
infix 4 `elem`


-- | @'elem1' a as@ tests whether @a@ is an element of the non-empty list
-- @as@.
elem1 :: Sql DBEq a => Expr a -> Expr (NonEmpty a) -> Expr Bool
elem1 = (<@) . nonEmptyOf . pure
where
(<@) = rawBinaryOperator "<@"
infix 4 `elem1`