I want to use this function let CopyDir target source filterFile ...(line 219) and specify a filter. The idea is that the filter will contains files which will be excluded. Right now I am using a string value log4net and it is working but I want to replace it with nugetDependencies which is a collection of strings. Would you help me pls
let nugetDependencies = getDependencies "./packages.config"
let excludeNuget (path : string) = path.Contains "log4net" |> not
CopyDir nugetToolsDir (buildDir ## package) excludeNuget
UPDATE:
Fixed wrong URL
I had to read the question a few times to understand it. My understand is that you want to filter a list of file paths by a list of exclusions--with "log4net" being an example of an exclusion.
I'd go something like this, taking advantage of List.exists:
let excludePaths (pathsToExclude : string list) (path: string) =
pathsToExclude |> List.exists (fun exPath -> path.Contains(exPath)) |> not
This implementation can actually curry the labda function fun exPath -> path.Contains(exPath) into simply path.Contains since the method takes a single argument, which would give us:
let excludePaths (pathsToExclude : string list) (path: string) =
pathsToExclude |> List.exists path.Contains |> not
Currying (the F# formal term is partial application) can also be put to use here to bind an argument to the function. To create a check for "log4net", you can simply do this:
let nugetExclusions = ["log4net"]
let excludeNuget = excludePaths nugetExclusions
Just add all of the nuget paths you need to exclude from the list.
Since you are comparing paths contains doesn't have a case-insensitive overload. At least not out of the box. You can add an extension function to string, though. A C# implementation is here on SO.
Here's a F# implementation of the extension method (note that I made this with a small-case contains--F# functions and overloads don't mix):
type System.String with
member x.contains (comp:System.StringComparison) str =
x.IndexOf(str,comp) >= 0
With this type extension in place we can change the excludePaths function to this (again, I'm currying the newly created contains extension method:
let excludePaths (pathsToExclude : string list) (path: string) =
pathsToExclude
|> List.exists (path.contains StringComparison.OrdinalIgnoreCase))
|> not
I hope you continue to use F#.
How about this?
let excludeNuget (path : string) =
nugetDependencies
|> Seq.exists (fun x -> path.Contains x)
Related
I have some code, 3 functions
ParseTemplate, ParseTemplates -> These two can be combined into one and will have to be to make this work I think.
And
loadTemplate
let ParseTemplate (template: Match) =
let templateName = template.Groups.[1] |> string
loadTemplate templateName
let ParseTemplates (string: string) =
Regex.Replace(string, "\[tpl\:(.*?)\]", MatchEvaluator ParseTemplate)
let rec loadTemplate templateName =
templateName
|> getTemplateFilePath
|> File.ReadAllText
|> ParseVariables
|> ParseArrays
|> ParseLanguageVariablesWithReplacements
|> ParseSimpleLanguageVariables
|> ParseTemplates
The problem is where to position these in the file/how to structure them differently, because as it is there's always a function calling a function below it (and therefore is not defined).
In this case
I like the loadTemplate function as it exists - it's very clean and readable and I'd rather avoid using lambda functions inside of it if possible. The problem is it calls ParseTemplates which can then call loadTemplate recursively.
Best way to approach?
I'm messing around in F# and tried to write a function that can take an int list or a string list. I have written a function that is logically generic, in that I can modify nothing but the type of the argument and it will run with both types of list. But I cannot generically define it to take both.
Here is my function, without type annotation:
let contains5 xs =
List.map int xs
|> List.contains 5
When I try to annotate the function to take a generic list, I receive a warning FS0064: the construct causes the code to be less generic than indicated by the type annotations. In theory I shouldn't need to annotate this to be generic, but I tried anyway.
I can compile this in two separate files, one with
let stringtest = contains5 ["1";"2";"3";"4"]
and another with
let inttest = contains5 [1;2;3;4;5]
In each of these files, compilation succeeds. Alternately, I can send the function definition and one of the tests to the interpreter, and type inference proceeds just fine. If I try to compile, or send to the interpreter, the function definition and both tests, I receive error FS0001: This expression was expected to have type string, but here has type int.
Am I misunderstanding how typing should work? I have a function whose code can handle a list of ints or a list of strings. I can successfully test it with either. But I can't use it in a program that handles both?
You are running into value restrictions on the automatic generalization of the type inference system as outlined here
Specifically,
Case 4: Adding type parameters.
The solution is to make your function generic rather than just making its parameters generic.
let inline contains5< ^T when ^T : (static member op_Explicit: ^T -> int) > (xs : ^T list) =
List.map int xs
|> List.contains 5
You have to make the function inline because you have to use a statically resolved type parameter, and you have to use a statically resolved type parameter in order to use member constraints to specify that the type must be convertible to an int. As outlined here
You can use inline to prevent the function from being fixed to a particular type.
In FSI, the interactive REPL:
> open System;;
> let inline contains5 xs = List.map int xs |> List.contains 5;;
val inline contains5 :
xs: ^a list -> bool when ^a : (static member op_Explicit : ^a -> int)
> [1;2;3] |> contains5;;
val it : bool = false
> ["1";"2";"5"] |> contains5;;
val it : bool = true
Note that the signature of contains5 has a generic element to it. There's more about inline functions here.
This is already answered correctly above, so I just wanted to chime in with why I think it's a good thing that F# appears to makes this difficult / forces us to lose type safety. Personally I don't see these as logically equivalent:
let inline contains5 xs = List.map int xs |> List.contains 5
let stringTest = ["5.00"; "five"; "5"; "-5"; "5,"]
let intTest = [1;2;3;4;5]
contains5 stringTest // OUTPUT: System.FormatException: Input string was not in a correct format.
contains5 intTest // OUTPUT: true
When inlined, the compiler would create two logically distinct versions of the function. When performed on the list<int> we get a boolean result. When performed on a list<string> we get a boolean result or an exception. I like that F# nudges me towards acknowledging this.
let maybeInt i =
match Int32.TryParse i with
| true,successfullyParsedInteger -> Some successfullyParsedInteger
| _ -> None
let contains5 xs =
match box xs with
| :? list<int> as ixs ->
ixs |> List.contains 5 |> Ok
| :? list<string> as sxs ->
let successList = sxs |> List.map maybeInt |> List.choose id
Ok (successList |> List.contains 5)
| _ ->
Error "Error - this function expects a list<int> or a list<string> but was passed something else."
let stringTest = ["5.00"; "five"; "5"; "-5"; "5,"]
let intTest = [1;2;3;4;5]
let result1 = contains5 stringTest // OUTPUT: Ok true
let result2 = contains5 intTest // OUTPUT: Ok true
Forces me to ask if some of the values in the string list cannot be parsed, should I drop out and fail, or should I just try and look for any match on any successful parse results?.
My approach above is horrible. I'd split the function that operates on the strings from the one that operates on the integers. I think your question was academic rather than a real use case though, so I hope I haven't gone off on too much of a tangent here!
Disclaimer: I'm a beginner, don't trust anything I say.
This code snippet reproduces a problem I am having with some production code. The function containsProperty represents a real world function that is actually in a library, so that I have no say in what the signature is.
The problem is that I can't figure out how to create a wrapper function that can take a normal function as argument, and then pass that on to containsProperty. I can call containsProperty directly with a function as a lambda expression, but I can't call it with a function that comes from some other source.
The function addToGroup is the best I've come up with so far, and it uses quotations. There are two problems with that approach, which I am trying to figure out. First, how do I get rid of the Func cast in the quotation? Perhaps somehow move it into addToGroup? Second, can I build on this in order to just pass a function? I haven't succeeded in finding something that doesn't produce either a compile time error or a runtime error.
The function addToGroup2 is what I'd like to do, but it doesn't compile. The error message is "No constructors are available for the type 'Quotations.Expr<'a>'".
Why do I bother to struggle with this? Because as long as I can't treat the passed in function as a first class value, I can't create the design I'm after. I want these functions to come along from a collection of records.
If you paste this snippet into LINQPad or something, comment out addToGroup2 and the calls to it, in order to make the snippet compile and run.
open System
open System.ComponentModel
open System.ComponentModel.DataAnnotations // Reference to this assembly required.
type CfgSettings = {
mutable ConnectionString: string
mutable Port: int
}
and CfgSettingsMetadata() =
static member containsProperty<'TProperty>(propertyExpression: Linq.Expressions.Expression<Func<CfgSettings,'TProperty>>) =
Console.WriteLine "good!"
static member addToGroup f =
CfgSettingsMetadata.containsProperty(FSharp.Linq.RuntimeHelpers.LeafExpressionConverter.QuotationToLambdaExpression f) |> ignore
static member addToGroup2 (f: CfgSettings -> 'TProperty) =
CfgSettingsMetadata.containsProperty(FSharp.Linq.RuntimeHelpers.LeafExpressionConverter.QuotationToLambdaExpression (Quotations.Expr<Func<CfgSettings,'TProperty>>f)) |> ignore
static member BuildMetadata () =
CfgSettingsMetadata.containsProperty(fun x -> x.ConnectionString)
CfgSettingsMetadata.containsProperty(fun x -> x.Port)
CfgSettingsMetadata.addToGroup <# Func<_,_>(fun x -> x.ConnectionString) #>
CfgSettingsMetadata.addToGroup <# Func<_,_>(fun x -> x.Port) #>
CfgSettingsMetadata.addToGroup2 (fun x -> x.ConnectionString)
CfgSettingsMetadata.addToGroup2 (fun x -> x.Port)
CfgSettingsMetadata.BuildMetadata()
Both answers in question Expression<Func<T, bool>> from a F# func helped me somewhat, but I haven't found a solution yet.
So, there are two questions here.
How to pass a function without having to wrap it in <# ... #>?
For this, you just need to add the [<ReflectedDefinition>] attribute to your method's parameter. It implicitly wraps the argument passed to it in a quotation.
type CfgSettingsMetadata() =
static member addToGroup([<ReflectedDefinition>] f: Expr<CfgSettings -> 'TProperty>) =
CfgSettingsMetadata.containsProperty(LeafExpressionConverter.QuotationToLambdaExpression f) |> ignore
// Example use:
CfgSettingsMetadata.addToGroup(Func<_, _>(fun x -> x.ConnectionString))
How to convert from Expr<a -> b> to Expression<Func<a, b>>?
This is indeed explained in the question you linked, although the API has changed a bit since then.
type CfgSettingsMetadata() =
static member addToGroup ([<ReflectedDefinition>] (f: Expr<CfgSettings -> 'TProperty>)) =
let call = LeafExpressionConverter.QuotationToExpression f :?> MethodCallExpression
let lambda = call.Arguments.[0] :?> LambdaExpression
let e = Expression.Lambda<Func<CfgSettings, 'TProperty>>(lambda.Body, lambda.Parameters)
CfgSettingsMetadata.containsProperty(e) |> ignore
// Example use:
CfgSettingsMetadata.addToGroup(fun x -> x.ConnectionString)
I'm trying to create some kind of interface, but i cannot find how to use custom attributes in F# as MSDN only shows usage of CLR attributes. This is what i want to achieve:
open System
type Command (name : string) =
inherit Attribute()
member this.Name = name
[<Command("something")>]
let doSomething () =
Console.Write("I'm doing something")
[<Command("somethingElse")>]
let doSomethingElse () =
Console.Write("I'm doing something else")
[<EntryPoint>]
let main args =
let command = Console.ReadLine()
// find function where Command.Name = command and call it
Console.Read()
0
To extend on your answer, a more generic approach would be to get all the types and then filter the functions that have the attribute you're looking for (as your approach would break down once your application grows and no longer has everything "packed" into the Program class):
let getCommands () =
let types = Assembly.GetExecutingAssembly().GetTypes()
let commands =
types
|> Array.collect (fun typ -> typ.GetMethods())
|> Array.choose (fun mi ->
mi.CustomAttributes
|> Seq.tryFind (fun attr -> attr.AttributeType = typeof<Command>)
|> Option.map (fun attr -> attr, mi))
let commandsMap =
commands
|> Seq.map (fun (attr, mi) ->
let name =
let arg = attr.ConstructorArguments.[0]
unbox<string> arg.Value
name, mi)
|> Map.ofSeq
commandsMap
This gets all the functions from all the types in the executing assembly, then filters out everything that doesn't have command attribute. Then it builds a map where the key is the attribute argument and the value is the MethodInfo of the function.
Ok, found it.
Reflection.Assembly.GetExecutingAssembly().GetType("Program").GetMethods()
Program typename is not viable in code so it cannot be used in typeof<Program>, but this type exists and can be taken from assembly.
I have following code that populates a System.Collections.Generic.List I don't like it so I was wondering if there is a better way to do this.
let getDirectories =
Directory.GetDirectories(_baseFolder)
let languagesList = new System.Collections.Generic.List<string>()
Seq.cast getDirectories
|> Seq.map(fun dir -> (new DirectoryInfo(dir)).Name)
|> fun range -> languagesList.AddRange(range)
Have you tried:
let list = new System.Collections.Generic.List<string>(arr)
List<'T> has a constructor that takes an IEnumerable<'T> so it happily takes any seq<'T> you pass to it.
In addition to Mehrdad's answer
I find it helpful to define helper modules for many standard collections and .Net types to make them more F# friendly. Here I would define the following
module BclListUtil =
let ofArray (arr: 'T array) = new System.Collections.Generic.List<'T>(arr)
let ofSeq (arr: 'T seq) = new System.Collections.Generic.List<'T>(arr)
Then you could change your original code to the following
let getDirectories =
Directory.GetDirectories(_baseFolder)
let languagesList =
getDirectiories
|> Seq.map (fun dir -> (new DirectoryInfo(dir)).Name)
|> BclListUtil.ofSeq
The F# alias for System.Collections.Generic.List<_> is ResizeArray<_> as kvb noted. The F# PowerPack includes a ResizeArray module for working with BCL Lists in an idiomatic F# fashion similar to the Seq and List modules.
However, for some strange reason this module seems to include ofArray and ofList and toSeq but not ofSeq.
Use ResizeArray that equals System.Collections.Generic.List
Example:
let languagesList = ResizeArray<string>()
languagesList.Add("1");
languagesList.Add("2");