Fsharpplus: Add NonEmptyList.unsafeOfList

Created on 5 Mar 2020  路  16Comments  路  Source: fsprojects/FSharpPlus

Just a very small helper - nothing more

let unsafeOfList xs = (tryOfList xs).Value

I think this should be part of the "default lib"

F#+ v1.1

All 16 comments

Thanks for the suggestion.

We just added Unchecked.toNonEmptyList here https://github.com/fsprojects/FSharpPlus/blob/d9e04660421f3a88f233cae1096988629d5d5a39/src/FSharpPlus/Data/NonEmptyList.fs#L131-L137

Do you think it serves the same purpose or do you see an additional value on the suggested one?

Btw: if you are writing NonEmptyList as literals now you can use this syntax let x = nel {1; 2; 3}

I think it's a little over-protective putting it in a separate module Unchecked. Compare List.head etc. And more importantly, it requires a sequence, which may lead to performance degradation on such a small operation.

If you could, consider putting them in List, Seq, Array and follow the semantics of F#. That is ofNonEmptyList and toNonEmptyList pairs on each module.

I think it's a little over-protective putting it in a separate module Unchecked.

Yes, I know, but the main reason NonEmptyList exists is to provide safety in this regard.

But I always thought by adding them to NonEmptyList.
I think that putting those functions in the Primitive modules as you suggest is not that bad.

If everybody agree we can revert that change in favor of this, before it goes into v1.1.0 when it will be too late.

My biggest beef with the proposed naming is discoverbility. It is kind of "natural" to type NonEmptyList. and then wait for the editor to show th candidates.

Yes, I agree about the method should be easily discoverable

Maybe we can refer to them in the tooltips for the try versions included in NonEmptyList to help discovery.

I should've included that in my suggestion, it pairs well with common style to do that as well.

While I understand that's the reason you created NonEmptyList in the first place, there'll be often cases where it needs to interact with existing apis, and I think users will know when they've tinkered with it and need to use the try versions.

So it comes down to this:

f# List.toNonEmptyList Seq.toNonEmptyList Array.toNonEmptyList List.ofNonEmptyList Seq.ofNonEmptyList Array.ofNonEmptyList NonEmptyList.toArray NonEmptyList.ofArray NonEmptyList.toList NonEmptyList.ofList NonEmptyList.toSeq NonEmptyList.ofSeq

I know it blows up the surface area a bit, but it definitely helps with discoverability.

I agree with that list.
Does anybody want to submit a PR?

Does the library need unnecessary partial functions?

"A partial function is a function which doesn鈥檛 terminate and yield a value for all given inputs. Conversely a total function terminates and is always defined for all inputs."

http://dev.stephendiehl.com/hask/#partial-functions
http://dev.stephendiehl.com/hask/#protolude

is the try prefix actually needed here? https://github.com/fsprojects/FSharpPlus/blob/master/src/FSharpPlus/Data/NonEmptyList.fs#L130

https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-List-NonEmpty.html#v:nonEmpty
nonEmpty :: [a] -> Maybe (NonEmpty a)

I agree we can accept partial functions in some cases.

Regarding the naming, the try prefix implies an option as a result. Apart from that, it could cause confusing with the other functions Seq.toList, Array.toList which don't return options, so I think the try prefix makes sense in that case.

I agree. Since not all lists, arrays and sequences are valid nels, converting from those to a nel should exist both with a try (you're unsure of the input) and without (you're sure of the input, or accept an exception).

Since all nels are also valid lists/arrays/seqs, the inverse (from nel to list etc) never requires a try-prefix.

When all's there, we'd end up with the following surface area, I think:

```f#
// array
Array.toNonEmptyList
Array.tryToNonEmptyList
Array.ofNonEmptyList
NonEmptyList.toArray
NonEmptyList.ofArray
NonEmptyList.tryOfArray

// list
List.toNonEmptyList
List.tryToNonEmptyList
List.ofNonEmptyList
NonEmptyList.toList
NonEmptyList.ofList
NonEmptyList.tryOfList

// seq
Seq.toNonEmptyList
Seq.tryToNonEmptyList
Seq.ofNonEmptyList
NonEmptyList.toSeq
NonEmptyList.ofSeq
NonEmptyList.tryOfSeq
```

Does anybody want to submit a PR?

Looks like someone's started! :100: https://github.com/fsprojects/FSharpPlus/pull/305

@abelbraaksma you are welcome to take it further if you want 馃槈

@wallymathieu, ok, np. We'll need a new exception (you used failwith so far). Or is there an exception I can reuse?

I thought of something like InsufficientItemsException, but I'm certainly open for other / better names.

Maybe invalidOp ?

I'll mark the PR as ready for review

Closed by #305

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gusty picture gusty  路  7Comments

cannorin picture cannorin  路  5Comments

btrepp picture btrepp  路  3Comments

aammfe picture aammfe  路  10Comments

NickDarvey picture NickDarvey  路  9Comments