Questions tagged [functional-programming]

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 program state. In this style of programming, side effects and mutable data are deprecated.

Filter by
Sorted by
Tagged with
1
vote
1answer
217 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 ...
2
votes
1answer
76 views

Most concise Python radix function using functional constructions?

The problem with computing a radix using a non-imperative style of programming is you need to calculate a list of successive quotients, each new entry being the quotient of the last entry divided by ...
13
votes
2answers
26k 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
100 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 ...
3
votes
1answer
118 views

Querying a data structure that contains various triples

I wrote 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 Web ...
3
votes
1answer
397 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
690 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 ...
7
votes
1answer
319 views

Sundaram's sieve in Common Lisp

I have this Sundaram's sieve to generate prime numbers up to limit in Common Lisp. ...
1
vote
1answer
128 views
3
votes
1answer
347 views

Functional Sundaram's sieve

I wrote this Sundaram's sieve in Coffeescript to quickly generate prime numbers up to limit: ...
3
votes
1answer
99 views

Function to find factors of a number, optionally including 1 and the number itself

I have a function for finding factors of a number in Python ...
1
vote
2answers
6k 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 ...
1
vote
0answers
328 views

Integer implementation in Idris

This is a try at implementing integers in Idris. Any advice or comments are most welcome. ...
1
vote
1answer
91 views

mapping & loops - counting up or counting down

I am building a time tracking application and my requirement says that any hours past 40 total should be counted as overtime and not regular hours. Which function is more clear? What can be done to ...
5
votes
1answer
179 views

Pile shuffle of a vector

My goal is to simulate a Pile shuffle of a vector. It takes 2 optional arguments for the number of piles to use and how many times to perform the shuffle. As this is my first attempt at clojure code, ...
4
votes
3answers
958 views

Cleaner way for coding a repetitive application of a function

I have this function definition which takes an r and applies the function f n times: ...
5
votes
1answer
3k views

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)) } ...
2
votes
2answers
1k 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: ...
3
votes
2answers
1k 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? ...
3
votes
1answer
273 views

Filling gaps in time series data

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 ...
15
votes
4answers
2k views

How to shorten this terrible HTTP header parser?

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 ...
7
votes
3answers
455 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 ...
4
votes
1answer
808 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
2k 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 ...
4
votes
2answers
825 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. ...
6
votes
3answers
2k views

A safer way to cut a string

I want to get just the first line of a big string. Currently, here's how I do it: ...
2
votes
2answers
200 views

Safe composition in python

...
1
vote
1answer
604 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 be ...
5
votes
2answers
290 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 ...
4
votes
1answer
196 views

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
214 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? ...
10
votes
2answers
596 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 ...
11
votes
1answer
368 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 ...
7
votes
1answer
732 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 ...
2
votes
1answer
977 views

Defining transpose on a collection of irregular collections

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

Replacing Python Classes with Modules [closed]

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 ...
4
votes
2answers
1k views

How do I dynamically create Python unit tests in a readable way?

I have Django unit tests that are pretty much the following format: ...
12
votes
1answer
1k views

Responsive/adaptive website code

I have all these functions that work together to create functionality on a page. Is the structure of these functions OK? Can I do anything to speed this up or make my code better? I'm not exactly ...
3
votes
2answers
159 views

Attempting to eliminate var (and imperative style) from my Piece class

I've been cooking with gas since I got Daniel C Sobral's help on my last question. I am now re-reading Odersky's "Programming in Scala, 2nd Edition" (finished my first reading about this time last ...
7
votes
1answer
925 views

F# Djikstras shortest path implementation

I'm just getting my feet wet with F#, by implementing some simple algorithms. First up: Djikstras shortest path. There is one piece I've written in this code which confuses me as to why it works: the ...
3
votes
1answer
1k views

Basic neural network

I just programmed a basic neural network in F# to learn the logical OR function. As I am very new to F# and especially functional programming, I did it the imperative way. And even tho it works, I ...
2
votes
1answer
128 views

Ideal FP/Scala way to vaildate rectangular list

I am learning Scala and FP (Functional Programming), coming from Java, OO and a strong imperative pardigm. I am now trying to implement a small puzzle solving project so I can get some deeper hands on ...
4
votes
1answer
669 views

Simple Haskell key value file store

As an exercise in learning Haskell, I implemented a simple key value store, where you can put and get values (as ByteStrings). (For reference this is inspired by ...
5
votes
3answers
1k views

Design pattern for adapter

I wrote a framework and I want to hear the thoughts of other on the design patterns it uses. Every method contains three design patterns - adapters(strategy), intercepting filters, and observers. A ...
4
votes
1answer
498 views

Project Euler question 2 in CoffeeScript

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. ...
21
votes
1answer
4k views

Java monad implementation

I'm learning functional programming and their concept of Monads. I've found nothing more effective in learning than writing an implementation in a programming language I have experience with. I came ...
8
votes
4answers
626 views

Printing a factorial from an integer

I am very new to functional programming. I wrote this program where the user can enter an integer and it will print the factorial. If the user enters a negative integer or not an integer at all, it'll ...
7
votes
4answers
762 views

Finding the sum of all the multiples of 3 or 5 below 1000, using list comprehension

I'm trying to compare my own implementation with another solution of Project Euler problem #1, which uses a list comprehension: ...
1
vote
1answer
2k views

Extending string mapping

Basically I'm trying to generalise and extend the notion of mapping one string into another. There are two methods I often find myself using for this: Functions, and Dictionairies. So here are my 3 ...
5
votes
3answers
379 views

Finding the first stream of non-repeating elements in Scala (without recursion or side-effects)

Here are some examples: [1, 2, 3, 4, 5] => [1, 2, 3, 4, 5] [10, 15, 10, 15, 30] => [10, 15] [1, 2, 3, 4, 1, 5, 6, 7] => [1, 2, 3, 4] Here's my best (and ...