patterncsharpMinor
Functional Programming Library
Viewed 0 times
functionalprogramminglibrary
Problem
I have started putting together a functional programming library in C# which is somewhat inspired by scalaz and the book Functional Programming in Scala.
I am hoping to get some feedback on the Monad Transformers implementations and comments.
Please understand this is implemented in C# and .NET, which does not support typeclasses and Higher Kindedness.
Here is an example demo of what I have been able to create so far with what has been developed.
```
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
namespace Sharper.Tests
{
internal class Student
{
public int Id{ get; set; }
public string Name{ get; set; }
}
internal class Score
{
public int StudentId{ get; set; }
public string Name{ get; set; }
public Decimal Result{ get; set; }
}
internal class ScoreServer
{
public ScoreServer()
{
students = new Dictionary();
scores = new Dictionary>();
students.Add(1, new Student{ Id = 1, Name = "Blair" });
students.Add(2, new Student{ Id = 2, Name = "Esther" });
scores.Add(1, new List {
new Score{ StudentId = 1, Name = "Maths", Result = 76 },
new Score{ StudentId = 1, Name = "English", Result = 56 },
new Score{ StudentId = 1, Name = "Science", Result = 67 }
});
scores.Add(2, new List {
new Score{ StudentId = 2, Name = "Maths", Result = 87 },
new Score{ StudentId = 2, Name = "English", Result = 72 },
new Score{ StudentId = 2, Name = "Science", Result = 59 }
});
}
public IO GetStudentScoreFor(int studentId, string classname)
{
var studentT = GetStudentById(studentId).OptionT();
var studentNameT = studentT.Map(x => x.Name)
.OrElse(new Some(s
I am hoping to get some feedback on the Monad Transformers implementations and comments.
Please understand this is implemented in C# and .NET, which does not support typeclasses and Higher Kindedness.
Here is an example demo of what I have been able to create so far with what has been developed.
```
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
namespace Sharper.Tests
{
internal class Student
{
public int Id{ get; set; }
public string Name{ get; set; }
}
internal class Score
{
public int StudentId{ get; set; }
public string Name{ get; set; }
public Decimal Result{ get; set; }
}
internal class ScoreServer
{
public ScoreServer()
{
students = new Dictionary();
scores = new Dictionary>();
students.Add(1, new Student{ Id = 1, Name = "Blair" });
students.Add(2, new Student{ Id = 2, Name = "Esther" });
scores.Add(1, new List {
new Score{ StudentId = 1, Name = "Maths", Result = 76 },
new Score{ StudentId = 1, Name = "English", Result = 56 },
new Score{ StudentId = 1, Name = "Science", Result = 67 }
});
scores.Add(2, new List {
new Score{ StudentId = 2, Name = "Maths", Result = 87 },
new Score{ StudentId = 2, Name = "English", Result = 72 },
new Score{ StudentId = 2, Name = "Science", Result = 59 }
});
}
public IO GetStudentScoreFor(int studentId, string classname)
{
var studentT = GetStudentById(studentId).OptionT();
var studentNameT = studentT.Map(x => x.Name)
.OrElse(new Some(s
Solution
First, this
So, you have a dictionary where the key is already included in the ID if the value? Why not make that a plain
However, looking beyond that, it appears we have to keep a set of
You use the value
Dictionary caught my eye:students = new Dictionary();
students.Add(1, new Student{ Id = 1, Name = "Blair" });
students.Add(2, new Student{ Id = 2, Name = "Esther" });So, you have a dictionary where the key is already included in the ID if the value? Why not make that a plain
Dictionary? If the keys are guaranteed to not be duplicates anyway, you could set up a List for simplicity.However, looking beyond that, it appears we have to keep a set of
Scores for each student, and you are doing that by keeping two lists. I would combine these into one list with a class Student with an Id, Name, and a List of Scores. Then, you can keep a list of students and their scores all together. This, however, has the trouble of allowing duplicate Ids, so perhaps a Dictionary is the best option here, even if the Id is in both the key and value.Testing_ScoreServer1() isn't very descriptive. I have to read the test to figure out what it is actually testing. Later on when you make changes that make this test fail, how are you going to know what is actually failing without reading the code? I would give it a name so descriptive that I could tell what failed from the list of failed tests.new Score{ StudentId = 1, Name = "Maths", Result = 76 },
new Score{ StudentId = 1, Name = "English", Result = 56 },
new Score{ StudentId = 1, Name = "Science", Result = 67 }You use the value
76 in your tests, as well as the value "Blair". What if you decide you don't like the number 76 and you change it to 75? That will break your tests. You should consider setting this value in one place as a variable so you just need to change the value in one place if it changes. Perhaps you could have a List of students that you take data from and use for all your tests?Code Snippets
students = new Dictionary<int, Student>();
students.Add(1, new Student{ Id = 1, Name = "Blair" });
students.Add(2, new Student{ Id = 2, Name = "Esther" });new Score{ StudentId = 1, Name = "Maths", Result = 76 },
new Score{ StudentId = 1, Name = "English", Result = 56 },
new Score{ StudentId = 1, Name = "Science", Result = 67 }Context
StackExchange Code Review Q#94330, answer score: 7
Revisions (0)
No revisions yet.