on master help sign in
Project individual columns out of ListTable and NonEmptyTable (#125)
commit6fad4d1f77f13f9f1639c2c48006ff12a8019b9b authorShane <shane.obrien@circuithub.com> dateFri 22 Oct 2021, 13:18 (5 years ago) parent35c6127
5 files changed+42−9
Changelog.md+4 src/Rel8.hs+2−2 src/Rel8/Schema/HTable/Vectorize.hs+8−1 src/Rel8/Table/List.hs+14−3 src/Rel8/Table/NonEmpty.hs+14−3
Changelog.md+4
@@ -1,3 +1,7 @@
1+# Unreleased
2+
3+ New `$*` and `$+` operators for projecting out of `ListTable` and `NonEmptyTable` respectively (analogous to the existing `$?` for `MaybeTable`).
4+
15# 1.1.0.0 (2021-07-16)
26
37## New features
src/Rel8.hs+2−2
@@ -90,7 +90,7 @@ module Rel8
9090
9191 -- ** @ListTable@
9292 , ListTable
93− , listTable
93+ , listTable, ($*)
9494 , nameListTable
9595 , many
9696 , manyExpr
@@ -99,7 +99,7 @@ module Rel8
9999
100100 -- ** @NonEmptyTable@
101101 , NonEmptyTable
102− , nonEmptyTable
102+ , nonEmptyTable, ($+)
103103 , nameNonEmptyTable
104104 , some
105105 , someExpr
src/Rel8/Schema/HTable/Vectorize.hs+8−1
@@ -22,6 +22,7 @@ module Rel8.Schema.HTable.Vectorize
2222 , hvectorize, hunvectorize
2323 , happend, hempty
2424 , hproject
25+ , hcolumn
2526 )
2627where
2728
@@ -36,9 +37,11 @@ import Rel8.FCF ( Eval, Exp )
3637import Rel8.Schema.Dict ( Dict( Dict ) )
3738import qualified Rel8.Schema.Kind as K
3839import Rel8.Schema.HTable ( HTable, hfield, htabulate, htabulateA, hspecs )
40+import Rel8.Schema.HTable.Identity ( HIdentity( HIdentity ) )
3941import Rel8.Schema.HTable.MapTable
40− ( HMapTable, HMapTableField( HMapTableField )
42+ ( HMapTable( HMapTable ), HMapTableField( HMapTableField )
4143 , MapSpec, mapInfo
44+ , Precompose( Precompose )
4245 )
4346import qualified Rel8.Schema.HTable.MapTable as HMapTable ( hproject )
4447import Rel8.Schema.Null ( Unnullify, NotNull, Nullity( NotNull ) )
@@ -133,3 +136,7 @@ hproject :: ()
133136 => (forall ctx. t ctx -> t' ctx)
134137 -> HVectorize list t context -> HVectorize list t' context
135138hproject f (HVectorize a) = HVectorize (HMapTable.hproject f a)
139+
140+
141+hcolumn :: HVectorize list (HIdentity a) context -> context (list a)
142+hcolumn (HVectorize (HMapTable (HIdentity (Precompose a)))) = a
src/Rel8/Table/List.hs+14−3
@@ -11,7 +11,9 @@
1111
1212module Rel8.Table.List
1313 ( ListTable(..)
14− , listTable, nameListTable
14+ , ($*)
15+ , listTable
16+ , nameListTable
1517 )
1618where
1719
@@ -28,7 +30,7 @@ import Rel8.Schema.HTable.List ( HListTable )
2830import Rel8.Schema.HTable.Vectorize
2931 ( hvectorize, hunvectorize
3032 , happend, hempty
31− , hproject
33+ , hproject, hcolumn
3234 )
3335import qualified Rel8.Schema.Kind as K
3436import Rel8.Schema.Name ( Name( Name ) )
@@ -46,7 +48,9 @@ import Rel8.Table.Alternative
4648 )
4749import Rel8.Table.Eq ( EqTable, eqTable )
4850import Rel8.Table.Ord ( OrdTable, ordTable )
49−import Rel8.Table.Projection ( Projectable, project, apply )
51+import Rel8.Table.Projection
52+ ( Projectable, Projecting, Projection, project, apply
53+ )
5054import Rel8.Table.Serialize ( ToExprs )
5155
5256
@@ -116,6 +120,13 @@ instance (context ~ Expr, Table Expr a) =>
116120 mempty = ListTable $ hempty $ \Spec {info} -> sempty info
117121
118122
123+-- | Project a single expression out of a 'ListTable'.
124+($*) :: Projecting a (Expr b)
125+ => Projection a (Expr b) -> ListTable Expr a -> Expr [b]
126+f $* ListTable a = hcolumn $ hproject (apply f) a
127+infixl 4 $*
128+
129+
119130-- | Construct a @ListTable@ from a list of expressions.
120131listTable :: Table Expr a => [a] -> ListTable Expr a
121132listTable =
src/Rel8/Table/NonEmpty.hs+14−3
@@ -11,7 +11,9 @@
1111
1212module Rel8.Table.NonEmpty
1313 ( NonEmptyTable(..)
14− , nonEmptyTable, nameNonEmptyTable
14+ , ($+)
15+ , nonEmptyTable
16+ , nameNonEmptyTable
1517 )
1618where
1719
@@ -29,7 +31,7 @@ import Rel8.Schema.HTable.NonEmpty ( HNonEmptyTable )
2931import Rel8.Schema.HTable.Vectorize
3032 ( hvectorize, hunvectorize
3133 , happend
32− , hproject
34+ , hproject, hcolumn
3335 )
3436import qualified Rel8.Schema.Kind as K
3537import Rel8.Schema.Name ( Name( Name ) )
@@ -44,7 +46,9 @@ import Rel8.Table
4446import Rel8.Table.Alternative ( AltTable, (<|>:) )
4547import Rel8.Table.Eq ( EqTable, eqTable )
4648import Rel8.Table.Ord ( OrdTable, ordTable )
47−import Rel8.Table.Projection ( Projectable, project, apply )
49+import Rel8.Table.Projection
50+ ( Projectable, Projecting, Projection, project, apply
51+ )
4852import Rel8.Table.Serialize ( ToExprs )
4953
5054
@@ -110,6 +114,13 @@ instance (Table Expr a, context ~ Expr) => Semigroup (NonEmptyTable context a)
110114 happend (const sappend1) as bs
111115
112116
117+-- | Project a single expression out of a 'NonEmptyTable'.
118+($+) :: Projecting a (Expr b)
119+ => Projection a (Expr b) -> NonEmptyTable Expr a -> Expr (NonEmpty b)
120+f $+ NonEmptyTable a = hcolumn $ hproject (apply f) a
121+infixl 4 $+
122+
123+
113124-- | Construct a @NonEmptyTable@ from a non-empty list of expressions.
114125nonEmptyTable :: Table Expr a => NonEmpty a -> NonEmptyTable Expr a
115126nonEmptyTable =