How to query SteemSQL in F# (A functional language from the ML family)

in programming •  7 years ago 

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 @arcange
  • 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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thanks. I didn't know about this. I saw the post on the AskSteem API and got a bazillion ideas on how to make use of it. This looks useful too.

I am glad to see I am not the only one having trouble with VS2017. F# is so great, though, I have been using mostly VS Code with the Ionide plugins lately, and I haven't skipped a beat.

I am excited to see a fellow F# programmer on here. Excuse me while I go read everything you have posted. Cheers.

The only issue I have had to date with F# and VS2017 appears to be the Data type providers and not resolving .NET run time that is actually installed. I might look at VS Code as a language like F# does not need the full force of VS.

We are a .NET house at work so I would love to get some F# into the code base but I can see the argument, training up the other devs to be able to work in it.

Hope you enjoy my F# articles.

Programmers is my favorite @woz.sofware

Is it an app (dev c ++?)

SteemSQL is a database representation of the steem blockchain provided by the witness @arcange

This is just a small scratch windows console program written in F# to show how to connect using that language.

Not an app in the sense of phone apps, and no C++ anywhere near this. This is a functional programming language so very different in style to all OO languages.

In F#

let x = 10 // bind x to 10
x = x + 1 // compile error, x = 10 and cant be changed ever