background preloader

Funcional programing

Facebook Twitter

Alex Falkowski: Functional Programming in PowerShell. Functional Programming in PowerShell A while back I got introduced to the concept of functional programming and like most of you out there I found it hard to understand some of the concepts. Over time I have slowly started to understand some of the concepts and thought I would try to once for all capture them as I have a memory of a goldfish. Please note that it is important to try to understand these concepts if you are going to understand this blog post. Functional Programming is defined as: A programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. Functional Programming is defined as using a style called Declarative programming rather than your traditional Imperative programming. Declarative Programming is defined as: A programming paradigm that expresses the logic of a computation without describing its control flow. Imperative Programming is defined as: Luckily for us is that Immutable Object Higher Order Functions.

Skilldrick » Closures vs Objects: FIGHT. In JavaScript, there are two main patterns for creating objects with state - plain JavaScript objects and closures. In this post I’m going to highlight the similarites and consider the pros and cons of the two approaches. An object is an entity with state and methods to access/modify that state. There are two main approaches to this, which I’ll be calling “objects” and “closures”. For the rest of this post I’ll only use the word object to refer to plain JavaScript objects, in an attempt to avoid confusion. Now, there are infinite variations to the above. Alternatively, I could come up with a more convoluted example that caches the sayHi method so it’s shared between instances. The point is, using objects, state is shared through this.

Var dave = new Person('Dave'); dave.sayHi(); this within the method will be equal to the object it was called upon. There are three ways to mutate the internal state of an object in JavaScript. This can be a blessing and a curse. Closures. A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time. Lexical scoping Consider the following: function init() { var name = 'Mozilla'; function displayName() { alert(name); } displayName();}init(); init() creates a local variable called name and a function called displayName().

Run the code using this JSFiddle link and notice that the alert() statement within the displayName() function successfully displays the value of the name variable, which is declared in its parent function. Closure Now consider the following example: function makeFunc() { var name = 'Mozilla'; function displayName() { alert(name); } return displayName;} var myFunc = makeFunc();myFunc(); The reason is that functions in JavaScript form closures.

Powershell

Functional thinking: Thinking functionally, Part 1. Let's say for a moment that you are a lumberjack. You have the best axe in the forest, which makes you the most productive lumberjack in the camp. Then one day, someone shows up and extols the virtues of a new tree-cutting paradigm, the chainsaw. The sales guy is persuasive, so you buy a chainsaw, but you don't know how it works. You try hefting it and swinging at the tree with great force, which is how your other tree-cutting paradigm works. You can probably relate to this story, but with functional programming instead of a chainsaw. Welcome to Functional thinking. This installment and the next two serve as a whirlwind tour of some subjects related to functional programming, including core concepts. Number classifier To talk about different programming styles, you must have code for comparison.

The requirements state that, given any positive integer greater than 1, you must classify it as either perfect, abundant, or deficient. Imperative number classifier Listing 1. Listing 2. Natural Language Processing for the Working Programmer. Web programming is functional programming. Everyone who has long programmed for both worlds, that is he has developed local applications and programmed for web, will tell you that web programming is very different from classical application development. And it's not just the programming language. You can develop both local and web applications using the same Java, Python or even C++. The difference lies in the medium of the web.

It defines a much different deployment and runtime environment. "What's that? " Let us see. We'll start by looking at the lifecycle of a typical local application and compare it with that of a typical web application. What is the lifecycle of a typical local application like? How does the lifecycle of a typical web application look like? For a development of a local application, a number of techniques would prove useful. Using them in web development is a different story entirely. What's straight? This discussion touches the subject: Is functional programming relevant to web development?

Measuring Measures - Measuring Measures - Mining the Web with Clojure. Erlang vs. Scala. In my time wasting activities on geeky social news sites, I've been seeing more and more articles about Scala. The main reasons I became interested in Scala are 1) Scala is an OO/FP hybrid, and I think that any attempt to introduce more FP concepts into the OO world is a good thing and 2) Scala's Actors library is heavily influenced by Erlang, and Scala is sometimes mentioned in the same context as Erlang as a great language for building scalable concurrent applications. A few times, I've seen the following take on the relative mertis of Scala and Erlang: Erlang is great for concurrent programming and it has a great track record in its niche, but it's unlikely to become mainstream because it's foreign and it doesn't have as many libraries as Java.

Scala, on the hand, has the best of both worlds. Its has functional semantics, its Actors library provides Erlang style concurrency, and it runs on the JVM and it has access to all the Java libraries. Concurrent programming Hot code swapping. Spencer Tipping. It's taken a while, but I've finally come up with a counterargument (by technological analogy) to my previously-held belief that a more powerful language would make me a more productive programmer. This is a hard kind of mindset to get out of, because the equation seems so obvious: productivity = (features delivered) / (development time). And if you can use a better language to decrease the development time for the same features, then you're more productive.

