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
startsWith / endsWith: We have them already. We can eventually add versions for other collections (Seq, List, Array).
contains: We have it already. Since List.contains exists, we need to match, so we'll take a single char. To check substrings we have isSubstring.
findIndex / findIndexAfter / findLastIndex / findLastIndexAfter : I think the most important is findIndex, since List.findIndex exists already, we'll need to match it by accepting a predicate. We can also match by adding tryFindIndex.
insertAt / removeAt / removeAfter: Not sure if they are that useful.
remove : Not sure it worths, if we take the indices out, it's simply replace x "" y.
substringAfter / substring: They use indices, if we have take and skip we have already that functionality in a function that exists also in other collections.
normalize / normalizeWithForm: We have normalize already which in fact is normalizeWithForm.
toLower / toUpper: We have them already, not accepting culture as it's more common to use them as CI, you can always use the member if you want to specify culture.
padLeft/ padLeftBy/ padRight/ padRightBy: We can add them but I would call padLeftWith instead of padLeftBy
trim : We have it as trimWhiteSpaces
trimBy / trimBySeq : We might have to add them, not sure it if worths.
trimStart / trimEnd / trimStartBy / trimStartBySeq : I would vote to add only the version that takes an array of strings since it's the most complete.
removeEmptyEntries : Not sure if it worths.
toChars / ofChars: We need to add them.
nth : We can add item instead.
rev : We can add it and make it match with rev for collections.
whileBase: Looks very specific.
take / skip : We need to add them, we have them already working with the generic functions, we can also add the safe versions limit / drop, see note (*).
takeWhile / skipWhile: We need to add them, we have them already working with the generic functions.
explode / implode : They seem to be the same as toChars / ofChars. Just a matter of naming, I personally like more the toChars / ofChars names.
splitOpt / split: Seems to be already covered by split.
contains / contains2 : contains2 exists as isSubString, the version with the arguments reversed is not worth adding IMHO.
trim : same comments as in the previous listing.
stripDQuotes / stripSQuotes / stripQuotes / digitsOnly / lettersOnly: Very specific ones, their implementation reads better and are easy to use IMHO.
startsWith: We have it already.
indexOf / indexOfC: I think we can add findIndex see comments above, in the first listing.
padLeft : same comments as in the previous listing.
toCodePoints / fromCodePoints: Not sure, looks interesting, are there good use cases around?
hasText / hasNonWhitespace: Very specific ones, their implementation reads better and are easy to use IMHO.
replace: we have it already.
take: same comments as in the previous listing.
truncate : We can add it, together with drop. Note (*).
getBytes : Nice to have, not sure about the name, we have already generic toBytes / ofBytes
subStr : Same comment as in the previous listing.
(*) String.truncate is the same as limit when used with a string. The decision not to use the name truncate was made in order to avoid shadowing the math truncate function. The question remains if we're going to add this function in the string module, should we add it as truncate, limit or both (by duplicating it)?
This is just a first overview, feel free to disagree, add your comments, also let us know if there are other interesting functions out there.
whileBase is used to implement takeWhile and skipWhile and not exposed to users so you can ignore it.
I can't remember why removeEmptyEntries exists while I have splitSkipEmpty. You can ignore it too.
You did't mention split* functions. What's your thought on them?
For functions which have counterparts in BCL string members, I have added them so that I don't have to call the members (which breaks type inference). I agree to your "Not sure if they are that useful" but I consider completeness more important than usefulness. The same applies to "you can always use the member if you want to specify culture".
For the note (*), I like to have both by duplicating it. truncate corresponds well to its BCL member counterpart while limit matches to the other naming convention of F#+, so I can't tell which is better.
I think I missed the explanation of split as I mentioned already but it was in the channel, not here.
So, if you go to the related issue (which is a fslang suggestion) in the very first comment, you'll see how the split function we have already covers most splitting cases, otherwise let me know a specific case and I will try to show you how it is covered. Also note that that split function is consistent with other collections, and it has a generic version that follows some rules if you consider a string as an array of chars. I'm happy to expand on this.
Regarding the functions accepting a culture, we could consider them if we find a nice way to organize them. Maybe with a naming convention, the problem with this approach is that when you start combining them they become a bit odd.
Another possibility is considering some sort of dependency injection, like opening a module which is in fact a type, accepting a parameter (something like OCaml functors) which provides the same functions but working with a specific culture, this has the advantage that leads to less repetitive code, because you specify the culture only once.
I saw the StringBuilder extensions, definitely we can consider them, do you have some specific use cases to illustrate?
I've also had issues around culture and how some libraries goes with thread culture. It can become quite annoying when the culture is per user and you don't know that execution related to specific users will be grouped into specific threads.
I don't think we'll allow culture sensitive functions as it will break referential transparency, nor would I allow them to be in the .net framework had I the power to decide over their inclusion. I guess we will all agree these functions are out of discussion.
But the functions from @cannorin are OK from that perspective, since they take the culture as an explicit parameter, so that lead us to some interesting discussions.
@gusty For toCodePoints / fromCodePoints, consider strings with surrogate pairs:
The following character: U+2C288 (𬊈), we'll use FSI to evaluate the following:
"𬊈" |> Seq.toArray
// val it : char [] = [|'�'; '�'|]
"𬊈" |> toCodePoints
// val it : seq<int> = seq [180872]
The char is a 16-bit data-type, and represents the UTF-16 values. This means, for a character like the above which is encoded as 0xD870 0xDE88, the UTF-16 string in .NET represents it with twochar values. Where Seq.toArray pulls just the char values, String.toCodePoints as I implemented takes the two surrogate-pairs and converts them to a singleint value.
Up to you if you want to take it or not, but I use it a lot to ensure I deal with internationalized text properly when I'm doing single-character work. It's also necessary for "emoji" work, as emojis fall under the supplementary planes (U+010000 to U+10FFFF).
I think it's more important to match the .truncate of other collections, than matching the generic limit function.
What is not clear to me is whether to add only truncate or add limit as well.
The current design encourage using the unqualified function for the generic versions, but that's not always possible. There is also another case with nth / item. Apart from the fact that nth was deprecated in F# core, the generic versions have a slightly different meaning (item is more tied to the indexable abstraction).
I don't see a need for limit, TBQH. I think it adds another thing that might cause confusion. Why would we have two identical functions? As a new user, I might now think they do different things. Seq doesn't have limit, and since a String is a char seq, I don't think String should have limit.
That's true. It's better to live with the exceptional case that truncate alone clash with our generic truncate and we use limit ONLY there, to move forward with that problem.
We do our best to have consistent names, specially if the base (FSharp.Core) is not in good shape in this regard.
Agreed. Consistency within the ecosystem should always be priority 1, that's what encourages people to take a look.
I also think the triple-slash XML doc comments are 100% necessary, so even if we want to change them that's fine, I just want to make sure that we use them for others.
All of my string functions have them. If you want to pull any in, you are more than welcome to and I'm waiving any "right to credit" or anything. For the purposes of this library, you have express access to mine and to ignore the MIT license credit / copyright requirements. You can pull the XML doc comments (triple-slashes) as well.
@gusty Functor-like dependency injection sounds good to me. For the StringBuilder extension, it will be used like below with the String.build function from my String.fs:
The motivation to use this extension is to avoid the repetitive use of ignore caused by the fact StringBuilder's Write and WriteLine returns the StringBuilder for method chaining. Also, sb.WriteLine(sprintf "%i" 42).WriteLine(sprintf "%s" "foo").ToString() looks odd and ugly imo, but this might be just a personal impression.
As from the previous discussions, these are the candidates so far:
findIndex / tryFindindex
padLeft / padRight / padLeftWith / padRightWith
trim / trimStart / trimEnd : taking an array.
toCharArray / ofCharArray (Alternate name toArray / ofArray) (*)
item (*)
rev (*)
take / skip / takeWhile / skipWhile / truncate / drop (*)
toCodePoints / ofCodePoints
getBytes : I propose to make it accept always an encoding.
(*) Generic version already exists.
Note: the build and the StringBuilder functions looks nice to me, but let's open another issue for StringBuilder extensions as at the moment we don't have any of them, and it might require more discussion.
Regarding the functions taking a culture as parameter, I propose also to discuss them in a different issue, because I think they need more in depth study as I can't find a good proposal at the moment. So let's move them to another issue in order to speed up this one.
skip does read more cleanly then Substring... but not as good as takeLast or takeFromEnd.
I didn't know about slices 👍 thanks! I'm learning FSharpPlus and even Fsharp slowly. I have more F# than C# experience so it's interesting :).
I note, you have padLeft/padRight and trimLeft/trimRight and startsWith/endsWith... those are for strings only... but I thought it would be good/consistent to support the same kind of thing working for slices - I find it easy to follow take, drop, but when working from the back of a string I'd have to think down to a lower level (get length subtract x... etc) - so I'd want that helper function.... I know I can always create my own but seems like the point of this library.
Why not add similar duals for end of string?
takeFromEnd, dropFromEnd, truncateEnd, skipEnd
Is it because would make a mess of generic types like Take, etc?
Negative input?
That negative slice index sounds like it would have been good.
Do you think it makes sense to have negative numbers on take, skip, truncate, drop?
drop -2 = remove last two chars take -2 = take last two chars truncate -2 = take last two chars skip -2 = remove last two chars
I suppose you want to be compatible with .net :(
Throwing...
take/skip use indexing and can throw, where truncate/drop are more lenient.
I first assumed take would not throw but I guess it's something that must be learned and not so obvious in FSharpPlus.
I'd like to have tryTake and trySkip to make it clearer....
I think that is very clear API and functions are self explanatory. They don't throw, and just cut off (acting like truncate/drop).
It's interesting that Elm 'List' has head/tail/take/drop more closer to Haskell, and doesn't try to match to a generic. You could do that here too... use specific functions?
Also, coming from Elm, I kind of wish the last/tryLast pattern was unsafeLast/last, but I'm not going to convince all the F# world :)
Finally, to be clear, I like FSharpPlus a lot and hope to maybe give an outsiders perspective. I haven't used it yet, but keep getting drawn back to it. Will probably pull it in soon to my F# codebases.
iirc Seq.take and Seq.skip are the only implementation in the standard library not throwing an exception (unless iterated to the end of the sequence, as it is lazily evaluated).
tryTake / trySkip variant which returns option<f<'a>> is very hard to implement in seq because they are lazy and their length is not available until iterated.
Similarly, takeFromEnd, dropFromEnd, truncateEnd, skipEnd are all hard to properly implement in seq (consider an infinite sequence). Personally I disagree to accept negative inputs on take etc, as it would act differently for different collections (seq does not implement slicing for the same reason above) and just confuse the users (read: me).
Also, imho F# is F# and not Elm or Haskell, and F#+, as the name suggests, is also not a library to make F# look like Elm of Haskell. Each language has its own culture, and we should respect it when writing that language (btw I agree Elm has clearer API, so don't get me wrong. It just does not fit to F# culture imo).
Indeed. I agree. Though Take and Skip works in a certain way in the standard library of .net. Defining take and skip over string you'd expect them to work and fail in a similar manner.
We could add them, but yes, it will duplicate the number of functions we have (skip, take, truncate, drop, skipWhile, takeWhile). IMHO when designing a library, specially for FP you should not aim to cover all with specific functions but to have composable functions that together can cover the most, and right now combining those functions with length gives you that functionality.
I am a bit less concerned for the infinite seq case since FSharp.Core already has functions that fail on infinite sequences or that are not as efficient in seq's as there was a decision some years ago in FSharp.Core to prioritize consistency. Here in F#+ that decision was from the beginning and it's written in our design guidelines.
Negative input?
I really, really like that idea and it's not just ELM, also python and javascript have them, but as already stated, F# didn't went in that direction (.NET in general didn't), though there is a long standing suggestion to add it, with interesting arguments against it. This is a sensitive topic and it needs some discussion, if they ever solve the "shadowing" problem for indices I would open an issue to start a discussion on negative slices.
Throwing...
Unlike the previous point, here's a decision that F# did which surprisingly did not went in the C# direction. For some reason (maybe by mistake or to align with slices) FSharp.Core came to light with take and skip functions that throw. As much as we disagree with it, we can't fix that here as it would cause confusion, possible unexpected and hidden breaking changes to existing code that is starting to reference this library.
So that's why this library added the safe versions with different names drop and limit (or truncate when non generic). The set of try functions have a different meaning, they would be return Some x (or None) and we'll be force to match all the time. I would like to see a scenario where they are justified, given that we have the "dropping" versions.
Just for comparison...
These comparisons are interesting and other languages are a great source of inspiration, I have a look at Haskell and Purescript mainly to see how they solve specific issues and if it doesn't create a problem in F# we can adopt it, but Haskell is old and also has its own problems due to wrong decisions made in the past, so it's not something to follow blindly and new languages (like ELM) are also interesting.
I do sympathize with the convention of unsafeX and I would like to add as much as possible total functions by default but unfortunately if we take it to extremes we will also start contradicting and introducing unexpected changed to F# core functions. Actually I think there is a problem in that there is no current convention as tryX normally returns an option but it could also return a Choice or a Result (Either in other languages).
I'm glad to have a view from someone without C# / F# experience, I can imagine that many critics from such point of view will be for things that are actually consequence decisions that were are actually made earlier, in FSharp.Core and that we are kind of forced in that direction, even if we're not happy with that. As stated before, we have to be careful in this concern, this library already went into fields that are controversials and not specially loved by some part of the F# community, but we can stand it as long as it doesn't break or force you to use it. We try not to be invasive, just add more stuff to F# as its name suggest it.
There were some discussions recently because people (possible like you) who are beginners in F# are learning F# with this library and getting confused and I think that's an aspect to consider, as this library aims for more consistency it should be exactly the opposite experience IMHO.
@gusty thanks for your thoughts too. I guess I'd prefer FSharpTotal but it's not reasonable with culture :)
I like the idea of being consistent. It helps discoverability.
BTW I'm not learning F# from this library, but keep hitting things I'm making little utils for (using Result and would like computation expression, or String functions, or Async.map... etc).
I learned C# a few years back, and after a few months, I translated our .net codebases to F#. They're not big, so doable - it's a smaller side system from the main app we work on. So more F# than C# 👍 but still need to look at how .net or C# works often.
Activity
cannorin commentedon Jul 16, 2019
Here is what I currently have: https://github.com/cannorin/prelude.fs/blob/master/src/String.fs
gusty commentedon Jul 17, 2019
Here's an exhaustive list with comments, function by function.
Looking at @cannorin link, we have:
startsWith/endsWith: We have them already. We can eventually add versions for other collections (Seq, List, Array).contains: We have it already. SinceList.containsexists, we need to match, so we'll take a single char. To check substrings we haveisSubstring.findIndex/findIndexAfter/findLastIndex/findLastIndexAfter: I think the most important isfindIndex, sinceList.findIndexexists already, we'll need to match it by accepting a predicate. We can also match by addingtryFindIndex.insertAt/removeAt/removeAfter: Not sure if they are that useful.remove: Not sure it worths, if we take the indices out, it's simplyreplace x "" y.substringAfter/substring: They use indices, if we havetakeandskipwe have already that functionality in a function that exists also in other collections.normalize/normalizeWithForm: We havenormalizealready which in fact isnormalizeWithForm.toLower/toUpper: We have them already, not accepting culture as it's more common to use them asCI, you can always use the member if you want to specify culture.padLeft/padLeftBy/padRight/padRightBy: We can add them but I would callpadLeftWithinstead ofpadLeftBytrim: We have it astrimWhiteSpacestrimBy/trimBySeq: We might have to add them, not sure it if worths.trimStart/trimEnd/trimStartBy/trimStartBySeq: I would vote to add only the version that takes an array of strings since it's the most complete.removeEmptyEntries: Not sure if it worths.toChars/ofChars: We need to add them.nth: We can additeminstead.rev: We can add it and make it match withrevfor collections.whileBase: Looks very specific.take/skip: We need to add them, we have them already working with the generic functions, we can also add the safe versionslimit/drop, see note (*).takeWhile/skipWhile: We need to add them, we have them already working with the generic functions.build: We need to add it, not sure of the name.Now looking at @EBrown8534 work we have:
explode/implode: They seem to be the same astoChars/ofChars. Just a matter of naming, I personally like more thetoChars/ofCharsnames.splitOpt/split: Seems to be already covered bysplit.contains/contains2:contains2exists asisSubString, the version with the arguments reversed is not worth adding IMHO.trim: same comments as in the previous listing.stripDQuotes/stripSQuotes/stripQuotes/digitsOnly/lettersOnly: Very specific ones, their implementation reads better and are easy to use IMHO.startsWith: We have it already.indexOf/indexOfC: I think we can addfindIndexsee comments above, in the first listing.padLeft: same comments as in the previous listing.toCodePoints/fromCodePoints: Not sure, looks interesting, are there good use cases around?hasText/hasNonWhitespace: Very specific ones, their implementation reads better and are easy to use IMHO.replace: we have it already.take: same comments as in the previous listing.truncate: We can add it, together withdrop. Note (*).getBytes: Nice to have, not sure about the name, we have already generictoBytes/ofBytessubStr: Same comment as in the previous listing.(*)
String.truncateis the same aslimitwhen used with a string. The decision not to use the nametruncatewas made in order to avoid shadowing the mathtruncatefunction. The question remains if we're going to add this function in the string module, should we add it astruncate,limitor both (by duplicating it)?This is just a first overview, feel free to disagree, add your comments, also let us know if there are other interesting functions out there.
cannorin commentedon Jul 17, 2019
whileBaseis used to implementtakeWhileandskipWhileand not exposed to users so you can ignore it.removeEmptyEntriesexists while I havesplitSkipEmpty. You can ignore it too.split*functions. What's your thought on them?StringBuilderwithprintf/printfnis very useful in practice: https://github.com/cannorin/prelude.fs/blob/master/src/String.fs#L137-L146 . I love to hear your comment about it too.Thanks for the input ❤️
cannorin commentedon Jul 17, 2019
For the note (*), I like to have both by duplicating it.
truncatecorresponds well to its BCL member counterpart whilelimitmatches to the other naming convention of F#+, so I can't tell which is better.gusty commentedon Jul 17, 2019
Great feedback.
I think I missed the explanation of
splitas I mentioned already but it was in the channel, not here.So, if you go to the related issue (which is a fslang suggestion) in the very first comment, you'll see how the
splitfunction we have already covers mostsplittingcases, otherwise let me know a specific case and I will try to show you how it is covered. Also note that thatsplitfunction is consistent with other collections, and it has a generic version that follows some rules if you consider a string as an array of chars. I'm happy to expand on this.Regarding the functions accepting a culture, we could consider them if we find a nice way to organize them. Maybe with a naming convention, the problem with this approach is that when you start combining them they become a bit odd.
Another possibility is considering some sort of dependency injection, like opening a module which is in fact a type, accepting a parameter (something like OCaml functors) which provides the same functions but working with a specific culture, this has the advantage that leads to less repetitive code, because you specify the culture only once.
I saw the StringBuilder extensions, definitely we can consider them, do you have some specific use cases to illustrate?
wallymathieu commentedon Jul 17, 2019
I've also had issues around culture and how some libraries goes with thread culture. It can become quite annoying when the culture is per user and you don't know that execution related to specific users will be grouped into specific threads.
gusty commentedon Jul 17, 2019
I don't think we'll allow culture sensitive functions as it will break referential transparency, nor would I allow them to be in the .net framework had I the power to decide over their inclusion. I guess we will all agree these functions are out of discussion.
But the functions from @cannorin are OK from that perspective, since they take the culture as an explicit parameter, so that lead us to some interesting discussions.
EBrown8534 commentedon Jul 17, 2019
@gusty For
toCodePoints/fromCodePoints, consider strings with surrogate pairs:The following character: U+2C288 (𬊈), we'll use FSI to evaluate the following:
The
charis a 16-bit data-type, and represents the UTF-16 values. This means, for a character like the above which is encoded as0xD870 0xDE88, the UTF-16 string in .NET represents it with twocharvalues. WhereSeq.toArraypulls just thecharvalues,String.toCodePointsas I implemented takes the two surrogate-pairs and converts them to a singleintvalue.Up to you if you want to take it or not, but I use it a lot to ensure I deal with internationalized text properly when I'm doing single-character work. It's also necessary for "emoji" work, as emojis fall under the supplementary planes (U+010000 to U+10FFFF).
EBrown8534 commentedon Jul 17, 2019
With regard to
truncate:Array,ListandSequsetruncate, if we intend to match those API's we must usetruncateas the name, even though it may shadow the math function.That said, so long as
RequireQualifiedAccessis used, we should be OK, because one would need toString.truncateanyway.gusty commentedon Jul 17, 2019
I agree, we need to match them.
I think it's more important to match the
.truncateof other collections, than matching the genericlimitfunction.What is not clear to me is whether to add only
truncateor addlimitas well.The current design encourage using the unqualified function for the generic versions, but that's not always possible. There is also another case with
nth/item. Apart from the fact thatnthwas deprecated in F# core, the generic versions have a slightly different meaning (itemis more tied to the indexable abstraction).EBrown8534 commentedon Jul 17, 2019
I don't see a need for
limit, TBQH. I think it adds another thing that might cause confusion. Why would we have two identical functions? As a new user, I might now think they do different things.Seqdoesn't havelimit, and since aStringis achar seq, I don't thinkStringshould havelimit.gusty commentedon Jul 17, 2019
That's true. It's better to live with the exceptional case that
truncatealone clash with our generictruncateand we uselimitONLY there, to move forward with that problem.We do our best to have consistent names, specially if the base (FSharp.Core) is not in good shape in this regard.
EBrown8534 commentedon Jul 17, 2019
Agreed. Consistency within the ecosystem should always be priority 1, that's what encourages people to take a look.
I also think the triple-slash XML doc comments are 100% necessary, so even if we want to change them that's fine, I just want to make sure that we use them for others.
All of my string functions have them. If you want to pull any in, you are more than welcome to and I'm waiving any "right to credit" or anything. For the purposes of this library, you have express access to mine and to ignore the MIT license credit / copyright requirements. You can pull the XML doc comments (triple-slashes) as well.
cannorin commentedon Jul 17, 2019
@gusty Functor-like dependency injection sounds good to me. For the StringBuilder extension, it will be used like below with the
String.buildfunction from my String.fs:The motivation to use this extension is to avoid the repetitive use of
ignorecaused by the fact StringBuilder'sWriteandWriteLinereturns the StringBuilder for method chaining. Also,sb.WriteLine(sprintf "%i" 42).WriteLine(sprintf "%s" "foo").ToString()looks odd and ugly imo, but this might be just a personal impression.gusty commentedon Jul 18, 2019
As from the previous discussions, these are the candidates so far:
(*) Generic version already exists.
Note: the
buildand the StringBuilder functions looks nice to me, but let's open another issue for StringBuilder extensions as at the moment we don't have any of them, and it might require more discussion.Regarding the functions taking a culture as parameter, I propose also to discuss them in a different issue, because I think they need more in depth study as I can't find a good proposal at the moment. So let's move them to another issue in order to speed up this one.
Thoughts? Anyone willing to take care of the PR ?
21 remaining items
adz commentedon Jul 25, 2019
skipdoes read more cleanly thenSubstring... but not as good astakeLastortakeFromEnd.I didn't know about slices 👍 thanks! I'm learning FSharpPlus and even Fsharp slowly. I have more F# than C# experience so it's interesting :).
I note, you have padLeft/padRight and trimLeft/trimRight and startsWith/endsWith... those are for strings only... but I thought it would be good/consistent to support the same kind of thing working for slices - I find it easy to follow
take,drop, but when working from the back of a string I'd have to think down to a lower level (get length subtract x... etc) - so I'd want that helper function.... I know I can always create my own but seems like the point of this library.takeFromEnd,dropFromEnd,truncateEnd,skipEndIs it because would make a mess of generic types like
Take, etc?That negative slice index sounds like it would have been good.
Do you think it makes sense to have negative numbers on take, skip, truncate, drop?
drop -2= remove last two charstake -2= take last two charstruncate -2= take last two charsskip -2= remove last two charsI suppose you want to be compatible with .net :(
take/skip use indexing and can throw, where truncate/drop are more lenient.
I first assumed
takewould not throw but I guess it's something that must be learned and not so obvious in FSharpPlus.I'd like to have
tryTakeandtrySkipto make it clearer....Elm has left/right (same as take/takeEnd), dropLeft/dropRight, and slice (allowing negatives)
https://package.elm-lang.org/packages/elm/core/latest/String
I think that is very clear API and functions are self explanatory. They don't throw, and just cut off (acting like truncate/drop).
It's interesting that Elm 'List' has head/tail/take/drop more closer to Haskell, and doesn't try to match to a generic. You could do that here too... use specific functions?
Also, coming from Elm, I kind of wish the
last/tryLastpattern wasunsafeLast/last, but I'm not going to convince all the F# world :)Thanks for work on this library ❤️
wallymathieu commentedon Jul 25, 2019
I would not assume that take would throw either, so could be that you have identified a bug.
cannorin commentedon Jul 25, 2019
iirc
Seq.takeandSeq.skipare the only implementation in the standard library not throwing an exception (unless iterated to the end of the sequence, as it is lazily evaluated).tryTake / trySkipvariant which returnsoption<f<'a>>is very hard to implement inseqbecause they are lazy and their length is not available until iterated.cannorin commentedon Jul 25, 2019
Similarly,
takeFromEnd, dropFromEnd, truncateEnd, skipEndare all hard to properly implement inseq(consider an infinite sequence). Personally I disagree to accept negative inputs ontakeetc, as it would act differently for different collections (seqdoes not implement slicing for the same reason above) and just confuse the users (read: me).Also, imho F# is F# and not Elm or Haskell, and F#+, as the name suggests, is also not a library to make F# look like Elm of Haskell. Each language has its own culture, and we should respect it when writing that language (btw I agree Elm has clearer API, so don't get me wrong. It just does not fit to F# culture imo).
wallymathieu commentedon Jul 25, 2019
Indeed. I agree. Though
TakeandSkipworks in a certain way in the standard library of .net. Defining take and skip over string you'd expect them to work and fail in a similar manner.gusty commentedon Jul 25, 2019
@adz
We could add them, but yes, it will duplicate the number of functions we have (skip, take, truncate, drop, skipWhile, takeWhile). IMHO when designing a library, specially for FP you should not aim to cover all with specific functions but to have composable functions that together can cover the most, and right now combining those functions with
lengthgives you that functionality.I am a bit less concerned for the infinite
seqcase since FSharp.Core already has functions that fail on infinite sequences or that are not as efficient in seq's as there was a decision some years ago in FSharp.Core to prioritize consistency. Here in F#+ that decision was from the beginning and it's written in our design guidelines.I really, really like that idea and it's not just ELM, also python and javascript have them, but as already stated, F# didn't went in that direction (.NET in general didn't), though there is a long standing suggestion to add it, with interesting arguments against it. This is a sensitive topic and it needs some discussion, if they ever solve the "shadowing" problem for indices I would open an issue to start a discussion on negative slices.
Unlike the previous point, here's a decision that F# did which surprisingly did not went in the C# direction. For some reason (maybe by mistake or to align with slices) FSharp.Core came to light with
takeandskipfunctions that throw. As much as we disagree with it, we can't fix that here as it would cause confusion, possible unexpected and hidden breaking changes to existing code that is starting to reference this library.So that's why this library added the safe versions with different names
dropandlimit(ortruncatewhen non generic). The set oftryfunctions have a different meaning, they would be returnSome x(or None) and we'll be force to match all the time. I would like to see a scenario where they are justified, given that we have the "dropping" versions.These comparisons are interesting and other languages are a great source of inspiration, I have a look at Haskell and Purescript mainly to see how they solve specific issues and if it doesn't create a problem in F# we can adopt it, but Haskell is old and also has its own problems due to wrong decisions made in the past, so it's not something to follow blindly and new languages (like ELM) are also interesting.
I do sympathize with the convention of
unsafeXand I would like to add as much as possible total functions by default but unfortunately if we take it to extremes we will also start contradicting and introducing unexpected changed to F# core functions. Actually I think there is a problem in that there is no current convention astryXnormally returns an option but it could also return a Choice or a Result (Either in other languages).I'm glad to have a view from someone without C# / F# experience, I can imagine that many critics from such point of view will be for things that are actually consequence decisions that were are actually made earlier, in FSharp.Core and that we are kind of forced in that direction, even if we're not happy with that. As stated before, we have to be careful in this concern, this library already went into fields that are controversials and not specially loved by some part of the F# community, but we can stand it as long as it doesn't break or force you to use it. We try not to be invasive, just add more stuff to F# as its name suggest it.
There were some discussions recently because people (possible like you) who are beginners in F# are learning F# with this library and getting confused and I think that's an aspect to consider, as this library aims for more consistency it should be exactly the opposite experience IMHO.
adz commentedon Jul 25, 2019
@cannorin Fair enough. I agree on most points.
I'd still like it on string :) I noticed FSharpLu went for 'right':
https://github.com/microsoft/fsharplu/blob/master/FSharpLu/Text.fs#L104
@gusty thanks for your thoughts too. I guess I'd prefer FSharpTotal but it's not reasonable with culture :)
I like the idea of being consistent. It helps discoverability.
BTW I'm not learning F# from this library, but keep hitting things I'm making little utils for (using Result and would like computation expression, or String functions, or Async.map... etc).
I learned C# a few years back, and after a few months, I translated our .net codebases to F#. They're not big, so doable - it's a smaller side system from the main app we work on. So more F# than C# 👍 but still need to look at how .net or C# works often.
gusty commentedon Jul 25, 2019
Indeed for
Stringthere's a good case for it, as opposed toSeq<_>.wallymathieu commentedon Jul 25, 2019
I didn't expect Seq.skip and Seq.take to work like that. I'm more used to the c# variant I guess 😉
Extend String module (#159)
Extend String module (#159)
cannorin commentedon Jul 25, 2019
Thanks @gusty!
Now we should create new issues regarding, iirc,
String.buildand StringBuilder extensionfindSliceIndexgusty commentedon Jul 25, 2019
Sure !
Feel free to go ahead with those issues, or any other you think.
Extend String module (#159)