Everything is Functions

December 22, 2022#Software Development
Article
Author image.

Kyle McMaster, Senior Consultant

This post is part of the F# Advent Calendar 2022 hosted by Sergey Tihon. The goal of this post is to provide a glimpse into the journey of two C# developers (myself and Sean G. Wright) who are learning F# together and to provide some insight into the motivations and resources that helped them along the way. We hope this post will be helpful to other developers who are interested in learning F#. All answers are opinions from the perspective of our individual experiences thus far in our F# learning journey. Please excuse our ignorance as we continue to learn!

What’s your background as a software developer?

Kyle

I’ve been a .NET developer for nearly a decade building mostly cloud-native solutions with extensive experience working in Azure. I’ve helped develop and maintain a wide range of legacy and greenfield projects from ETL pipelines in VB.NET, scalable microservices with Azure Function’s Durable Orchestrations, and quite a few modern ASP.NET applications in .NET 6. I’ve spent time working with TypeScript using frameworks like Angular and Vue and continue to dabble in those ecosystems occasionally. Nowadays I am a part of a great team at a small consulting company called NimblePros, where I am a proponent of clean architecture, domain-driven design, and test-driven development. My day-to-day programming is primarily done in C#, often while pairing programming whenever possible. I still consider myself to be a novice F# enthusiast with an understanding of the basics like syntax but lacking an understanding of more advanced language features.

Sean

I’ve been a professional software developer for almost 10 years, focusing almost exclusively on web development. I’ve spent most of that time working with .NET and C#, but I also spent several years mostly writing JavaScript and TypeScript (primarily with Angular.js and Angular). Currently, I work as Chief Solutions Architect for WiredViews, an agency headquartered in Ohio, USA, building digital marketing solutions and content-driven sites on a platform called Kentico Xperience. I’ve been awarded the Kentico MVP status for the past 3 years for my blogging, technical, and community contributions. I mostly work with C# and ASP.NET Core on the backend and TypeScript with Vue or Alpine.js on the frontend.

What interested you about F#?

Kyle

I was briefly exposed to functional programming while in college studying computer science but the paradigm didn’t catch my interest until I started building event-driven systems in .NET with patterns like asynchronous messaging and CQRS. Throughout the same period, C# began adding functional features to the language under the guidance of Mads Torgersen so I decided to read Functional Programming in C# by Enrico Buonanno. This opened my eyes to the possibility of functional programming in C# and I was intrigued to explore this topic further.

In early 2020, Sean and I began collaborating on a side project of mine called Payroll-Processor which became a playground (or smorgasbord as we eventually called it) for us to try out any patterns or frameworks we were interested in at the time. Sean embraced using LanguageExt to create railway-oriented APIEndpoints which I found to be really enjoyable to work with. There were times when C# felt difficult to work with and we felt like making the jump to F# but never committed to it due to us both exploring other patterns and frameworks at that time.

Then in 2021, F# began to appear more and more on my radar as a language I should try learning. The primary reason F# was of particular interest to me over other languages with functional features like Clojure or TypeScript is because of the .NET ecosystem. I was already familiar with .NET and wanted to continue working with it as opposed to learning a new framework and a new language at the same time. The F# snippets I had seen on Twitter and blogs also looked very clean and readable, which was a big plus for me. It seemed to have a lot of different patterns that would force a change in my mindset and I was hoping this would improve not just my F# skills, but also my C# understanding as well. As I am learning F# now, it feels like a natural progression for me as my journey with C# and .NET has matured and I should continue to explore F# as a complementary tool in my .NET toolkit.

Sean

As a novice software developer, using C# and learning object-oriented concepts, I had the impression that F# was for “math stuff” and must be similar to MATLAB or Scheme (which I used while in school). No one I knew used F# and most of my peers didn’t even know of or talk about functional programming.

About 5-6 years ago I was writing a lot of JavaScript, which was my true introduction to functional programming. JavaScript taught me concepts like closures and higher-order functions - I had used these concepts in C# in the past but I never knew their names and always felt uncomfortable with them. They did, however, feel natural in JavaScript and enabled me to express ideas much more simply than I could when writing traditional object-oriented C#.

