functional-programming

Comparing javascripts native "for loop" to prototype's each()

So I was having a debate with a fellow engineer about looping in JavaScript. The issue was about the native for loop construct and prototype's each() method. Now I know there are lots of docs/blogs about for and for-each, but this debate is somewhat different and I would like to hear what some of you think. Let's take the following loop...

Is there any algebraic structures used in functional programming other then monoid?

I recently getting to know about functional programming (in Haskell and Scala). It's capabilities and elegance is quite charming. But when I met Monads, which makes use of an algebraic structure named Monoid, I was surprised and glad to see the theoretic knowledge I have been learning from Mathematics is made use of in programming. T...

What's a good and functional way to swap collection elements in Scala?

Hi! In a project of mine one common use case keeps coming up. At some point I've got a sorted collection of some kind (List, Seq, etc... doesn't matter) and one element of this collection. What I want to do is to swap the given element with it's following element (if this element exists) or at some times with the preceding element. I'm...

Python vs F#: which language I should learn

Hey, I know this is an subjective question, but please don't vote to close it before I get the acceptable answer. I'm a .NET programmer (somewhat experienced), and really want to learn a functional language. My preferred choices are F# and Python, and I'm really doubting about which one to choose. Please clear my doubts. I'm totally new...

Functional Reactive F# - Storing States in Games

I am a student currently learning about Functional Reactive paradigm using F#. It's radically new viewpoint for me. Yesterday I learned about creating a simple ping-pong game using this paradigm. The idea I grasp so far is : we think values as functions of time. On its pure form, it's stateless. However, I need to remember the position o...

Scheme: Data serializing, efficient [and functional]

I'm serializing data which may be an integer, an object (list) with other nested objects, and trying to make a choice as to what approach to be using. Of two, the first one is to create bytevectors recursively and copy them in the calling functions to a larger single bytevector; the second one is to use some kind of the stream I could wr...

Ruby passing different attribute arguments to same function

Hello, I have a job model which has many attributes. I would like to use graphics to display the sum of some of these attributes. For that, I am using the following code in the javascript portion of the view which produces the graph: <% for job in @daily_jobs %> ['<%= job.day %>',<%= job.walltime %>], <% end %> This returns ...

What's the difference between closures and traditional classes?

What are the pros and cons of closures against classes, and viceversa? Edit: As user Faisal put it, both closures and classes can be used to "describe an entity that maintains and manipulates state", so closures provide a way to program in an object oriented way using functional languages. Like most programmers, I'm more familiar with ...

Are there any nice-n-simple Computation Expression tutorials?

Hi all, I'm a C# guy looking to learn F# and functional programming. I'm hearing a lot about "monads" .. which I think (thanks to Google), are called "Computation Expressions" in F#? One aspect I'm trying to bend my head round is the State Monad/Computation Expression... But I need an idiot-proof, line-by-line, explanation about what'...

Diagramming in functional languages

Hello, I would like to know if there are some tools and techniques for diagramming in functional languages like Lisp, Clojure, etc. Something like UML in OOP languages, perhaps? ...

[SOLVED] Perl's tr/// is not doing what I want...

EDIT: tr/// does not support variable interpolation, so I went with s/\Q$_\E//g; instead Or, more likely, I'm not doing something right... I have the following code: #!/usr/bin/perl use strict; use warnings; use utf8; sub strip_invalid { my ($str, @chars) = @_; map { $str =~ tr/$_//; } @chars; return $str; } my @invalid = q...

What are R's equivalents to Perl's map and grep?

I am interested in (functional) vector manipulation in R. Specifically, what are R's equivalents to Perl's map and grep? The following Perl script greps the even array elements and multiplies them by 2: @a1=(1..8); @a2 = map {$_ * 2} grep {$_ % 2 == 0} @a1; print join(" ", @a2) # 4 8 12 16 How can I do that in R? I got this far, us...

What principles should I follow to code in PHP with fewer Java-like objects and more similar to Clojure's functional programing principles?

I was introduced to Clojure not long ago, and while I haven't fully assimilated all of it's concepts, It has given me an alternative to Java and PHP's OO that I really want to move towards. I consider Clojure's systems to be my ideal. I know that I want to let it inform my PHP coding style as much as I can. I really don't like OO in P...

How to show row numbers in PostgreSQL query?

I'd like to show the observation number for each record returned by a PostgreSQL query. I think in 8.4 windowing functions can perform this capability. Edit: I think I just found the answer, but since this hasn't been asked before, I'll keep the question open. I also welcome non-windowing function possibilities. Edit 2: So a little t...

Have J style adverbs, forks etc been emulated via libraries in mainstream functional languages?

Has an emulation of J style of super condensed tacit programming via verbs, adverbs, forks, etc., ever been attempted via libraries for mainstream functional languages? If so, how successful was the result? If not, is there a technical issue that makes this impossible, or is it just not worth doing? I'm particularly interested in cons...

Concise Lisp code to apply a list of functions all to the same argument(s) and get a list of the return values?

Suppose I have a single element, and I have a list of predicates (functions). I want to apply each of these predicates to the single element and get a corresponding list of return values. I know that map and friends can apply a single function to each a list of arguments, but is there any concise syntax to apply many functions to a singl...

Best way to change list type in scala

Hi all, I have a list in scala called l : List[AType] that I want to change to list[String]. This may sound like a very dirty, inefficient approach, but I'm not quite sure of the best way to do this. My code was: var result = new Array[String]("a","b") l foreach { case a => result = result :+ (a.toString().toUpperCase()); } result toL...

What is an appropriate data structure or algorithm for producing an immutable concrete syntax tree in a functionally pure manner?

Given a LL(1) grammar what is an appropriate data structure or algorithm for producing an immutable concrete syntax tree in a functionally pure manner? Please feel free to write example code in whatever language you prefer. My Idea symbol : either a token or a node result : success or failure token : a lexical token from source text...

Is there some literature on this type of programming?

In college I took on a class on modern physics, in which we learned about special relativity. I was completely blown away by how different frames of reference could actually observe physical properties of an object to be different and neither be incorrect. Over time, this concept has slowly been changing the way I program, to the point...

Rebol anonymous function behavior is weird

My anonymous func test below is executed only once: repeat i 5 [ func[test][ print test ] rejoin ["test" i] ] I am obliged to name it to be able to execute it 5 times as expected: repeat i 5 [ test: func[test][ print test ] test rejoin ["test" i] ] This is weird. isn't it really possible to use anonymous function in...