First commit.
authorGreg Burri <greg.burri@gmail.com>
Sun, 23 Nov 2014 15:03:46 +0000 (16:03 +0100)
committerGreg Burri <greg.burri@gmail.com>
Sun, 23 Nov 2014 15:03:46 +0000 (16:03 +0100)
FSharpRef.sln [new file with mode: 0644]
FSharpRef/App.config [new file with mode: 0644]
FSharpRef/FSharpRef.fsproj [new file with mode: 0644]
FSharpRef/Functional.fs [new file with mode: 0644]
FSharpRef/Main.fs [new file with mode: 0644]
FSharpRef/Object.fs [new file with mode: 0644]

diff --git a/FSharpRef.sln b/FSharpRef.sln
new file mode 100644 (file)
index 0000000..2e89cee
--- /dev/null
@@ -0,0 +1,22 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2013
+VisualStudioVersion = 12.0.21005.1
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharpRef", "FSharpRef\FSharpRef.fsproj", "{CFF39041-0222-46AD-BF2A-6C93955DD06E}"
+EndProject
+Global
+       GlobalSection(SolutionConfigurationPlatforms) = preSolution
+               Debug|Any CPU = Debug|Any CPU
+               Release|Any CPU = Release|Any CPU
+       EndGlobalSection
+       GlobalSection(ProjectConfigurationPlatforms) = postSolution
+               {CFF39041-0222-46AD-BF2A-6C93955DD06E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+               {CFF39041-0222-46AD-BF2A-6C93955DD06E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+               {CFF39041-0222-46AD-BF2A-6C93955DD06E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+               {CFF39041-0222-46AD-BF2A-6C93955DD06E}.Release|Any CPU.Build.0 = Release|Any CPU
+       EndGlobalSection
+       GlobalSection(SolutionProperties) = preSolution
+               HideSolutionNode = FALSE
+       EndGlobalSection
+EndGlobal
diff --git a/FSharpRef/App.config b/FSharpRef/App.config
new file mode 100644 (file)
index 0000000..c9bdc98
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <startup> 
+        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
+    </startup>
+    <runtime>
+      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
+        <dependentAssembly>
+          <assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
+          <bindingRedirect oldVersion="4.0.0.0" newVersion="4.3.0.0"/>
+          <bindingRedirect oldVersion="2.3.5.0" newVersion="4.3.0.0"/>
+          <bindingRedirect oldVersion="2.0.0.0" newVersion="4.3.0.0"/>
+          
+        </dependentAssembly>
+      </assemblyBinding>
+    </runtime> 
+</configuration>
\ No newline at end of file
diff --git a/FSharpRef/FSharpRef.fsproj b/FSharpRef/FSharpRef.fsproj
new file mode 100644 (file)
index 0000000..cd169ed
--- /dev/null
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>cff39041-0222-46ad-bf2a-6c93955dd06e</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>FSharpRef</RootNamespace>
+    <AssemblyName>FSharpRef</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <Name>FSharpRef</Name>
+    <TargetFSharpCoreVersion>4.3.0.0</TargetFSharpCoreVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <Tailcalls>false</Tailcalls>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <WarningLevel>3</WarningLevel>
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DocumentationFile>bin\Debug\FSharpRef.XML</DocumentationFile>
+    <Prefer32Bit>true</Prefer32Bit>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <Tailcalls>true</Tailcalls>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <WarningLevel>3</WarningLevel>
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DocumentationFile>bin\Release\FSharpRef.XML</DocumentationFile>
+    <Prefer32Bit>true</Prefer32Bit>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+      <Private>True</Private>
+    </Reference>
+    <Reference Include="mscorlib" />
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Numerics" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Functional.fs" />
+    <Compile Include="Object.fs" />
+    <Compile Include="Main.fs" />
+    <None Include="App.config" />
+  </ItemGroup>
+  <PropertyGroup>
+    <MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
+  </PropertyGroup>
+  <Choose>
+    <When Condition="'$(VisualStudioVersion)' == '11.0'">
+      <PropertyGroup>
+        <FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
+      </PropertyGroup>
+    </When>
+    <Otherwise>
+      <PropertyGroup>
+        <FSharpTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets</FSharpTargetsPath>
+      </PropertyGroup>
+    </Otherwise>
+  </Choose>
+  <Import Project="$(FSharpTargetsPath)" Condition="Exists('$(FSharpTargetsPath)')" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>
\ No newline at end of file
diff --git a/FSharpRef/Functional.fs b/FSharpRef/Functional.fs
new file mode 100644 (file)
index 0000000..424dc35
--- /dev/null
@@ -0,0 +1,115 @@
+module Functional
+
+open System.IO
+open System.Linq
+
+// For dynmic typing. To print anything in our case.
+let inline print a = printfn "%A" a
+
+// Write something on a file.
+let appendFile (filename: string) (text: string) = 
+    use sw = new StreamWriter(filename, true)
+    sw.WriteLine(text)
+
+// Recursive function.
+let rec fac n = 
+    if n <= 1 then 1
+    else n * fac (n - 1)
+
+// Mutual recursive functions.
+let rec isEven n =
+    if n = 0 then true
+    elif n = 1 then false
+    else isOdd (n - 1)
+and isOdd n =
+    if n = 0 then false
+    elif n = 1 then true
+    else isEven (n - 1)
+
+// Algebraic data type.
+type Suit =
+    | Diamond
+    | Spade
+    | Heart
+    | Club
+
+type PlayingCard =
+    | Ace of Suit
+    | King of Suit
+    | Queen of Suit
+    | Jack of Suit
+    | NumCard of int * Suit
+    member this.Value = // Property.
+        match this with
+            | Ace (_) -> 14
+            | King (_) -> 13
+            | Queen (_) -> 12
+            | Jack (_) -> 11
+            | NumCard (n, _) when n >= 2 && n <= 10 -> n
+            | NumCard (_) -> failwith "Card has invalid value!"
+
+let deckOfCards = 
+    [
+        for suit in [ Spade; Club; Heart; Diamond ] do
+            yield Ace(suit)
+            yield King(suit)
+            yield Queen(suit)
+            yield Jack(suit)
+            for n in [2 .. 10] do
+                yield NumCard(n, suit)
+    ]
+
+// Records.
+type Person = { First: string; Last: string; Age: int }
+let steve = { First = "Steve"; Last = "Holt"; Age = 17 }
+let steves'twin = { steve with First = "Paul" }
+
+// Queries. Should be used with SQL or other external data sources.
+let youngPersonNames persons =
+   query {
+      for p in persons do
+      where (p.Age < 15)
+      select p.First
+   }
+
+// Infinite lazy sequence.
+let allPositiveInts =
+    seq {
+        for i in 1 .. System.Int32.MaxValue do
+            yield i
+    }
+let rec fibs x y =
+   seq {
+      yield x
+      yield! fibs y (x + y)
+   }
+let testYieldBang =
+    seq {
+        yield 10
+        yield! [1; 2; 3] // Yields a collection.
+    }
+// Generator.
+let testUnfold =
+    Seq.unfold (fun a -> if a > 10 then None else Some(10 * a, a + 1)) 1
+
+// Functions composition.
+let SizeOfFolder = 
+    let getFiles folder = 
+        Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories)
+    getFiles
+    >> Array.map (fun file -> (new FileInfo(file)).Length)
+    >> Array.sum
+
+// Matches literals.
+[<Literal>]
+let Bill = "Bill Gates" // Must begin with a capital!
+let greet name =
+   match name  with
+   | Bill -> "Hello rich Bill!"
+   | name -> sprintf "Hello poor %s" name
+
+// Matches tuples.
+let testXor x y =
+   match x, y with
+   | a, b when a <> b -> true
+   | _ -> false
diff --git a/FSharpRef/Main.fs b/FSharpRef/Main.fs
new file mode 100644 (file)
index 0000000..d070829
--- /dev/null
@@ -0,0 +1,28 @@
+module Main
+
+open System.Linq
+open Functional
+
+[<EntryPoint>]
+let main args = 
+    appendFile "test.txt" "Pouet"
+    printfn "Size of folder: %A" (SizeOfFolder ".")
+    print "youpi"
+    print (isEven (fac 4))
+    //
+    print steve.First
+    printfn "Young persons: "
+    (youngPersonNames (Queryable.AsQueryable [steve ; steves'twin ; { First = "Paul"; Last = "Atreides"; Age = 11 }])) |> Seq.iter (fun name -> printfn "   name: %s" name)
+    //
+    print <| Seq.take 10 allPositiveInts
+    print <| Seq.take 10 (fibs 1 1)
+    //
+    print testYieldBang
+    print testUnfold
+    //
+    print <| greet Bill
+    print <| greet "Paul"
+    //
+    print <| testXor 1 2
+    print <| testXor 3 3
+    0
diff --git a/FSharpRef/Object.fs b/FSharpRef/Object.fs
new file mode 100644 (file)
index 0000000..c89cd72
--- /dev/null
@@ -0,0 +1,2 @@
+module Object
+