I tried to bring my JavaScript functional skills back to C# and was amazed to see it was possible, but the fit could be really awkward if I wasn’t careful. After trying many ways to represent “missing data” or “failed operations” in C# I discovered the CSharpFunctionalExtensions library and the Maybe and Result monads. I incorporated this library into most of the projects I worked on but continued to see that “functional” C# only got me so far.

Over the past several years I started looking more at F#, watching DevOwl and Scott Wlaschin videos on YouTube, and trying to understand why F# might be a more ergonomic fit with the functional patterns I’d been exploring. I eventually realized its type system and functional-first (but still multi-paradigm) design had lots of things to teach me and decided to try to learn it in earnest.

What kinds of resources did you use as an F# beginner to help you learn the language?

Kyle

I have mostly been consuming books and watching a few PluralSight courses. I think my first dedicated F# resource that I watched was A Functional Architecture in F# by Mark Seemann on Pluralsight a few years ago. This course is a great demonstration of building a web application with real-time messaging in F#. Next, I was curious to read Domain Modeling Made Functional by Scott Wlaschin. As someone who regularly practices domain-driven design, this book was a great introduction to how F# could be used alongside these principles to create a rich, expressive, and maintainable domain model.

Currently, I am reading Get Programming With F# while patiently waiting on the MEAP version of F# In Action by Isaac Abraham. One thing I might have done differently looking back was to read Get Programming With F# before Domain Modeling Made Functional, as it would have given me a better understanding of the language and its syntax before diving into the more advanced concepts like higher-order functions, monads, and function composition. Domain Modeling Made Functional was a great resource and since F# is fairly readable most of the time, I was able to get a decent understanding of the concepts and examples in the book. However, I think it would have been easier to understand the concepts if I had a better understanding of the language and its syntax beforehand.

Sean

Initially, I focused on functional concepts, like the aforementioned monads and higher order functions, because I could learn these and use them in F#, C#, and TypeScript. I picked up a lot of these concepts from F# for Fun and Profit, one of the definitive sources on the web for F#.

But concepts only got me so far when my goal was to write real programs in F#. I felt I needed to learn the syntax and some of the development workflow (like F# interactive and how to debug in F#) since I relied on that kind of stuff heavily with C#. I purchased the first edition of Get Programming with F# by Isaac Abraham and started coding along with it while on flights to conferences. It’s a great book, starts at the ground level, and ramps up rather quickly with exercises and problem-solving.

What I ended up realizing though is that there wasn’t a single resource that helped me begin to learn F# and my focus on “this one tool will teach me everything” wasn’t tangible. I kept getting stuck and feeling like I wasn’t learning in an environment that I connected with - web development.

The key that really helped me feel more confident and learn faster was starting the FShopOnWeb project with Kyle. This project had an existing reference implementation in C# (so we could verify functionality) and was a web application, which felt more familiar to me than console apps.

I started reading F# source code on GitHub, reading the Microsoft F# docs, and working in a “solve the next problem” development cycle where I’d figure out how to do one thing to solve my current blocker, and then solve the next thing and the next. Often the problems were completely unrelated in syntax or patterns and this helped me explore F# more naturally.

What did you not use that you typically do?

Kyle

I’ve watched less youtube content and followed fewer blogs than I typically have for C#. Part of that is my own growth and accumulation of those resources over time, but I’ve noticed there isn’t a go-to personality like Nick Chapsas or CSharpFritz in the F# community. Nick’s content is great because it appeals to junior and senior developers alike. Most of the content I’ve consumed on F# is primarily focused on established developers learning a new language. I immediately noticed a gap in new programmer resources for F# compared to the immense amount of resources available to new programmers learning C#.

Sean

In the past, I used a lot of PluralSight for both quickly learning a new programming topic and diving into a niche, but I didn’t find much F# content there.

