-
Notifications
You must be signed in to change notification settings - Fork 2
WIP support sets (well, kinda of) #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |||
| module Data.API.Tools.Traversal | ||||
| ( traversalTool | ||||
| , traversalsTool | ||||
| , traverseSet | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is needed in the public API?
Suggested change
|
||||
| ) where | ||||
|
|
||||
| import Data.API.NormalForm | ||||
|
|
@@ -73,6 +74,12 @@ traversalType x an = [t| forall f . Applicative f => ($x' -> f $x') -> $ty -> f | |||
| ty = nodeT an | ||||
|
|
||||
|
|
||||
| -- | Traverse the elements of a 'Set.Set'. This is not a lawful 'Traversal' | ||||
| -- (it cannot be, because 'Set.Set' is not a 'Functor'), but it is what the | ||||
| -- traversals generated by 'traversalsTool' use for set-valued fields. | ||||
|
Comment on lines
+77
to
+79
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add this to the comments on |
||||
| traverseSet :: (Applicative f, Ord b) => (a -> f b) -> Set.Set a -> f (Set.Set b) | ||||
| traverseSet f = fmap Set.fromList . traverse f . Set.toList | ||||
|
|
||||
| -- | Construct a traversal of the X substructures of the given type | ||||
| traverser :: NormAPI -> Set.Set TypeName -> TypeName -> APIType -> ExpQ | ||||
| traverser napi targets x ty = fromMaybe [| const pure |] $ traverser' napi targets x ty | ||||
|
|
@@ -81,6 +88,11 @@ traverser napi targets x ty = fromMaybe [| const pure |] $ traverser' napi targe | |||
| -- or return 'Nothing' if there are no substructures to traverse | ||||
| traverser' :: NormAPI -> Set.Set TypeName -> TypeName -> APIType -> Maybe ExpQ | ||||
| traverser' napi targets x (TyList ty) = fmap (appE [e|(.) traverse|]) $ traverser' napi targets x ty | ||||
| -- 'Data.Set.Set' is not 'Traversable' (the element type is constrained by | ||||
| -- 'Ord'), but we can still traverse the elements via the element list. Note | ||||
| -- that this may shrink the set, if the function maps distinct elements of the | ||||
| -- original set to the same value. | ||||
| traverser' napi targets x (TySet ty) = fmap (appE [e|(.) traverseSet|]) $ traverser' napi targets x ty | ||||
| traverser' napi targets x (TyMaybe ty) = fmap (appE [e|(.) traverse|]) $ traverser' napi targets x ty | ||||
| traverser' napi targets x (TyName tn) | ||||
| | tn == x = Just [e| id |] | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully if we can get rid of
SetListthenUpdateSetalso becomes unnecessary?