functional-programming

Efficient Immutable Map Implementation?

Hi, I'm wondering if there is an implementation of a map which is: Immutable, so that I can use it in functional programming, and effortlessly ensure transactions and concurrency. Fast. I've checked out Binary Search Trees (RB, AVL) and Tries, but none of them seemed to be as fast as Hash Tables. Is there a map implementation that supp...

JavaScript: Can a constructor function create a documentElement object?

I want to make a constructor function that creates a documentElement object. As an example, consider the new Audio() constructor - it creates a documentElement object, and if you pass it some variables it populates the new documentElement with attributes. It doesn't insert it into the DOM, it simply creates the object. So, the questio...

Closures: why are they so useful?

As an OO developer, maybe I have difficulty seeing its value. What added value do they give? Do they fit in an OO world? ...

Help in designing a tree structure - Tension between functional and OOP

Hi, I've been learning f# in the previous days, writing a small project which, at last, works (with the help of SO, of course). I'm trying to learn to be as idiomatic as possible, which basically means that I try to not mutate my data structures. This is costing me a lot of effort :-) In my search for idiomatic functional programming,...

PHP count amount in array with a given value

Say I have an array like this: $array = array('', '', 'other', '', 'other'); How can I count the number with a given value (in the example blank)? And do it efficiently? (for about a dozen arrays with hundreds of elements each) This example times out (over 30 sec): function without($array) { $counter = 0; for($i = 0, $e = co...

Calculating the Moving Average of a List

This weekend I decided to try my hand at some Scala and Clojure. I'm proficient with object oriented programming, and so Scala was easy to pick up as a language, but wanted to try out functional programming. This is where it got hard. I just can't seem to get my head into a mode of writing functions. As a expect functional programm...

Why is appending to a list bad?

I've recently started learning scala, and I've come across the :: (cons) function, which prepends to a list. In the book "Programming in Scala" it states that there is no append function because appending to a list has performance o(n) whereas prepending has a performance of o(1) Something just strikes me as wrong about that statement. ...

F# - how to write nested loops in a recursive way?

Given the following C# code: var product = new List<int>(); for (int n1 = 100; n1 < 1000; n1++) { for (int n2 = 100; n2 < 1000; n2++) { product.Add(n1 * n2); } } What would be the equivalent F# code written in a functional style? ...

How to prevent passing down of parameter from one function to another

Example: Class A { public function __construct() { $this->b_Instance = new B(); } public caller() { $this->b_Instance->call_me($param1,$param2,$param3); } } Class B { public function __construct() { //lots of variables here } public function call_me($param1,$param2,$param3)...

What is the best functional language for scientific programming

I am coming from C/C++, Python background and I am looking to learn a functional language that (Hopefully) can do Serious Matrix Computation expressive real world modelling database integration concurrency/parallelism Battery (library) included Strong integration with other well tuned library in other language Have a positive future ...

Functional programming: currying

Writing as an unreconstructed imperative & OO programmer... Have messed about with Erlang and also Haskell lately. I like Erlang, not sure yet about Haskell. Functional seems more like math than programming, hope that makes sense. Functional programming seems very powerful. Reading docs on the interwibble wrt functional programming I c...

How do I refer to std::sin(const valarray<double> &) ?

I'm having trouble with some valarray function pointer code: double (*fp)(double) = sin; valarray<double> (*fp)(const valarray<double> &) = sin; The first compiles, the second gives: error: no matches converting function 'sin' to type 'class std::valarray<double> (*)(const class std::valarray<double>&)' ...

Any AS3 library needs to be compiled with standard mode instead of strict mode?

I see a lot of AS3 libraries are written in strict mode compile fashion. They are all very Java like. I haven't seen any libraries that requires compilation in normal mode. More functional programming like, and probably use a lot of prototype and scoping magic, since ActionScript 3 can be very much JavaScript like if used normal mode com...

How to get optimization from a "pure function" in C#?

If I have the following function, it is considered pure in that it has no side effects and will always produce the same result given the same input x. public static int AddOne(int x) { return x + 1; } As I understand it, if the runtime understood the functional purity it could optimize execution so that return values wouldn't have to ...

Algorithm to create fair / evenly matched teams based on player rankings

I have a data set of players' skill ranking, age and sex and would like to create evenly matched teams. Teams will have the same number of players (currently 8 teams of 12 players). Teams should have the same or similar male to female ratio. Teams should have similar age curve/distribution. I would like to try this in Haskell but the...

Build a Form Builder with Ruby and JQuery

I'm looking for some advice on designing the above for a RoR application that basically has two sections: 1) Admin Form Builder 2) End User Form (that was created by the admin in step 1) I've prototyped the JQuery form (end result) I want and it uses "progressive disclosure" (similar to Yehuda Katz's Bamboo example in the JQuery in Ac...

Why is this J function not running?

I am attempting to learn J and the book I am using says this is the proper way to define a monadic function function =: 3:0 function statements so I followed this format and wrote the folding code. Can you tell me why this is throwing a syntax error when I try to call it with input but if I just call p it returns 3 h=:>:@i.@<....

Purely functional data structures with copy-on-write?

I want to have the advantage of functional data structures (multiple versions of data that can share structure) but be able to modify it in an imperative style. What I'm thinking about (and a possible use): a RPG game in which whole game history is stored (for example, to allow for travelling back in time). Using copy-on-write, I could...

Finding advanced programming classes

I have an intense job and large family. Finding time and energy to explore/expand technical skills on my own is difficult. I'd like to find some courses on advanced software topics without having to enroll in a PhD program. I'm not quite sure where to start. There are some great resources out there for learning different programming top...

What Functional Language suits best for a Database Application?

I want to develop an application to connect to a PostgreSQL Database, I want to do it in a Functional Language, but I'm not sure what could be the best, I wanted to do it in Erlang, but it doesn't have a good and reliable driver, what language would you choose or use? ...