I have a Frame with two columns of String,
let first = Series.ofValues(["a";"b";"c"])
let second = Series.ofValues(["d";"e";"f"])
let df = Frame(["first"; "second"], [first; second])
How do I produce a third column as the concatenation of the two columns?
In python pandas, this can be achieved with simple + operator, but deedle gives error if i do that,
error FS0043: No overloads match for method 'op_Addition'.
It sounds like what you want is to have something that returns something like:
Series.ofValues(["ad"; "be"; "cf"])
Then I think you need to define an addition operator with something like this:
let additionOperator = (fun (a:string) (b:string) -> (a + b))
And then you can add them like this:
Series.zipInto additionOperator first second
I get as the result:
val it : Series<int,string> = series [ 0 => ad; 1 => be; 2 => cf]
However if you are alright with tuples as your result, you can just use:
Series.zip first second
I come across this after facing the same issue, the trick is to get the values as seq and use Seq.map2 to concat the two seqs, my solution is
let first = Series.ofValues(["a";"b";"c"])
let second = Series.ofValues(["d";"e";"f"])
let df = Seq.map2 (fun x y -> x+y) first.Values second.Values
|> Series.ofValues
|> (fun x -> Frame.addCol "third" x (Frame(["first"; "second"], [first; second])))
Result:
df.Print()
first second third
0 -> a d ad
1 -> b e be
2 -> c f cf
I believe this would work... Clearly not the most beautiful way to write it but... Will try to do some time testing later.
let df3c = df |> Frame.mapRows (fun _ b -> b.GetAt(0).ToString() + b.GetAt(1).ToString())
|> (fun a -> Frame.addCol "test" a df)
Related
I have recently started learning f# and I have a problem with a task like the one in the subject line. I managed to solve this task but not using a recursive function. I have tried to convert my function to a recursive function but it does not work because in the function I create arrays which elements I then change. Please advise me how to convert my function to a recursive function or how else to perform this task.
let list = [8;4;3;3;5;9;-7]
let comp (a,b) = if a>b then a elif b = a then a else b
let maks (b: _ list) =
let x = b.Length
if x % 2 = 0 then
let tab = Array.create ((x/2)) 0
for i = 0 to (x/2)-1 do
tab.[i] <- (comp(b.Item(2*i),b.Item(2*i+1)))
let newlist = tab |> Array.toList
newlist
else
let tab = Array.create (((x-1)/2)+1) 0
tab.[(((x-1)/2))] <- b.Item(x-1)
for i = 0 to ((x-1)/2)-1 do
tab.[i] <- (comp(b.Item(2*i),b.Item(2*i+1)))
let newlist = tab |> Array.toList
newlist
It is worth noting that, if you were doing this not for learning purposes, there is a nice way of doing this using the chunkBySize function:
list
|> List.chunkBySize 2
|> List.map (fun l -> comp(l.[0], l.[l.Length-1]))
This splits the list into chunks of size at most 2. For each chunk, you can then compare the first element with the last element and that is the result you wanted.
If this is a homework question, I don't want to give away the answer, so consider this pseudocode solution instead:
If the list contains at least two elements:
Answer a new list consisting of:
The greater of the first two elements, followed by
Recursively applying the function to the rest of the list
Else the list contains less than two elements:
Answer the list unchanged
Hint: F#'s pattern matching ability makes this easy to implement.
Thanks to your guidance I managed to create the following function:
let rec maks2 (b: _ list,newlist: _ list,i:int) =
let x = b.Length
if x >= 2 then
if x % 2 = 0 then
if i < ((x/2)-1)+1 then
let d = (porownaj(b.Item(2*i),b.Item(2*i+1)))
let list2 = d::newlist
maks2(b,list2,i+1)
else
newlist
else
if i < ((x/2)-1)+1 then
let d = (porownaj(b.Item(2*i),b.Item(2*i+1)))
let list2 = d::newlist
maks2(b,list2,i+1)
else
let list3 = b.Item(x-1)::newlist
list3
else
b
The function works correctly, it takes as arguments list, empty list and index.
The only problem is that the returned list is reversed, i.e. values that should be at the end are at the beginning. How to add items to the end of the list?
You can use pattern matching to match and check/extract lists in one step.A typical recursive function, would look like:
let rec adjGreater xs =
match xs with
| [] -> []
| [x] -> [x]
| x::y::rest -> (if x >= y then x else y) :: adjGreater rest
It checks wether the list is empty, has one element, or has two elements and the remaining list in rest.
Then it builds a new list by either using x or y as the first element, and then compute the result of the remaing rest recursivly.
This is not tail-recursive. A tail-call optimized version would be, that instead of using the result of the recursive call. You would create a new list, and pass the computed valuke so far, to the recursive function. Usually this way, you want to create a inner recursive loop function.
As you only can add values to the top of a list, you then need to reverse the result of the recursive function like this:
let adjGreater xs =
let rec loop xs result =
match xs with
| [] -> result
| [x] -> x :: result
| x::y::rest -> loop rest ((if x >= y then x else y) :: result)
List.rev (loop xs [])
My data is a SEQUENCE of:
[(40,"TX");(48,"MO");(15,"TX");(78,"TN");(41,"VT")]
My code is as follows:
type Csvfile = CsvProvider<somefile>
let data = Csvfile.GetSample().Rows
let nullid row =
row.Id = 15
let otherid row =
row.Id= 40
let iddata =
data
|> Seq.filter (not nullid)
|> Seq.filter (not otherid)
I create the functions.
Then I want to call the "not" of those functions to filter them out of a sequence.
But the issue is that I am getting errors for "row.Id" in the first two functions, because you can only do that with a type.
How do I solve this problem so I can accomplish this successfully.
My result should be a SEQUENCE of:
[(48,"MO);(78,"TN");(41,"VT")]
You can use >> operator to compose the two functions:
let iddata =
data
|> Seq.filter (nullid >> not)
|> Seq.filter (othered >> not)
See Function Composition and Pipelining.
Or you can make it more explicit:
let iddata =
data
|> Seq.filter (fun x -> not (nullid x))
|> Seq.filter (fun x -> not (othered x))
You can see that in action:
let input = [|1;2;3;4;5;6;7;8;9;10|];;
let is3 value =
value = 3;;
input |> Seq.filter (fun x -> not (is3 x));;
input |> Seq.filter (not >> is3);;
They both print val it : seq<int> = seq [1; 2; 4; 5; ...]
Please see below what an MCVE might look in your case, for an fsx file you can reference the Fsharp.Data dll with #r, for a compiled project just reference the dll an open it.
#if INTERACTIVE
#r #"..\..\SO2018\packages\FSharp.Data\lib\net45\FSharp.Data.dll"
#endif
open FSharp.Data
[<Literal>]
let datafile = #"C:\tmp\data.csv"
type CsvFile = CsvProvider<datafile>
let data = CsvFile.GetSample().Rows
In the end this is what you want to achieve:
data
|> Seq.filter (fun x -> x.Id <> 15)
|> Seq.filter (fun x -> x.Id <> 40)
//val it : seq<CsvProvider<...>.Row> = seq [(48, "MO"); (78, "TN"); (41, "VT")]
One way to do this is with SRTP, as they allow a way to do structural typing, where the type depends on its shape, for example in this case having the Id property. If you want you can define helper function for the two numbers 15 and 40, and use that in your filter, just like in the second example. However SRTP syntax is a bit strange, and it's designed for a use case where you need to apply a function to different types that have some similarity (basically like interfaces).
let inline getId row =
(^T : (member Id : int) row)
data
|> Seq.filter (fun x -> (getId x <> 15 ))
|> Seq.filter (fun x -> (getId x <> 40))
//val it : seq<CsvProvider<...>.Row> = seq [(48, "MO"); (78, "TN"); (41, "VT")]
Now back to your original post, as you correctly point out your function will show an error, as you define it to be generic, but it needs to operate on a specific Csv row type (that has the Id property). This is very easy to fix, just add a type annotation to the row parameter. In this case your type is CsvFile.Row, and since CsvFile.Row has the Id property we can access that in the function. Now this function returns a Boolean. You could make it return the actual row as well.
let nullid (row: CsvFile.Row) =
row.Id = 15
let otherid (row: CsvFile.Row) =
row.Id = 40
Then what is left is applying this inside a Seq.filter and negating it:
let iddata =
data
|> Seq.filter (not << nullid)
|> Seq.filter (not << otherid)
|> Seq.toList
//val iddata : CsvProvider<...>.Row list = [(48, "MO"); (78, "TN"); (41, "VT")]
I am trying to find a functional correct way for the following piece of code:
let mutable u = initialize cities pms
for i in 0 .. 10 do
u <- randomIteration u pms distances
randomIteration is a simple function which takes an array with 2 more parameters and returns a modified array. This process has to be repeated n-times (10 here).
I came up with a solution, which uses fold, but I am creating a "dummy" sequence just to be able to fold on it, which does not seem right.
let result = Seq.init 10 (fun i -> i) |> Seq.fold (fun uNext i -> randomIteration uNext pms distances) u
I could also use recursion with a counter variable, but that as well seems ackward. Am I just missing a simple right solution?
Just trying to think outside the box here, but instead of folding over randomIteration with different arguments each time, you could create a chain of N randomIteration calls and call this chain once:
let repeat n =
Seq.init n (fun _ u -> randomIteration u pms distances)
|> Seq.reduce (>>)
initialize cities pms
|> repeat 10
|> printfn "Result: %A"
I could also use recursion with a counter variable, but that as well seems ackward.
That would seem natural to me: allowing the result of one call to be passed to the next without mutable state. Something like:
let interateSelf func initial count =
let rec inner intermediate n =
if n = 1 then
func intermediate
else
inner (func intermediate) (n - 1)
inner initial count
One easy change to make that less awkward is to use a sequence expression rather than Seq.init.
let result = {1..10}
|> Seq.fold (fun uNext i -> randomIteration uNext pms distances) u
If you really want to keep the Seq.init you could replace the identify function with the build-in one like this:
let result = Seq.init 10 id
|> Seq.fold (fun uNext i -> randomIteration uNext pms distances) u
An alternative would be to create a recursive function something like this:
let result = let rec loop x i =
match i with
| 0 -> x
| i -> loop (randomIteration x pms) (i-1)
loop u 10
This is just a slight variation on Nikon-the-Third's answer. One could create a more general function that repeatedly applies another function:
let repeat n fn = List.init n (fun _ -> fn) |> List.reduce (>>)
This could then be used to create a named function that repeats the first 10 times:
let getNext10Times = repeat 10 (fun u -> randomIteration u pms distances)
getNext10Times u
or it could be used anonymously:
u |> repeat 10 (fun u -> randomIteration u pms distances)
I am prototyping how I am going to handle Double.NaN values in an F# array, and the first step, trying to simply count how many there are, has me stumped. The value "howMany" comes back as zero in my code, but I know there are 2, because I set 2 value to be Double.NaN. Can anyone point out what I am missing? Thanks!
let rnd = new System.Random()
let fakeAlphas = Array.init 10 (fun _ -> rnd.NextDouble());;
fakeAlphas.[0] <- Double.NaN;
fakeAlphas.[1] <- Double.NaN;
let countNA arr = arr |> Array.filter (fun x -> x = Double.NaN) |> Array.length;;
let howMany = countNA fakeAlphas;;
To answer the general question in the title:
let HowManySatisfy pred = Seq.filter pred >> Seq.length
for example
let nums = [1;2;3;4;5]
let countEvens = nums |> HowManySatisfy (fun n -> n%2=0)
printfn "%d" countEvens
Double.NaN = n is false for all n. See the MSDN page for Double.NaN.
Instead use Double.IsNaN. See the MSDN page for more information.
I think you need to use the Double.IsNan method. So your filter function would be:
(fun x -> Double.IsNan x)
I believe the issue is that NaN never equals anything -- even another NaN!
Lets say I have a string "COLIN".
The numeric value of this string would is worth:
3 + 15 + 12 + 9 + 14 = 53.
So
A = 1, B = 2, C = 3, and so on.
I have no idea how to even start in F# for this.
let mutable nametotal = 0
let rec tcalculate name =
name.ToString().ToCharArray()
|> Seq.length
Here is what I have so far. The seq.length is just there for testing to see if the toCharArray actually worked.
What you have is decent; here's another version:
#light
let Value (c:char) =
(int c) - (int 'A') + 1
let CalcValue name =
name |> Seq.sum_by Value
printfn "COLIN = %d" (CalcValue "COLIN")
// may be of interest:
printfn "%A" ("COLIN" |> Seq.map Value |> Seq.to_list)
It assumes the original input is uppercase. "int" is a function that converts a char (or whatever) to an int; Seq.sum_by is perfect for this.
I also show an example of using map, not sure what you're interested in.
If the 'mapping' is more arbitrary, you could use a strategy like the code below, where you can specify a data structure of what value each letter maps to.
#light
let table = [
'C', 3
'O', 15
'L', 12
'I', 9
'N', 14
]
let dictionary = dict table
let Value c =
match dictionary.TryGetValue(c) with
| true, v -> v
| _ -> failwith (sprintf "letter '%c' was not in lookup table" c)
let CalcValue name =
name |> Seq.sum_by Value
printfn "COLIN = %d" (CalcValue "COLIN")
I've found a hackish way to do this using the ascii value of the character, and getting the number from there but i think there might be a better way.
let tcalculate name =
name.ToString().ToLower().ToCharArray()
|> Seq.map (fun char -> Convert.ToInt32 char - 96)
|> Seq.sum
work's beautifully and maybe even more efficient then "mapping" but I'd like to view the solution I asked for
thanks all.
all you need to do, is make the string lowercase, turn it into a char array like you have done, loop through each letter, take the value of each char and subtract the value of 'a' and add one. that will make each letter have the value of its position in the alphabet.
I realize this is very old but I am recently learning F# and playing with the ideas in this question. Maybe someone will find it useful:
let table =
Seq.zip ['A'..'Z'] (Seq.initInfinite((+) 1))
|> Map.ofSeq
let calc (input : string) =
let s = input.ToUpper()
match s with
| _ when Seq.forall System.Char.IsLetter s ->
Some (Seq.sumBy (fun c -> table.[c]) s)
| _ -> None
let sumOfChar name = // F# functional answer
name
|> List.ofSeq // to char array
|> List.map (fun c -> int (System.Char.ToUpper c) - int 'A' + 1) // to value
|> List.fold (+) 0 // sum
sumOfChar "Herb" // 33
// Or simply this version:
let sumOfCharBy name =
let value c = int (System.Char.ToUpper c) - int 'A' + 1
List.sumBy value (List.ofSeq name)
sumOfCharBy "HerbM" // 46
// or simply:
let sumOfCharBy name =
name |> Seq.sumBy (fun c -> int (System.Char.ToUpper c) - int 'A' + 1)
sumOfCharBy "HMartin" // 83