Giter Club home page Giter Club logo

functional-csharp-code-2's Introduction

Functional Programming in C#, 2nd Edition

This repo contains the code samples, exercises and solutions for the book Functional Programming in C#, 2nd Edition published by Manning. The 2nd Edition builds on the success of the 1st, but has been updated to reflect new features in C# 10 and .NET 6.

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 12
  • LaYumba.Functional.Tests: also illustrative of topics explained in the book, and useful to better understand the constructs in the library

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 6, 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 6.0 or greater

One-time setup

$ git clone [email protected]:la-yumba/functional-csharp-code-2.git
$ cd functional-csharp-code-2
$ dotnet build

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 or csi 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-2\LaYumba.Functional\bin\Debug\net6.0\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:

> var plus = (int a, int 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
    

functional-csharp-code-2's People

Contributors

la-yumba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

functional-csharp-code-2's Issues

NuGet.Config does not contain a source for FeatherHttp

When attempting dotnet restore or dotnet build, the system responds with

error NU1101: Unable to find package FeatherHttp. No packages exist with this id in source(s): fsCheck-appVeyor, nuget.org

Adding a source for FeatherHttp would solve this.

2nd edition?

Does this repo mean that 2nd edition of your amazing book is coming...? ;)

NuGet.config breaks `dotnet build`

Hi, TDE for the book here - pleased to meet you!

The certificate for dotnet.myget.org has expired and from what I read online, they're not planning to renew it at all. This line therefore causes issues with dotnet build - but at least in my environment, when I removed it entirely it "just worked" ("it" being dotnet restore && dotnet build), probably because dotnet-core can already be found in official NuGet repos?

<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />

Why is Try delegate returning Exceptional?

Hi, I don’t get why Try delegate is returning Exceptional if it is Run()ks purpose to wrap the result of the lambda call. Invoking „raw” Try delegate can still throw exception but return type of Exceptinal suggests that ir will not happen or am I completly off here?

.NET 5.0 re-targeting breaks build

Hi. It seems that provided code does not build with net5.0 target. I have for example a couple of compiling errors in EnumerableExt.cs (inside LaYumba.Functional lib) such as:

public static (IEnumerable<T> Passed, IEnumerable<T> Failed) Partition<T>
      (
         this IEnumerable<T> source,
         Func<T, bool> predicate
      )
      {
         var grouped = source.GroupBy(predicate);
         return
         (
            // Argument 2: cannot convert from 'System.Collections.Generic.IEnumerable<T>' to 'System.Func<System.Linq.IGrouping<bool, T>, bool>'
            Passed: grouped.Where(g => g.Key).FirstOrDefault(Enumerable.Empty<T>()),
            Failed: grouped.Where(g => !g.Key).FirstOrDefault(Enumerable.Empty<T>())
         );
      }

Maybe FirstOrDefault in .net6.0 provides a new overload taking an Enumerable?

Will the 2nd edition reference Language-Ext?

Just discovered that you're writing a 2nd edition! Excellent news, my copy is getting very dog-eared from having been read many times!

Anyway, without wishing to be rude, may I make a request (that may be too late). The only drawback I felt the book had was that you used your own library, instead of using Language-Ext. I understand the educational reasons for developing a library as you went along, but at the end of the day, you yourself recommend Language-Ext as "a more fully-fledged functional library."

Thing is, Language-Ext suffers badly from a lack of good documentation. Your book could easily be that documentation, making it even more useful than it already is. Even if you just added a small paragraph at the end of each section showing how the concepts and code you just described would be done in Language-Ext, it would give a kick-start to those of us who are keen to put your ideas into practice, but are struggling to translate them.

This is NOT meant in any way to put down La Yumba, it's meant as a constructive comment to give the book a whole new dimension.

Thanks for listening, and thanks for writing what is (in my opinion anyway) probably the most important C# book ever written. Really, I mean that, no flattery here!

Contact regarding EventStoreDB

Hi there, My name is Steph Gill, I'm Community Engager from Event Store. Several of the team at Event Store have read your book and really enjoyed it, especially Alexey Zimarev and Yves Lorphelin. I'd love to talk to you a bit more about it! Can you please email me at [email protected] so we can talk further?

Thanks and have a good day!

Steph Gill

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.