I was introduced to a simple optimization problem by one of my interns last week. He was about to participate in a friendly competition where they were asked to find a performant solution to the problem:
open System | |
open System.IO | |
let find_files dir = Directory.GetFiles( dir, "*.fs?", SearchOption.AllDirectories ) | |
let not_start (s:string) p = not <| s.StartsWith p | |
let has_type (s:string) = if s.Contains @"type" then 1 else 0 | |
let has_module (s:string) = if s.Contains @"module" then 1 else 0 | |
let has_binding (s:string) = if s.Contains @"let" || | |
s.Contains @"member" then 1 else 0 |
Full source for JSON Transformer: https://gist.github.com/mrange/7c39333de480a8de5c812a1f6ba70173
Full source for XML Transformer: https://gist.github.com/mrange/552cb0b474b517b706333cebb64f44aa
A popular approach in Functional Languages to create parsers is to use parser combinators.
module FsTransducers = | |
type Context = | |
{ | |
mutable Continue : bool | |
} | |
static member New () : Context = { Continue = true } | |
type Initializer = Context -> unit | |
type Folder<'S, 'T> = 'S -> 'T -> 'S | |
type Completer<'S> = Context -> 'S -> 'S | |
type [<Struct>] Reducer<'S, 'T> = R of (Initializer*Folder<'S, 'T>*Completer<'S>) |
let rec distribute e = function | |
| [] -> [[e]] | |
| x::xs' as xs -> (e::xs)::[for xs in distribute e xs' -> x::xs] | |
let rec permute = function | |
| [] -> [[]] | |
| e::xs -> List.collect (distribute e) (permute xs) | |
let generatePermutations r (vs : _ []) = | |
let inline swap f t = |
module AnyTransformer = | |
type [<AbstractClass>] BadCause () = | |
class | |
abstract Describe : string | |
override x.ToString () = x.Describe | |
end | |
type [<Sealed>] MessageBadCause (msg: string) = | |
class | |
inherit BadCause () |
This blog created for F# Advent 2016 (English)
Full source code can be found here
- 2016-12-10
- New performance test - Anthony Lloyd (@AnthonyLloyd) suggested that I compare against Prime.Vmap.
How to finish/polish the integration between msbuild based .net core sdk and f# (fsc, FscTask, etc)
This doc contains current issues i known and a proposed solution (already implemented in a PR, and works) on how to fix these (obv ihmo).
AFAIK all the solution i choose are based on where dotnet/sdk
, microsoft/msbuild
and dotnet/cli
are going with development, and
updated to latest release (preview4), and vnext in development now.
type FatalError(message: string) = | |
inherit System.Attribute() | |
member __.Message = message | |
type Errors = | |
| [<FatalError("The value specified is currently not available")>] UnknownValue | |
| NotAnError | |
let PrintErrorMessage : Errors -> string = | |
fun err -> |