S02E20 1h 04mSeason 2

Episode Transcription
Hello everyone and welcome to THE .NET Core podcast - the only podcast which is devoted to:
- .NET Core
- ASP.NET Core
- EF Core
- SignalR
and not forgetting The .NET Core community, itself.
I am your host, Jamie “GaProgMan” Taylor, and this is Episode 52: Functional C# With Simon Painter. In this episode I interviewed Simon about the functional programming paradigm, a little about the history of functional programming, whether C# is becoming more of a functional language, and what a monad is. You may know of Simon from his blog, the talks that he gives, or his twitter account: @madsimonj.
So let’s sit back, open up a terminal, type in dotnet new podcast and let the show begin.
The following is a machine transcription, as such there may be subtle errors. If you would like to help to fix this transcription, please see this GitHub repository
Jamie
Simon
(both laugh)
Jamie
Simon
I started off working for little local companies here in Telford and worked for folks like Cap Gemini, outsourcing work for the HMRC done some work for places like The Works. You know, the bookshops that they’re on practically every town centre hereabouts and ah, just finished a stint with Europe Scientific overboard Rampton. But these days, I’m one of those contractors that your mum always warned you about when you were little.
(both laugh)
Jamie
So my question to you before we even start is can you describe functional programming for people who are maybe from an OOP background - an object oriented background - or a procedural background, or something? Is there something there must be something that sets it apart right? Otherwise, it wouldn’t be a named thing
Simon
So a paradigm is, if you want to metaphor, that the one I usually use is, like, imagine a musical instrument while the instrument is your programming language and your paradigm is the style of music you play and whether you’re playing, pop for rock or whatever. You know that those are paradigms. They’ve got certain rules, certain restrictions, some things you must do some things you mustn’t. That’s what set paradigms apart there, sort of defined by what you can’t do in a way.
So functional. There’s not exactly a one single thing it is. It’s more a cluster of properties,
So it always favours statements over functions that is things like
if and where, for things like that. Those are statements. Those tend not to exist in a functional code. There are other ways of do accomplishing that exact thing. So in a way, if you just remove those from C# and find other ways you’re most of the way there.It features variables which are immutable, rather immutable. That means once you’ve set the value of a variable, you can’t change it no matter what.
Now C# can actually do that as such, but the way forward is kind of pretend it can, because when it comes down to it, there’s always going to be a fun little area where you’re not able, to make everything immutable, however hard you try, but it also makes you serve what’s called higher order functions. That another property ah high order function is kind of a funky sounding term for something that’s not terribly complicated. It’s just functions passed around as if they were variables. So if you return a function out of another functional past, one in is a parameter or store. One locally is a variable, so
funcs func<T>. That’s that’s higher order functions that all it is.It makes use of recursions, which is something that we probably all been you aware of since we started out in this business.
It’s stateless, meaning there is no state stored in a variable somewhere that you refer to. There shouldn’t really be any concept of that. They’re kind of other ways of tracking state.
It uses some legal pattern recognition, which it’s a little tricky to describe. But basically switch statements with knobs on is like the nearest taken get. It’s like this super advanced version of switches where you could do an awful lot more than just a simple check against value.
And the other important concept is pure functions. It’s referential transparency is the technical term for this.
The whole idea is that given the same parameters, a function will always return the same value no matter what, no matter the life of the universe, no matter the state of the system. No matter if you always give these same values and you always get this same value back. It’s one of the very powerful properties of functional in that it makes it extremely testable. And that is one of the huge draws these days is functional compared to OO(P) is extremely easy to test and very robust. As a result,
I’d also argue it’s not terribly difficult. A lot of people sort of think it is, and I think a lot of it is just lack of exposure. It’s a little bit of a mental leap to get around to it, but once you’re over that, it’s actually not that difficult at all.
I’ve chatted to folks coming at the university when I’ve done talks on when I describe a lot of stuff that I do to make C# functional, they say, Well, this is what the teachers at the university now, so it seems like functional is very much the coming paradigm. It’s becoming more and more popular as years go by. I notice on the events list for NDC Oslo. Whenever I look at the schedule, there’s always more functionally there and not less so. It’s definitely here to stay, I think, especially when you consider that it’s something like over 100 years old, which it is.
Jamie
Simon
Jamie
Simon
I think they’ve sort of salvaged every tiny bit of his name and used it somewhere.
Jamie
Simon
In fact Alonzo Church came along when functional programming had kind of already been around for a long time. It wasn’t called functional programming. In those days it had a different name. I can’t remember something like composable functions or they were a cluster of schools of maths that we’re focused around the same area. But they are very, very, very hard to read if you’re not a mathematician, and I am not a mathematician, I’m an engineer, so.
One of the other things I think that makes some people struggle a bit with functional is if you go looking up like the proper definitions of how this stuff works on what it is. You’re kind of see the maths driven stuff, and it’s all like
f of g = x [[[....And honestly, I can’t make any sense of that at all as I’m an engineer. So what I want to know is, what does it do? What’s it for? How do I use it? And I think once you’ve let it go and actually doing it, it’s not all that hard.
Jamie
Simon
So if you go use Linq and you know I would, because Linq is, well, cool. But Linq is functional. It really is. It’s Ah, it’s immutable. So when you do a select, it’s not modifying the original array that you came from. It’s actually creating a new one based on the old one. That’s a functional concept.
And what you’re doing is you’re passing in a function as a parameter and getting back an interface. That’s again that’s higher order functions. It’s another functional concept. So things like
select things like aggregate or those handy little link helper functions. That’s all functional stuff. So all we’re doing is kind of trying to take C# and put the whole thing into a link. In a way, you’re trying to make everything and see shot look of it, Linqy. And if you do that, you most of the way there.Jamie
If I do, I’m gonna use the word lambda. It’s not the actual name for it in C#, they’re delegates. But that’s essentially what a function is, isn’t it? Right you’re passing in a function of the type
T to get, is it work to get a func<T>.Simon
He described a way of shorthand writing functions that used little arrow that we use in C# today. The only difference was that he used the Greek letter lambda to denote parameters. Which is why we called the lambda expressions when he did that back in the 1950s, I think.
But yeah, that’s that’s exactly what it is. So, yeah, where is a
func<T> and that generates kind of behind the scenes? What we’re doing is generating a function based on that on the arrow, I guess. I don’t know a great deal about the IO behind the scenes, but it’s, I think it generates and converts it all once you press the run.Jamie
goes to the equals and then the arrow sign. Then that doesn’t actually operate until I iterated over it. I have a collection of numbers and they say, where X > 3. When I say go and iterate over that list to produce maybe a list or an array when I say .toList, when I say .toArray, it will actually then go and perform that. So then, at that point, I guess it’s passing that function. Like you said earlier, It’s passing the function I’ve generated over that rather function I’ve written into the where function to do the thing is doing that almost like a pure function. Like you said, it’s passing a function as an argument. I guess.Simon
Yeah, I’ve done an awful lot of playing with with enumerables that you can kind of leave them in the box as it were, without springing it open for as long as possible, but still do all of your work. But that’s just a neat feature of C#, but yes, absolutely and also, if you call the function returns a func, because that’s something that you can do, then that would be higher order function as well.
So one technique that I sometimes use is you can have an arrow function that turns another arrow function, and there’s all sorts of things you can do with that I often use. It is a shorthand way of generating the lambdas for inserting into things like
select and where and similar, just mostly to make it a bit more descriptive. Rather than having the sort of big, long chains of arrows with mathematical symbols in them,I might actually do something like Create a function that is, let’s say, an ad where we have a single parameter. Let’s call it
int x, and then we’ll do arrow. And then after that, we’ll have y and then another arrow so and then at the end of it, we do x + y.This is a functional technique called Currying. So what it means is that effectively, there are two parameters,
x and y, why which you add together. But you can call them one after the other. And when you call the first you get back a function where the first is already filled in for you, and then you need to provide the second into that function to get an actual answer.That’s this is like the simplest version of this I can think of. Currying, of course, after Haskell Curry.
And In that particular instance, I’ve mostly used that for something like, if I knew that I was gonna do a whole load of chains where there’s gonna be some adds and subtracts and stuff like that, I might do something like just call
add(4) and then passed out into, say, a select or whatever to make it more obvious or make it more descriptive.That’s That’s a really simple example, but you can do all sorts of very complicated things. I’ve used a similar technique in the past for having a func, which is used to break apart CSV files. We have to supply what am I breaking each line by, what am I breaking each field by, what conversions am I playing at which point stuff? What data typed I want out. And if you do arrow functions that generate other arrow functions, you could just kind of write one and then apply the parameters in batches to get out of function, which is already part filled for you. So instead of writing, you know, 50 different pauses. You just write one, and then you supply the bits that you need as you need them. If that makes sense,
Jamie
Simon
Similarly, really object orientated is a lot more complicated than we think it is. If I have tried to explain object orientation, too, the folks that don’t know what it is or haven’t been exposed to it, it’s surprising how difficult it is to bring across the idea to them, because we’ve probably all been living with it. Certainly, I’ve been living with object orientation for 20 years, so to me it’s just obvious.
But when you haven’t had that exposure. It’s very complicated to explain. The whole concept of class was his object. And then we start getting into things like inheritance, polymorphism. And they’re not we as it were, that would just be mind blowing
and similarly function was not all that complicated. And once you’ve actually been practising for a while, it just becomes obvious after a bit.
Jamie
Trying to describe to someone how you break something down into its thing-ness properties and then splitting those out so that a
cat is a type of feline but also a leopard is a type of feline. So there are a number of things which describe both of them. The example that I used to give with shoes. You know, you have a shoe class but there are different types of shoe running shoe walking shoe, dancing shoes. They’re gonna think, and then you can branch off from there. But all of the issues will have common properties. You stick them in that sort of base implementation and use polymorphism to extend each one.But yeah, that you like. You say it does get a bit confusing to start with. So I guess like you say is functional. But like that once you’ve gotten past that bashed your head against the wall for a number of hours and it just clicks. Is that the case? Or is it just keep going? And in 10 years time, it’ll all make sense. I promise.
Simon
It’s not, actually, all that difficult. Once you understand what it is, it’s not hard at all to understand it and how to use it. If you want briefly to understand what that is. Although allegedly, according to Douglas Cropfield, the curse of the moan at is that once you’ve gained the ability to understand that, you lose the ability to explain it. To be honest, I’d argue it’s not necessarily all that hard.
So if you want to build the simplest monad in all of the world right now in C#, you create an extension method. You do something like
public static T output. Call it map for now, that’s as good a name as any. And then we’ll do our our squared chevron’s I don’t know how you call them. The angle brackets and do <TInput, TOutput>. So we’ve got an input type to an output type, and we don’t really care what they are, and then we’ll attach to this TInput.So it’s an extension method that attaches to everything. And then you take a single parameter, which is a func, which turns a
TInput into a TOutput. So func<TInput, TOutput>. But call it whatever you like. call it f call it func, I don’t really mind.And then the entire body of the function is call that function that was passed in as a parameter and stuff this into it. That’s it. You just do that and you’ve created your first monad. Now this is ridiculously simple and quite fragile.
But what this means is that everything everywhere in C# has got this extension method now where? Let’s say you’ve got a string. Anyone starts adjusting it, you can do
yourString.map() and then passing lambda expression, which I don’t know, maybe it doesn’t int pars. Maybe it splits or something, and then that returns a new value. And then you can whatever new value came out there. Let’s say, for example, that we’re doing a CSV parse so we’ll do "my giant string, which is everything .map and we’ll call a string.split inside that, and then we’ll do another map because now the input is an strings. So because map attaches to the entire object, it’s like a select except it operates on the entire array at once, not on an individual element like a select does.So inside the second one Maybe we’ll do another split. So now we’ve got an array of arrays, which is like the first dimension is the row. The second dimension is the column. And then after that, we could do a map to, I don’t know, convert everything into integers. And then we’ve got an array of an array of integers, and so what? We can do all sorts of messing around with this. But that’s actually all the Monad, really is that it’s very, very, most ridiculously simple.
It’s something which allows you to call functions in sequence, and each one takes the output of the last one and then pass it into the next one, like a relay race. That’s all it is. So we’re just doing chains of functions, each one feeding into the next one after the other. At its very very, very, very most simple. That’s all it is. Where it gets interesting is way. Start delving into the Why don’t we start putting some slightly more complicated logic into the point at which we switch from one function to the next?
So what we’re effectively doing is separating out the function that the user’s given us from how we carry it out. How we executed? So maybe we might want to put a
try...catch around that, in which case now your code has sort of become immune to error because there’s a try...catch built into every call to the to the map, say function.There’s a whole load more that can be done with this, but that’s it. It’s most simple. But does that make sense.
Jamie
Simon
I mean Monad are cool. Monads are amazingly useful. I’m like scratching the surface on this because there’s all sorts of amazing ways that you can use thumb toe right code, which is kind of entirely free of errors but also very descriptive. But no, you CAN do without them.
Like I say, if you just start using a lot more Linq, then you’re getting a chunk of the way there. Stop trying to adjust variable values and just start to think that each one should be based on the last but converted over using probably a function. Incidentally, that is a rule of thumb with functional. If there’s a question, the answer’s usually functions, so that’s another way that you know you’re on the right track. It’s definitely very, very useful that there is a wonderful article by Scott Wlaschin called Something like Railway Orientated Architecture
It’s a beautiful illustration of how to use a Monad effectively. So what you can do is something like, Imagine having an abstract based class, so we’ll call it I don’t know what we’ll call it. Maybe we’ll call it.
Maybe. That’s a good thing as any. So don’t worry about why I’m calling it that for the moment. So that’s an abstract. And all that is is something which has a single property anf the abstract class takes their generic. It takes a t. So it’s got something like public class Maybe<T> like that. And its just init. All it has is the property, which is of type T and locally value.And that’s all there is. That’s all we’ve got at the basic level, and I will inherit from it twice. On the once. We’ll call it
something the once we’ll call it nothing. So you’ve got you’ve got to actual classes that you can instantiate at any time.And the something is just a straight inheritance out from the abstract, so it was just a value. Think of it as thing in a box it’s just a value in a box. That’s all it is,
And then the other one, you override the value with with default whatever. So that would mean if it’s a string you get
null, it’s an object, you get null if it an int you get zero, if its a boolean you get false. So whatever the default value is, it’s nothing. And then you go and create your map style extension method where you’re just saying attach this to everything,But this time was saying that you have to be a type of
maybe so you’ll create two extension methods is to do this. The first one attaches to t everything as in the whole of C#, and then just convert whatever you’ve just called it from into a Maybe so you’re create from that and you something class, and then take whatever valley was this and stuff it inside. So if you started with a string which would say “my name”, then you call that too may be Now what you got is they may be containing a string which is valued with “my name”. And then we also need to do a new a new map-y thing, which is where we ask for a function to converted from the old one to the next one.And a better name for that, in this case, is bind for reasons that escape me. That’s what it’s generally called.
What we’ll do is first off will put a
try, catch around the whole thing.And then the second thing we’ll do is we’ll do a switch statement of the value. And, you know, in the latest person of C#, we could do so much more fun things with switches where you can switch against all sorts of awesome things. But all we really want to do is to say, is the value that’s just come into us, which, actually, as it happens at the moment, is “my name”. But let’s say do the generic version of a null check and If it comes in at no or default, rather, then let’s return them nothing.
If it’s not default, then we’ll take the function that was passed in as a parameter to our bind.
And then we’ll run. It will pass in this and then pass on out with another,
maybe containing contained any value. So that’s an awful lot of talking. But the way that Scott Wlaschin explains it is imagine that you’ve got two sets of train tracks running in parallel. They’re running from the beginning to the end and. There’s a series of points that is shared by each set of tracks,And each one of these points represents a function. The top track is everything works. That’s the track we go along with. Everything worked. So you know, Runner, I don’t know. We’d start with a string. We’ll do a database operation. Did it work yet? Carry on along the same track. We’ll do something else. Maybe we’ll try and convert it. Maybe we’ll try and pass to him. Did it worked? Great. Carry along the top track. But the moment that something doesn’t work, either it errors or it returns. Default will switch to the other track by which it means return The nothing class.
And, we put into the logic that if the maybe is actually a nothing, we don’t run the function. So the power of the monad here is the first off we put a try catch every call. And then the second amazing thing is that you can have this single chain of functions one after the other, like lambda or whatever, all defined in one single chain.
And then if at any point one of those functions fails or doesn’t return, then all of the subsequent functions are not called.
So all of that usual logic that we have to write into every single function like, you know, get a value run process one check it against null check it against empty check for errors. All of that is actually entirely contained within the structure of the Monad.
So this is one of the other amazing advantages of functional programming is that it’s extremely concise compared to object orientated programming. You’re talking about reducing the number of lines of code dramatically, because what you’re doing is you’re embedding the structure of the code into the language and, just sort of giving it the bits that you really care about and allowing the background paradigm to be taking care of all that code noise that we don’t want to be bothered reading. Are we making sense?
Jamie
I’ll make sure that there’s a link to the blog post that you mentioned Railway Orientated Architecture just full. You know, folks who want to go and read that and get it. That’s slightly more detailed description of it. Just because there are a lot of people will take the majority of that in, but not really kind of get it, I guess, until they’ve seen that diagram that you’re talking about with the two train tracks and it makes sense. It’s almost like a state machine, isn’t it? We get to this operation. Did it work? Carry on to the next one, otherwise drop out to the second railway track.
Simon
Jamie
So I mean, there’s nothing stopping from being a functional language because you could do functional programming in it. But in your opinion is see sharp, slowly becoming a functional programming language and world just being slowly nudged in that direction.
Simon
So I don’t know about it because I don’t think any intentions it become the dominant paradigm of C#. But I have seen talks from the head developer of C# in he outright. Right said that the whole intention is that C# to become this sort of hybrid, functional/object orientated language where you can, right to the paradigm that suits you In effect.
I do believe that what’s happening is that the F# guys on the C# guys kind of feeding off each other’s work. So whenever the F# guys request new features into C# to allow them to do more of their functional stuff, the sea shot because it’ll shares the same common language in the background. The C# guys benefit from it as well, and so we, to some degree, can start using it ourselves. It’s no coincidence that as of C# 8, we’ve started having the option to turn on the compiler warnings for if you write code, that might change the value of a variable that’s all designed to allow you to start moving more in the direction of the functional paradigm.
So, yeah, absolutely I don’t think it’s in anyway a controversial opinion. But C# is absolutely intended to be a hybrid functional language.
Jamie
Simon
Jamie
I start thinking about JavaScript and its ability to have the like. So we talked about Linq right, so Javascript has things like
map and, those kinds of things where you could pass a function in to allow you to somehow iterated over a collection of things. Maybe it’s an array. Maybe it’s a collection of objects or something and filter things that map filter, those kinds of things. And then I start thinking about when you do asynchronous Web requests or http requests you can do .then and passing her function or .catch and passing a function on, they start to think a little bit like that. So is that where we’re going? Do you think so? We can have, Like you said this hybrid language because, like I see JavaScript as being a hybrid it’s an, at the same time, procedural, object oriented, functional. Whatever other techniques you want to talk about language, so is seashell heading in that direction? And what does that mean? Does that mean that hopefully people aren’t thinking? will become JavaScript. The sky is falling because that’s no great,Simon
So there is a scary sort of like a knee-jerk reaction gets Monad, but they’re kind of friendly and we’ve been using them for ages. But yes, absolutely. You can do some really wild, certainly. So it is a really wild, functional programming in JavaScript. Er, yeah, I honestly think that functional at the moment, at least, is very much part of the way forward.
It’s definitely useful right now. I think one of the reasons has become particularly popular is the increased interesting stuff, like serverless because a serverless function is, in effect, a pure function. It’s a function that operates without state and can’t depend on anything except for it’s own parameters give and take. Oh yeah, I think that’s one of the reasons that we’re seeing. An acceleration of the interest in functional is because it supports stuff like that, and it the stateless nature of its support stuff like async.
So. One of the things that tends to make large, asynchronous apps struggle is when you’ve got common shared state between functions. Well, that’s the sort of thing the functional paradigm simply doesn’t have.
So, yeah, if you wanted to do an awful lot of a think, if you wanted to do an awful lot of serverless than functional, is the stuff you want to be looking into.
I don’t think that’s a bad thing at all. I don’t think that we’re looking at a world where you know pure, functional languages are the norm. I think that’s I think the object orientation is fine. It’s always could have its place. I rather like, C#’s kind of hybrid approach because there are benefits to be had from object orientation and the inheritance system that’s quite nice and concise.
But hopefully C# will allow us to use the best features of both.
Jamie
But I think you’re absolutely right. The idea that serverless functions. Lambdas those kinds of things. The idea is yet to make the code as small as possible, such that it doesn’t require any dependencies and can be ran by itself. It doesn’t have any kind of holding of any state or anything. I mean, they can read and write to an external thing if it needs to in a way store state. You shouldn’t have to rely on it because what if that function goes down?
Simon
But if you follow the functional paradigm and keep everything kind of neat and concise, it becomes potentially very easy to know where to make a change and to understand quickly the code that’s in front of you because there’s every chance that you’ll be regularly delving into areas of the code. Basically, we’ve never seen before, so we’re actually saving ourselves an awful lot of time and therefore an awful lot of money by moving in this direction.
Jamie
There are hundreds and thousands of person hours have gone into developing the compiler and people with incredible amounts of knowledge about how CPUs work in the virtual machine that .Net runs on works. All of this knowledge has been provided for you for free, such that your code will be as optimised as possible within certain limits. But the C#, F#, VB, javascript, whatever language you’re using, that code needs to be a simple as possible to follow for another human being because the other human being is going to be maintaining or changing that gold and that other human being. Maybe you in six months time.
Simon
Simon
An awful lot Linq, I believe, is based around recursion, certainly for you use something like Aggregate, which is one of the coolest bits of Linq’s. People, if you haven’t tried it. Go try it. But that’s that’s just based on tail recursion.
Jamie
Simon
But generally speaking, I mean, obviously you can still write unreadable functional code the same way if you know that lazy code is unreadable, no matter what paradigm and you’re working with us. But generally speaking, it tends towards a set of very good practises for keeping your code in a nice, neat order. One of my particular pet aches is one of those gigantic functions I would imagine a lot of us have dealt with where it’s a putz of the 1000 lines long, and it’s got variables at the top. That reference to, you know, hundreds of lines down and still in scope all the way at the end of the project. And it’s got if statements that looked like a family tree, you know, the sort of thing I’m sure we’ve all dealt with this. And you simply general don’t get that in functional code. It’s it’s far too neat for that.
It is perhaps worth mentioning on performance, though I haven’t done any really in depth benchmarks into the using your mostly see functional code in C#. But I don’t believe it’s necessarily the most performant way to write it.
That’s not to say that it’s Unperformant. It’s mostly pretty good. But if performance is your absolute 100% driver, then it may not necessarily be for you. I’ve seen I’ve dug into the There’s a book come out just recently. High performance C#, some marvellous stuff in that, but it’s kind of looking at it is kind of antithesis to what we’re trying to achieve in the functional world. It’s code that isn’t necessarily terribly read or what is extremely performant. So it’s probably a matter of, as is always the case in the computing world. What are you trying to achieve?
And if it’s if it’s, you know, business applications of business logic. And the most important thing is that we keep turning this stuff out and getting out changes done and into production. They’re functional, probably more the sign that we want to go towards.
Whereas if it’s the absolute most important that we get this as performant as it’s humanly possible to make it, you certainly have C#. You probably want to be very more towards the style of code in that book. C# isn’t necessarily 100% optimised towards functional, but it’s still it’s still very good. It’s still very performance,
Jamie
Simon
It’s got a much more advanced pattern recognition feature than although that might actually not be correct. As of the latest version of C#, which has got the new function style switches, I have played with those a little bit, and they are very, very cool indeed. And we’re we’re kind of bordering now on the proper F# style pattern recognition system, which is where your case isn’t just like a simple value. It’s more like a whole series of like a whole cluster of statements, potentially or some more complex. Select based on some properties of your object to decide if this is the case and then the case itself, maybe something more sophisticated than just a return or whatever. So we’re getting there. We’re getting there, but I’m not disparaging F#. I do like the look of it, and I would like to learn it. My only concern was just picking up F# and running with it is I would always be a bit reluctant to learn it too much because ultimately, I’m not sure whether we’re at the point. Yet as a developer community, where we’re likely to get a whole production applications written in F# at least as a common thing. I know that they exist.
Simon
Whereas there aren’t necessarily that many people that know F#. And in order for us to be comfortable to take on a programming language in production, we want to make sure that we can that the whole team is comfortable to work in it and that we could easily get more folks in whatever we need. to, continue working on it in the moment F# a little bit more of a niche skill compared to see sharp. So that’s why I think we’re not quite there yet. I’m not saying that one day there won’t be widespread F# usage everywhere. I could imagine that happening, but I can foresee that being more in the medium future rather or even the far future rather than the near, but it is. It is very cool on it is worth your time, I think.
Jamie
Simon
Jamie
So I’ve always found it’s the syntax or the concepts that stops me from learning some new techniques and new language simply paradigm.
But if you can learn one without the other, which is totally doable, then come back and learn the other. Like you said earlier, you don’t necessarily need to know what a moan that is. Even though you gave a really simple example, you don’t need to know what one is to be able to use.
Simon
But if you pick up most of the textbooks, all the examples tend to be Python and a Python looks remarkably cool. But I don’t know Python, and I don’t work in a company that makes use of Python, so I don’t get much exposure. So there’s that sort of, like you say, there’s a double overhead of. I need to learn the machine learning concepts. Plus, I need to learn Python, and that’s a bit of a double hit and makes it rather hard. So what I’m doing is similarly, too. What I’ve been doing a C# and that is I’m taking just like the textbooks, the talk entirely about the concepts, and then I’m implementing them. The algorithms in a language I’m familiar with, which, hey, it’s C#. That’s what I do and then if I ever want a transfer that knowledge over to Python, then it’s just simply a matter of picking up the the syntax. I’ve already got the concept. It’s going pretty well so far. I could imagine that Python is probably a lot more optimised to machine learning that C#, but that’s that’s not the point of learning it that way.
Jamie
Because, it has a similar sort of syntax tree to it. So once you have the concepts of one you can kind of fumble your way through. So, like if somebody was to show me a page full of Java and be able to figure out what he’s doing, I wouldn’t be able to immediately make changes and make individual bit twiddling changes. But it’s similar enough that it gets you over that barrier and then you know in your example of I’m going to learn the concepts, implement them in C# and then learned how to do that in Python. Perhaps that works as sort of like it’s almost like double testing your your knowledge because you’ve implemented already and . And then you take that C shop and convert it to python and then go look back at the concept and go Oh, yeah, I did it. Definitely. I definitely understand this concept.
Simon
So it’s all to play for.
Jamie
Simon
I think once you’ve actually had a play with some of these somebody’s structures certainly like the Monad is. There is the poster child of concepts that is supposed to be hard to understand. But I think if you just sit down and implement one, I give an example from my talks of just a very simple process for converting Fahrenheit two Celsius, which is, you know, a simple series of operations where you have to. I can’t know what it is. It’s something like Divide by 9 multiply by 5 and Deduct 32 because, (whispers) of course you have to.
Simon
They’re tough. I mean, some of them, some of them are easy, but most of them are pretty tough. And I, being an absolute, you know, coding addict, and I enjoy that sort of thing. So my so I tend to practice my functional style coding on on those because they’re really nice, meaty algorithms to try and get into. So my challenge to myself is, I’m planning to solve all of them using only arrow functions in the entire application that will solve all of them. It’s a bit hardcore, but that’s that’s kind of like the hardcore functional approach to everything. So by using only arrow functions, I am forced, not to have state. I’m forced to use nice, concise, small functions. It is forcing me to think a little sideways about how to solve things that otherwise I probably do with, you know, half a dozen lines of normal object orientated code. I’m not saying I’d necessarily do it exactly that way in production.
The whole idea is that since I’m doing this for fun, let’s go all the way. And then the best bits of what I’m working out how to do to write these nice, concise little structures. I’ll bring back to my production work later on. It is working out. I’ve found all sorts of awesome little hacks that you can stick into C# to make it happen.
Jamie
But FizzBuzz and hello world, I’m going to help you in your day to day work. Unless, of course, you are going to lots of job interviews because all they’re going to do is ask you how to do fizzbuzz, and you gotta pass the job interview. But then, when you get to the actual meat and potatoes of doing your daily grind, you’re not going to be able to actually do anything, because all you know how to do is fizzbuzz and hello world. It doesn’t take a lot of time, I guess, to figure how to do those kinds of problems solved those kinds of problems in a new language or framework. But looking at real problems that maybe you won’t face in real world, you know, like you know, the heavy mathematical problems of Project Euler or Advent of Code. You’re probably not going to solve those on a daily basis if you’re a Web developer, but it gives you enough experience of using those higher order. I don’t say higher order functions because it’s not what I mean, but that how you were thinking of how do I solve this problem with effectively with the Lego set that I have been given off this program or this paradigm or this framework?
Simon
In fact, day-to-day coding is probably nowhere near as complicated as that is, which it’s so it’s definitely good to kind of keep yourself shop as it were. So keep practising and thinking of, like, better ways to do the stuff that we’re doing and to look up algorithms that might have all sorts of uses later on. There’s no such thing as useless. Knowledge is something I’ve always try out saying the folks.
Jamie
Simon
Jamie
Simon
Jamie
Jamie
Simon
Jamie
Simon
Jamie
Simon
Jamie
I have noticed that they’re awesome programming books out there. That’s just a personal opinion. There are some programming books out there. Where is very much a case of “copy of this code. Now that you’ve got the code copy this next block of code. Now that you copied that block of code. Look, you’ve just made a website” and it’s like that’s not really teaching me anything. That’s just that could be a blog post, you know?
Simon
There’s even plenty of resources to have a look around in JavaScript as well, because there’s a lot of folks doing functional code in the Java script world. Most of us are probably pretty familiar with Javascript, enough to feel comfortable reading out reading stuff from that.
Jamie
I’ve heard it referred to as goes to. So I’ve always said goes to. You know, So you might say
X goes to X.toString() or something like that.But yeah, they all tend to use a similar syntax either equals and then the right chevron or a minus or hyphen on the right. Chevron. That whole idea of pointing this is where we go. We take this variable and put it into this function. So if you can understand it in one language, you probably will be able to understand it in another.
Simon
Jamie
Okay, Well, like I said earlier, one has been wonderful to check with you this evening and. Thank you ever so much.
Simon
The above is a machine transcription, as such there may be subtle errors. If you would like to help to fix this transcription, please see this GitHub repository
Wrapping Up
That was my interview with Simon Painter. Be sure to check out the show notes for a bunch of links to some of the stuff that we covered, and full transcription of the interview. The show notes, as always, can be found at dotnetcore.show, and there will be a link directly to them in your podcatcher.
And don’t forget to spread the word, leave a rating or review on your podcatcher of choice - head over to dotnetcore.show/subscribe for ways to do that - and to come back next time for more .NET Core goodness.
I will see you again real soon. See you later folks.