I have a list like this
let data = [{ A = "one"; B = 2020; C = Some 5L; D = None };
{ A = "two"; B = 2020; C = None; D = Some 3L}]
how do I replace C = Some 5L; with C = 5;
i want like this ->
[{ A = "one"; B = 2020; C = 5; D = null };
{ A = "two"; B = 2020; C = null; D = 3}]
i tried let newData = data |> List.choose id
It does not work.
You can use System.Nullable to represent nullable types that C# should recognize.
Also you will need to create a separate record type to represent your data with nullable fields.
I can imagine that you need to make this kind of transaction when leaving F# world and entering C#.
type A = { A:string; B: int; C: int64 option; D: int64 option}
type T = { A2:string; B2: int; C2: System.Nullable<int64>; D2: System.Nullable<int64>}
let data = [{ A = "one"; B = 2020; C = Some 5L; D = None };
{ A = "two"; B = 2020; C = None; D = Some 3L}]
let getNullable (x: int64 option) =
match x with
| Some v -> System.Nullable(v)
| None -> System.Nullable()
let nullablData (data : A list)=
data |> List.map (fun x -> {A2 = x.A; B2 = x.B; C2 = getNullable(x.C); D2 = getNullable(x.D)})
let res= nullablData data
//result
//val res : T list = [{A2 = "one"; B2 = 2020; C2 = 5L; D2 = null;};
// {A2 = "two"; B2 = 2020; C2 = null; D2 = 3L;}]
You actually want to map the elements of the list into a different type. Since the record you are using inside the list looks like
type X = {
A: string
B: int
C: int option
D: obj option
}
and you want to have a
type Y = {
A: string
B: int
C: int
D: obj option
}
(mind the type for C), you need to transform the elements of the list with List.map using a function that maps type X into type Y.
data |> List.map (fun x -> { A = x.A; B = x.B; C = (x.C |> Option.defaultValue 0); D = x.D})
You have to handle the C=None case properly by giving it a default value.
Related
My old notes on ML say that
let (๐ฃโ, โฆ , ๐ฃโ) = (๐กโ, โฆ , ๐กโ) in ๐กโฒ
is a syntactic sugar for
(ฮป ๐ฃโ. โฆ (ฮป ๐ฃโ. ๐กโฒ)๐กโ โฆ )๐กโ
and that
let (๐ฃโ, ๐ฃโ) = ๐ก ๐กโฒ in ๐กโณ
is equivalent to
let ๐ฃ = ๐ก ๐กโฒ in
let ๐ฃโ = snd ๐ฃ in
let ๐ฃโ = fst ๐ฃ in
๐กโณ
where
each ๐ฃ (with or without a subscript) stands for a variable,
each ๐ก (with or without a sub- or a superscript) stands for a term, and
fst and snd deliver the first and second component of a pair, respectively.
I'm wondering whether I got the evaluation order right because I didn't note the original reference. Could anyone ((confirm or reject) and (supply a reference))?
It shouldn't matter whether it's:
let ๐ฃ = ๐ก ๐กโฒ in
let ๐ฃโ = snd ๐ฃ in
let ๐ฃโ = fst ๐ฃ in
๐กโณ
Or:
let ๐ฃ = ๐ก ๐กโฒ in
let ๐ฃโ = fst ๐ฃ in
let ๐ฃโ = snd ๐ฃ in
๐กโณ
Since neither fst nor snd have any side-effects. Side-effects may exist in the evaluation of ๐ก ๐กโฒ but that's done before the let binding takes place.
Additionally, as in:
let (๐ฃโ, ๐ฃโ) = ๐ก ๐กโฒ in ๐กโณ
Neither ๐ฃโ nor ๐ฃโ is reliant on the value bound to the other to determine its value, so the order in which they're bound is again seemingly irrelevant.
All of that said, there may be an authoritative answer from those with deeper knowledge of the SML standard or the inner workings of OCaml's implementation. I simply am uncertain of how knowing it will provide any practical benefit.
Practical test
As a practical test, running some code where we bind a tuple of multiple expressions with side-effects to observe order of evaluation. In OCaml (5.0.0) the order of evaluation is observed to be right-to-left. We observe tthe same when it comes to evaluating the contents of a list where those expressions have side-effects as well.
# let f () = print_endline "f"; 1 in
let g () = print_endline "g"; 2 in
let h () = print_endline "h"; 3 in
let (a, b, c) = (f (), g (), h ()) in a + b + c;;
h
g
f
- : int = 6
# let f () = print_endline "f"; 1 in
let g () = print_endline "g"; 2 in
let h () = print_endline "h"; 3 in
let (c, b, a) = (h (), g(), f ()) in a + b + c;;
f
g
h
- : int = 6
# let f _ = print_endline "f"; 1 in
let g () = print_endline "g"; 2 in
let h () = print_endline "h"; 3 in
let a () = print_endline "a" in
let b () = print_endline "b" in
let (c, d, e) = (f [a (); b ()], g (), h ()) in
c + d + e;;
h
g
b
a
f
- : int = 6
In SML (SML/NJ v110.99.3) we observe the opposite: left-to-right evaluation of expressions.
- let
= fun f() = (print "f\n"; 1)
= fun g() = (print "g\n"; 2)
= fun h() = (print "h\n"; 3)
= val (a, b, c) = (f(), g(), h())
= in
= a + b + c
= end;
f
g
h
val it = 6 : int
- let
= fun f() = (print "f\n"; 1)
= fun g() = (print "g\n"; 2)
= fun h() = (print "h\n"; 3)
= val (c, b, a) = (h(), g(), f())
= in
= a + b + c
= end;
h
g
f
val it = 6 : int
- let
= fun f _ = (print "f\n"; 1)
= fun g() = (print "g\n"; 2)
= fun h() = (print "h\n"; 3)
= fun a() = print "a\n"
= fun b() = print "b\n"
= val (c, d, e) = (f [a(), b()], g(), h())
= in
= c + d + e
= end;
a
b
f
g
h
val it = 6 : int
Be aware that, in OCaml, due to the (relaxation of the) value restriction, let a = b in c is not equivalent to (fun a -> c)b. A counterexample is
# let id = fun x -> x in id 5, id 'a';;
- : int * char = (5, 'a')
# (fun id -> id 5, id 'a')(fun x -> x)
Error: This expression has type char but an expression was expected of type int
#
This means that they are semantically not the same construction (the let ... = ... in ... is strictly more general that the other).
This happens because, in general, the type system of OCaml doesn't allow types of the form (โฮฑ.ฮฑโฮฑ) โ int * char (because allowing them would make typing undecidable, which is not very practical), which would be the type of fun id -> id 5, id 'a'. Instead, it resorts to having the less general type โฮฑ.(ฮฑโฮฑ) โ ฮฑ * ฮฑ, which doesn't make it typecheck, because you can't unify both ฮฑ with char and with int.
Nested F# Record with generic type parameter, how do I statically cast between types in nested structure equivalent to traversing and performing 'T |> 'K, e.g. float |> int?
Currently I am Naively traversing the nested records and explicitly converting the type with from:float |> to:int or equivalently int(from). However, this is not very beautiful.
type Person<'T> = {Id : int; Value : 'T}
type Family<'T> = {Id : 'T; People : seq<Person<'T>>}
let fam1 = {Id = 1.0; People = [{Id = 1.1; Value = 2.9}; {Id = 1.2; Value = 4.4}]} : Family<float>
let fam2 = {Id = 2.0; People = [{Id = 2.1; Value = 3.9}; {Id = 2.2; Value = 5.4}]} : Family<float>
let partyFloat = seq{ yield fam1; yield fam2}
// In general, how to do this from a type T to a type K where conversion using T |> K will work
let partyInt : seq<Family<int>> = partyFloat
How to statically and/or
lazily convert to seq<Family<int>>?
In my real world case I have a DiffSharp D type that can be converted to a float with D |> float or float(D).
There is no magic way to cast the insides of types, you have to write your own.
It is idiomatic for F# and functional programming in general (and I personally recommend it, too) to write small functions for simple data transformations, and then assemble them together:
let mapPerson f p = { Id = p.Id; Value = f p.Value }
let mapFamily f fm = { Id = f fm.Id; People = Seq.map (mapPerson f) fm.People }
let mapParty f = Seq.map (mapFamily f)
let partyInt = mapParty int partyFloat
But of course you can do it in one big messy go:
let partyInt =
partyFloat
|> Seq.map (fun fm ->
{ Id = int fm.Id
People =
fm.People
|> Seq.map (fun p ->
{ Id = p.Id; Value = int p.Value }
)
}
)
It seems like what you are asking for are covariance ie that this should compile
let vs : obj list = ["1"; "2"]
F# doesn't support covariance (or contravariance) and probably never will. C# does however so you could write something like this
using System.Collections.Generic;
interface IPerson<out T>
{
int Id { get; }
T Value { get; }
}
interface IFamily<out T>
{
int Id { get; }
IEnumerable<IPerson<T>> Members { get; }
}
static class Program
{
static IFamily<string> CreateFamily()
{
return null;
}
static void Main(string[] args)
{
IFamily<string> familyOfString = CreateFamily();
IFamily<object> familyOfObject = familyOfString;
}
}
However, there's a functional pattern that could help us called polymorphic lenses.
(Picture from reddit thread: https://www.reddit.com/r/haskell/comments/2qjnho/learning_curves_for_different_programming/)
I used to think that polymorphic lenses isn't possible in F# due to the lack of higher-rank types. However, there's a hidden gem out there: http://www.fssnip.net/7Pk
Vesa Karvonen (IIRC he is also behind hopac so he's pretty cool) implements polymorphic lenses in F# using some pretty interesting tricks.
We can then map the inner values of an immutable structure reasonably easy.
let input : Family<int> =
{
Id = 1
Members = [{ Id = 10; Value = 123}; { Id = 11; Value = 456}]
}
printfn "%A" input
let output : Family<string> =
input
|> over Family.membersL (overAll Person.valueL ((+) 1 >> string))
printfn "%A" output
Full source code
// ----------------------------------------------------------------------------
// The code below taken from: http://www.fssnip.net/7Pk
// by Vesa+Karvonen - http://www.fssnip.net/authors/Vesa+Karvonen
// ----------------------------------------------------------------------------
type LensFunctor<'a> =
| Over of 'a
| View
member t.map a2b =
match t with
| Over a -> Over (a2b a)
| View -> View
type Lens<'s,'t,'a,'b> = ('a -> LensFunctor<'b>) -> 's -> LensFunctor<'t>
module Lens =
let view l s =
let r = ref Unchecked.defaultof<_>
s |> l (fun a -> r := a; View) |> ignore
!r
let over l f =
l (f >> Over) >> function Over t -> t | _ -> failwith "Impossible"
let set l b = over l <| fun _ -> b
let (>->) a b = a << b
let lens get set = fun f s ->
(get s |> f : LensFunctor<_>).map (fun f -> set f s)
let fstL f = lens fst (fun x (_, y) -> (x, y)) f
let sndL f = lens snd (fun y (x, _) -> (x, y)) f
// ----------------------------------------------------------------------------
// The code above taken from: http://www.fssnip.net/7Pk
// by Vesa+Karvonen - http://www.fssnip.net/authors/Vesa+Karvonen
// ----------------------------------------------------------------------------
let overAll l f = List.map (over l f)
open Lens
type Person<'T> = { Id : int; Value : 'T }
module Person =
let idS i p = { p with Id = i }
let valueS v { Id = i } = { Id = i; Value = v }
let idL f = lens (fun {Id = i } -> i) idS f
let valueL f = lens (fun {Value = v } -> v) valueS f
type Family<'T> = { Id : int; Members : Person<'T> list }
module Family =
let idS i f = { f with Id = i }
let membersS m { Id = i } = { Id = i; Members = m }
let idL f = lens (fun {Id = i } -> i) idS f
let membersL f = lens (fun {Members = m } -> m) membersS f
[<EntryPoint>]
let main argv =
let input =
{
Id = 1
Members = [{ Id = 10; Value = 123}; { Id = 11; Value = 456}]
}
printfn "%A" input
let output =
input
|> over Family.membersL (overAll Person.valueL ((+) 1 >> string))
printfn "%A" output
0
I wonder if it is possible in OCaml to use one field of record in the other field in the same record.
Basicaly, I have field with function in which I would like to use also other, values, fields of the same record so when the value change the function will use the new value.
I can do it with setting the function field mutable and update it after the record is created e.g.
type 'a cell =
{ mutable value: 'a
; mutable fn: unit -> 'a }
let create_cell ~(value : 'a) : 'a cell =
let c = {value; fn= (fun () -> value + 42)} in
let _ = c.fn <- (fun () -> c.value + 42) in
c
I was wondering if it is possible without fn field being mutable and in one go.
You can use let rec to make the function refer to the record it is part of:
# type 'a cell = { mutable value : 'a ; fn : unit -> 'a };;
type 'a cell = { mutable value : 'a; fn : unit -> 'a; }
# let rec r = { value = 14; fn = fun () -> r.value + 14 };;
val r : int cell = {value = 14; fn = <fun>}
# r.fn ();;
- : int = 28
# r.value <- 10;;
- : unit = ()
# r.fn ();;
- : int = 24
If I understand correctly, this what you'd like to do.
So then your create_cell function might look like this:
let create_cell ~(value : 'a) : 'a cell =
let rec c = {value; fn= (fun () -> c.value + 42)} in
c
It seems to work:
# let mycell = create_cell ~value: 88;;
val mycell : int cell = {value = 88; fn = <fun>}
# mycell.fn ();;
- : int = 130
# mycell.value <- 100;;
- : unit = ()
# mycell.fn ();;
- : int = 142
I want to build a module I with memoization. The type I.t contains a real complex content c, and some mutable properties (eg, mutable is_cool). The module provides outside with functions to calculate and get the properties (eg, is_cool), which can be costly, that is why mutable properties are used:
(*in i.ml *)
module Content = struct
type t = { i: int; mutable j: int }
let is_cool (x: t) : bool = ...
end
module I : sig
type t
val get_c: t -> Content.t
val is_cool: t -> bool
...
end = struct
type t = {
c : Content.t;
mutable is_cool : bool option;
mutable property_a : int option }
let get_c (x: t) -> Content.t = x.c
let is_cool (x: t) : bool =
match x.is_cool with
| Some r -> r
| None -> (* not yet calculated *)
let r = Content.is_cool x.c in
x.is_cool <- Some r;
r
end
...
A concern that I have, is how to code the module I and the code outside such that for any value of type I.t throughout the program execution, its mutable properties are always consistent with its content c. Being consistent means "the mutable properties should either be None or be Some v where v represents the current property of the content".
For instance, the following code tempting to modify directly a property is well forbidden by the signature of the module I.t:
(* in main.ml *)
open I
let test (x: I.t) =
x.is_cool <- Some false
However, it seems that it is not easy to forbid this code, which changes the content:
(* in main.ml *)
let test (x: I.t) (i_new: int) =
let c = I.get_c x in
let c_new = { c with Content.i = i_new } in
let y = { x with c = c_new } in
(* now the content and the properties in y are likely to be inconsistent *)
One way to improve this is to add set in the module I, and always use set x c_new at the place of { x with c = c_new }:
(*in i.ml *)
let set (x: t) (c: Content.t) : t =
{ c = c; is_cool = None; property_a = None }
However, there are still problems, for instance,
1) it is still impossible to forbid people from writing { x with c = c_new }
2) a modification of the mutable components in Content.t (eg, mutable j: int) can also make I.t inconsistent:
(* in main.ml *)
let test (x: I.t) (j_new: int) =
let c = I.get_c x in
c.Content.j <- j_new;
(* now the content and the properties in x are likely to be inconsistent *)
Does anyone know any existing reflexion or solution face to this inconsistency caused by memoization? If we always use set at the place of "record with", and do not allow mutable components in Content.t, can we guarantee 100% the consistency for any code scenario?
Well, you could use private
Your code would look like this :
module type C = sig
type t = private { i: int; mutable j: int }
val new_c : int -> int -> t
val is_cool : t -> bool
end
module Content : C = struct
type t = { i: int; mutable j: int }
let new_c = ...
let is_cool (x: t) : bool = ...
end
module I : sig
type t
val new_i : int -> int -> t
val get_c: t -> Content.t
val is_cool: t -> bool
...
end = struct
type t = {
c : Content.t;
mutable is_cool : bool option;
mutable property_a : int option }
let new_i = ...
let get_c (x: t) -> Content.t = x.c
let is_cool (x: t) : bool =
match x.is_cool with
| Some r -> r
| None -> (* not yet calculated *)
let r = Content.is_cool x.c in
x.is_cool <- Some r;
r
end
...
And if you try to write :
let c = {i = 3; j = 3};;
it will reject you answering :
Error: Cannot create values of the private type Content.t
How much memory is shared between objects during an immutable record or object update in OCaml? For example, for the records in the code:
type foo = {
a : int;
b : int;
c : int}
let f1 = {a = 1; b=2; c=3}
let f2 = {f1 with c=4}
How much memory is shared between f1 and f2? Basically, do they share memory for a and b? Similarly, for the objects in the code:
type ('i, 'j) lens = { get : 'j; set : 'j -> 'i }
class bar = object
val a = 1
method a = {
get = a;
set = fun a' -> {< a = a' >}
}
val b = 2
method b = {
get = b;
set = fun b' -> {< b = b' >}
}
val c = 3
method c = {
get = c;
set = fun c' -> {< c = c' >}
}
end
let b1 = new bar
let b2 = b1#c.set 4
How much memory is shared between b1 and b2?
Basically, imagine a situation where the fields a, b, and c are really, really big. I'd like to do an immutable update, but I don't want to have to copy all of that memory if possible.
For records, there would be no shared memory between f1 and f2 since an int takes as much memory as a pointer. There would be shared memory if you had more complex objects instead of int.
For example in
type foo = {
a : int list;
b : int list;
c : int list;
}
let f1 = {a = [1; 1; 1]; b = [2; 2; 2]; c = [3]}
let f2 = {f1 with c = [4]}
the lists of 1s and 2s would be shared between the two records.
It is a general rule in Ocaml that simple types (int, char, bool, โฆ) are copied but complex types ('a list, 'a array, โฆ) are shared. This is why immutable data structures are good: you can easily share. But beware, data is shared even if it is mutable:
type foo = {x : int ref; y : int ref}
let a = {x=ref 0; y = ref 0}
let b = {a with y = ref 1}
let () = b.x := 2
then a.x equals 2.