Skip to content
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

feat(Setter): add: prepends setter #1058

Merged
merged 4 commits into from
Mar 7, 2024
Merged

feat(Setter): add: prepends setter #1058

merged 4 commits into from
Mar 7, 2024

Conversation

ncaq
Copy link
Contributor

@ncaq ncaq commented Mar 1, 2024

Add a setter that prepends the element, like cons. For balance, we also add a setter that also appends the element to the tail, like snoc. We often use this when adding elements to list structures and other order-conscious things. When we are doing natural language processing, for example, and we are adding processed data, it is more natural to add the resulting processed data to the head side if the original input source is closer to the head. Naturally we can also construct the processing by adding the tail if we think hard enough, but we don't want to work hard at that since we only need to prepare a function. The naming of the <>:~ function was lost with ~<>, but considering that the ~ symbol in the lenses setter operator is basically on the right, I used the : operator to indicate that it is closer to the cons of the list, rather than focusing on the contrast.

Add a setter that prepends the element, like `cons`.
For balance, we also add a setter that also appends the element to the tail, like `snoc`.
We often use this when adding elements to list structures and other order-conscious things.
When we are doing natural language processing, for example, and we are adding processed data, it is more natural to add the resulting processed data to the head side if the original input source is closer to the head.
Naturally we can also construct the processing by adding the tail if we think hard enough, but we don't want to work hard at that since we only need to prepare a function.
The naming of the `<>:~` function was lost with `~<>`, but considering that the `~` symbol in the lenses setter operator is basically on the right, I used the `:` operator to indicate that it is closer to the `cons` of the list, rather than focusing on the contrast.
@RyanGlScott
Copy link
Collaborator

RyanGlScott commented Mar 4, 2024

Thanks, @ncaq!

Looking at the lens wiki page on infix operators, I wonder if it would make sense to add "with result" variants of each of these operators. That is, introduce a (<<>:~) operator alongside (<>:~), and so on for the other operators.

ncaq added 2 commits March 6, 2024 23:52
Add "with result" variants.
Code review reflected.
What
===

Move `(<|~)`, `(<|=)`, `(|>~)`, `(|>=)` from `Control.Lens.Setter` to `Control.Lens.Cons`.

Why
===

I try impl with result variants to `Control.Lens.Lens` like other.
This requires the import of `Control.Lens.Cons` in `Control.Lens.Lens`.
But it cause cycle import.
So I decided to implement Cons-related operators with `Control.Lens.Cons`.
@ncaq
Copy link
Contributor Author

ncaq commented Mar 6, 2024

@RyanGlScott
"with result" variants have been added to each operator.
Cons operators are now written in the Cons module to avoid circular imports.

Add Cons operators of "with result" variants.
@ncaq
Copy link
Contributor Author

ncaq commented Mar 6, 2024

I made a mistake in describing the test...
I didn't feel the significance of leaving this in the commit log, so I force pushed it.

Copy link
Collaborator

@RyanGlScott RyanGlScott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @ncaq!

@RyanGlScott RyanGlScott merged commit 94236e7 into ekmett:master Mar 7, 2024
11 checks passed
@ncaq ncaq deleted the prepend branch March 8, 2024 06:23
RyanGlScott added a commit that referenced this pull request May 4, 2024
RyanGlScott added a commit that referenced this pull request May 11, 2024
RyanGlScott added a commit that referenced this pull request May 12, 2024
RyanGlScott added a commit that referenced this pull request May 12, 2024
RyanGlScott added a commit that referenced this pull request May 12, 2024
RyanGlScott added a commit that referenced this pull request May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants