I'm limited to .net 3.5 and want to use Reactive Extensions with F# 3.0.
Apparently, F# and RX introduce different versions of System.IObservable<T>
open System.Reactive
open System.Timers
open System
[<EntryPoint>]
let main argv =
let timer = new Timer(500.0)
timer.AutoReset <- true
timer.Start()
timer.Elapsed
|> Observable.scan (fun i _ -> i + 1) (-1)
:> IObservable<int>
|> ignore
0
This fails with:
The type 'System.IObservable' is not compatible with the type 'System.IObservable'
Is it possible to use both? Or is there a version of RX compatible with .net 3.5 without conflicts? I also tried looking for sources of rx 1, but didn't find, although I'm not sure if it will be useful
Related
i have a solution called Algos
on Solution explorer i have 2 projects inside this solution
one called Algos (again ! maybe i should change the name for avoiding confusion ?)
which is a console application
one called MyLibrary which is a Class Library
I have in the solution explorer added in the References of the Project Algo MyLibrary and i can see it in the list.
// useful functions
// returns the minimum + index of the minimum
namespace Misc
exception InnerError of string
module Search =
let mini (s : (int*int) list) =
match s with
| [] -> (-1,(-1,-1))
| _ -> s |> Seq.mapi (fun i x -> (i, x)) |> Seq.minBy snd
let maxi (s : (int*int) list) =
match s with
| [] -> (-1,(-1,-1))
| _ -> s |> Seq.mapi (fun i x -> (i, x)) |> Seq.maxBy snd
module Bit =
let rec sumbits (n:int):int=
let rec helper acc m =
match m with
| 0 -> acc
| 1 -> acc+1 // enlever cela ?
| _ -> let r = m%2
helper (acc+r) (m>>>1)
helper 0 n
let power2 k =
let powers_of_2 = [|1;2;4;8;16;32;64;128;256;512;1024;2048;4096;8192;16384;32768;65536;131072;262144;524288;1048576;2097152;4194304;8388608;16777216|]
if ((k >= 24) || (k<0)) then raise (InnerError("power exponent not allowed"))
else powers_of_2.[k]
i'm just to use Misc.Bit.power2 in the main code
open MyLibrary
let a = Misc.Bit.power2 3
but Misc.Bit will be underlined and I have a compiler error
Severity Code Description Project File Line Suppression State
Error The value, constructor, namespace or type 'Bit' is not defined Algos C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Algos\Algos\TSP.fs 50
what have i done wrong ? does it come from other parts of the source code perhaps ?
there are no other warnings.
both projects use .NET Framework 4.5.2
MyLibrary uses Target F# Runtime 4.3.1 while there is no similar indication for Algos.
thanks
Two things come to mind here: I don't see where the namespace MyLibrary is defined, and you haven't mentioned that you actually compiled the dependency.
Generally, to reference and use a library within the same solution, you need to:
add the library to the using program's dependency, preferably via: References – right-click – add Reference (Reference Manager) – Projects – Solution, by checking the checkbox of the dependency.
compile the dependency. When referenced properly, this should automatically occur before dependent code is compiled, but IntelliSense will only update after compiles! So hit compile when you see outdated errors.
use identifiers from the library via their correct namespace. Make sure that the qualified names or open declarations in the using code are correct.
To my knowledge, this should be all you need to do.
There are rare cases where files get write-locked but never released on compilation and you need to restart Visual Studio to be able to compile again. I've also encountered a case where some interaction of Git and Visual Studio created corrupted, half-deleted files with effectively no file owner; this required a reboot to fix. If you're really scratching your head and the errors are clearly nonsensical, maybe try moving the folder to check for file system damage.
This should certainly work. Please also make sure that you target same versions of F# and .NET: .NET 4.5.2 and 4.4.0.0 in the library and the console application
Tested on F# 3.1 on windows 7
fsi.PrintLength <- 5000;;
[1..5000];;
Process is terminated due to StackOverflowException.
Session termination detected. Press Enter to restart.
on Mono (F# 4.0), there doesn't seem to be such a limitation.
I think this is a bug in the formatting module that takes care of pretty printing to F# Interactive.
There are some non-tail recursive functions that uses PrintLength e.g. boundedUnfoldL in this line. Implementation of boundedUnfoldL is indeed not tail-recursive:
let boundedUnfoldL
(itemL : 'a -> layout)
(project : 'z -> ('a * 'z) option)
(stopShort : 'z -> bool)
(z : 'z)
maxLength =
let rec consume n z =
if stopShort z then [wordL "..."] else
match project z with
| None -> [] // exhaused input
| Some (x,z) -> if n<=0 then [wordL "..."] // hit print_length limit
else itemL x :: consume (n-1) z // cons recursive...
consume maxLength z
I don't know why it doesn't blow up on Mono. It would be surprising if F# Interactive on Mono can handle length > 5000 successfully.
You can report this as a bug to https://visualfsharp.codeplex.com/workitem/list/basic.
I'm currently trying to learn f# using the book Real-World Functional Programming by Petricek and Skeet (2010) but have been encountering problems when using continuations to avoid stack overflow.
The problem that I have been encountering is that my code using continuations works perfectly when launched in the f# interactive, but still causes stack overflow when placing the code in the program.fs file and then launching it through the debugger in Visual Studio.
It is unclear to me why this happens, and would very much appreciate if anyone could give me an explanation to why this happens.
In case the version of Visual Studio is relevant, I am using:
Visual Studio Ultimate 2012
Version 11.0.61030.00 Update 4
The .Net framework used is:
Version. 4.5.51641
The code presented in the book that is causing this problem is presented below:
open System
let rand = new Random()
//A tree is either a leaf with a value or a node that contains two children
type IntTree =
| Leaf of int
| Node of IntTree * IntTree
//A list of that will decide all leaf values
let numbers2 = List.init 1000000 (fun _ -> rand.Next(-50,51))
///Creates an imbalanced tree with 1000001 leafs.
let imbalancedTree2 =
numbers2 |> List.fold (fun currentTree num ->
Node(Leaf(num), currentTree)) (Leaf(0))
//Sums all leafs in a tree by continuation and will execute the inserted function with the total
//sum as argument once all values have been summed.
let rec sumTreeCont tree cont =
match tree with
| Leaf(num) -> cont(num)
| Node(left, right) ->
sumTreeCont left (fun leftSum ->
sumTreeCont right (fun rightSum ->
cont(leftSum + rightSum)))
//Sums the imbalanced tree using the sumTreeCont function
let sumOfTree = sumTreeCont imbalancedTree2 (fun x -> x)
Thanks in advance!
//Tigerstrom
If you are running the program in Debug mode, then the default Visual Studio project setting disables tail calls. The main reason is that, with tail calls enabled, you do not get very useful information in the call stack (which makes debugging harder).
To fix this, you can go to your project options and check "Generate tail calls" on the "Build" page. In release mode, this is enabled by default.
I'm building TrueSkill, the F# app, from 2008, on Mono 3.0 with F# 3.0. The two errors I get are
fList |> ResizeArray.iter (fun f -> f.ResetMarginals()) and
let sumLogS = fList |> ResizeArray.fold_left (fun acc f -> acc + (f.LogNormalisation ())) 0.0.
For F# 1.9, ResizeArray came from the PowerPack. Apparently there's a PowerPack on github now. But the standard Mono docs show that ResizeArray<T> is just an alias for List<T>. Do I need to get the original ResizeArray, and if so, how would I do just that from the PowerPack, using the ResizeArray.fs? What's the current relationship between List and ResizeArray?
To clarify, ResizeArray<'T> is also an alias for List<'T> in .NET. Only high-order functions from ResizeArray module are provided by F# PowerPack.
Because there is no dependency on this module, it is recommended to copy ResizeArray.fs directly to your project. You probably have to change a few function names to match the new ResizeArray module e.g. changing fold_left to fold and fold_right to foldBack.
I am learning RX (Reactive Extensions), I tried to use some code samples from internet, the source code was in C#:
var input = Observable.Range(1, 100);
input.Sum().Subscribe(x => Console.WriteLine("The Sum is {0}", x));
Since I don't really "SPEAK" C#, so I want to use F# to do the same thing.
I download RX, and create a F# windows application, add reference to System.Reactive. My IDE is VS 2010 Ultimate, RX version is: 1.1.11111
Here is my code:
#light
open System
open System.Collections.Generic
open System.ComponentModel
open System.Linq
open System.Text
open System.Reactive
open System.Reactive.Linq
let input = Observable.Range(1, 100)
let x = input.Sum().Subscribe()
printfn "%A" x
The result should be 5050, as the sum of 1 to 100 is 5050. However, I can see only this:
System.Reactive.AutoDetachObserver`1[System.Int32]
Please let me know how I can see the result for 5050.
I hope the C# code will work in F# too. If not, please let me know what I can do.
Thanks and Happy New Year to you all!
The Subscribe method takes a function as its argument (x => foo is an anonymous function (lambda expression) in C#). So you should also call it with a function as its argument:
let input = Observable.Range(1, 100)
input.Sum().Subscribe(fun x -> printfn "%A" x)
Or
let input = Observable.Range(1, 100)
input.Sum().Subscribe(printfn "%A")