F# and IronPython Sample Code to List Installed Fonts

In my quest to have a mind-expanding experience this year, I’m learning functional programming. I’ve started with F# because of its great interop with existing .NET code. I just began my learning this weekend and thought I’d post a *simple*, but real-world example to help anyone else who is learning about F#.

Purpose of sample: List all the fonts installed on my system.

I’ve provided two samples: one in F# and one in IronPython to help anyone making the transition.

F# Version

open System.Drawing.Text
let ifc = new System.Drawing.Text.InstalledFontCollection()
let family_names = ifc.Families |> Array.to_list |> List.map (fun f -> f.Name )
do List.iter (fun f -> Printf.printf “%s\n” f) family_names

IronPython Version

import clr
import System.Drawing

ifc = System.Drawing.Text.InstalledFontCollection()
fontnames = [font.Name for font in ifc.Families]
for fn in fontnames:
print “\”%s\”” % fn

Comments (0)

Skip to main content