You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The initial need was to group a list a words with punctuation into sentences:
isFinalWord word =
case String.right 1 word of
"!" -> True
"." -> True
"?" -> True
_ -> False
groupWhen isFinalWord ["Hello!", "I", "am", "Sébastien.", "I", "love", "elm!"]
== [ ["Hello!"], [ "I", "am", "Sébastien."], ["I", "love", "elm!"]]
So when the predicate returns True, we close the "current" group including the current element.
So first wording question, I'm not very happy with the "group" name since it is already use in this lib for other thing. Thoughts?
Then I had to group those sentences in paragraphs such that the total length of each paragraph is maximum X chars long.
So I created groupWhenWithAcc.
groupWhenWithAcc : (a -> acc -> ( Bool, acc )) -> acc -> List a -> List (List a)
groupWhenWithAcc
(\ sentence charCount ->
let newCount = String.length sentence + charCount in
if newCount < X then
(False, newCount)
else
(True, 0)
)
[ "Hello, I'am Sébastien.", "I love elm.", "Thiiiiiiiis iiiiiiis a verrrrrrrrrrrry loooooooong sentenceeeeeee.", "Bye."]
And here I was a bit uncomfortable: the "long sentence" should have its own paragraph, but it was not possible with the current design, the splitWhenWithAcc would either:
Group [ "Hello, I'am Sébastien.", "I love elm.", "Thiiiiiiiis iiiiiiis a verrrrrrrrrrrry loooooooong sentenceeeeeee."]
or group ["Thiiiiiiiis iiiiiiis a verrrrrrrrrrrry loooooooong sentenceeeeeee.", "Bye."]
I went into the conclusion that the predicate should return something more elaborated than a bool -- my function doesn't have to decide whether include the current element in the current group or not. This is my wording question: what should be the name of the type and following variants:
type GroupWhenResponse=ContinueAndDontCreateANewGroup|FinishTheGroupIncludingTheCurrentElement|FinishTheGroupWithoutTheCurrentElementAndPutTheCurrentInASingleton|FinishTheGroupWithoutTheCurrentElementAndStartANewGroupWithCurrentElement
My function became so:
groupWhenWithAcc : (a -> acc -> ( GroupWhenResponse, acc )) -> acc -> List a -> List (List a)
groupWhenWithAcc
(\ sentence charCount ->
if String.length sentence > X then
( FinishTheGroupWithoutTheCurrentElementAndPutTheCurrentInASingleton, 0)
else
let newCount = String.length sentence + charCount in
if newCount < X then
(ContinueAndDontCreateANewGroup, newCount)
else
(FinishTheGroupWithoutTheCurrentElementAndStartANewGroupWithCurrentElement, 0)
)
[ "Hello, I'am Sébastien.", "I love elm.", "Thiiiiiiiis iiiiiiis a verrrrrrrrrrrry loooooooong sentenceeeeeee.", "Bye."]
The FinishTheGroupIncludingTheCurrentElement variant being used in my first function:
isFinalWord word =
case String.right 1 word of
"!" -> FinishTheGroupIncludingTheCurrentElement
"." -> FinishTheGroupIncludingTheCurrentElement
"?" -> FinishTheGroupIncludingTheCurrentElement
_ -> ContinueAndDontCreateANewGroup
groupWhen isFinalWord ["Hello!", "I", "am", "Sébastien.", "I", "love", "elm!"]
== [ ["Hello!"], [ "I", "am", "Sébastien."], ["I", "love", "elm!"]]
So what do you think about all of that? Ideas for wording? Am I going too far in customization?
The text was updated successfully, but these errors were encountered:
sebsheep
changed the title
splitWhen and splitWhenWithAcc proposal/design questiongroupWhen and groupWhenWithAcc proposal/design question
Mar 6, 2021
I think having a type with these very particular constructors adds a kind of complexity to the function that I think makes it harder to use in more general use cases.
Maybe having a function like (a -> acc -> (Bool, acc)) has some usefulness. I am not sure what the real world use case would be for that, but that function seems to have properties that dont exist in the current List.Extra api.
At work, I came to the need of some functions which I hoped to find in List.Extra. I'll expose what was my initial issue, and have some questions.
So here is the first function:
The initial need was to group a list a words with punctuation into sentences:
So when the predicate returns
True
, we close the "current" group including the current element.So first wording question, I'm not very happy with the "group" name since it is already use in this lib for other thing. Thoughts?
Then I had to group those sentences in paragraphs such that the total length of each paragraph is maximum X chars long.
So I created
groupWhenWithAcc
.And here I was a bit uncomfortable: the "long sentence" should have its own paragraph, but it was not possible with the current design, the
splitWhenWithAcc
would either:[ "Hello, I'am Sébastien.", "I love elm.", "Thiiiiiiiis iiiiiiis a verrrrrrrrrrrry loooooooong sentenceeeeeee."]
["Thiiiiiiiis iiiiiiis a verrrrrrrrrrrry loooooooong sentenceeeeeee.", "Bye."]
I went into the conclusion that the predicate should return something more elaborated than a bool -- my function doesn't have to decide whether include the current element in the current group or not. This is my wording question: what should be the name of the type and following variants:
My function became so:
The
FinishTheGroupIncludingTheCurrentElement
variant being used in my first function:So what do you think about all of that? Ideas for wording? Am I going too far in customization?
The text was updated successfully, but these errors were encountered: