functional-programming

Monad equivalent in Ruby

What would an equivalent construct of a monad be in Ruby? ...

Cross product of 2 sets in Scala

val cross = (for (x<-setA; y<-setB) yield (x,y)) val cross2 = (setA flatMap (x => setB map ((x,_))) Is there a more elegant way to do this with a cross product operator, or some such? E.g: val cross3 = setA cross setB ...

Haskell - Parsec Parsing <p> element

I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: This is the first paragraph example\n with two lines\n \n And this is the second paragraph\n And my output should be: <p>This is the first paragraph example\n with two lines\n</p> <p>And this is the second paragraph\n</p> I defined: line= d...

Haskell Parsec items numeration

I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: - First type A\n -- First type B\n - Second type A\n -- First type B\n --Second type B\n And my output should be: <h1>1 First type A\n</h1> <h2>1.1 First type B\n</h2> <h1>2 Second type A\n</h2> <h2>2.1 First type B\n</h2> <h2>2.2 Second ty...

How would the 'Model' in a Rails-type webapp be implemented in a functional programming langauge?

In MVC web development frameworks such as Ruby on Rails, Django, and CakePHP, HTTP requests are routed to controllers, which fetch objects which are usually persisted to a backend database store. These objects represent things like users, blog posts, etc., and often contain logic within their methods for permissions, fetching and/or muta...

What are these lines doing?

I'm starting learning javascript for a project, I've found a script that does a part of what I need to do, I'd like to know how it works, both for me and in case it needs to be modified. Originally it was used inside the page, now I've put it in a file on its own and does not work anymore, so I'm dividing it in parts, because I fail to ...

How can I map a String to a function in Java?

Currently, I have a bunch of Java classes that implement a Processor interface, meaning they all have a processRequest(String key) method. The idea is that each class has a few (say, <10) member Strings, and each of those maps to a method in that class via the processRequest method, like so: class FooProcessor implements Processor { ...

How do functional programming languages work?

I was just reading this excellent post, and got some better understanding of what exactly object oriented programming is, how Java implements it in one extreme manner, and how functional programming languages are a contrast. What I was thinking is this: if functional programming languages cannot save any state, how do they do s...

How to structure an application written in a functional language?

How is code commonly organized in FP applications? Along what lines do you separate files, modules, directories, etc.? ...

Functional data structures in C++

Does anyone know of a C++ data structure library providing functional (a.k.a. immutable, or "persistent" in the FP sense) equivalents of the familiar STL structures? By "functional" I mean that the objects themselves are immutable, while modifications to those objects return new objects sharing the same internals as the parent object wh...

Flowcharting functional programming languages

Flowcharting. This ancient old practice that's been in use for over 1000 years now, being forced upon us poor students, without any usefulness (or so do I think). It might work well with imperative, sequentially running languages, but what about my beloved functional programming? Sadly, I'm forced to create a flow chart for my programm ...

F# replace ref variable with something fun

I have the following F# function which makes use of a ref variable to seed and keep track of a running total, something tells me this isn't in the spirit of fp or even particular clear on its own. I'd like some direction on the clearest (possible fp, but if an imperative approach is clearer I'd be open to that) way to express this in F#...

Make All Types Constant by Default in C++

What is the simplest and least obtrusive way to indicate to the compiler, whether by means of compiler options, #defines, typedefs, or templates, that every time I say T, I really mean T const? I would prefer not to make use of an external preprocessor. Since I don't use the mutable keyword, that would be acceptable to repurpose to indic...

Is there a functional version of begin...rescue...end (exception block) in ruby?

I'd like to do something like this in ruby: safe_variable = begin potentially_nil_variable.foo rescue some_other_safe_value end ... and treat the exception block (begin/rescue/end) as a function/block. This doesn't work as written, but is there a way to get a similar result? NB what I'm actually doing is this, which works but is ...

Persistent (purely functional) Red-Black trees on disk performance

I'm studying the best data structures to implement a simple open-source object temporal database, and currently I'm very fond of using Persistent Red-Black trees to do it. My main reasons for using persistent data structures is first of all to minimize the use of locks, so the database can be as parallel as possible. Also it will be eas...

What is the origin of "launch the missiles"?

In the context of functional programming, a typical example of a side effect is "launch the missiles". I was just wondering where that expression comes from historically. ...

Creating Delegates With Lambda Expressions in F#

Why does... type IntDelegate = delegate of int -> unit type ListHelper = static member ApplyDelegate (l : int list) (d : IntDelegate) = l |> List.iter (fun x -> d.Invoke x) ListHelper.ApplyDelegate [1..10] (fun x -> printfn "%d" x) not compile, when: type IntDelegate = delegate of int -> unit type ListHelper = stat...

More FP-correct way to create an update sql query

I am working on access a database using F# and my initial attempt at creating a function to create the update query is flawed. let BuildUserUpdateQuery (oldUser:UserType) (newUser:UserType) = let buf = new System.Text.StringBuilder("UPDATE users SET "); if (oldUser.FirstName.Equals(newUser.FirstName) = false) then buf.Append("S...

How to return the output of a recursive function in Clojure

Hi everyone! I'm new to functional languages and clojure, so please bear with me... I'm trying to construct a list of functions, with either random parameters or constants. The function that constructs the list of functions is already working, though it doesn't return the function itself. I verified this using println. (edit: Okay, it ...

Multiplying numbers on horizontal, vertial, and diagonal lines

I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit a stumbling block. One of the problem provides a 20x20 grid of numbers and asks for the greatest product of 4 numbers on a straight line. This line can be either horizontal, vertical, or diagonal. Using a procedural language I'd have no prob...