I’m also a regular reader of a lot of bloggers who write about C#/.NET and JavaScript/TypeScript and I find that most of my continuing education in these languages is a healthy dose of blogs, YouTube videos, source code and docs. As I mentioned, I did learn some F# from YouTube, GitHub, and Microsoft’s docs, but I didn’t learn much from blogs. I think this is because I don’t know who the good F# bloggers are and I also think that blog content tends to not be oriented for those new to a concept since blogs need to make some assumptions to keep from getting too long.

What’s most difficult about F# coming from C#?

Kyle

I’ve hit the usual hurdles one would expect when learning new tooling and a new language. The C# ecosystem benefits from a top-tier development experience inside Visual Studio. While I’ve been using Ionide inside Visual Studio Code for F# development and it’s worked out well, the two don’t have the same level of maturity and support so learning a new development workflow has been difficult. From a language perspective, F# is pretty elegant in terms of readability and it isn’t until the syntax gets verbose that I found it to be difficult to read. Although, writing it is a whole different matter! I’ve found the large number of operators available in F# (such as |> and >> vs <| and <<) to be a bit tricky to reason about and I’m still working on internalizing the composability of functions in what would be considered idiomatic F# style.

Another thing I’ve struggled with is how to structure my application and group functions in F# which is something that I rarely even think about when using C#. In C#, every method belongs to a class and if you practice SOLID or clean architecture then those decisions usually feel trivial. While in F#, I still don’t have a good feel for how to structure a large F# web application yet.

Sean

I think the most difficult thing was being very experienced with .NET and very inexperienced with F#. This created an uncanny valley in many places where I knew how to do something in C# with the .NET Base Class Library (BCL), and was able to do that thing in F# in a similar way, but didn’t know if that was the F# way to do things, or if I was going to run into issues I didn’t even know about.

One standout example was when trying to get C# and F# collection types to play well together or async in F# working with Task in C#.

C# developers don’t have this problem since calling F# libraries from C# is very rare and the BCL has always been more aligned with C# (having been written in C#).

What’s easiest about F# coming from C#?

Kyle

F# syntax has great readability, can be very expressive, and lacks a lot of boilerplate like classes, brackets, and parenthesis which makes F# code easier to read and write. The ability to define custom types so that you can trust the compiler more and spend less time debugging feels very efficient from a workflow perspective. Nested functions are another language feature I’ve found rewarding when using F#. While C# has comparable features with local functions or private methods, the syntax makes a world of a difference in readability in my opinion.

Sean

While the uncanny valley of .NET made things confusing at times, it also allowed me to skip learning a lot of things I’d need to learn with a completely different language like Go or Rust. F# project files use the same MSBuild XML as C# projects, they consume libraries with project references and NuGet packages (I didn’t try to adopt Paket because that seemed like added complexity for little gain - am I missing something?), and the BCL is the same in both languages.

What is FShopOnWeb?

Kyle & Sean

FShopOnWeb is meant to be an F# implementation of the eShopOnWeb ASP.NET Core reference application. Previously, there was no community extension to F# of this project and it felt like a great place to apply the knowledge we were gaining of F#. One of its primary goals is to fill the lack of robust sample projects in the F# ecosystem and hopefully aid others in taking their F# projects from “Hello World” level applications to a production-ready state. Since it is meant to be comparable to eShopOnWeb, this is great for C# developers looking to learn F# because they can directly compare the two projects and run them side by side. We are always looking for feedback from the F# community and would be happy to accept submissions from other contributors.

Name one Wow moment while working with F#

Kyle

Early on in the development of FShopOnWeb, we were discussing how to structure our code. When trying to reason about grouping functions, we were trying to reference how we typically organize code in C# but it became problematic because C# code is often organized by the purpose of the class. An example of this might be organizing entities by their aggregate root in a C# application’s domain layer. In F#, that style of organization felt out of place because “Everything is functions!”. There isn’t a direct need to apply the same principles to F# projects. I found the responses to this tweet to be really illuminating on how few issues the community has had overall with structuring projects. With FShopOnWeb, I’ve felt the organization of the project has come much more organically rather than following explicit conventions even if the outcomes have been similar.

