Code samples for Functional Programming in C#
  • C# 99.7%
  • F# 0.3%
Find a file
2021-12-20 21:03:46 +01:00
Boc.Domain migrate Boc.Domain project.json to fsproj 2017-11-28 09:31:40 +08:00
Examples migrate Examples to netcoreapp2.0 2017-11-28 10:00:00 +08:00
Exercises added solutions for ch8 2021-07-09 20:12:47 +02:00
LaYumba.Functional bumped package version to 1.0.0 2021-12-20 21:03:46 +01:00
LaYumba.Functional.Data removed obsolete project.json files 2017-11-27 12:45:20 +01:00
LaYumba.Functional.Tests Func: Apply with up to 9 arguments 2019-08-26 21:02:38 +02:00
.gitignore removed old ignored file 2017-03-31 22:43:37 +02:00
cover.jpg updated README 2017-11-27 18:45:05 +01:00
LICENSE MEAP v8 2017-02-01 03:31:31 -08:00
NuGet.Config migrated VS2017.sln and .csproj to sdk1.0 2017-03-07 14:55:24 -08:00
README.md update README 2017-11-28 10:07:22 +08:00
setupRepl.csx MEAP v8 2017-02-01 03:31:31 -08:00
VS2017.sln migrated VS2017.sln and .csproj to sdk1.0 2017-03-07 14:55:24 -08:00

Functional Programming in C#

This repo contains the code samples, exercises and solutions for the book Functional Programming in C# published by Manning.

Functional Programming in C#

The code samples are organized in the following projects:

  • Examples: examples used throughout the book, by chapter
  • Exercises: placeholders for you to do the exercises, compile and run them; and compare to the provided solutions
  • LaYumba.Functional: a functional library that we develop throughout the book
  • LaYumba.Functional.Data: very simple functional data structures discussed in Chapter 9
  • LaYumba.Functional.Tests: also illustrative of topics explained in the book, and useful to better understand the constructs in the library
  • Boc.Domain: an example of using F# for domain objects

Note: you are welcome to reference LaYumba.Functional from your projects via NuGet, and submit PRs with improvements, but the main intent of this library is pedagogical. For a more fully-fledged functional library, consider LanguageExt

Working with the code samples

The code samples use .NET Standard 1.6 and .NET Core 2.0, and should compile and run on all major OS's via the dotnet CLI.

Prerequisites

To check this is available, type dotnet --version at the command prompt, and you should get 2.0.3 or greater

One-time setup

$ git clone git@github.com:la-yumba/functional-csharp-code.git
$ cd functional-csharp-code
$ dotnet restore

Running the tests

Use the dotnet test command, for example:

$ cd LaYumba.Functional.Tests
$ dotnet test

tests in the Exercises and Examples projects can be run in the same way.

Running examples

Many code samples in the book can be run, in case you'd like to debug or "see that it works". The pattern is:

$ cd Examples
$ dotnet run Greetings
Section Command
7.1 dotnet run Greetings

Setting up the REPL

Throughout the book, I encourage readers to try things out in the REPL.

  • If you use Visual Studio, you can start the REPL by going to View > Other Windows > C# Interactive (short tutorial here)
  • On Mono, use the csharp command
  • There are several other REPLs available, some even run in the browser

You'll often need to import LaYumba.Functional in the REPL. In C# Interactive, this can be done like so:

#r "functional-csharp-code\LaYumba.Functional\bin\Debug\netstandard1.6\LaYumba.Functional.dll"

The path above may not work for you, in which case use an absolute path to the dll, or type Directory.GetCurrentDirectory() into the REPL to see what to use as a base for a relative path.

Next, add these imports:

using LaYumba.Functional;
using static LaYumba.Functional.F;

You're now ready to experiment with functional code right in the REPL, for example:

> Func<int, int, int> plus = (a, b) => a + b;
> Some(plus).Apply(1).Apply(2)
[Some(3)]
> Some(plus).Apply(1).Apply(None)
[None]

Doing the exercises

  • edit the code in Exercises as needed

  • edit Exercises/Program.cs to start the class you want

  • run it with:

    $ cd Exercises
    $ dotnet run
    
  • run your tests:

    $ cd Exercises
    $ dotnet test