F# has a static type system but looks as clean as Python imho.
For those not familiar with F# it is a .NET language that is part of the ML family of languages, closest to OCAML. It does lack some of the more advanced OCAML features due to the limitation of the host platform, the key one being higher kinded types. A great language feature but normally the point my head starts to spin with a language like Haskell.
The type inference of the language works well, there are very few times you need to help the compiler with type notations, hence the reason it looks so clean.
I have been playing around with the type provider system and thought I would share a small snippet of some code that:
- Connects to SteemSQL provided by
- Builds an entity model for SteemSQL
- Fires a simple query that gets all my articles.
- Reads the results
I will dive directly into the code then explain it, although if you have a solid dev background it will be obvious:
open System
open System.Linq
open FSharp.Data.TypeProviders
type private connection = SqlEntityConnection<"Data Source=sql.steemsql.com;user id=steemit;password=steemit;MultipleActiveResultSets=true",Pluralize = true>
[<EntryPoint>]
let main argv =
let context = connection.GetDataContext()
let myPosts =
query {
for row in context.Comments do
where (row.root_title.Length <> 0 && row.title.Length <> 0 && row.author = "woz.software")
groupBy (row.author + " " + row.root_title) into articles
select (articles.Key, articles.Sum(fun x -> x.net_votes), articles.Min(fun x -> x.created))
} |> Seq.toArray
0 // return an integer exit code
Here is a quick blow by blow walk through of the code:
- The type "connection" is the connection to the DB, it also builds the entity framework model.
- "context" is the current data context, manages connection lifetime etc
- "query" is the essentially .NET LINQ query syntax. This builds the SQL
- "Seq.toArray" executes the query and stores the results in an array
There are also type providers that support SQL queries but I thought I should demo the query DSL.
Finally a screen grab of debug with the results in view. The code is slightly difference as I cleaned it up for this post, it was a scratch project.
I have not pushed this much further at the moment, for some reason my install of VS2017 on my main dev box has a messed up F# install, so this was done on my Surface with VS 2015.
Enjoy
Woz