DEV Community

Discussion on: Write a function that shows off something unique or interesting about the language you're using

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

There are lots of things I like about F#. Probably one that is unique though is its computation expressions. Here is a list example.

// a list normally is defined like this: [ 1; 2; 3 ]
let validate course =
    [
        // but the compiler knows this is a CE
        // because we put logic in it

        if isBlankish course.Name then
            yield "Course Name blank"

        match course.Exam with
            | None -> () // do nothing
            | Some exam ->
                yield! Exam.validate exam
                // the ! is for other lists, flattens
    ]

...

// errors : string list
let errors = validate course

This provides a list of errors. And it is composable with other validations. It could be called in the same way that this function calls Exam.validate.