Functional Programming

Functions?
Sounds strange... or impossible, ... but that's exactly what can be done once a language is sufficient enabled for FP.

Of the languages I've mentioned in this thread only Swift is really powerful enough to support a large portion of the FP concepts. C#, Java and PHP have ostensively being OOP languages, but they made huge strides in the last few years to incorporate FP functionality, but it's always going to be more difficult to change a established language.
 
This is amazing. Most of the stuff is beyond me though :P
Why don't you blog this?
It was also beyond me in the beginning. I started hearing about FP just over 10 years ago, but many of the languages I used didn't adequately support it until recently. It's really powerful, but also really difficult to grasp at first, probably because these concepts were invented by mathematicians; and they picked some really scary names to explain things: monads, monoids, functors, applicatives, endofunctors, ...

So the first hurdle is always going to be tough; my eyes glazed over so many times, in truth I'm still struggling with some topics; once you grasp a particular area of FP, you're going to find there's another part to make your eyes glaze over again.

It's daunting, and wikipedia doesn't help much in that regard, as they tend have their FP articles littered with mathematical and type category formulas; which in time becomes readable, but is the worst possible introduction for someone trying to start out. Since I started actively using FP over OOP in my code, I've experienced two things, my code in shorter (far less duplication), my code is logically easier to reason about, and it's making me a better a programmer... because it forces you to reason differently.

As for complexity, we should also acknowledge that OOP is also difficult, it also has weird concepts like, objects, inheritance, polymorphism, design patterns, ... yet for most of us that became easy in time, as it does with most things.

As for blogging; I written the occasional article on medium, and I've also considered starting my own blog; but for now I'm going to continue posting FP topics on this thread, it's helps my comprehension to reason about this in more than one language and to try to present FP in hopefully a more understandable way & I'm hoping some of this is beneficial to others.
 
Last edited:
you should actually be posting these to CodeProject...
Thanks.
I'm still a little undecided atm about where this belongs; many years back I posted a bit on codeproject, and a few others, and more recently on medium; but neither of those really work for me; for different reasons; guess I'm probably at an point where I should really reconsider a blog; Mybb forums really isn't ideal when it comes to posting programming articles.
 
Article 4: C#, Java and Swift code examples.
I've decided to take a break away from this for this evening and maybe tomorrow, btw I still going to finish all those examples, plus I plan on writing quite a bit more about FP.

The reason for the break away, is to explore options for setting up a programming blog; let me know if you have any recommendations of services I should look at: primary need is code of course i.e. has to present it better than MyBB (and that's not asking much).
 
Last edited:
a free wordpress.com site with a plugin with code-highlighter should work fine. I'm interested in learning more in FP so I can apply this in my personal projects and maybe grow out / apply this professionally too.
 
[)roi(];18330288 said:
Thanks.
I'm still a little undecided atm about where this belongs; many years back I posted a bit on codeproject, and a few others, and more recently on medium; but neither of those really work for me; for different reasons; guess I'm probably at an point where I should really reconsider a blog; Mybb forums really isn't ideal when it comes to posting programming articles.

Correct. You are reaching a very small audience here and not _really_ getting anything for it. If you publish more publicly, then you earn 'credibility' and often that turns into work and $$$.
 
First Look at my Programming Blog.

As suggested, I took time off from writing more Functional Programming articles to start a blog.

Finally after trying a lot of different options and making a lot of custom modifications, I am ready to share it with you. I re-published three of the articles from this Functional Programming thread with a lot of fixes and functionality enhancements, for example:
  • Better code formatting
  • Improved layout
  • Language (you can pick from C#, Java, PHP or Swift) and the page will be oriented to only show you that language. However at this stage it won't remember your preference.
I will be actively adding more content over the next few days, plus make some more functionality improvements.

Your feedback and comments will be greatly appreciated.

Here's the link: https://codefunc.github.io
 
Sweet. May I also suggest a platform like Medium? I read the blog posts on this thing almost daily when it comes to programming concepts and related matters.
Thanks for the visit.:)

I tried medium for a while; for general writing it's nice; but it really sucked when it came to code; the only real workable solution was to create all the code snippets as github gists, which meant to had to navigate & cut/paste between two sites (a pain). That's basically the reason I stopped blogging there, and many other guys in the industry.

A separate blog allows me to overcome all those issues, and to implement many enhanced features like programming language selection (for multi language articles). Granted running my own blog has a few challenges; attracting visitors being the biggest, site maintenance + bugs, instead of just writing articles -- but if I think of the frustration of trying to work with medium and even worse MYBB, I think I'm up for challenge. It should come down to the articles; and hopefully I can provide something enticing enough + I still plan on tying threads on MyBB to the articles I post.

Was also thinking at some of inviting other bloggers... Prose.io allows in browser article creation and editing similar to CMS websites (using markdown), so that could be an easy way in for other bloggers i.e. no need for a complicated Jekyll setup on your computer + no need to deal with git commits.

If you're interested, let me know. The general idea is publish articles on any programming subject; whilst I'll blog on FP, I certainly won't restrict myself to that.
 
Thanks for the visit.:)

