The Art of Functional Programming

Rate this content
Bookmark

Functional Programming (FP), a paradigm in which programs are made up of pure, stateless functions, is adored by many programmers for how easy it makes it to predict, test, and debug the behavior of the code we write. Although FP has an unfortunate reputation as an ivory-tower domain full of obscure jargon comprehensible only to those with PhDs in category theory, the core concepts are straightforward ideas all of us can understand, and even have fun learning!


In this talk, we’ll create generative SVG art as a way to explore fundamental FP concepts such as recursion and function composition, and tackle the practical challenges of FP in the real world, such as dealing with the tricky parts like side effects and randomness which don’t fit into the safe, sterile world of pure functions. By the end of the talk we’ll not only have the key concepts & techniques we need to start writing our own functional programs, we’ll have some pretty functional art to look at too!

This talk has been presented at React Summit 2022, check out the latest edition of this React Conference.

FAQ

The main topic of Anjana Vakil's talk was functional programming, its principles, and how it can be applied in the real world.

Functional programming is considered predictable and easy to debug because it uses pure functions, which always return the same output given the same inputs, making it deterministic and free of side effects.

A pure function in functional programming is a function that transforms input values into an output value without altering any state or causing side effects. It always produces the same output for the same set of inputs.

Functional programming handles side effects by pushing them to the outermost edge of the program, creating an imperative shell around a core of pure, deterministic functions.

Recursion in functional programming is a technique where a function calls itself to achieve repetition. It replaces traditional iterative loops, allowing for a functional approach to repetitive tasks.

State in functional programming can be managed by treating it as data. Functions receive the current state as an input and return a new state as part of their output, ensuring that the state remains immutable.

Functional programming handles randomness using pseudo-randomness. Deterministic pseudo-random number generators produce values based on a seed, ensuring consistent outputs for given inputs.

The techniques demonstrated to create generative art using functional programming included handling side effects with an imperative shell, using recursion for repetition, managing state as data, and incorporating pseudo-randomness for organic variation.

You can find more information about Anjana Vakil's work on functional programming through her talk 'Learning Functional Programming in JavaScript' and her course 'Functional JavaScript First Steps' on Frontend Masters.

The advantage of using pseudo-randomness in functional programming is that it allows the generation of random-seeming values while maintaining determinism, ensuring that functions remain predictable and testable.

Anjana Vakil
Anjana Vakil
26 min
17 Jun, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Functional programming is a beautiful paradigm that allows us to understand programs and how entities behave and interact. It has nice characteristics like predictability, which makes testing and debugging easier. We explore how functional programming handles side effects and generative art. We learn about drawing patterns and grids with recursive functions, handling state in functional programming, and creating fashion using functional programming techniques. We also discuss handling randomness in functional programming and how to introduce organic variation and randomness to art while maintaining predictability.

1. Introduction to Functional Programming

Short description:

Functional programming is a beautiful paradigm that allows us to understand programs and how entities behave and interact. It has nice characteristics like predictability, which makes testing and debugging easier. Many programmers find functional programming easy to love, but it can also be intimidating.

Oh, my goodness! We're in three dimensions! I'm really excited about it. I have missed you all. It's great to see you. See you. See you. Very far away. Very far away. I'm really excited to see everybody virtually tuning in at home.

My name is Anjana Vakil. I am here to talk to you about React. Just kidding. No. I'm here to talk to you about functional programming. Which is sort of related to React, because it is the programming paradigm behind the way that we work with everything in a functional framework like React. So I want to talk to you about functional programming, because I think that it is a really beautiful paradigm. A really beautiful way of understanding what programs are, what entities they're made up of, how those entities behave and interact. In other words, a programming paradigm, that I think is pretty great. And I don't think I'm the only one.

Because functional programming has some really nice characteristics. So first of all, functional programs are really easy to predict. They're very predictable. Which means that they're easy to test to make sure they're doing the right things. And they're easy to debug to make sure that when they're not doing the right things, we find out why and we fix it. And so that is why as programmers, I think we should most of us find functional programming pretty easy to love. Give it up if you got some love for functional programming. Yeah. Functions. Functions. Whoo. But I actually think that functional programming is also pretty easy to be scared of.

2. Introduction to Pure Functions

Short description:

