3 people like it.

Elegant Sum of digits

Sums a sequence of decimal digits by repeatedly adding pairs of digits resulting in a single digit result.

1: 
2: 
3: 
4: 
5: 
6: 
// The input sequence is expected to contain only integers between 0 - 9.

let condense digits = 
    digits |> Seq.reduce (fun x y -> [|0;1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9|].[x + y])

// e.g.  condense [1;2;3;4;5;6;7;8;9;]  gives a result 9
val condense : digits:seq<int> -> int

Full name: Script.condense
val digits : seq<int>
module Seq

from Microsoft.FSharp.Collections
val reduce : reduction:('T -> 'T -> 'T) -> source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.reduce
val x : int
val y : int
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/7R5
Posted:8 years ago
Author:Hugh Gleaves
Tags: challenge