Guide - Web Programming with F#

2    08 Nov 2015 03:10 by u/roznak

2 comments

0

F# is a language that will fry your brains, but a very interesting language because it is so unlike all other languages and yet combined with C# a fantastic mix.

It has become open source now and I hope that this language finally gets the love it deserves.

0

A silly example:

namespace LogParser
open System.IO
type FSLogParser() = 
    member public this.LoadFile(fileToLoad : string) =
        let lines = File.ReadLines(fileToLoad)                
        lines |> Seq.iter(fun x -> printfn  "%s" x) 
        lines
    member public this.Process(fileToParse : string)= 
        let ParseFile = File.ReadAllLines(fileToParse)
        let ParseFileForErrors = 
            ParseFile
            |> Seq.filter(fun x -> if x.Contains("_warning_") then false else true)
        let ParseFileForWarnings = 
            ParseFile
            |> Seq.filter(fun x -> if x.Contains("_warning_") then true else false)
        let myFileSet = 
            let mySubFileSet = Seq.append ParseFileForErrors ParseFileForWarnings    
            let FileSummary = "Number of Entries : " + ParseFile.Length.ToString() + " Number of Errors : " + (ParseFileForErrors |> Seq.length).ToString()
            Seq.append ( FileSummary |> Seq.singleton) mySubFileSet
        File.WriteAllLines("Z:\\Test.txt", myFileSet)     
        ParseFile

C# unit code:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using LogParser;
namespace LogParserTest
{
    [TestClass]
    public class NUnitTest1
    {
        [TestMethod]
        public void TestMethod1() {
            var logParser = new FSLogParser();
            var result = logParser.Process("TextFile1.txt");
        }
    }
}

In F# this is your class method: member public this.Process(fileToParse : string)=

In F# open System.IO is basically your C# using System.IO

So C# can use F# classes and F# classes can use C#.

What Makes F# interesting is that it can process lists in parallel with almost no effort.