background preloader

Functional Programming

Facebook Twitter

Introduction to Clojure. Simplifying Asynchronous Code With Scala Async Recorded at. Functional Programming in Scala - Part 1: Core Concepts. Scala is an hybrid language in that it is object oriented and functional at the same time. While most programmers are accustomed to OOP, the same cannot be said about functional programming. This is the first of a series of articles about the fundamental concepts and techniques of this paradigm and their relative implementation in Scala.

Functions as first-class citizens Functions are first-class citizens in Scala , which means that they can be passed as arguments to other functions, returned as result or be assigned to variables. In mathematics, a function consuming or producing other functions is referred to as a higher-order function. An example of a higher-order function is List.map, which accepts a function that is then applied to each element of the List.

(x: Int) => x + 1 is assigned to the variable increment and then it is passed to map. Side effect and immutability An operation has a side effect when it modifies some state. An hybrid language Closures Recursion Conclusions. Aspects of Functional Programming in Java. Functional programming is a trending topic these days and a lot of Java programmers are hot for the features that modern functional programming languages might offer. Waiting for Java 8 and native closure support is a nice thing but for now we’re going to take a look at several Java frameworks that are trying to implement typical structures from those functional languages where possible using the capabilities of the Java language to emulate elements like higher-order-functions, closures, options and others … lambdaj Lambdaj is a framework that calls itself a library to “manipulate collections in a pseudo-functional and statically typed way” and it indeed offers a nice API to apply conversions, filtering, sorting, grouping, indexing, extractions and aggregations on java collections.

More detailed examples and documentation for lambdaj can be found on the project’s website. Maven Dependency We need to add the following one dependency to our pom.xml to work with lambdaj: Preparation Guava Optional. It’s time for Functional Programming. By Aaron Contorer - CEO In 1999 I went to Bill Gates with an idea to create a software tools group dedicated to shipping complex software faster. Engineers’ time is valuable, and more importantly, software that ships on time with fewer defects is worth a lot.

I organized a team that analyzed what was missing from the old toolsets for our most valuable products. Based on developers’ input, we conceived and delivered five in-house tools within our first year, spanning areas from build to source control to localization. Since then a lot has changed, but one thing has not: software is still hard to write. In the face of all these challenges, the one exciting development is the maturing of Functional Programming; in particular what we consider to be the most robust FP system, Haskell. We created FP Complete to realize the dream of Functional Programming: unprecedented software power, accuracy, and flexibility available to a broad number of programmers.

Functional Programming 101. In this blog post, I'll attempt to explain some basic concepts of Functional Programming, using Haskell. This blog post isn't about Haskell per-se, but about one way of approaching this problem, which demonstrates the benefits of functional programming. You can run most of these examples in ghci, by saving the contents of the example into a .hs file, loading up ghci and running :load file.hs. Many thanks to Mattox Beckman for coming up with the programming exercise, and Junjie Ying for coming finding a better data structure for this explanation than I came up with. The Problem You are Hercules, about to fight the dreaded Hydra. The Hydra has 9 heads. When a head is chopped off, it spawns 8 more heads.

Our job is to figure out how many chops Hercules needs to make in order to kill all heads of the Hydra. Prelude: Simple Overview Of Haskell Syntax Before we dive into code, i'll explain a few constructs which are unique to Haskell, so it's easy for non Haskellers. Choosing a data structure. Functional Programming in Java. Functional Programming brings new and interesting paradigms to code development. Several of those languages (Scala, Clojure) run on top of a Java VM, but Java itself is NOT as functional programming language..., how is that possible, there must be a way!

One of the aspects of the functional programming languages is this possibility to use a pointer on a function as a parameter. Here is a possible approach, even easier to access since the diamond notation has been implemented in the recent versions of the Java language. We are going to use a simple example as an illustration. We will choose the function to execute, based on the value of a parameter.

What we want to do here is to greet someone in the language passed as a parameter. Package functions; public interface FunctionOneArgReturnVoid<SomeType> { public void execute(SomeType a); } And let us write the following implementation: Prompt> java functions.ImplementationSampleOne "FR" Bonjour Joe Shmow ! The main like that: In this tutorial we will implement the Ribbit application in Scala. We'll be covering how to install the Play web framework, a NetBeans plugin for it, and finally the code in Scala.

