I would like to encode a Maybe String to string if it has a concrete value, or null if it's Nothing.
At the moment, I use a helper function encodeOptionalString myStr to get the desired effect. I was wondering if there's a more Elm-like way of doing this. I really like the API of elm-json-decode-pipeline that allows me to write Decode.nullable Decode.string for decoding.
encodeOptionalString : Maybe String -> Encode.Value
encodeOptionalString s =
case s of
Just s_ ->
Encode.string s_
Nothing ->
Encode.null
You could generalize this into an encodeNullable function yourself:
encodeNullable : (value -> Encode.Value) -> Maybe value -> Encode.Value
encodeNullable valueEncoder maybeValue =
case maybeValue of
Just value ->
valueEncoder value
Nothing ->
Encode.null
Or if you want a slightly shorter ad hoc expression:
maybeString
|> Maybe.map Encode.string
|> Maybe.withDefault Encode.null
The package elm-community/json-extra has exactly the method you desire.
maybe : (a -> Value) -> Maybe a -> Value
Encode a Maybe value. If the value is Nothing it will be encoded as null
Related
I'm trying to go from:
sprintf "%3.1f" myNumber
to:
sprintf myFormatter myNumber
which is not possible
I have a situation where number precision depends on some settings, so I would like to be able to create my own formatter string.
I know it can be done with String.Format, but I am curious if there is a F# way with sprintf, or ksprinf; can it be done?
Simple answer
EDIT: Diego Esmerio on F# Slack showed me a simpler way that I honestly never thought of while working out the answer below. The trick is to use PrintfFormat directly, like as follows.
// Credit: Diego. This
let formatPrec precision =
PrintfFormat<float -> string,unit,string,string>(sprintf "%%1.%if" precision)
let x = 15.234
let a = sprintf (formatPrec 0) x
let b = sprintf (formatPrec 1) x
let c = sprintf (formatPrec 3) x
Output:
val formatPrec : precision:int -> PrintfFormat<(float -> string),unit,string,string>
val x : float = 15.234
val a : string = "15"
val b : string = "15.2"
val c : string = "15.234"
This approach is arguably much simpler than the Expr-based approach below. For both approaches, be careful with the formatting string, as it will compile just fine, but break at runtime if it is invalid.
Original answer (complex)
This isn't trivial to do, because functions like sprintf and printfn are compile-time special-case functions that turn your string-argument into a function (in this case of type float -> string).
There are some things you can do with kprintf, but it won't allow the formatting-argument to become a dynamic value, since the compiler still wants to type-check that.
However, using quotations we can build such function ourselves. The easy way is to create quotation from your expression and to change the parts we need to change.
The starting point is this:
> <# sprintf "%3.1f" #>
val it : Expr<(float -> string)> =
Let (clo1,
Call (None, PrintFormatToString,
[Coerce (NewObject (PrintfFormat`5, Value ("%3.1f")), PrintfFormat`4)]),
Lambda (arg10, Application (clo1, arg10)))
...
That may look like a whole lot of mess, but since we only need to change one tiny bit, we can do this rather simply:
open Microsoft.FSharp.Quotations // part of F#
open Microsoft.FSharp.Quotations.Patterns // part of F#
open FSharp.Quotations.Evaluator // NuGet package (with same name)
// this is the function that in turn will create a function dynamically
let withFormat format =
let expr =
match <# sprintf "%3.1f" #> with
| Let(var, expr1, expr2) ->
match expr1 with
| Call(None, methodInfo, [Coerce(NewObject(ctor, [Value _]), mprintFormat)]) ->
Expr.Let(var, Expr.Call(methodInfo, [Expr.Coerce(Expr.NewObject(ctor, [Expr.Value format]), mprintFormat)]), expr2)
| _ -> failwith "oops" // won't happen
| _ -> failwith "oops" // won't happen
expr.CompileUntyped() :?> (float -> string)
To use this, we can now simply do this:
> withFormat "%1.2f" 123.4567899112233445566;;
val it : string = "123.46"
> withFormat "%1.5f" 123.4567899112233445566;;
val it : string = "123.45679"
> withFormat "%1.12f" 123.4567899112233445566;;
val it : string = "123.456789911223"
Or like this:
> let format = "%0.4ef";;
val format : string = "%0.4ef"
> withFormat format 123.4567899112233445566;;
val it : string = "1.2346e+002f"
It doesn't matter whether the format string is now a fixed string during compile time. However, if this is used in performance sensitive area, you may want to cache the resulting functions, as recompiling an expression tree is moderately expensive.
I have the following type :
type MultiSet<'a when 'a: comparison> = MSet of Map<'a, int>
and I now want to declare af map function for this type with the signature :
('a -> 'b) -> Multiset<'a> -> Multiset<'b> when 'a : comparison and 'b : comparison
I have tried :
let map m ms =
match ms with
| MSet s -> MSet ( Map.map (fun key value -> m key) s )
But that it has the signature :
('a -> int) -> Multiset<'a> -> Multiset<'a> when 'a : comparison
What is wrong with my implementation when I want the first mentioned function signature?
Map.map maps values, not keys. And with good reason: it can't just go and plug the mapped keys instead of the original ones - they might not fit. Heck, they might not even be unique for all Map.map knows!
If you want to construct a map with different keys, you'll have to take it apart as a sequence, convert it, then construct another Map from it:
let map m (MSet s) =
MSet ( Map.ofSeq <| seq { for KeyValue (key, value) in s -> m key, value } )
This implementation has your required signature.
(also, notice how you don't have to do match, you can include the pattern right in parameter declaration)
Beware that this implementation does nothing for validating the new keys: for example, if they turn out to be non-unique, some counts will be lost. I leave this as an exercise for the reader.
The following f# function works great if I pass references to objects, but will not accept structs, or primitives:
let TryGetFromSession (entryType:EntryType, key, [<Out>] outValue: 'T byref) =
match HttpContext.Current.Session.[entryType.ToString + key] with
| null -> outValue <- null; false
| result -> outValue <- result :?> 'T; true
If I try to call this from C# with:
bool result = false;
TryGetFromSession(TheOneCache.EntryType.SQL,key,out result)
I get The Type bool must be a reference type in order to use it as a parameter Is there a way to have the F# function handle both?
The problem is that the null value in outValue <- null restricts the type 'T to be a reference type. If it has null as a valid value, it cannot be a value type!
You can fix that by using Unchecked.defaultOf<'T> instead. This is the same as default(T) in C# and it returns either null (for reference types) or the empty/zero value for value types.
let TryGetFromSession (entryType:EntryType, key, [<Out>] outValue: 'T byref) =
match HttpContext.Current.Session.[entryType.ToString() + key] with
| null -> outValue <- Unchecked.defaultof<'T>; false
| result -> outValue <- result :?> 'T; true
I still think this is not "pretty"/idomatic F# code and would probably do some more seremonial with the following:
let myCast<'T> o =
match box o with
| :? 'T as r -> Some(r)
| _ -> None
let GetFromSession<'T> entryType key =
match HttpContext.Current.Session.[entryType.ToString + key] with
| null -> None
| r -> myCast<'T> r
This is also kind of "safer" and will (should?) not throw any exception, and it removes the null-stuff in F#. In C# it will return and work ok too, but None are returned as null, and if some result, well yeah it will be Some ;-)
Mind that the above code are not tested, not run in any setting or even compiled, so regard it as pseudo code. It might even have other issues...
Check also:
https://msdn.microsoft.com/en-us/library/dd233220.aspx
and
http://fsharpforfunandprofit.com/posts/match-expression/
On the last link especially: Matching on subtypes
On a side note, I do not like the missing checking of entire hierachy from HttpContext to Session are non-null, but that might just be me...
Update for some C# code using None/Some
var x = GetFromSession<MyTypeInSession>(entryType, key)?.Value??defaultValue;
There is absolutely no need for going full arabic, reading from right to left, and from down and up with a pyramidal scheme of ifs and buts and no candy or nuts, for null-checking et al ad nauseam.
And again code is to be regarded as pseudo code...
In F# interactive, I can find the type of sprintf.
>sprintf;;
val it : (Printf.StringFormat<'a> -> 'a) = <fun:clo#163>
I can find the type of sprintf curried with the first parameter, if the curried function is not generic.
> sprintf "%g";;
val it : (float -> string) = <fun:it#134-16>
But if it is generic, then I get the value restriction error.
> sprintf "%A";;
error FS0030: Value restriction. The value 'it' has been inferred to have generic type
val it : ('_a -> string)
Either make the arguments to 'it' explicit or, if you do not intend for it to be generic, add a type annotation.
I can add a type annotation to get rid of the value restriction like this, specializing the function for a type, eg. DateTime.
>let f : (DateTime -> string) = sprintf "%A";;
val f : (DateTime -> string)
How can I add the type annotation without the binding? I've tried the following ...
>sprintf "%A" : (DateTime -> string);;
error FS0010: Unexpected symbol ':' in interaction. Expected incomplete structured construct at or before this point, ';', ';;' or other token.
This is a similar example but harder ...
>sprintf "%a";;
error FS0030: Value restriction. The value 'it' has been inferred to have generic type
val it : ((unit -> '_a -> string) -> '_a -> string)
Either make the arguments to 'it' explicit or, if you do not intend for it to be generic, add a type annotation.
You just need to enclose your expression in parenthesis:
open System;;
(sprintf "%A" : DateTime -> string);;
val it : (DateTime -> string) = <fun:it#2>
That way you can specify the type annotation without the binding.
What is actually happening is that fsi binds the last thing you type to a variable called it. It effectively does
let it = sprintf "%a";;
Type annotations would need to go on the left hand side of the = which you can't access. The problem is that you need a concrete type to give to any variable (in this case it). A workaround could be
(fun t:DateTime -> sprintf "%a" t)
I have a function declared as
let GetLength (value : option<string>) =
if value.IsSome then value.Value.Length else 0
And I have the variable
let a : string = "tom"
How do I pass a to the function GetLength?
The accepted answer doesn't compile, and produces...
GetLength Some a;;
^^^^^^^^^^^^^^
error FS0003: This value is not a function and cannot be applied
F# thinks you are building a function (GetLength Some) to apply to the value a. That is because it's a functional language.
The correct form is
GetLength (Some a);;
You don't cast it. You need to use the Some constructor:
GetLength Some a
An alternative to parentheses:
GetLength <| Some a
I think it's important to address this question:
Why would anyone willing type 18
tokens over 3 lines when you can get
the exact same thing using 12 tokens
on one line?
Writing code isn't just about conciseness - it's also about readability and maintainability. Suppose you need to handle the case where a is null. With pattern matching, you could go from this:
let GetLength (value : string option) =
match value with
| Some s -> s.Length
| _ -> 0
To this:
let GetLength (value : string option) =
match value with
| Some s when s <> null -> s.Length
| _ -> 0
To an F# programmer, the meaning is clear. To fix your implementation would look something like this:
let GetLength (value : option<string>) =
if value.IsSome && value.Value <> null then value.Value.Length else 0
The result might be the same, but I don't find it particularly easy to see, at a glance, what's happening.
It's fine if pattern matching doesn't resonate with you, but the extra "cost" in the simple case is often made up for many times over as the logic evolves.
To answer the general question of casting 'a to option<'a>, if you wanted to do it safely without just applying the object to the Some constructor, there is a simple function that I would use:
let to_option = function
| null -> None
| obj -> Some obj