I tried medium for a while; for general writing it's nice; but it really sucked when it came to code; the only real workable solution was to create all the code snippets as github gists, which meant to had to navigate & cut/paste between two sites (a pain). That's basically the reason I stopped blogging there, and many other guys in the industry.

A separate blog allows me to overcome all those issues, and to implement many enhanced features like programming language selection (for multi language articles). Granted running my own blog has a few challenges; attracting visitors being the biggest, site maintenance + bugs, instead of just writing articles -- but if I think of the frustration of trying to work with medium and even worse MYBB, I think I'm up for challenge. It should come down to the articles; and hopefully I can provide something enticing enough + I still plan on tying threads on MyBB to the articles I post.

Was also thinking at some of inviting other bloggers... Prose.io allows in browser article creation and editing similar to CMS websites (using markdown), so that could be an easy way in for other bloggers i.e. no need for a complicated Jekyll setup on your computer + no need to deal with git commits.

If you're interested, let me know. The general idea is publish articles on any programming subject; whilst I'll blog on FP, I certainly won't restrict myself to that.
No worries, I went ahead and cloned your repository last night, and added JavaScript + Python to the mix, well for the page that was there any way. Sent you a pull request.
 
No worries, I went ahead and cloned your repository last night, and added JavaScript + Python to the mix, well for the page that was there any way. Sent you a pull request.
Wow.. thanks a lot, but going to unfortunately have to reject the pull requests; you inadvertently removed the php code. I'll add both your Python and Javascript additions; thanks again.

Was busy this AM with some cosmetic changes, but now eager to finalise the current loaded articles, and converting the rest.
 
[)roi(];18346344 said:
Wow.. thanks a lot, but going to unfortunately have to reject the pull requests; you inadvertently removed the php code. I'll add both your Python and Javascript additions; thanks again.

Was busy this AM with some cosmetic changes, but now eager to finalise the current loaded articles, and converting the rest.

Did I? I could have sworn I put the php below the python.

Edit: Seems I did. I'll fix it and recommit.
 
Last edited:
Did I? I could have sworn I put the php below the python.

Edit: Seems I did. I'll fix it and recommit.
No worries, I've added your adjustments... have a look it's live. PR if you have any others.

Also to ensure we don't overlap; I'll start working on another article.
 
Last edited:
Article update for https://codefunc.github.io (my new blog);

The following articles are live:

I should hopefully complete all the remainder of the Functional programming articles by Tuesday; whereafter I plan to start work on a new article:
  • Which as I promised would be on combinatorial logic; i.e. combining multiple small lambda style expressions and using this result to assist with more complex transformation processes, similarly function currying usually pairs well with this topic.
All in all it will be the start of the discussions around "Monoids" which are essentially elements that are mathematically theorized to combinable; for example Strings; or the addition of a sequence of numbers. Whilst that might sounds very arbitrary; the real power in Functional Programming comes from the ability to combine functions. i.e. complex combined functions as parameters.

Anyway I'm considering adding discussion on the blog; and would like to your views on this. Are you ok with Disqus.com? If not what else can you suggest?


/EDIT: I gone ahead and activated Disqus on the site; if you disapprove; please let me know why, and what else you'd suggest.
 
Last edited:
code ➤ func

apple-touch-icon-114x114.png
http://codefunc.github.io

We (Xennox & me) have completed major updates on Article 1 and Article 2; specifically we've extended the supported languages to the following:
  • C#
  • C++
  • Java
  • JavaScript
  • Perl
  • PHP
  • Python
  • Ruby
  • Rust
  • Swift
So in terms of languages we've probably got you covered. Let me know if you feel left out.

Picking a language is done within the article; and the article will be reoriented for that language, including text or code specific to that language. The articles have been enhanced compared to what I initially published on MyBB, have a look; I hope you like it.
Screen Shot 2016-09-22 at 12.51.31 PM.png

Article should hopefully be completed within a day or two; understandably it's quite a bit more work, since this increase from 4 to 10 supported languages.

As for completely new articles; they'll probably start appearing from new week; First on the list is probably demonstrating JSON parsing similar to what I did for PHP in the other languages. i.e. Partial Application.

Thereafter next on the list is Combining Functions (Combinatorial Logic) and function currying and more on Partial Application. We'll also probably also do more short articles on combining Higher Order Functions i.e. demonstrate just how powerful these functions are.

Anyway we're happy to entertain ideas; if there is something you either want to know about or your are struggling with; then let's us know and we'll plan an article on it.
 
Thanks for this, thought I would give it a bump so that it is easier for me to find while I go through the links.

I was reading an interesting article the other day describing how functional programming could be used to solve some problems within the current trends of front end frameworks so thought that I would share it too.
 
Thanks for this, thought I would give it a bump so that it is easier for me to find while I go through the links.

I was reading an interesting article the other day describing how functional programming could be used to solve some problems within the current trends of front end frameworks so thought that I would share it too.
Finally found some free time to read this.

