Skip to content

Add Set.singleton >>/|> Set.toList fix#476

Merged
jfmengels merged 4 commits intojfmengels:mainfrom
morteako:set-singleton-to-list
Mar 28, 2026
Merged

Add Set.singleton >>/|> Set.toList fix#476
jfmengels merged 4 commits intojfmengels:mainfrom
morteako:set-singleton-to-list

Conversation

@morteako
Copy link
Copy Markdown
Contributor

Solves the set-part of #470

Set.toList (Set.singleton v) to [ v ]
Set.singleton >> Set.toList to List.singleton

Comment thread src/Simplify.elm
Comment on lines +11575 to +11595
, { call =
\checkInfo ->
AstHelpers.getSpecificUnreducedFnCall Fn.Set.singleton checkInfo.lookupTable checkInfo.firstArg
|> Maybe.map
(\setSingletonCall ->
Rule.errorWithFix
{ message = qualifiedToString Fn.Set.toList ++ " on " ++ qualifiedToString Fn.Set.singleton ++ " will result in singleton list with that element"
, details = [ "You can replace this call by a singleton list with that element." ]
}
checkInfo.fnRange
[ Fix.replaceRangeBy checkInfo.parentRange ("[ " ++ checkInfo.extractSourceCode (Node.range setSingletonCall.firstArg) ++ " ]") ]
)
, composition =
(onSpecificFnCallCanBeCombinedCheck
{ args = []
, earlierFn = Fn.Set.singleton
, combinedFn = Fn.List.singleton
}
).composition
}
]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done this way to ensure that the rule give the literal list value for the call-version.

I guess one could use onSpecificFnCallCanBeCombinedCheck to suggest the List.singleton in both cases, and it will in the end just be literal i think?

Or if there is a cleaner option

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I agree that the call case should be handled separately.

As for onSpecificFnCallCanBeCombinedCheck, I think that deserves a rework in a separate PR anyway as it's often called with args = [] which it is pretty wasteful. That simpler helper could then also use a composition helper which we can use here instead :)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

as it's often called with args = [] which it is pretty wasteful

How is that wasteful? It's only allocating another field, which is pretty cheap I'd say. There is no allocation for [] since that's just a constant reference. I feel like I'm missing something.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

onSpecificFnCallCanBeCombinedCheck has multiple lets and a bunch of code specific to handling these args that could be skipped. I think you'll agree once I make the PR, especially because args = [] is used a whole bunch.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Ah gotcha. Makes sense 👍

@morteako morteako marked this pull request as ready for review March 28, 2026 06:44
Copy link
Copy Markdown
Collaborator

@lue-bird lue-bird left a comment

Choose a reason for hiding this comment

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

looks nice, thank you!

Comment thread src/Simplify.elm Outdated
Comment on lines +11581 to +11583
{ message = qualifiedToString Fn.Set.toList ++ " on " ++ qualifiedToString Fn.Set.singleton ++ " will result in singleton list with that element"
, details = [ "You can replace this call by a singleton list with that element." ]
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

small possible improvements

-will result in singleton list with that element
+will result in a singleton list

(omitting "that element" because we don't know what that refers to. Its probably better explained in the details)

-You can replace this call by a singleton list with that element.
+You can replace this call by a singleton list containing the element given to the ++ Fn.Set.singleton ++ call.

(making it a bit more clear what "that element" is)

Comment thread src/Simplify.elm
Comment on lines +11575 to +11595
, { call =
\checkInfo ->
AstHelpers.getSpecificUnreducedFnCall Fn.Set.singleton checkInfo.lookupTable checkInfo.firstArg
|> Maybe.map
(\setSingletonCall ->
Rule.errorWithFix
{ message = qualifiedToString Fn.Set.toList ++ " on " ++ qualifiedToString Fn.Set.singleton ++ " will result in singleton list with that element"
, details = [ "You can replace this call by a singleton list with that element." ]
}
checkInfo.fnRange
[ Fix.replaceRangeBy checkInfo.parentRange ("[ " ++ checkInfo.extractSourceCode (Node.range setSingletonCall.firstArg) ++ " ]") ]
)
, composition =
(onSpecificFnCallCanBeCombinedCheck
{ args = []
, earlierFn = Fn.Set.singleton
, combinedFn = Fn.List.singleton
}
).composition
}
]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I agree that the call case should be handled separately.

As for onSpecificFnCallCanBeCombinedCheck, I think that deserves a rework in a separate PR anyway as it's often called with args = [] which it is pretty wasteful. That simpler helper could then also use a composition helper which we can use here instead :)

Comment thread src/Simplify.elm Outdated
, details = [ "You can replace this call by a singleton list with that element." ]
}
checkInfo.fnRange
[ Fix.replaceRangeBy checkInfo.parentRange ("[ " ++ checkInfo.extractSourceCode (Node.range setSingletonCall.firstArg) ++ " ]") ]
Copy link
Copy Markdown
Collaborator

@lue-bird lue-bird Mar 28, 2026

Choose a reason for hiding this comment

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

usually fixes use keepOnlyAndSurroundWithFix:

keepOnlyAndSurroundWithFix
    { parentRange = checkInfo.parentRange
    , keep = Node.range setSingletonCall.firstArg
    , left = "[ "
    , right = " ]"
    }

This is faster (and IMO extractSourceCode should be a last resort)

@morteako morteako force-pushed the set-singleton-to-list branch from c59aa98 to 1c0560a Compare March 28, 2026 09:52
Comment thread src/Simplify.elm Outdated
Rule.errorWithFix
{ message = qualifiedToString Fn.Set.toList ++ " on " ++ qualifiedToString Fn.Set.singleton ++ " will result in singleton list with that element"
, details = [ "You can replace this call by a singleton list with that element." ]
{ message = qualifiedToString Fn.Set.toList ++ " on " ++ qualifiedToString Fn.Set.singleton ++ " will result in singleton list"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

-will result in singleton list
+will result in a singleton list

@jfmengels jfmengels merged commit 8cdeb99 into jfmengels:main Mar 28, 2026
2 checks passed
@jfmengels
Copy link
Copy Markdown
Owner

Thank you for the PR ☺️

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.

3 participants