Sean

I had heard the saying from the functional programming community that while object-oriented languages use many different language constructs and design patterns to solve problems, functional languages try to focus on functions as the primary tool. That definitely played out in my experience with F# and was a very noticeable shift from the comparably large toolset that C# offers (or requires).

Being able to use one tool to create so many features of an application was really amazing and exciting.

The nuanced outcome of this reliance on functions is that I needed to become much more skilled in how I authored and composed them. This skill doesn’t come for free - it needs to be developed over time and is very important to using F# effectively, and it is something I’m still working on and struggling with (in a good way).

What do you think of the F# developer community?

Kyle

I agree with Sean about the F# community being much smaller although it’s been rewarding for me to see the modest amount of engagement I’ve experienced on Twitter and my experiences have been much more genuine than some of my discussions with other languages and frameworks. It feels like other languages and frameworks have a ton of engagement but there can be a lot of hustle culture and toxicity. I rarely experience the type of self-promotion or framework/library bashing that I see on C# and Javascript Twitter. The ability to participate directly in discussions with Don Syme or Isaac Abraham has been very encouraging for me to continue my F# journey. Another great example is getting engagement with Pim Brouwers as Sean and I were tweeting about using Falco Framework for FShopOnWeb. It’s been my experience that these interactions are just not as likely to happen in other languages and communities.

Sean

The F# developer community is very interesting. It feels small (especially when compared to C# and JavaScript), but it makes up for that in its enthusiasm and positive attitude. While all communities have their hangups, folks using F# are consistently supportive and it seems like they try hard to make newcomers feel welcome.

What do you think of the F# Open-Source Software (OSS) options?

Kyle

I’m not sure I have a strong opinion yet on many of the open-source software options available in F# to give preference to certain projects over others. I’ve really enjoyed using Falco since it is based on ASP.NET Core for creating FShopOnWeb. I agree with Sean that it feels like the number of contributors to F# OSS is much smaller and the projects remain relatively immature compared to many of the mainstream C# projects.

Sean

I think OSS in F# is a reflection of the community - small and enthusiastic. I saw many projects that were creative explorations into problems, which might have “already been solved”, in new and interesting ways. It reminds me of the “Let one thousand flowers bloom” mindset (without the negative connotations) that the JavaScript ecosystem has.

At the same time, some of the projects I encountered seemed either abandoned, inactive, or unfinished. I think I’m biased having worked with C# for so long that I look for the “all-in-one” solution that has the standard features I’ll need for production scenarios, but I found many libraries had key features missing and I either couldn’t get some basic functionality to work (and the incomplete docs didn’t help) or I had to write a handful of functions on top of the library to “finish it up”. Maybe I don’t know what the key libraries are, I shouldn’t bring my C# mindset to F#, or I would have the same issues in C# but I’m much more comfortable there and don’t notice them as much.

Where do you think F# could be useful in the types of projects you work on?

Kyle

I would enjoy applying what I learned in Domain Modeling Made Functional for documenting business requirements through the process of event storming utilizing F#. I am intrigued by how that book described that easily readable F# code can be useful for domain experts and developers. The idea of creating business specifications through DSLs in F# feels incredibly powerful to me.

Although it may be treasonous to say on the F# Advent Calendar, C# will likely remain my primary language for most applications that I work on for the foreseeable future. That being said, there is immense value both personally and professionally to expanding my toolkit of .NET skills and that’s something I’ve really enjoyed having the support of from NimblePros, friends like Sean, and the F# community.

Sean

I’m not exactly sure where I’d use F# in the projects I work on for my day job. My team has been writing C# for years, our clients projects are all C# for backend functionality, and there aren’t any plans to change this.

Today, I view F# as a language that expands my brain and I think that’s an important exercise for me as a software developer. Whether or not I introduce it to teams, use it in a production environment, or switch over from C# to F# full time is less important to me at the moment.

