How do you provide ResizeArray to legacy F# apps? - f#

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.

Related

F# standard function that invokes its argument

Probably a newbie question, but is there a standard function like
let apply f = f()
in F#?
No, there is not a standard function for this.
In most cases, just calling the function is shorter and more obvious than using apply would be, so I'm not entirely sure how this would be useful:
foo ()
apply foo
Now, you can also write application using |>, but that's not very nice either:
() |> foo
I guess the only place where apply would be useful is:
functions |> List.map apply
functions |> List.map (fun f -> f ())
Here, the version without apply is shorter, but I don't think it is worth having a named function in the library just for this one use case.
You could actually use |> here to avoid fun, which makes for a lovely piece of ASCII art :-), but not for something that I would ever want to see in my codebase:
functions |> List.map ((|>) ())

Using an F# type provider to instantiate types and present them as properties

I'd like to do the following:
let allTypes = AllTypes (t, assemblies)
... where AllTypes is a type provider, the properties of which are instances of all types in the given array of assemblies that subclass type t. (All of the types have a single constructor that takes no arguments.)
Is this doable using F# type providers? I have no experience creating my own provider, and I don't want to waste my time attempting to do this if it isn't feasible.
I'd greatly appreciate any links to pages that would get me started coding this.
there's a lot of activity going on in the FSharp.Data github repo. There is a learning curve, but tuning into that repo might be useful.
Beyond that, this intro tutorial covers some of the basics, and here's a Type Provider starter pack that's been prepared by the F# open source community.
The fsharp.org site, and this projects page covers a cross-section of what's going on (including type providers).
You could take the list that Mark suggests here and turn it into a type provider. I think an exploratory way of interacting with namespaces would be useful. Why not? I'd use it. Please publish on GitHub if you get around to it.
You don't need a type provider for that; you can write that code using basic reflection:
open System.Reflection
let allTypes (baseClass : Type) (assemblies : Assembly seq) =
assemblies
|> Seq.collect (fun x -> x.GetExportedTypes())
|> Seq.filter (fun x -> baseClass.IsAssignableFrom x)
|> Seq.collect (fun x -> x.GetConstructors())
|> Seq.filter (fun x -> x.GetParameters().Length = 0)
|> Seq.map (fun x -> x.Invoke([||]))
The allTypes function has this signature: Type -> Assembly seq -> obj seq.

Right associative operator in F# [duplicate]

This question already has an answer here:
Function Application Operator ($) in F#?
(1 answer)
Closed 8 years ago.
Sometimes I have to write:
myList |> List.iter (fun x -> x)
I would really like to avoid the parentheses. In Haskell there is an operator for this ($)
It would look like this
myList |> List.iter $ fun x -> x
I created a custom operator
let inline (^!) f a = f a
and now I can write it like this
myList |> List.iter ^! fun x -> x
Is there something like this in F#?
There is no way to define custom operator with an explicitly specified associativity in F# - the associativity is determined based on the symbols forming the operator (and you can find it in the MSDN documentation for operators).
In this case, F# does not have any built-in operator that would let you avoid the parentheses and the idiomatic way is to write the code as you write it originally, with parentheses:
myList |> List.iter (fun x -> x)
This is difference in style if you are coming from Haskell, but I do not see any real disadvantage of writing the parentheses - it is just a matter of style that you'll get used to after writing F# for some time. If you want to avoid parentheses (e.g. to write a nice DSL), then you can always named function and write something like:
myList |> List.iter id
(I understand that your example is really just an example, so id would not work for your real use case, but you can always define your own functions if that makes the code more readable).
No, there's nothing like this in a standard F# library. However, you have almost done creating your own operator (by figuring out its name must start with ^).
This snippet by Stephen Swensen demonstrates a high precedence, right associative backward pipe, (^<|).
let inline (^<|) f a = f a
This single-liner from the linked page demonstrates how to use it:
{1..10} |> Seq.map ^<| fun x -> x + 3
And here is an example how to use it for multi-line functions. I find it most useful for real-world multi-liners as you no longer need to keep closing parenthesis at the end:
myList
|> List.map
^<| fun x ->
let ...
returnValue
In F# it's <|
So it would look like:
myList |> List.iter <| fun x -> x

Itertools for F#

I'm used to Python's itertools for doing functional things with iterators (F#: sequences) and wondered if there were equivalents in F# or a commonly used library since they're so handy.
The top tools for me are:
product : cartesian product, equivalent to a nested for-loop
combinations
permutations
takewhile
dropwhile
chain : chain multiple iterators together into a new longer iterator
repeat* : repeat(5) -> 5, 5, 5...
count* : count(10) -> 10, 11, 12...
cycle* : cycle([1,2,3]) -> 1,2,3,1,2...
* I suppose these 3 would yield monads in F#? How do you make them infinite?
I'm prompted to ask because I saw this question on permutations in F# and was surprised it was not part of a library or built into the language.
I don't know if there's a commonly used library that contains functions like product, combinations and permutations, but the others you've mentioned are already in Seq and List modules or can be implemented without much trouble, and there are also useful methods in System.Linq.Enumerable.
takewhile -> Seq.takeWhile
dropwhile -> Seq.skipWhile
chain -> Seq.concat
repeat -> Seq.initInfinite
count(10) -> Seq.initInfinite ((+) 10)
cycle([1, 2, 3]) -> Seq.concat <| Seq.initInfinite (fun _ -> [1; 2; 3])
You also might want to check out the excellent FSharpx library -- it contains a lot of useful functions to work with collections and whatnot.
For a cartesian product in F# (avoiding a nested for loop ^_^), you need to use List.allPairs list1 list2 :
https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-listmodule.html#allPairs

'MaxDegreeOfParallelism' for Array.Parallel?

Is it possible to set 'MaxDegreeOfParallelism' (that is maximum number of threads to use) for Array.Parallel module since under the hood it uses Parallel.For?
According to this post, it seems that there is no way to limit the number of threads globally in the final version of Parallel Extensions. An alternative to what brian suggests would be to use PLINQ (which works with parallel sequences) instead of functions that work with arrays.
This can be done using the PSeq module from F# PowerPack. It provides functions such as PSeq.map, PSeq.filter and many other that work with parallel sequences (which can be also nicely composed using pipelining). For parallel sequences, you can use the WithDegreeOfParallelism extension method to specify the behavior.
You could implement a wrapper function for it:
[EDIT: It is already there!]
let withDegreeOfParallelism n (pq:ParallelQuery<_>) =
pq.WithDegreeOfParallelsm(n)
And then write:
let res =
data |> PSeq.map (fun n -> ...)
|> PSeq.withDegreeOfParallelism ParallelOptions.MaxDegreeOfParallelism
|> Array.ofSeq
This may have different perfromance, because it is implemented differently than functions in the Array.Parallel module, but this certainly depends on your scenario.
No, I don't think so.
You can always create your own versions of any of the methods in the Array.Parallel module, using the source code from array.fs (in the CTP release) as a starter.
Assuming I want say at most 10 threads I've been replacing:
myArray
|> Array.Parallel.iter (fun item -> doWork item)
with
let maxPara = 10
myArray
|> Array.splitInto maxPara
|> Array.Parallel.iter (fun items -> items |> List.iter (fun item -> doWork item))

Resources