• + 0 comments

    Hi Guys! what do you think about this approach? focused on referencial transparency and immutability. I really wanted to avoid the so popular imperative way.

    C#

    public readonly record struct BreakingRecords(int maxScore, int minScore, int most, int least)
    {
        private BreakingRecords Init(int first)
            => new(first, first, 0, 0);
            
        private BreakingRecords Apply(int score)
            => 
                score > this.maxScore ? this with { most = most + 1, maxScore = score } : 
                score < this.minScore ? this with { least = least + 1, minScore = score } :
                this;    
        
        public string GetRecords(IReadOnlyList<int> scores)
        {                                                               
            var state = Init(scores[0]);
            var finalState = scores.Skip(1).Aggregate(state, (st, t) => st.Apply(t));
            return $"{finalState.most} {finalState.least}";
        }
    }