Writing in F# helps me think about problems in different ways, with new perspectives, and avoid the pitfalls of comfortable assumptions that come with working in the same language (or two) year after year.

For me, the question of using a programming language “for work” is important, but even if I don’t, it doesn’t lessen the value of learning and exploring F#.

That said, learning F# could very well help me feel much more comfortable adopting an F# library that solves a key problem in the C# applications I work on, even if my team doesn’t write our code in F#.

What is your experience working on F# Advent of Code?

Kyle

Advent of Code has been a great training exercise for me to reinforce my F# knowledge and learn new tactics along the way. I’ve tried to focus on a particular language feature each day to force myself to try something I haven’t used before or am inexperienced with. When I compare my solutions to others on Sergey Tihon’s private leaderboard I feel like my submissions are C# solutions written in F# so I have a ways to go before I am truly writing F# in all its greatness. Overall, it’s been really fun and I hope to participate more next year!

Sean

I’ve never done Advent of Code before, but working with Kyle on one of the month’s problems was really interesting.

I felt as though the solution would write itself if I was using C# and in comparison F# felt slow and kind of painful, but that says more about my skills than F# as a language. When I looked online and saw solutions to the challenge in F# from folks, who were well-versed in it, I realized how awesome F# can be in knowledgeable hands - the code was extremely readable and terse, which I find can be very difficult to accomplish in other languages.

What kind of project are you excited to try next with F#?

Kyle

As I mentioned before, I would really enjoy applying F# to domain-driven design and explore how patterns like aggregates and commands translate to the language. I can also see value in using F# in the event storming process while using F# as a sort of specification and ultimately implementation of that results of gathering business requirements.

For the foreseeable future, FShopOnWeb will likely be my main source for developing my F# skills and continuing to improve on my knowledge of functional principles.

Sean

I’d like to continue to explore F# in FShopOnWeb. I feel the practical nature of building a full stack web application really helps me stay motivated in working with F# and also helps me, like I mentioned earlier, organically learn the language which I’ve discovered is a good fit for me.

It’s big enough to feel real, but small enough to not get lost in. Simple enough to not require advanced techniques and also has some areas that don’t translate from C# to F# easily, which means learning more of “the F# way”.

What feature of the F# language do you really want to learn more about?

Kyle

Overall, I’d like to continue to improve my overall competency in F# as a whole. I think there are functional principles like higher-order functions that I still need to develop a deeper understanding of and improve my ability to recall how those are implemented in F#. Some specifics about F# that stand out to me are operators for composing functions and becoming more familiar with F#’s built-in List operations (I see you List.Fold 😜).

Sean

I’ve dipped my toes into computation expressions with Falco, which uses them in several places, and async or Task-based F# code. So, I’d like to learn more about them as they seem to be pretty versatile and are especially useful for cross-cutting concerns (like logging).

I’m a huge fan of pattern matching in F# (and C#, more recently). It’s an amazing language feature that I notice when it’s missing in other languages. I think learning active patterns would help me grow my skills here. I’ve watched some videos about them but nothing has clicked for me yet.

Finally, I think I’d like to become more intuitive with function composition. While I’ve become accustomed to the “|>” operator and love writing function pipelines with it, I’m not at all familiar with the other function operators. I want to understand these tools better so I’m well-equipped to use the right solution for a given problem.

Conclusion

We want to thank Sergey Tihon for giving us the opportunity to participate in the F# advent blog event. Diving into learning and working with F# over the past several months has been challenging, exciting, and rewarding. We’ve enjoyed learning about the F# OSS ecosystem and engaging with the F# community on Twitter.

We hope this post gives new developers (especially those well versed in C#) some perspective on what F#, the language and the people behind it, can offer. We also hope the F# community finds our perspectives - a couple of C# developers trying to bridge the language gap and shed some ignorance - enlightening.

Please provide us feedback if you see things we missed, or have some thoughts or tips that other developers might find valuable. We hope this post leads to more opportunities for discussion, exploration, and self-education.

Reach out to us!


Copyright © 2024 NimblePros - All Rights Reserved