If you are new to Scala, check out this previous tutorial which will help you set up your environment and provides you with a general platform that you can build upon. Even though the essence of Ribbit is to create/send/read Ribbits (our version of tweets), we will spend a large part of this tutorial explaining how Play works, authentication, and persistence. After these are in place, the rest becomes much easier. We will also implement ribbit creation, submission and listing out all ribbits. Following someone, advanced user settings, and direct messages will be an extra assignment for you to complete on your own. Download and Install Play There are quite a few web frameworks for Scala. So, go ahead and download Play for Scala. Installing the Play Plugin for NetBeans Creating the Project Getting the Files.

Functional Programming HOWTO ¶ In this document, we’ll take a tour of Python’s features suitable for implementing programs in a functional style. After an introduction to the concepts of functional programming, we’ll look at language features such as iterators and generators and relevant library modules such as itertools and functools. Introduction This section explains the basic concept of functional programming; if you’re just interested in learning about Python language features, skip to the next section. Programming languages support decomposing problems in several different ways: Most programming languages are procedural: programs are lists of instructions that tell the computer what to do with the program’s input. C, Pascal, and even Unix shells are procedural languages.In declarative languages, you write a specification that describes the problem to be solved, and the language implementation figures out how to perform the computation efficiently. In a functional program, input flows through a set of functions.

Functional Programming in Python | Pypix. What is Functional Programming ? Functional Programming is when functions, not objects or procedures, are the fundamental building blocks of a program. The idea is that you can pass functions as parameters to other functions and return them as values. Functional programming involves writing code that does not change state. The primary reason for doing so is so that successive calls to a function will yield the same result. Q1. Q2. Functional Programming in Python Before diving into the concepts of Functional Programming in python, lets understand some basic programming concepts like Procedural Programming and Object Oriented Programming. 1.

The output of a routine does not always have a direct correlation with the input.Everything is done in a specific order.Execution of a routine may have side effects.Tends to emphasize implementing solutions in a linear fashion. 2. A more complete real world example: 3. 3.1.1 chain() 3.1.2 izip() 3.1.3 islice() 3.1.4 imap() 3.1.5 starmap() 3.1.6 count() Q3. Getting started with Scala development using IntelliJ IDEA SBT. Pretty damn pretentious: Input and Output. Programs take input and produce output. The output is the result of doing something with the input. Input, transform, output, done.

This pattern is easy to see when the program is a UNIX tool. Take a string, count the words, print out the result. What’s the input and output? The input is all the sources of action for your app. The output is often a change in the app’s UI. But unlike the classic input/output design, this input and output happens more than once. To put it another way, the output at any one time is the result of combining all inputs. So why should we care? State There’s no intrinsic idea of state from this perspective. Most problems worth solving have some intrinsic state. State is bad. But happily this perspective of our app’s output as a function of its inputs over time gives us a new tool: functional reactive programming.

Time “Time-varying values” might sound like a bit of sleight-of-hand. Before, state was discrete pieces of data all moving independently. Example Fin. Introduction to Systematic Programming – Week 2 - Marketing & PR - Infragis. Late to the Party? Check out the earlier posts in this Series: Intro to “Introduction to Systematic Programming” Course Introduction to Systematic Programming – Week 1 Week 2 of Introduction to Systematic Programming focused primarily around How to Design Functions, or the HtDF method. We went over the HtDF Recipe itself and then dove directly into 4 example problems, and discovered lots of interesting tips and tricks for working more accurately and efficiently. Lesson 1: HtDF Recipe The purpose of the HtDF Recipe is to “systematize the design of a function” to ensure it is well tested accomplishes its stated purpose. A great tip from this lesson is to always have your Design Recipe “Cheat Sheet” with you when you’re writing your code, so that you can refer to it and make sure you’re on the right path.

