I am trying to create an immutable collection type that behaves as a hybrid
of multiset/bag and Map that records the number of occurrences of each item.
I can write a mutable one with code a little like below and I tried to write an immutable one by inheriting from Map but Map is sealed and will not let me define any overrides.
type TallySet<'k_t when 'k_t : comparison>() = class
// inherit Map<'k_t, int>
let m_map:((Map<'k_t, int>) ref) = ref (Map.empty)
member x.add item =
m_map :=
match (!m_map).TryFind item with
| None -> (!m_map).Add(item, 1)
| Some n -> (!m_map).Add(item, 1 + n)
!m_map
member x.Count with get() = Map.fold (fun cc k v -> cc + v) 0 !m_map
end
What should I write ?
Have a look at ExtCore.Collections.Multiset. As in your code, it's just a map with the value type set to the count. Multiset.add and Multiset.count correspond to the members in your example.
Related
Wondering how to create a list of instantiated objects
I have this
type myType = GoodState | BadState
let create =
seq {for i in 1 .. 100000 do yield new myType}
this doesnt seem to work
anyone know a way of doing this?
thanks
myType is a Discriminated Union, so you have to choose one of the options.
type myType = GoodState | BadState
//using your syntax to create a sequence of BadState
let create1 = seq{for _ in 1 .. 100000 do BadState} //yield is not required anymore
//another syntax to create a list of GoodState
let create2 = List.init 100000 (fun _ -> GoodState)
By the way, I used an underline "_" because I'm not using the index in this example, but you could replace for a variable name if you inted to use it for something.
You construct values of myType using one of the two constructors you defined - GoodState and BadState. Neither have any arguments so you create values with:
let good : myType = GoodState
let bad : myType = BadState
You can construct a sequence of such values with e.g.
let create = seq {for i in 1 .. 100000 do yield GoodState}
New to F#
Have a list of objects.
Objects share the same base class, but the timestamp attribute we want to order by is not present on the base class.
Every object in the list will have the timestamp attribute.
Requirement is to order the objects by the timestamp descending.
Attempted
let sortedList = unsortedList.Sort();
This results in
System.InvalidOperationException: Failed to compare two elements in
the array. ---> System.ArgumentException: At least one object must
implement IComparable. at System.Collections.Comparer.Compare(Object
a, Object b)
You didn't post code so I can't give you the exact code to solve it.
Typically you will have a property in the Base Class or alternatively in an Interface, but let's suppose you have this:
type A() = class end
type B(x) =
inherit A()
member val timestamp = x with get, set
type C(x) =
inherit A()
member val timestamp = x with get, set
let lst = [ new B(5) :> A ; new C(15) :> A; new B(4) :> A ;]
And you can't touch that code. Then what you can do is this:
let getTimeStamp (x:A) =
match x with
| :? B as x -> x.timestamp
| :? C as x -> x.timestamp
| _ -> failwith "subtype not handled"
lst |> List.sortBy (fun x -> -getTimeStamp x)
Using reflection is another possibility (see Mau's answer).
If you post a code sample I can give you a more specific solution and test it.
You can access the timestamp property using the dynamic operator ? as shown here:
C#'s 'dynamic' in F#
I want a function that receives a seq<DateTime*int> and returns a DateTime*seq<DateTime*int>. The first part of the tuple is the DateTime of the first element of the incoming argument, and the list is the rest of the elements.
I've tried to code this in F# this way, but it gives a compiler error:
static member TheFunc(lst: seq<DateTime*int>)=
match lst with
| (h_d, h_i)::tail -> (h_d,tail)
| [] -> raise (new ArgumentException("lst"))
The error, highlighted in (h_d, h_i), is:
The expression was expected to have type
seq<DateTime*int>
but here has type
'a list
If I use a list instead of a sequence in the signature:
static member TheFunc(lst: List<DateTime*int>)=
match lst with
| (h_d, h_i)::tail -> (h_d,tail)
| [] -> raise (new ArgumentException("lst"))
With:
The expression was expected to have type
List<DateTime*int>
but here has type
'a list
Any idea why this doesn't work and how to make it work?
Use (DateTime * int) list instead of List<DateTime * int>.
The types List<T> and T list are different if you opened System.Collections.Generic. Remarkably, if you didn't, they're not!
If you did, then List<int> is an instance of the mutable lists typically used in C#:
> open System.Collections.Generic
> let t0 = typeof<List<int>>
val t0 : Type = System.Collections.Generic.List`1[System.Int32]
And int list is an instance of the immutable lists typically used in F#:
> let t1 = typeof<int list>
val t1 : Type = Microsoft.FSharp.Collections.FSharpList`1[System.Int32]
Confusingly, if you did not open System.Collections.Generic, they are the same:
(* New session *)
> let t0 = typeof<List<int>>
val t0 : Type = Microsoft.FSharp.Collections.FSharpList`1[System.Int32]
> let t1 = typeof<int list>
val t1 : Type = Microsoft.FSharp.Collections.FSharpList`1[System.Int32]
The problem is you are trying to match a seq with a list (as the error says). You want to use
static member TheFunc(lst: seq<DateTime*int>)=
match lst |> List.ofSeq with
| (h_d, h_i)::tail -> (h_d,tail)
| [] -> raise (new ArgumentException("lst"))
(convert the list to a seq then pattern match).
Alternatively, use
static member TheFunc(lst: list<DateTime*int>)=
The lower case l in list is because you probably have System.Collections.Generic open, and the List there is not the same as the F# list
I have a list of tuples like so:
let scorecard = [ for i in 0 .. 39 -> i,0 ]
I want to identify the nth tuple in it. I was thinking about it in this way:
let foundTuple = scorecard |> Seq.find(fun (x,y) -> x = 10)
I then want to create a new tuple based on the found one:
let newTuple = (fst foundTuple, snd foundTuple + 1)
And have a new list with that updated value
Does anyone have some code that matches this pattern? I think I have to split the list into 2 sublists: 1 list has 1 element (the tuple I want to replace) and the other list has the remaining elements. I then create a new list with the replacing tuple and the list of unchanged tuples...
You can use List.mapi which creates a new list using a specified projection function - but it also calls the projection function with the current index and so you can decide what to do based on this index.
For example, to increment second element of a list of integers, you can do:
let oldList = [0;0;0;0]
let newList = oldList |> List.mapi (fun index v -> if index = 1 then v + 1 else v)
Depending on the problem, it might make sense to use the Map type instead of list - map represents a mapping from keys to values and does not need to copy the entire contents when you change just a single value. So, for example:
// Map keys from 0 to 3 to values 0
let m = Map.ofList [0,0;1,0;2,0;3,0]
// Set the value at index 1 to 10 and get a new map
Map.add 1 10 m
I went back and thought about the problem and decided to use an array, which is mutable.
let scorecard = [| for i in 0 .. 39 -> i,0 |]
Since tuples are not mutable, I need to create a new tuple based on the existing one and overwrite it in the array:
let targetTuple = scorecard.[3]
let newTuple = (fst targetTuple, snd targetTuple + 1)
scorecard.[3] <- newTuple
I am using the "<-" which is a code smell in F#. I wonder if there a comparable purely functional equivalent?
I'm reading Expert F# book and I found this code
open System.Collections.Generic
let divideIntoEquivalenceClasses keyf seq =
// The dictionary to hold the equivalence classes
let dict = new Dictionary<'key,ResizeArray<'T>>()
// Build the groupings
seq |> Seq.iter (fun v ->
let key = keyf v
let ok,prev = dict.TryGetValue(key)
if ok then prev.Add(v)
else let prev = new ResizeArray<'T>()
dict.[key] <- prev
prev.Add(v))
dict |> Seq.map (fun group -> group.Key, Seq.readonly group.Value)
and the example use:
> divideIntoEquivalenceClasses (fun n -> n % 3) [ 0 .. 10 ];;
val it : seq<int * seq<int>>
= seq [(0, seq [0; 3; 6; 9]); (1, seq [1; 4; 7; 10]); (2, seq [2; 5; 8])]
first for me this code is really ugly, even if this is safe, It looks more similar to imperative languages than to functional lang..specially compared to clojure. But the problem is not this...I'm having problems with the Dictionary definition
when I type this:
let dict = new Dictionary<'key,ResizeArray<'T>>();;
I get this:
pruebafs2a.fs(32,5): error FS0030: Value restriction. The value 'dict' has been inferred to have generic type
val dict : Dictionary<'_key,ResizeArray<'_T>> when '_key : equality
Either define 'dict' as a simple data term, make it a function with explicit arguments or, if you do not intend for it to be generic, add a type annotation.
is It ok?...
thanks so much
improve question:
Ok I've been reading about value restriction and I found this helpfull information
In particular, only function definitions and simple immutable data
expressions are automatically generalized
...ok..this explains why
let dict = new Dictionary<'key,ResizeArray<'T>>();;
doesn't work...and show 4 different techniques, although in my opinion they only resolve the error but aren't solutions for use generic code:
Technique 1: Constrain Values to Be Nongeneric
let empties : int list [] = Array.create 100 []
Technique 3: Add Dummy Arguments to Generic Functions When Necessary
let empties () = Array.create 100 []
let intEmpties : int list [] = empties()
Technique 4: Add Explicit Type Arguments When Necessary (similar to tec 3)
let emptyLists = Seq.init 100 (fun _ -> [])
> emptyLists<int>;;
val it : seq<int list> = seq [[]; []; []; []; ...]
----- and the only one than let me use real generic code ------
Technique 2: Ensure Generic Functions Have Explicit Arguments
let mapFirst = List.map fst //doesn't work
let mapFirst inp = List.map fst inp
Ok, in 3 of 4 techniques I need resolve the generic code before can work with this...now...returning to book example...when the compile knows the value for 'key and 'T
let dict = new Dictionary<'key,ResizeArray<'T>>()
in the scope the code is very generic for let key be any type, the same happen with 'T
and the biggest dummy question is :
when I enclose the code in a function (technique 3):
let empties = Array.create 100 [] //doesn't work
let empties () = Array.create 100 []
val empties : unit -> 'a list []
I need define the type before begin use it
let intEmpties : int list [] = empties()
for me (admittedly I'm a little dummy with static type languages) this is not real generic because it can't infer the type when I use it, I need define the type and then pass values (not define its type based in the passed values) exist other way define type without be so explicit..
thanks so much..really appreciate any help
This line
let dict = new Dictionary<'key,ResizeArray<'T>>();;
fails because when you type the ;; the compiler doesn't know what 'key and 'T are. As the error message states you need to add a type annotation, or allow the compiler to infer the type by using it later or make it a function
Examples
Type annotation change
let dict = new Dictionary<int,ResizeArray<int>>();;
Using types later
let dict = new Dictionary<'key,ResizeArray<'T>>()
dict.[1] <- 2
using a function
let dict() = new Dictionary<'key,ResizeArray<'T>>();;
This actually doesn't cause an issue when it's defined all together. That is, select the entire block that you posted and send it to FSI in one go. I get this:
val divideIntoEquivalenceClasses :
('T -> 'key) -> seq<'T> -> seq<'key * seq<'T>> when 'key : equality
However, if you type these individually into FSI then as John Palmer says there is not enough information in that isolated line for the interpreter to determine the type constraints. John's suggestions will work, but the original code is doing it correctly - defining the variable and using it in the same scope so that the types can be inferred.
for me this code is really ugly, even if this is safe, It looks more similar to imperative languages than to functional lang.
I agree completely – it's slightly tangential to your direct question, but I think a more idiomatic (functional) approach would be:
let divideIntoEquivalenceClasses keyf seq =
(System.Collections.Generic.Dictionary(), seq)
||> Seq.fold (fun dict v ->
let key = keyf v
match dict.TryGetValue key with
| false, _ -> dict.Add (key, ResizeArray(Seq.singleton v))
| _, prev -> prev.Add v
dict)
|> Seq.map (function KeyValue (k, v) -> k, Seq.readonly v)
This allows sufficient type inference to obviate the need for your question in the first place.
The workarounds proposed by the other answers are all good. Just to clarify based on your latest updates, let's consider two blocks of code:
let empties = Array.create 100 []
as opposed to:
let empties = Array.create 100 []
empties.[0] <- [1]
In the second case, the compiler can infer that empties : int list [], because we are inserting an int list into the array in the second line, which constrains the element type.
It sounds like you'd like the compiler to infer a generic value empties : 'a list [] in the first case, but this would be unsound. Consider what would happen if the compiler did that and we then entered the following two lines in another batch:
empties.[0] <- [1] // treat 'a list [] as int list []
List.iter (printfn "%s") empties.[0] // treat 'a list [] as string list []
Each of these lines unifies the generic type parameter 'a with a different concrete type (int and string). Either of these unifications is fine in isolation, but they are incompatible with each other and would result in treating the int value 1 inserted by the first line as a string when the second line is executed, which is clearly a violation of type safety.
Contrast this with an empty list, which really is generic:
let empty = []
Then in this case, the compiler does infer empty : 'a list, because it's safe to treat empty as a list of different types in different locations in your code without ever impacting type safety:
let l1 : int list = empty
let l2 : string list = empty
let l3 = 'a' :: empty
In the case where you make empties the return value of a generic function:
let empties() = Array.create 100 []
it is again safe to infer a generic type, since if we try our problematic scenario from before:
empties().[0] <- [1]
List.iter (printfn "%s") (empties().[0])
we are creating a new array on each line, so the types can be different without breaking the type system.
Hopefully this helps explain the reasons behind the limitation a bit more.