far from a new concept • Rooted in the concepts of Lambda Calculus (~1930s) • Introduced in many early languages such as Lisp • Recently undergoing a renaissance
far from a new concept • Rooted in the concepts of Lambda Calculus (~1930s) • Introduced in many early languages such as Lisp • Recently undergoing a renaissance • Functional Languages like Haskell, Clojure and Scala gaining popularity as “real” tools
far from a new concept • Rooted in the concepts of Lambda Calculus (~1930s) • Introduced in many early languages such as Lisp • Recently undergoing a renaissance • Functional Languages like Haskell, Clojure and Scala gaining popularity as “real” tools • Traditionally imperative languages such as C++ are gaining concepts from functional languages in recent revisions
and our code by learning from & leveraging FP • Avoiding Mutable values and State for referential transparency • Using functions as a higher level concept for composition
and our code by learning from & leveraging FP • Avoiding Mutable values and State for referential transparency • Using functions as a higher level concept for composition • ... and thinking in terms of functions being closer to the composable, reusable units they are meant to be
to introduce some functional programming (as well as a little bit of my favorite language for it, Scala) • This is by no means a comprehensive course on functional programming
to introduce some functional programming (as well as a little bit of my favorite language for it, Scala) • This is by no means a comprehensive course on functional programming • If you like what you see, I recommend a few books
to introduce some functional programming (as well as a little bit of my favorite language for it, Scala) • This is by no means a comprehensive course on functional programming • If you like what you see, I recommend a few books • “Learn You A Haskell For Great Good” (Lightweight & lighthearted)
to introduce some functional programming (as well as a little bit of my favorite language for it, Scala) • This is by no means a comprehensive course on functional programming • If you like what you see, I recommend a few books • “Learn You A Haskell For Great Good” (Lightweight & lighthearted) • “The Little Schemer” (if you like Lisp)
to introduce some functional programming (as well as a little bit of my favorite language for it, Scala) • This is by no means a comprehensive course on functional programming • If you like what you see, I recommend a few books • “Learn You A Haskell For Great Good” (Lightweight & lighthearted) • “The Little Schemer” (if you like Lisp) • “Real World Haskell” (heavy and in depth, if you want a real dive)
x * y; } That’s right... it’s a function (in C/C++) We can invoke it to calculate a value... int main() { int x = 512000; int y = 128; int z = multiply(x, y); std::cout << x << "*" << y << " is: " << z << std::endl; }
* y if __name__ == '__main__': x = 512000 y = 128 z = multiply(x, y) print "%i*%i is %i" % (x, y, z) Python has some FP influence though... print "Multiply Function: %s" % multiply # Multiply Function: <function multiply at 0x1101ef938> foobar = multiply print "%i*%i is %i" % (x, y, foobar(x, y))
functions to be “first class” • a function named “multiply” is simply a variable of type “function” • Technically you can use pointers to do similar tricks in C/C++ but it’s far from easy (or transparent)
functions to be “first class” • a function named “multiply” is simply a variable of type “function” • Technically you can use pointers to do similar tricks in C/C++ but it’s far from easy (or transparent) • Many languages segregate function as if they are special, meaning they are not truly composable units of work.
functions to be “first class” • a function named “multiply” is simply a variable of type “function” • Technically you can use pointers to do similar tricks in C/C++ but it’s far from easy (or transparent) • Many languages segregate function as if they are special, meaning they are not truly composable units of work. • In FP, we try to leverage the function as just another tool - meaning it must be flexible, reusable and first class.
but instead borrows some concepts of functional programming • Instead, let’s look at Scala... which is a true blend of Object Oriented + Functional programming with a full arsenal of FP tools.
but instead borrows some concepts of functional programming • Instead, let’s look at Scala... which is a true blend of Object Oriented + Functional programming with a full arsenal of FP tools. • Scala was created by Martin Odersky (author of Java Generics & the 1.5+ javac) at EPFL in Switzlerand
but instead borrows some concepts of functional programming • Instead, let’s look at Scala... which is a true blend of Object Oriented + Functional programming with a full arsenal of FP tools. • Scala was created by Martin Odersky (author of Java Generics & the 1.5+ javac) at EPFL in Switzlerand • JVM based, able to interoperate cleanly with Java & existing libraries
but instead borrows some concepts of functional programming • Instead, let’s look at Scala... which is a true blend of Object Oriented + Functional programming with a full arsenal of FP tools. • Scala was created by Martin Odersky (author of Java Generics & the 1.5+ javac) at EPFL in Switzlerand • JVM based, able to interoperate cleanly with Java & existing libraries • Used by companies such as Twitter, Foursquare, The Guardian, LinkedIn, and many more
= { x * y } val n = 512000 val o = 128 val p = multiply(n, o) println("%s*%s is %s".format(n, o, p)) // 512000*128 is 65536000 println("Multiply Function: %s".format(multiply _)) val foobar = multiply _ // Multiply Function: <function2> println("%s*%s is %s".format(n, o, foobar(n, o))) // 512000*128 is 65536000
& reusable unit, we should also be able to define it ‘anonymously’ • This concept is sometimes referred to as lambdas • Lambdas are one of the most borrowed FP concepts in traditional languages
& reusable unit, we should also be able to define it ‘anonymously’ • This concept is sometimes referred to as lambdas • Lambdas are one of the most borrowed FP concepts in traditional languages • In some cases, Lambdas exist exclusively of “first class” functions (but can be used to tackle similar needs)
well (but restricts to one liners) • C++ 11 is even adding them to C++! lambda x, y: x * y # <function <lambda> at 0x10acce938> multiply = lambda x, y: x * y multiply(5, 2) # 10
well (but restricts to one liners) • C++ 11 is even adding them to C++! lambda x, y: x * y # <function <lambda> at 0x10acce938> multiply = lambda x, y: x * y multiply(5, 2) # 10 [](int x, int y) { return x * y; }
the most leveraged patterns in functional programming • The idea is to truly take advantage of functions as values • A Higher Order Function either ...
the most leveraged patterns in functional programming • The idea is to truly take advantage of functions as values • A Higher Order Function either ... • Accepts one or more functions as “input”
the most leveraged patterns in functional programming • The idea is to truly take advantage of functions as values • A Higher Order Function either ... • Accepts one or more functions as “input” • Produces a function as its result (“output”)
the most leveraged patterns in functional programming • The idea is to truly take advantage of functions as values • A Higher Order Function either ... • Accepts one or more functions as “input” • Produces a function as its result (“output”) • Or, technically... both!
User gets limited (as in you control the view of the resource) access to some resource (often IO based) • Much easier to handle cleanup, restrict access to certain values/params and “hide implementation”
=> println(line) ) // Even simpler... implicit... fh.getLines.foreach(println) // Filter out blank lines val isBlank = (x: String) => x == "" val noBlank = fh.getLines.filterNot(isBlank) val blankOnly = fh.getLines.filter(isBlank) // Manipulate on the fly, producing a new datasource val commented = fh.getLines.map{ l => "//%s".format(l) }
into play... • Enforces encapsulation and value “leakage”/”exposure” • Limits access to the “core resource” • More importantly, allows us to more safely control two incredibly important concepts:
into play... • Enforces encapsulation and value “leakage”/”exposure” • Limits access to the “core resource” • More importantly, allows us to more safely control two incredibly important concepts: • State
into play... • Enforces encapsulation and value “leakage”/”exposure” • Limits access to the “core resource” • More importantly, allows us to more safely control two incredibly important concepts: • State • Mutability
into play... • Enforces encapsulation and value “leakage”/”exposure” • Limits access to the “core resource” • More importantly, allows us to more safely control two incredibly important concepts: • State • Mutability • ... which we’ll elaborate on shortly.
taking multiple argument lists instead of one • Many functional languages allow this (as well as tricks around them) • Think about how we can clean up our multiply function based on the tricks we just pulled with partial application
lists (technically, to “curry” they must each take a single argument) partially applied • This gives us a chain of callable functions • With partial application, we can “lazily” evaluate this chain
of as the “beauty” of currying can also be used to clean up things like a “loan” method which takes initialization params • Separate out our function arg from our non-function args ...