functional

Reduce a set of functions over a value?

Hi, I'm looking for a clean, idiomatic way to do a "backwards reduce" in Clojure. I've got (def fns '(fn1 fn2 fn3)) (def val 42) I'd like to obtain (fn3 (fn2 (fn1 val))), and I'm not picky about the order. So I'd like to consecutively apply a sequence of functions to a value, rather than consecutively a sequence of values to a fu...

Php: functional style, light-weight alternatives to the separation of concerns in OO MVC patterns?

The problem: I have a legacy php app that is coded in no particular style (some OO, function libraries, some templates, no templates), which I am slowly rewriting, cleaning up, and adding to. When I introduced templates to the system, it was immediately evident that that would really clean up and simplify the code a lot because of th...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda function going to stay? ...

Creating a generator expression from a list in python

What is the best way to do the following in Python: for item in [ x.attr for x in some_list ]: do_something_with(item) This may be a nub question, but isn't the list comprehension generating a new list that we don't need and just taking up memory? Wouldn't it be better if we could make an iterator-like list comprehension. ...

Execution flow in MVC

Hi all, I am trying to learn MVC in detail, and I am wondering whats the exact functional flow internally, in the sense of which functions(important functions) are called and what they does when the application starts and which function are called apart from the controller actions that we write in our application as we proceed. Help in...

C# foreach vs functional each

Which one of these do you prefer? foreach(var zombie in zombies) { zombie.ShuffleTowardsSurvivors(); zombie.EatNearbyBrains(); } or zombies.Each(zombie => { zombie.ShuffleTowardsSurvivors(); zombie.EatNearbyBrains(); }); ...

Why can't I use a String array with a method taking an Iterable as a parameter?

I'm trying to write my own "functional" little lib in Java. If I have this function : public static <T> List<T> filter(Iterable<T> source,BooleanTest predicate) { List<T> results = new ArrayList<T>(); for(T t : source) { if(predicate.ok(t)) results.add(t); } return results; } why can't I use it...

Scheme: what are the benefits of letrec?

While reading "The Seasoned Schemer" I've begun to learn about letrec. I understand what it does (can be duplicated with a Y-Combinator) but the book is using it in lieu of recurring on the already defined function operating on arguments that remain static. An example of an old function using the defined function recurring on itself (no...

Can I pass an object's instance method to a method expecting a callback in Scala?

Let's say I have a method expecting another method as a parameter. Is it possible to send an object's instance methods for that parameter? How would I handle methods that have no parameters? I'll write some pseudocode: void myMethod1(callback<void,int> otherFunc); // imagine a function returning void, and taking a int parameter void m...

monad tranformers and stacking of multiple monads

I have a f function with signature f :: [a] -> StateT Int Reader b [c], and f' with signature f' :: a -> StateT Int Reader b [c] The computation in f (very simplified) looks like that: f [] = return [] f (s:st) = f' s >>= \x -> f st >>= \y -> return $ ... And in place of the ... I would like to return the [c] pa...

Redirect calls to a member of a class in python

Hi, I was trying to 'extend' a closed class collections.defaultdict(lambda: 1) by addint it 2 methods, called 'vocabulary', and 'wordcount' apparently it's impossible to setattr method to builin types, nor can I inherit from defaultdic, so I decided to write a class and redirect calls to it to the type I want to extend. class BagOfW...

C++ member function applied to object

hi I want to call member function by passing it as template parameter, without using boost is possible. Here is an example off what I tried to do, class object { void method(); } { object object_instance; ... apply<object:: method>(); ... template<class F> void apply() { F(object_instance); } // want to call object_instance.F() } t...

Substitute values in a string with placeholders in Scala

I have just started using Scala and wish to better understand the functional approach to problem solving. I have pairs of strings the first has placeholders for parameter and it's pair has the values to substitute. e.g. "select col1 from tab1 where id > $1 and name like $2" "parameters: $1 = '250', $2 = 'some%'" There may be many more t...

Managing flexible, typed, immutable data structures in Scala in 2.8.x

This is a follow-up now that Scala 2.8.0 beta is out to this question: http://stackoverflow.com/questions/1710813/what-is-a-proper-way-to-manage-flexible-typed-immutable-data-structures-in-scal The new technique is to copy a case class, e.g. case class Person(name:String, email:String) val bob = Person("Bob", "[email protected]") val jill = ...

can if be a proper function rather than a special form

hello I finally started learning functional languages (emacs lisp) and it makes explicit distinction between functions and special forms such as flow control , for example if. Is there a fundamental/theoretical reason why special forms are distinct from functions? do any languages provide functional if? Thanks ...

what languages are used in AI research today?

hi. I am currently dabbling in expert systems, emacs lisp, and reading up about artificial intelligence. Traditionally, artificial intelligence is associated with LISP and expert systems with CLIPS. However, I have noticed in computational sciences how much Python is being used. What about the area of artificial intelligence and mach...

WDSL - at what stage of the project would you expect delivery?

Hi, I'm working on my first project using an external supplier, who will develop some web services for the company I work for. My question is - at what stage of the project lifecycle would you expect the WDSL (and any associated Schemas) to be delivered? Personally, I think it's a service contract, so I wouldn't expect it to be unre...

bash case statements evaluate to strings

I've caught the functional programming bug, so naturally nothing is good enough for me anymore. ;) So, in bash one could write: case $status in "foo") status="bar" ;; "baz") status="buh" ;; *) status=$status ;; esac but I'm afraid of typos, so I'd prefer to write: status=case $status in "foo") "bar" ;; "baz") "buh" ;; *...

functional programming scala map and fold left

Hi can someone tell me some good tutorials on fold left and map ...

Conversion from imperative to functional programming [Python to Standard ML]

I have a function specification that states it that should evaluate a polynomial function of one variable. The coefficient of the function is given as a list. It also accepts the value of the variable as a real. For example: eval(2, [4, 3, 2, 1]) = 26 (1*x^3 + 2*x^2 + 3*x^1 + 4*x^0, where x = 2) Here's the function in python, but I'...