And now I am back to F# again 🙂
I started learning F# by plural sight videos.
Two of which I found really helpful , which at least made me get enthusiastic about opening Visual studio and doing some code samples were
F# Jumpstart and F# Functional Data Structures by Kit Eason
rest all seemed too high level for me at that moment , mainly because I am just a beginner.
Then I bought a book F# for C# developers hoping that the good old way of reading and understanding might help. I am in no way expert to judge this book, but for me the content was very dull and so it is in now a Book of “Dust” 🙂
and gradually I jumped into other work related things and couldn’t move forward with the idea.
But recently I had a brief conversation with a person who had experience in F# and while talking he mentioned that F# koans in git hub as a nice way to learn F#.
So I went to github , searched and downloaded the project and fired up my VS 2015 and it straight away showed me the first “FILL ME IN” puzzle.
I now have completed till AboutTheStockExample and to me this seemed like a nice way of learning F#.
I will tell you why I liked the idea.
When you are watching a video or reading a book, you tend to feel like, oh I already knew that, lets skip the part.
But in learning by debugging, now we are faced with a challenge; and we need to get out of the problem. For a coder, trying to solve a problem is the most enjoyable thing (IMO); where he is unraveling all the mysteries behind the badly behaving piece and trying to put it back properly.
So here goes my try on stock solution:- btw, if some one is reading this and can suggest a better solution , it will be really helpful.
[<Koan(Sort = 15)>]
module ``about the stock example`` =
let stockData =
[ "Date,Open,High,Low,Close,Volume,Adj Close";
let YouGotTheAnswerCorrect() =
let getMaxVarianceOfStock =
|> Seq.map(fun t -> t.Split(','))
|> Seq.map(fun t -> t.,abs(System.Double.Parse(t.)- System.Double.Parse(t.)))
|> Seq.maxBy returnSecondTupleValue
let result, _ =
AssertEquality "2012-03-13" result
I will explain getMaxVarianceOfStock and how I struggled to get this one correct.
mostly confused with brackets and how lambda functions needs to be constructed, I put the logic for splitting returning a tuple into the first lambda function and then VS started to complain about missing brackets and expected IN but found !!! what was that.
Then I understood that I cannot attack this in one go and have to find out how Seq.map and lambdas work, so stack-overflow to the rescue; got the sufficient hints to construct the first Seq.map, and then things started to fall in place.
So what this function currently does is it splits Seq String to Seq String then the result is fed to my second sequence map which converts the inner array to a tuple format and now my resultant sequence looks like Seq (String,double) ;yes a sequence of tuples.
Now what I do is, I feed the result to a Seq.maxBy which accepts a function, I went this route because in the final result I need the date string , but for computing max I just need the second tuple value;so
returnSecondTupleValue just returns the absolute values we calculated and then applies max function on to it and returns back the tuple which had the maximum value.
Voila, test passed. Oh by the way , I used stockData.Tail because I don’t want the header to mess up my conversion logic in constructing tuple.
Wow! I feel like, I have used a lot of LINQ projection (keeping C# world in mind) statements invisibly. But really tidy and readable; impressed with F#.