Functional programming is a programming paradigm which primarily uses functions as means for building abstractions and expressing computations that comprise a computer program.
2
votes
1answer
99 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
0answers
15 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
2answers
69 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
50 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
89 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 ...
2
votes
0answers
99 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
55 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
52 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
40 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
183 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
99 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
140 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
34 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
101 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
71 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
36 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 ...
0
votes
1answer
36 views
Generalize subsets function for any order subset, concisest way possible? [closed]
Implementing a toy Apriori algorithm for a small-data association rule mine, I have a need for a function to return all subsets.
The length of the subsets is given by parameter i. I need to ...
1
vote
3answers
86 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
131 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
108 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
230 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
137 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
74 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
34 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
48 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
173 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
78 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
55 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
131 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
221 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
97 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
81 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
81 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
113 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
283 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
50 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 ...
4
votes
0answers
120 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
112 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
171 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
1answer
132 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
212 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)
{
...
3
votes
1answer
103 views
Help make nested folds look less terse
My Haskell set/map/list folds often become terse and difficult to read. I'm looking for tips on how to make my functional code easier to follow.
I'm working around a bug/feature in a plotting ...
10
votes
4answers
1k views
How to shorten this terrible code?
I am trying to read a line ending in \r\n from a Handle. This is an HTTP header.
I’m fairly new to functional programming, so I tend to use a lot of case expressions and stuff. This makes the code ...
6
votes
3answers
189 views
Overusing JavaScript closures?
I've finally gotten around to learning Lisp/functional programming. However, what I've noticed is that I'm trying to bring ideas back into JavaScript.
Example
Before
var myPlacemark,
...
3
votes
1answer
225 views
Connect Four: Bitboard checking algorithm
I'm rather new to Clojure and so I decided to program a Connect Four for fun and learning.
The code below is a Clojure implementation of this bitboard algorithm.
The whole code can be found here: ...
2
votes
2answers
222 views
simple stupid F# async telnet client
Did I write this code to correctly be tail call optimized? Am I forcing computations more than I need with !? Did I generally write this asynchronously correctly to allow sends and receives to occur ...
4
votes
2answers
147 views
Writing an infinitely running( while(true) { } ) user input function in haskell
I'm trying to implement a lexer in Haskell. For easy console input and output, I've used an intermediate data type Transition Table.
type TransitionTable = [(Int, Transitions String Int)]
type ...
2
votes
1answer
92 views
Calculate FFT from windowed time-based signal
I have a time-based signal (Raw signal) sampled at 6 MHz and need to analyze it in freq. domain.
I'm learning DSP and this is very first time I work with DSP. Could you please help to check if this ...
2
votes
2answers
63 views
Looking for a functional collapse/group-by of this data in ruby
Can anyone come up with a functional (no mutating variables) way in ruby to do the following?
Given this sorted data:
data = [{1 => "A"},
{2 => "B"},
{3 => "B"},
{4 ...

