Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of Environment Variables to Dictionary with Values - Idiomatic F#

What is the idiomatic way to take a F# list of string like

let myList = ["BUILD_NUMBER"; "GIT_COMMIT"; "GIT_BRANCH"]

and convert that to a Dictionary where the words above are the keys and the corresponding Environment Variable is the value.

Should all that be done in a one-liner or piped expression? I am missing the FP background to know how to go from list to Dictionary. What is the most idiomatic way to do this in F#?

like image 646
BuddyJoe Avatar asked Dec 09 '22 02:12

BuddyJoe


1 Answers

Environment.GetEnvironmentVariable and the dict function should do the work :

let env =
  myList
  |> Seq.map (fun var -> var, Environment.GetEnvironmentVariable var)
  |> dict
like image 57
Sehnsucht Avatar answered Jan 06 '23 14:01

Sehnsucht