Lesson 2: HtDF Details! After explaining the reasoning behind the HtDF Recipe in Lesson 1, Lesson 2 dives straight into explaining the different elements that make up the HtDF Recipe. (…n)) Introduction to Systematic Programming – Week 1 - Marketing & PR - Infragis. For those of you just tuning in, check out the “Intro” to the Introduction to Systematic Programming series here for some more background on what we’re about to get into. If you’ve been here since last week, I’m sure you’re waiting with bated breath to hear how the first week of classes went. Well, the wait is over! Week 1 consisted of 9 lessons centered around the topic of Primitives in Dr.

Racket. I’ll go through each lesson here one by one, giving a quick description of the main topic points we covered and any interesting additional tidbits I may have picked up. Please remember that some terms in this class, as mentioned in post 1, are used in a different manner than most traditional programming. Lesson 1: Introduction to Systematic Program Design The primary point covered in this first lesson is that, “Computation is what makes ‘things’ happen!”

Next, we reviewed the question, “What’s the ‘Design Method’?” Lesson 2: Expressions Dr. In Dr. For example, to have Dr. Lesson 3: Evaluation. Learning Scala by building Scala. While trying to come up with a compelling reason to get back at blogging I thought about all I've been learning over these last few months and it's all about functional programming. Coursera courses, books, learning new languages, it all boils down to learning how you can build programs the functional way, so, why not put a bit of all this into a couple blog posts and, better yet, use Scala for it? After doing a bunch of Standard ML and Racket for Dan Grossman's Programming Languages course, it's sad to see that most of the mainstream languages are still so far away in terms of features and functionalities when compared to something as ancient as ML (from the early 70's!). It's great to see the resurgence of these ideas in languages like Scala and Clojure and if you haven't jumped the bandwagon yet, it's time for you to do it :) The full source code for these examples can be found here.

This is our basic project configuration, we will only have Specs2 as a dependency for this tutorial. Learning Scala by building Scala. And off we go to build a couple more list operations and understand new concepts and styles of working with collections and values in Scala. If you didn't see the fist two parts of of this tutorial, check them below before moving forward: Finding an item in a list given a predicate At part 2 we saw how we could filter a list given a predicate function, but what if I wanted to find and get an item given a predicate?

Imagine I have a list of Person instances and I'd like to find someone with the Josh first name. I could use filter for this and grab the first item in the list, but that would be awkward. How are people usually doing it? Let's see how this would look like in Ruby: found = people.find { |p| p.first_name == 'Josh' }puts "#{found.first_name} #{found.last_name}" As you can see, just like we had a filter method at our LinkedList, the find method for enumerables in Ruby accept a predicate function and will return the first item for which the block returns true. Can we do better? Introduction to Systematic Programming – Part 3. Originally authored by Dara Monasch Week 3 of Introduction to Systematic Program Design was definitely a LOT more video content than I was used to from before, and I can safely say that having started watching the videos from Week 4 already, it’s only going to get crazier from here on out!

That said, as always, here are the links to the starting posts from this series, in case you missed out on them: Intro to “Introduction to Systematic Programming” Course Introduction to Systematic Programming – Week 1 Introduction to Systematic Programming - Week 2 So Week 3 of class focused primarily on learning How to Design Data and I’ll walk you through each lesson we learned step by step as best I can. Interestingly, I’ve also found that writing up these summaries assists with my own learning as well, so I hope that reading them does something similar for those of you who are my classmates!

Lesson 1: cond Expressions Standard cond expression template: To form a cond QA expression: Points to Remember: Dynamic Programming. Wow, it’s been a while since I’ve written anything here. Between changing jobs, working on my PhD and moving to a new country I guess you could say I’ve been pretty busy. But at the same time, together with all these changes in my life there’s a ton of new things that I’m learning almost every day. And, as many others, I feel that writing about them in a way that is easy for others to understand is one of the best ways to consolidate my own learning. So I’ll try to write here more often from now on.

So with that covered lets move to the important stuff. For some reason, lately I’ve been very interested in refreshing old concepts from algorithms and data structures that were a little rusty. Together with these old concepts also came some new concepts and techniques which I’ve never heard about before. On this post I wanted to talk about Dynamic Programming. So what exactly is Dynamic Programming? F(n) = F(n-1) + F(n-2) and F(1) = F(2) = 1 You might also find it defined as: Cheers!