Functional programming is programming with functions, specifically pure functions. A pure function takes input values and returns an output value based on those inputs. It is simple, predictable, and does not involve any state.

Give it up if you got some fear for functional programming. Yeah, me too. Me too. Exactly. Because it's got this reputation, which is unfortunate, but probably pretty well deserved of being this very opaque kind of academic scary world full of weird words, like terms from category theory and stuff like that, that you need a Ph.D. to understand. So it's pretty easy to be a little afraid of.

I think that functional programming actually in its core principles can be really simple to understand. And so I kind of make it my mission to try to make functional programming more accessible. You might know me from this talk I gave called Learning Functional Programming in JavaScript. If you haven't encountered functional programming before or if you know somebody that hasn't and wants to, check that talk out. It goes into a lot more about how functional programming really works under the hood. And to go even deeper, you can also check out a course I have on frontend masters called Functional JavaScript First Steps. This talk is not going to be really going into the details of how we do functional programming except in so far as to answer the questions of how we do functional programming in the real world.

So what is functional programming just in a high level? Well, it is programming with functions. That's super insightful. My talk is over. Bye. Okay. So just kidding. Functional programming is programming with functions, as you probably know. In fact, it's programming with a special type of functions called pure functions. So what is a pure function? Well, a pure function is a function that turns input into output. You give it input values and it returns an output value. And that is literally all that it does. It returns an output based on its inputs. So you maybe give it a width and a height value as inputs and it maybe returns that aspect ratio by dividing them as its output. Very simple, very predictable, because that means a pure function is totally deterministic. So if you give it the same inputs, it will always give you the same output. And that's because it doesn't involve any kind of state.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
Tom Pressenwurter introduces Redwood.js, a full stack app framework for building GraphQL APIs easily and maintainably. He demonstrates a Redwood.js application with a React-based front end and a Node.js API. Redwood.js offers a simplified folder structure and schema for organizing the application. It provides easy data manipulation and CRUD operations through GraphQL functions. Redwood.js allows for easy implementation of new queries and directives, including authentication and limiting access to data. It is a stable and production-ready framework that integrates well with other front-end technologies.
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
Envelope is a powerful GraphQL plugin system that simplifies server development and allows for powerful plugin integration. It provides conformity for large corporations with multiple GraphQL servers and can be used with various frameworks. Envelope acts as the Babel of GraphQL, allowing the use of non-spec features. The Guild offers GraphQL Hive, a service similar to Apollo Studio, and encourages collaboration with other frameworks and languages.
Rock Solid React and GraphQL Apps for People in a Hurry
GraphQL Galaxy 2022GraphQL Galaxy 2022
29 min
Rock Solid React and GraphQL Apps for People in a Hurry
The Talk discusses the challenges and advancements in using GraphQL and React together. It introduces RedwoodJS, a framework that simplifies frontend-backend integration and provides features like code generation, scaffolding, and authentication. The Talk demonstrates how to set up a Redwood project, generate layouts and models, and perform CRUD operations. Redwood automates many GraphQL parts and provides an easy way for developers to get started with GraphQL. It also highlights the benefits of Redwood and suggests checking out RedwoodJS.com for more information.
Adopting GraphQL in an Enterprise
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
Adopting GraphQL in an Enterprise
Today's Talk is about adopting GraphQL in an enterprise. It discusses the challenges of using REST APIs and the benefits of GraphQL. The Talk explores different approaches to adopting GraphQL, including coexistence with REST APIs. It emphasizes the power of GraphQL and provides tips for successful adoption. Overall, the Talk highlights the advantages of GraphQL in terms of efficiency, collaboration, and control over APIs.
Step aside resolvers: a new approach to GraphQL execution
GraphQL Galaxy 2022GraphQL Galaxy 2022
16 min
Step aside resolvers: a new approach to GraphQL execution
GraphQL has made a huge impact in the way we build client applications, websites, and mobile apps. Despite the dominance of resolvers, the GraphQL specification does not mandate their use. Introducing Graphast, a new project that compiles GraphQL operations into execution and output plans, providing advanced optimizations. In GraphFast, instead of resolvers, we have plan resolvers that deal with future data. Graphfast plan resolvers are short and efficient, supporting all features of modern GraphQL.

Workshops on related topic

Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura