Sunday, March 7, 2010

Here's Something Surprising

One of the most fun things about F# is the surprises it holds. For example, I learned that one can "banana clip" a function on input to another function. I'd like to say it was unexpected, but I learned about it by coming up with the idea and then trying it to see whether it would work. Still I was surprised that it did work. I don't really know of any good application for which this is "just right," but I'll keep looking.

Here's a minimal example using options, but it also works with parameterized patterns, etc.

let f (|A|_|) =
match None with
| A -> printfn "Some()"
| _ -> printfn "None"

f (fun _ -> None)
f (fun _ -> Some())

(Standard disclaimers, no warranties, etc.)

No comments: