Tagged Questions

Functional programming is a programming paradigm which primarily uses functions as means for building abstractions and expressing computations that comprise a computer program.

learn more… | top users | synonyms

2
votes
1answer
34 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
30 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
31 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
1answer
62 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
88 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 ...
0
votes
0answers
11 views

Calling functions passed as arguments in Lua [migrated]

I have this code Option = { } function Option.nothing( ) local self = { isNone = true, isSome = false } function self:orElse( alt ) return alt end function self:map( f ) return ...
2
votes
2answers
74 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 ...
5
votes
4answers
194 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...
1
vote
2answers
105 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
55 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
31 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
32 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
81 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
73 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
49 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 ...
1
vote
0answers
79 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
176 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
80 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
69 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
76 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
108 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
150 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
42 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
96 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
104 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
126 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
98 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
156 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
95 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
178 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
174 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
185 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
131 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
71 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 ...
5
votes
3answers
204 views

Scala: a safer way to cut string

I want to get just the first line of a big string. Currently, here's how I do it: def getFirstParagraph(txt: String) = { val newLineIdx = txt.indexOf("\n") match { case i: Int if i > 0 ...
1
vote
2answers
100 views

Safe composition in python

def json_response(response): assert response.code == 200, 'bad http response' return json.loads(response.body) def custom_json_response(response): response = json_response(response) ...
1
vote
1answer
113 views

Solution to 99 lisp problems: P08 with functional javascript

Solution to 99 lisp problems: P08, with functional javascript If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not ...
5
votes
2answers
168 views

Change code to use functional style

I tried to solve a programming contest problem in Scala, but I have a feeling, that it could be done in a more functional way. I compared it with the solution written in imperative style and it is not ...
3
votes
1answer
69 views

Need feedback on my first Common Lisp vector math code

I'm studying Common Lisp on my own. Coming from C++ Common Lisp feels kind of strange sometimes. I'd like to get some feedback on what I understood so far. For learning purposes I wrote this simple ...
1
vote
1answer
57 views

Optimize this Scheme-written chess engine module

I'd like to know how to optimize this by shadowing symbols 'lo and 'hi inside nested function f. I guess CPS conversion would solve this but how? ;; chess engine - early development ;; Alist: (cons ...
6
votes
2answers
111 views

Naive Bayes classifier

I just started learning Haskell, and I must say it is unlike anything else. As my first not-entirely-trivial piece of code I tried to write a simple Bayes Classifier. It is in two parts, a classifier ...
3
votes
0answers
63 views

Concatenative PostScript library

As a part of picking up concatenative programming, I decided to implement the common concatenative operations in PostScript. Here is my attempt at implementing some of the words in other concatenative ...
6
votes
1answer
329 views

Clojure TicTacToe (Logic, no Gui)

I whipped this up last night and was wondering if anyone had any thoughts/comments on it; for example, on: Code organisation Coding style Other improvements Semantic errors All feedback is ...
1
vote
0answers
59 views

Defining transpose on a collection of irregular collections

I was asked to submit my request for code review on http://stackoverflow.com/questions/10672046/defining-transpose-on-a-collection-of-irregular-collections here. This is a follow-up to the post I ...
10
votes
2answers
296 views

Replacing Python Classes with Modules

I try to avoid using classes in Python as much as possible; if I don't plan on building on it, I don't build it in the first place. It helps me avoid Java-like classes like FileDownloader(), when I ...
4
votes
1answer
151 views

How to dynamically create Python unit tests in a readable way

I have django unittests that are pretty much the following format: class Tests(unittest.TestCase): def check(self, i, j): self.assertNotEquals(0, i-j) for i in xrange(1, 4): for j in ...
5
votes
1answer
296 views

Optimizing and consolidating a big jQuery function

I understand this is a very vague question and a lot of code to look over. My question is basically: I have all these functions that work together to create functionality on a page: is the structure ...

1 2