Yeah; MVC has been around for quite a while (since Xerox Parc) and it's certainly not going anywhere soon; the problem with GUIs in functional programming is not yet a solved problem; if it was we'd have less divisive view points on how to tackle this.

Naturally the benefits of FP is clear, at least for anyone who has taken the time to learn this paradigm; but even then some things are just ATM very difficult to implement with immutable state or unassignment. Many things in the computing arena just ATM are far easier to model with mutable state. Futures, Promises, State Machines are all attempts to control state; yet for all that good work, it remains a unsolved problem.

But this is only a problem if you're wanting to strictly adhere to FP, like many Haskell'rs do, or at least those in the field of FP theoretical studies; even Haskell has had to introduce some compromises ATM by for exampel: wrapping many external stateful libraries in order to facilitate a more complete solution.

Many new comers to FP, well at least the OOP guys, tend to want have a list of FP patterns, but there really aren't any, well not in the traditional sense; so if we consider a distinction between a pattern and a library -- both are reusable solutions to a programming problems, but expressed quite differently. We generally describe patterns using our spoken language; whereas a library is written as code in a programming language. I think the GoF wrote about "Design Patterns" because they were struggling to express their approaches directly in the OO languages of the day. If they were able to succesfully solve that problem, they would simply have written a distributable and reusable library.

In Functional Programming: functors, monads, list comprehensions, streams, lenses, futures, etc., are all libraries. It's all developed in the programming language. Yet for all the expressive power of functional languages, we have to admit there are currently some limits, so you're definetly going to find these type of articles on patterns; they are however not the solution... because they are expressed in words, i.e. Not code.

In conclusion with FP; I'm definitely not one of the FP pundits who believe FP shouldn't be mixed with other paradigms; to the contrary; all the most popular languages are just that, mixed paradigm; employing strengths from many different paradigms. So until the FP theorists crack the state problem, I'm going to be content to mix where it makes sense to do so. I've said it before... Where feasible I push for most of the build to be in FP, but I don't fight the approximate 20% that can't be: that I happily still use OOP. The end result is that mutable state is contained to a far smaller part of the build.
 
Last edited:
thought I would add this here, and maybe get some feedback, but I found a good use case for adding a bit of functional programming to our codebase.

I added a PSR-6 caching interface, but I was getting pretty annoyed with the constant duplication of the following block of code.


PHP:
        $cacheItem = $app['cacheService']->getItem('THE_ITEM_KEY');
        $result = null;
        if (!$cacheItem->is_Hit()) {
            $result = $app['defaultModelBuilder']->buildModel($app, $request); //function to be called if there is no cache hit
            $cacheItem->set($result );
            $app['cacheService']->save($cacheItem);
        } else {
            $result  = $cacheItem->get();
        }
        return $result;

basically anywhere you wanted to use caching, you would have to duplicate this block, and the only change would be the `key`, and the `function that gets the result if not in the cache`.

someone might say, just add a function that takes a key and a result, eg

PHP:
function getItemFromCache($key, $item) {
        $cacheItem = $app['cacheService']->getItem($key);
        $result = null;
        if (!$cacheItem->is_Hit()) {
            $result = $item;
            $cacheItem->set($result );
            $app['cacheService']->save($cacheItem);
        } else {
            $result  = $cacheItem->get();
        }
        return $result;
}

$item = getItemFromCache('THE_ITEM_KEY', $app['defaultModelBuilder']->buildModel($app, $request));

that obviously would not work, as the 2nd parameter would get evaluated/executed immediately.

so, functional programming to the rescue

PHP:
//ContainerConfig.php
$app['getItemFromCache'] = $app->protect(function ($key, $cacheMissFunction) use ($app) {
            $cacheItem = $app['cacheService']->getItem($key);
            $result = null;
            if (!$cacheItem->is_Hit()) {
                $result = $cacheMissFunction();
                $cacheItem->set($result);
                $app['cacheService']->save($cacheItem);
            } else {
                $result = $cacheItem->get();
            }
            return $result;
        });

//Some code block that has access to the Pimple DI $app
$item = $app['getItemFromCache']('THE_ITEM_KEY', function () use ($app, $request) {
                 return $app['defaultModelBuilder']->buildModel($app, $request);
            });

//another code block that also wants to get an item from the cache, and add it if it doesnt exist
$item2 = $app['getItemFromCache']('THE_ITEM_KEY_2', function () {
                 return 'cats > dogs';
            });


sure, these functions are not pure (I dont think), but they save ton of boiler plate.
 
Last edited:
@_kabal_ I'll find some time later to give a more complete response, but in short I'm happy to hear a little functional style code (lambda function) has helped to strip out boilerplate.

As for whether your implementation is pure -- in the sense of PHP it is, thanks to the fact that you're passing all the parameters, and secondly because the cache object is copied before evaluation 'use ($app)'. FYI One way to test if a function is pure is evaluating it's ability to be memoized.
What I mean 'in the sense of PHP...'; because PHP doesn't support value types, so it's not always easy to ensure immutability.

Anyway it's also btw possible to get the function example you shared to work; I'll share some examples later today, or at the latest tomrrow.
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X