functional-programming

How do I make a callable JS object with an arbitrary prototype?

I'm looking to make a callable JavaScript object, with an arbitrary prototype chain, but without modifying Function.prototype. In other words, this has to work: var o = { x: 5 }; var foo = bar(o); assert(foo() === "Hello World!"); delete foo.x; assert(foo.x === 5); Without making any globally changes. ...

How do I declare a method using Func<T, TResult> as input argument ?

Hy, I'm writing a class which should help me in unit tests. The class offers methods to perform Assertions on Exceptions. Until now I was able to write methods which take a function with no parameters and no return value as input. To do this I use the System.Action - delegates. My class looks like this: internal static class Exceptio...

What are the best practices for functional programming and database interaction?

I know that in pure object-oriented languages like Java it usually makes sense to use ORMs like Hibernate. But what would I do when writing a CRUD-type functionality in Clojure or Common LISP? Passing around SQL as the first-order functions? But isn't having SQL in HTML-generating code very ugly? Thanks, Olek ...

What areas of code are you using f# for?

For those of you out there who are using f#, what areas of functionality are you coding with it? What is the language really well suited to and what does it do with far more power and ease than say c#? ...

Using foldl to count number of true values

I'm trying to find a way to do the following function with foldl: count a = length (filter (\i -> i) a) It just counts the number of values that are true in a list of booleans. I did try it myself with count = foldl (\i -> case i of True -> (1+) False -> (0+) ) 0 Which did not even compile. Any suggestion...

Efficient String Implementation in Haskell

I'm currently teaching myself Haskell, and I'm wondering what the best practices are when working with strings in Haskell. The default string implementation in Haskell is a list of Char. This is inefficient for file input-output, according to Real World Haskell, since each character is separately allocated (I assume that this means tha...

Scala: splitting a list using a predicate for sublists

I just had a use case where I needed to split a list into n sublists, so that elements are taken in order from the original list and grouped while the predicate is true for the sublist (when it is false, a new sublist is started). I didn't find this functionality in the standard library, and I thought it was a good exercise to try to sol...

Project Euler Problem 27 in F#

Hi all, I've been trying to work my way through Problem 27 of Project Euler, but this one seems to be stumping me. Firstly, the code is taking far too long to run (a couple of minutes maybe, on my machine, but more importantly, it's returning the wrong answer though I really can't spot anything wrong with the algorithm after looking thr...

What functional language techniques can be used in imperative languages?

Which techniques or paradigms normally associated with functional languages can productively be used in imperative languages as well? e.g.: Recursion can be problematic in languages without tail-call optimization, limiting its use to a narrow set of cases, so that's of limited usefulness Map and filter have found their way into non-fu...

Implement map in javascript that supports object methods as mapped functions?

I recently tried to use an implementation of map in javascript to create a bunch of items, then apply them to an objects add method. Firstly with a bog standard implementation of map. var map = function (fn, a) { for (i = 0; i < a.length; i++) { a[i] = fn(a[i]); } } Setup. var translateMenu = new Menu; var langu...

What is "Lambda Lifting"?

I just ran into this while going through the Erlang compiler source. I'm not really getting it. (go figure ;)), considering that I just realized that there is such a thing 5 minutes ago). Forgive me for asking first without first trying to understand reasons for its existence. There is a wikipedia article about it, but it is pret...

Are some data structures more suitable for functional programming than others?

In Real World Haskell, there is a section titled "Life without arrays or hash tables" where the authors suggest that list and trees are preferred in functional programming, whereas an array or a hash table might be used instead in an imperative program. This makes sense, since it's much easier to reuse part of an (immutable) list or tr...

Self-referential data structures in Lisp/Scheme

Is there a way to construct a self-referential data structure (say a graph with cycles) in lisp or scheme? I'd never thought about it before, but playing around I can find no straightforward way to make one due to the lack of a way to make destructive modification. Is this just an essential flaw of functional languages, and if so, what a...

Book "Higher order C#"?

Higher order perl shows how to exploit functional programming features of perl. Is there a similar book for C# ...

How do I get my brain moving in "lisp mode?"

My professor told us that we could choose a programming language for our next programming assignment. I've been meaning to try out a functional language, so I figured I'd try out clojure. The problem is that I understand the syntax and understand the basic concepts, but I'm having problems getting everything to "click" in my head. Doe...

Do you find you still need variables you can change, and if so why?

One of the arguments I've heard against functional languages is that single assignment coding is too hard, or at least significantly harder than "normal" programming. But looking through my code, I realized that I really don't have many (any?) use patterns that can't be written just as well using single assignment form if you're writing...

Do you think functional language is good for applications that have a lot of business rules but very few computation?

I am convinced that functional programming is an excellent choice when it comes to applications that require a lot of computation (data mining, AI, nlp etc). But is it wise to use functional programming for a typical enterprise application where there are a lot of business rules but not much in terms of computation? Please disregard ...

Two argument Memoization

In C# how do I memoize a function with two arguments? Do I have to curry before memoization? Wes Dyer wrote the Memoization code I typically use, but now I need two arguments ...

Manipulating lists in OCaml

I am having issues manipulating deeply nested lists in OCaml in the below context. class foo (pIn:int)= object (self) val p = pIn val even = if (pIn mod 2) = 0 then true else (false) method doIt = "doIt" method isEven = even method getP = p end;; let rec createListOfElements howMany = ( Random.self_init (); ...

How can I get started with functional programming?

With all the hype around functional programming, which are the best resources to getting started in functional programming [for a C# programmer]? I am not looking for C# 3.0 language improvements. ...