\$\begingroup\$
\$\endgroup\$
I’m learning F# and trying to find a more ‘functional’ way to code a simple program that retrieves the price of BTC and calculates the EUR value of an amount of Bitcoin.
open System
open System.Net
open Newtonsoft.Json.Linq
let myBTC = 0.1234567
let client = new WebClient()
client.UseDefaultCredentials = true
let priceInfo = client.DownloadString("https://blockchain.info/ticker")
let jPrice = JObject.Parse priceInfo
let eurPrice = float (jPrice.["EUR"].["buy"] :?> JValue)
let calcWorth = myBTC * eurPrice
printfn "%s" calcWorth.ToString("N")
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Mar 3, 2019 at 18:11
1 Answer 1
\$\begingroup\$
\$\endgroup\$
3
My take is that there is no harm in adding a few named functions. Also it is probably better to ignore the result of setting a standard .net property.
open System
open System.Net
open Newtonsoft.Json.Linq
let downloadPriceInfo () =
let client = new WebClient()
client.DownloadString("https://blockchain.info/ticker")
let getPriceOfEuro (price: JObject) = float (price.["EUR"].["buy"] :?> JValue)
let euroToBtc euro = 0.1234567 * euro
let btcOfEuro =
downloadPriceInfo ()
|> JObject.Parse
|> getPriceOfEuro
|> euroToBtc
printfn "%s" (btcOfEuro.ToString("N"))
answered Mar 3, 2019 at 22:54
-
1\$\begingroup\$ Some suggestions, which may or may not be useful...
|> parseJObject
is equivalent to|> JObject.Parse
.jPrice
might better be namedprice
since the type definition is fully specified here.euroPrice
might be better namedeuro
since the function isEuroToBtc
. I'm pretty sureclient.UseDefaultCredentials = true
doesn't actually do anything. \$\endgroup\$VoronoiPotato– VoronoiPotato2019年03月13日 14:27:46 +00:00Commented Mar 13, 2019 at 14:27 -
\$\begingroup\$ @VoronoiPotato - You make some good points. \$\endgroup\$ChaosPandion– ChaosPandion2019年03月13日 16:29:14 +00:00Commented Mar 13, 2019 at 16:29
-
\$\begingroup\$ always happy to help :) \$\endgroup\$VoronoiPotato– VoronoiPotato2019年03月15日 15:31:07 +00:00Commented Mar 15, 2019 at 15:31
Explore related questions
See similar questions with these tags.
lang-ml