Functional programming is a paradigm which attempts to solve computational problems by the chained evaluation of functions whose output is determined by their inputs rather than the programme state. In this style of programming, side effects and mutable data are deprecated.

learn more… | top users | synonyms

1
vote
0answers
37 views

How to adjust Foreground extraction in prediction

This is my program for ForegroundExtraction, which I've made into a function: int ForegroundExtraction() { VideoCapture capture("recognition.mp4"); if (!capture.isOpened()) return 0; ...
3
votes
2answers
197 views

How to rewrite this code in functional way?

My code looks like imperative style code. I want to make my code more functional. How I can rewrite my program so it become functional style code? val _map = getMap(); if(_map.contains(x) ...
3
votes
0answers
38 views

Can this be written more concise and aligned with the Functional Reactive Programming paradigm?

Trying to learn FRP and Elm. Is the program below "ok" from the perspective of FRP and Elm? What could be improved? The program can be executed here: http://elm-lang.org/try import Random import ...
2
votes
1answer
33 views

Functional Programming Critique Ocaml

I'm doing an exercise where I need to convert 'ints' to 'bignums' in ocaml, for example: 123456789 -> {neg:false; coeffs:[123;456;789]} There were a lot of things going inside my head when writing ...
1
vote
4answers
143 views

Using Func<T> instead of helper methods in a class

I really like functional programming in C#, it gives you a lot of flexibility. I am wondering if it is the right idea to use functions instead of helper methods, and to make the code more readable. ...
0
votes
0answers
32 views

Haskell Bencoded Dictionary Parser

I have been working on a Bencoded string parser in order to improve my knowledge of Haskell. After running the code through hlint, I still have a few questions: As I noted in the comments, the key ...
1
vote
2answers
109 views

Scala: What takes precedence: Readability or Elimination of Side Effects

I have a method: def removeExpiredCookies(jar: CookieJar): CookieJar = { val currentDate = System.currentTimeMillis / 1000L jar.filter(_._2._2 > currentDate) } You use said method ...
1
vote
1answer
42 views

Simple Comonads in Scala?

trait Comonad[M[_]] { // map def >>[A,B](a: M[A])(f: A => B): M[B] // extract | coeta def counit[A](a:M[A]): A // coflatten | comu def cojoin[A](a: M[A]): M[M[A]] } ...
1
vote
1answer
53 views

Jquery Geocoding Script

The rules for this script are simple: If geocode is hit, then just geocode If geocode and submit is hit, then geocode and then submit If an autosuggest link is hit, then geocode instantly In this ...
2
votes
1answer
134 views

Is a unit test which uses LINQ to check multiple values acceptable?

I'm writing unit tests for a library which parses colors from user input (example: the user input “#f00” gives a red color; the user input “60,100%,100%” gives a yellow color, etc.) One of the ...
1
vote
1answer
37 views

Matlab: speeding up repetitive initialisation with structured data?

I need to speed up this code: it creates a multilinear function. I considered the below methods but no idea which with the largest benefit. replacing dummy variables with things such as ...
3
votes
3answers
91 views

is there a better and more functional way to write this

I'm learning Scala and functional programming. Can I make it more functional? Thanks class Student(val firstname: String, val lastname: String) { override def toString: String = firstname + " " + ...
3
votes
1answer
63 views

How to make this code more functional?

I have a simple task: Given a sequence of real numbers. Replace all of its members larger than Z by Z. And count the number of replacements. I solved this task using Scala. But my code looks ...
2
votes
0answers
105 views

How to improve readability and memory footprint of this haskell script?

I have this small haskell script that I wrote some time ago to parse a set of financial CSV files to produce other CSV files. I've recently had a problem with large input files (around 300Mb) that I ...
3
votes
0answers
114 views

Immutable C++ stack - thoughts && performance

What are your thoughts on the fallowing immutable stack implementation ? It is implemented having as a basis a C# immutable stack (where garbage collector assistance does not impose using a reference ...
1
vote
1answer
72 views

Recursive Determinant

The following code I devised to compute a determinant: module MatrixOps where determinant :: (Num a, Fractional a) => [[a]] -> a determinant [[x]] = x determinant mat = sum [s*x*(determinant ...
2
votes
0answers
88 views

functional javascript - recursively walking a tree to build up a new one; could this be better factored as a reduce?

/** * recursively build a nested Backbone.Model or Backbone.Collection * from a deep JSON structure. * * undefined for regex values */ exports.modelify = function modelify (data) { if ...
1
vote
2answers
43 views

functional javascript - how could I generalize this code that correlates parallel async requests with their results?

/** * takes a list of componentIDs to load, relative to componentRoot * returns a promise to the map of (ComponentID -> componentCfg) */ function asyncLoadComponents (componentRoot, components) ...
3
votes
0answers
220 views

Connect Four AI (Minimax) in Clojure

I wrote a Connect Four game inlcuding a AI in Clojure and since I'm rather new to Clojure, some review would be highly appreciated. It can include everything, coding style, simplifications, etc. But ...
3
votes
1answer
104 views

The eternal dilemma: bar.foo() or foo(bar)?

I was using foo(bar) as it's adopted for functional programming. console.log( join( map(function(row){ return row.join(" "); }, tablify( ...
3
votes
2answers
199 views

Palindrome test in Haskell

I am new to Haskell and, coming from a OO/procedural programming background, I want to make sure my code follows the functional way of doing things. I built this short module to test if a given string ...
1
vote
0answers
40 views

Is this code for getting the dependency tree of a file correct?

function dep_tree(graph,node,prev_deps){ return uniq(flatten(graph[node].map(function(child_node){ if (contains(prev_deps,child_node)) throw "Circular reference between ...
0
votes
0answers
109 views

Could someone profile the space and time complexity of this Haskell snippet?

I'm new to programming and even more new to Haskell, below is a little tid-bit I wrote that operates on a bunch of lists. I am wondering if someone would be kind enough to walk through the function ...
4
votes
2answers
77 views

Is there a way to simple Haskell class instance?

I define a Pixel data with different size like this. data Pixel = RGB8 Word8 Word8 Word8 | RGBA8 Word8 Word8 Word8 Word8 | RGB16 Word16 Word16 Word16 | RGBA16 Word16 ...
1
vote
1answer
41 views

Any way to optimize or improve this python combinations/subsets functions?

I believe this is essentially the same method as the itertools.combinations function, but is there any way to make this code more more perfect in terms of speed, code size and readability : def ...
1
vote
3answers
103 views

List-flattening function in erlang feels too wordy

I wrote a tail-recursive list-flattening function, but I'm not too happy with it. a) Is tail-recursion necessary here, or will the function be optimized without it? b) It's ugly how many clauses ...
1
vote
1answer
173 views

Functional Exception handling with TryCatchFinally Statement helpers

Hello All, I wrote something to the effect of a try catch finally statement helper in functional style so that I can shorten much of the code I'm writing for my Service Layer that connects to a sql ...
2
votes
2answers
125 views

How to make this python function and its inverse more beautiful and symmetric?

I like the radix function and found a really neat way to compute it's inverse in python with a lambda and reduce (so I really feel like I'm learning my cool functional programming stuff) : def ...
6
votes
4answers
231 views

Which is considered better: push(array,value) or push(value,array)?

This is valid for many other functions. get(array,key) or get(key,array) ? find(array,value) or find(value,array) ? Etc...
2
votes
2answers
163 views

Option type in C# and implicit conversions

Good morning everyone, I have decided to create a simple Option type for C#. I have essentially based it on the Scala's Option. It was mostly a fun exercise, but it might come in handy at work or ...
1
vote
3answers
93 views

How to improve this functional python fast exponentiation routine?

I have the following python functions for exponentiation by squaring : def rep_square(b,exp): return reduce(lambda sq,i: sq + [sq[-1]*sq[-1]],xrange(len(radix(exp,2))),[b]) def ...
1
vote
1answer
45 views

How to improve this functional python trial division routine?

The following functional program factors an integer by trial division. I am not interested in improving the efficiency (but not in decreasing it either), I am interested how it can be made better or ...
1
vote
0answers
63 views

List builder in groovy

I'm a big fan of yield return found in C# and computation expressions found in F#. That is, language features which allow you to build streams of objects which are computed on demand (state-machines ...
1
vote
2answers
341 views

Flatten dictionary in Python (functional style)

I'm trying to learn how to write functional code with Python and have found some tutorials online. Please note that I know Python is not a promoter for functional programming. I just want to try it ...
2
votes
1answer
81 views

Implementing logic in a different way

I need to write a function which will do the following functionalities I would get a string and a array as input parameters The string would be like one of the following for example catalog ...
1
vote
1answer
58 views

Refactor Python code with for-loops and continue

I wrote a code that queries a data structure that contains various triples - 3-item tuples in the form subject-predicate-object. I wrote this code based on an exercise from Programming the Semantic ...
2
votes
1answer
157 views

Idiomatic Clojure? Performant & functional enough?

Problem Disclaimer: This is my first clojure function & I'm still busy finishing the last chapters of "Programming Clojure". Thanks! :) I am writing a function to randomly flood-fill parts of a ...
5
votes
2answers
251 views

Typesafe Tic-Tac-Toe API

So I've been working on Tony Morris' Tic-Tac-Toe challenge. The rules are as follows: Write an API for playing tic-tac-toe. There should be no side-effects (or variables), at all, for real. The ...
6
votes
1answer
109 views

Sundaram's sieve in Common Lisp

I have this Sundaram's sieve to generate prime numbers up to limit in Common Lisp. (defun sundaram-sieve (limit) (let ((numbers (range 3 (+ limit 1) 2)) (half (/ limit 2)) (start ...
2
votes
1answer
84 views

How to rewrite recursion in a more ruby way

def get_all_friends(uid, client) friends = client.friendships.friends(uid: uid) total = friends.total_number get_friends(total, 0, uid, client) end def get_friends(total, cursor, uid, client) ...
4
votes
2answers
84 views

Refactor when complex expression in multiple parts of list comprehension

In Python a have a nested for-loop, that populates list with elements: a = [] for i in range(1, limit+1): for j in range(1, limit+1): p = i + j + (i**2 + j**2)**0.5 if p <= ...
3
votes
1answer
123 views

Functional Sundaram's sieve

I wrote this Sundaram's sieve in Coffeescript to quickly generate prime numbers up to limit: sieve_sundaram = (limit) -> numbers = (n for n in [3...limit+1] by 2) half = limit / 2 ...
0
votes
2answers
384 views

Functional prime factor generator

I have this prime factor generator for some number n. It returns list of all prime factors. In other words, product of the list equals n. def prime_factors_generate(n): a = [] prime_list = ...
1
vote
0answers
55 views

This is a try at implementing integers in Idris. Any advice or comments are most welcome.

-- tries to implement integers using fold and a generalized notion of 'reduction' instead of -- utilizing recursion explicitly in all operations./ -- maximum (reasonable) code reuse, information ...
5
votes
0answers
127 views

Shepard Tone stream generation in Clojure

This is my work to generate an infinite Shepard Tone. It is written in Clojure and works by generating repeating streams of incrementing frequencies, converting those to values on a sine wave and then ...
5
votes
1answer
120 views

First Go at Clojure and Functional Programming in General

Here's the code... My goal is to simulate a Pile shuffle of a vector. Here's the function.. (defn pile ([cards] (pile cards 3 1)) ([cards num_piles] (pile cards num_piles 1)) ([cards ...
0
votes
1answer
50 views

What web programming skill need to do the following task? [closed]

First, I am sorry if I ask this question in an incorrect place. I built a simple program which I want to convert it to web base. I did my program in VB, but I am not sure what programming language I ...
2
votes
1answer
247 views

Scala: tail-recursive factorial

Is there anything what could be improved on this code? def factorial(n: Int, offset: Int = 1): Int = { if(n == 0) offset else factorial(n - 1, (offset * n)) } The idea is to have tail-recursive ...
2
votes
2answers
160 views

Counting Ways to Make Change — Is this good functional/Lisp style?

I have just started learning some Scheme this weekend. I recently solved a problem that goes something like: Count the number of ways possible to give out a certain amount of change using 1 5 10 25 ...
3
votes
2answers
250 views

Format names to title case

So I'm writing a program to format names to title case and display them in alphabetic order. Any advice please? And how can i add more methods? public static void main (String [] args) { ...