Obviously. So, with that said, here's a hand-wavy, ostensibly mathematical (but not really), don't-try-this-at-home analysis of software development economics. Obviously the above equation is true, but it's capturing only the "coder" element of the software development supply chain. Problems solved features value = --------------- x ---------------- features development time This new term, (problems solved) / (features), is really important. Here's the reason this matters so much. Let's approach the problem top-down first. Continuation-passing and tail call elimination in Javascript | Eric Wendelin's Blog. I’ve been spending a lot of time recently tinkering with different constructs and methodologies in Javascript, and one of the most fascinating things I’ve come across is Spencer Tipping’s use of continuation-passing style. It’s ok if you aren’t familiar with CPS, but I think anyone hoping to make the cognitive leap to functional programming should study it.

As a bare miniumum, you need to know that a continuation is: [a reification of] an instance of a computational process at a given point in the process’s execution Therefore, continuation-passing style is just: a style of programming in which control is passed explicitly in the form of a continuation NOTE: I have replaced my crappy examples just below with Outis’s much more reasonable (and correct) ones.

Consider the naive implementation of the recursive fibonacci function: Then, identify function calls and returns. And you get: Apply this process to the linear recursive fibonacci function and you get: How CPS and TCO can be practical. Blogging at the speed of thought » Aggregate operations, the Lambda Goodness in Java. Clojure and math problems(solving project euler No. 11) Recently I started working on project euler as an interesting way to learn new languages and to refresh knowledge from mathematics.

In the last few months i have been playing with Clojure so I wanted to see how it can be used for solving mathematical problems. There are many interesting problems and the most recent one i’ve done is the task 11: What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20×20 grid? First of all we need some way of transforming matrix of string values to the matrix of integers. The input is one large string where each row is separated with newline and each value in the row with space. So the easiest way is to create matrix of integers is to first split one string into the multiple string where each string is one row and then transform each row into the row of integers. to achieve that we can use Now when we have matrix of integer values how do we find the biggest product of 4 elements in the matrix?

Functional Scala: Introduction. Welcome to the first part of a series of episodes about ‘Functional Scala’. While positioning itself as a so called object-functional language, most of the discussion and articles about Scala centered around its object oriented features so far. If you’re reading this, chances are you want to learn more about the functional side of Scala. Well, you’ve come to the right place. The idea for the following episodes arose out of some talks i gave about ‘Functional Scala’.

I decided to write and talk about it because I wanted to solidify my own knowledge about Functional Programming in general and about Scala in particular … and because I thought I could help some people new to Scala to learn its functional features from my perspective. Not least, there were some critical discussions in the past whether Scala is rightfully characterized as a functional Language. Functional Programming vs. But most importantly, Scala taught me to program and reason about programmingdifferently. How can this be? October 2010 Archives. I awoke Sunday to a beautiful, sunny, crisp October morning, not sure of what to expect.

My training for the Des Moines Marathon had begun back in July, and it had been full of ups and downs. My mileage had been reasonably steady, after one down week at the end of July, but I'd never developed much "long speed". Long runs were slow when they went well and tough when they didn't. I had a goal in my mind -- 8:36 miles -- but had no concrete evidence that I was ready to achieve it. I arrived downtown later than I had been planning. This year, Des Moines drew over 1600 marathoners, along with many, many more half-marathoners. Just before the 3-mile mark, the marathoners and half-marathoners split off onto different routes. It turns out, marathoners are insane. I held steady in my plan to just run. This was so different from my usual practice. Around the 11-mile mark, I noticed that my legs felt heavy. As the race wore on, I added two elements to my "positive thinking" regimen.

Erlang

LambdajExtensibility - lambdaj - Demonstrates how it is possible to extend and customize the library features - Project Hosting on Google Code. Lambdaj comes with lots of features that should help to make your code more readable and in many cases shorter than its original imperative version. Conciseness anyway it's just a side effect in using the DSL provided by lambdaj and in my opinion not the most remarkable one. The main purpose in using lambdaj should be to improve the readability of the code, decrease its cyclomatic complexity and promote reusability and maintainability. That's why lambdaj has been thought to be easily extensible allowing to deal with the specific needs of your domain model that of course cannot be directly covered out-of-the-box by the library.

Let's see in more details how you can do that. To convert objects in a different kind of objects is something very common when manipulating a given domain model. Lambdaj already offers this feature in a very basic way as in the following example: List<Double> speeds = extract(cars, on(Car.class).getSpeed()); and apply it to all the cars through the convert method: What is Functional Programming? - Christopher Bennage. Disclaimer: I’m still pretty green with functional programming. This is me working out my own understanding. Wikipedia defines Functional Programming (FP) this way: “functional programming is a programming paradigm that treats computations as the evaluation of mathematical functions and avoids state and mutable data.”

[reference] Let’s break this apart. Programming Paradigm What’s a “programming paradigm”? It seems to me that most popular languages allow for the use of more than one paradigm. Computations In this definition, I take the term very generically. Evaluations of Mathematical Functions This is the core concept behind functional languages.

F(t) = -4.9t2 + 19.6t + 3 With this function, we pass in a value for t and we get back a value representing the height. Avoids State and Mutable Data This point follows naturally from the last one, however it was very confusing to my object-oriented mind. Characteristics of Functional Languages I’ll begin by discussing the first three items.

Scala