Skip to content

Commit 0039e21

Browse files
Add elem and elem1 to Rel8.Array
1 parent 80a4449 commit 0039e21

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Added
2+
3+
- Add `elem` and `elem1` to `Rel8.Array` for testing if an element is contained in `[]` and `NonEmpty` `Expr`s.

src/Rel8/Array.hs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
{-# LANGUAGE FlexibleContexts #-}
2+
{-# LANGUAGE MonoLocalBinds #-}
3+
{-# LANGUAGE OverloadedStrings #-}
4+
15
module Rel8.Array
26
(
37
-- ** @ListTable@
@@ -6,13 +10,15 @@ module Rel8.Array
610
, index, indexExpr
711
, last, lastExpr
812
, length, lengthExpr
13+
, elem
914

1015
-- ** @NonEmptyTable@
1116
, NonEmptyTable
1217
, head1, head1Expr
1318
, index1, index1Expr
1419
, last1, last1Expr
1520
, length1, length1Expr
21+
, elem1
1622

1723
-- ** Unsafe
1824
, unsafeSubscript
@@ -21,11 +27,34 @@ module Rel8.Array
2127
where
2228

2329
-- base
24-
import Prelude hiding (head, last, length)
30+
import Data.List.NonEmpty (NonEmpty)
31+
import Prelude hiding (elem, head, last, length)
2532

2633
-- rel8
34+
import Rel8.Expr (Expr)
35+
import Rel8.Expr.Array (listOf, nonEmptyOf)
36+
import Rel8.Expr.Function (rawBinaryOperator)
2737
import Rel8.Expr.List
2838
import Rel8.Expr.NonEmpty
2939
import Rel8.Expr.Subscript
40+
import Rel8.Schema.Null (Sql)
3041
import Rel8.Table.List
3142
import Rel8.Table.NonEmpty
43+
import Rel8.Type.Eq (DBEq)
44+
45+
46+
-- | @'elem' a as@ tests whether @a@ is an element of the list @as@.
47+
elem :: Sql DBEq a => Expr a -> Expr [a] -> Expr Bool
48+
elem = (<@) . listOf . pure
49+
where
50+
(<@) = rawBinaryOperator "<@"
51+
infix 4 `elem`
52+
53+
54+
-- | @'elem1' a as@ tests whether @a@ is an element of the non-empty list
55+
-- @as@.
56+
elem1 :: Sql DBEq a => Expr a -> Expr (NonEmpty a) -> Expr Bool
57+
elem1 = (<@) . nonEmptyOf . pure
58+
where
59+
(<@) = rawBinaryOperator "<@"
60+
infix 4 `elem1`

0 commit comments

Comments
 (0)