Fsharpplus: Inverse of ToSeq/ToList

Created on 11 Jan 2017  路  5Comments  路  Source: fsprojects/FSharpPlus

Description

Is there a function that is the inverse of toList?

for Example NonEmptyList<'a> can be easily transformed into 'a list. What would be the name of the inverse function and is there an overloaded static member for that available?

enhancement question

All 5 comments

Yes, following the F# naming standards, the inverse of toList function is called ofList which exists already as a generic function but regarding the NonEmptyList there is a (logical) issue because you cannot always construct a NonEmptyList from a list, specifically when the list is empty, so it will not be a total function, in other words it would be an un-safe operation.

Here's an interesting discussion about it in FSharpX suggesting the way to convert.

From the abstraction here represented point of view the issue comes from the fact that NonEmptyList is not a Monoid since it doesn't have the empty List element, it is rather a Semigroup, so it cannot be always converted from list which is the canonical monoid.

We can discuss all the alternatives, I think we can either:

1 Add it and let it fail
2 Add a function returning an option
3 Don't allow unsafe function in this library and make FSharpPlus the dreamed library that contains only total functions.

Alternatives 2 and 3 are not exclusive.

There are some unsafe functions like these but those functions comes from the original F# definition, as described there, an additional set of total functions were added.

It is also interesting to note that there is an 'inverse' issue with functions like head which are normally an unsafe operation but for the NonEmptyList it is a total function.

A generalization of the function head for lists is the comonad extract function and they were intentionally removed for potential empty lists.

Any thoughts?

reading thru your post and the ones you linked my preference would be option 2).

which then would obviously mean that the generic implementation would always need to return an 'a option even for those types we absolutely know wont throw an exception and where an option type is unneccessary. Or we adorn all functions with a tryFunctionname counterpart. ugh. Also ugly

So the name would be something like tryOfList, yes I agree it doesn't sounds good.
However it seems to be inline with two standard naming conventions in F#: the XXX.ofList functions and the tryXXX functions

Also F# has the function tryHead which solves the 'inverse' issue which I mentioned.

I think there is a 4th option, which is to create an even more generic and thus more likely to fail function, something like tryConvert, the disadvantage is that it will be hard to come up with a set of rules for that function.

Yet another different approach is something like this function which can be added as an extension of List.

Seems that ofList exists and the non empty list discussed functions will be coming as part of this PR https://github.com/fsprojects/FSharpPlus/issues/259 - so closing this one.

Was this page helpful?
0 / 5 - 0 ratings