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.
2
votes
1answer
31 views
Loop filling a set with logging
After watching Raymond Hettingers talk Transforming Code into Beautiful, Idiomatic Python I got back to a function I wrote.
I'm not quite sure how to make it more pythonic but I think this might be a ...
5
votes
1answer
37 views
Monadic Immutable Linked List in the Least Functional Language Evar
I've written a List monad as an example for a related question. This is a rather frustrating experience as I wanted to use Java, which (as of Java 7) still lacks lambda expressions and lacks ...
3
votes
0answers
14 views
Review of genetic algorithm code in Erlang
I've written an Erlang implementation of the genetic algorithm for a hello world program described here: http://www.puremango.co.uk/2010/12/genetic-algorithm-for-hello-world/
This is my first time ...
4
votes
0answers
25 views
Conversion from decimal to negabinary and vice versa in Haskell
Recently I set about to writing conversion functions in Haskell between decimal and negabinary. I tried to write them in as much functional a style as possible, also aiming at brevity. Below I present ...
3
votes
3answers
121 views
Better way to write this code in functional manner using map and reduce?
I have an array of items on which I have to perform 2 tasks:
Applying a function on the array item
Checking if the value is true or false.
The functional approach to solving this would be
var ...
4
votes
1answer
37 views
Good OCaml style
I wanted to write a small but non-trivial function in OCaml to test my understanding. I have only a basic understanding of the libraries, and no idea at all of what is considered good style. The ...
5
votes
1answer
68 views
Read text from stream char by char
I've started learning F# and functional programming in general, but the code I wrote doesn't seems to be really functional. Could you take a look and say how to make it more as it should be in ...
6
votes
2answers
110 views
Split list into sub lists - Functional
Here is my functional approach to split a list into sub lists, each having nondecreasing numbers, so the input
[1; 2; 3; 2; 4; 1; 5;]
computes to
[[1; 2; 3]; [2; 4]; [1; 5]]
The code works, but ...
2
votes
1answer
70 views
Filtering a dictionary by subject of definitions
I am writing a function to filter a "dictionary" by the "subject" of its definitions.
The dictionary data structure is a hash-map of this kind:
{ "word1" {:foo :bar
:definitions ...
9
votes
1answer
94 views
(How) should I avoid writing procedural code in Haskell?
I'm trying to implement a left leaning red black tree as described here. This is their snippet for insert
private Node insert(Node h, Key key, Value value) {
if (h == null) return new Node(key, ...
6
votes
1answer
48 views
Reading input from the console in F#
In the process of learning F#, I wrote this code that reads lines of input from the console and stores them in a list.
As far as I can tell the code works correctly, but since I'm new to functional ...
3
votes
0answers
28 views
Elixir pipes and anonymous functions
I recently got started with Elixir. I'm used to F#'s pipes, and Seq.map and LINQ's .Select statements. Things are different in Elixir, and the code I have seems very ugly. Anon functions in anon ...
1
vote
1answer
53 views
Functional Re-Write in Scala: Rows of Strings with a Max Width
I came across an issue the other day where I really just could not think of a functional solution. So I fell back on my imperative programming skills. Since a functional solution still eludes me, I ...
2
votes
1answer
60 views
Centroid of a polygon in Clojure
I am studying clojure and functional programming.
I wrote this function to compute the centroid of a polygon specified as a vector of points, e.g. [[10 20] [0 11] [50 30]]).
Here you can read how to ...
5
votes
1answer
111 views
Code with many “early returns (exits)” into the functional style
I have some Scala code that uses Internet to authorize a user. Therefore, it can throw Exceptions like IOException in the method.
The original code was written in Java. So, yes, it uses variables, ...
6
votes
1answer
607 views
Clojure Neural Network
After reading this article about Neural Networks I was inspired to write my own implementation that allows for more than one hidden layer.
I am interested in how to make this code more idiomatic - ...
1
vote
2answers
115 views
“Hell Difficulty” Haskell Fast & Hard
I'm a complete neophyte to Haskell with only a smidge of FP experience – I did some Racket programming academically and have written some semi-FP JavaScript professionally, but now I'm trying to learn ...
1
vote
0answers
70 views
Review my F# Red Black Tree Implementation [closed]
I have written this implementation for red black tree. I also have some helper methods to verify whether the generated tree is really balanced or not.
Looks good to me.... but I find that most of the ...
3
votes
1answer
70 views
Hangman game background image inefficient?
I'm making a Hangman game and it seems that my code doesn't provide me much freedom with using layouts. I added an image to my JFrame then I added a JPanel to my image which I'm using for all the ...
1
vote
1answer
83 views
Review my F# QuickSort Program
OK... last sort of the day and probably the most fun. F# is really awesome.
let rec quickSort list =
match list with
| [] -> []
| [x] -> [x]
| hd :: _ ->
quickSort ...
1
vote
1answer
55 views
MergeSort F# Program
I have written the following program to implement merge sort in F#.
Can you please review this and let me know how can I write this in the most functional way (also most efficient and concise)?
In ...
1
vote
2answers
36 views
Review my F# BubbleSort Program
I have written this code for BubbleSort in F#. Can you review and let me know how I can do this in most functional way.
let rec getHighest list =
match list with
| [x] -> x
| [x; y] ...
2
votes
1answer
58 views
Using types in F#
I've played with F# for over a year, but I've only recently begun tentatively using it for professional work. I'm concerned that my c-style OOP principles are blinding me to the advantages of a ...
4
votes
3answers
86 views
Improving F# conditional assignment with match expression
In my code I declared a dictionary to store various counters
let counters = Dictionary<Type, int>()
Later, I need to generate several labels using these counters, like this:
First type a ...
4
votes
1answer
94 views
Better or more idiomatic way to apply a sequence of functions in Haskell
This is one of my Haskell solutions to a variation of the N-Queens problem, where the queens can move like knights in addition to their normal moves. It has been optimised somewhat to avoid ...
6
votes
1answer
83 views
Monad Transformers in C#
I am working on using monad transformers in C#. I would like to know if the following code I present, shows that I have understood this. I am fairly new to this so any feedback / comments are really ...
4
votes
1answer
63 views
is there a more functionally idiomatic way of generating valid dates in f#?
open System
type Date = System.DateTime
type SafeDate =
| WorkingDate of Date
| InvalidDate of Exception
let dateAttempt (x: int, y:int, z:int) =
try
let returnDate = ...
5
votes
1answer
123 views
Generic pure functions memoization
I like to create tools for memoization since it can sometimes be useful. I recently created a generic tool for memoizing pure functions results. Here is an example og how it works:
// Dummy function
...
0
votes
0answers
82 views
Optimizing my Quadratic Sieve, with advice on style?
I'm still new to Haskell, and I'd like others' opinions on optimizing my basic quadratic sieve, in addition to feedback on the code's clarity and functional style.
The qs function currently runs out ...
3
votes
2answers
175 views
Function that builds dictionary based on lambda params
I've written a method in c# that allows me to do create a dictionary from a passed in object and N lambda expressions that reference that objects properties and methods. It's working the way I want it ...
4
votes
1answer
101 views
Functional linked list
I'm looking for feedback in general: code correctness, best practices, design patterns; everything you think about this. Is it bad code? Where can it be improved?
I've been implementing functional ...
3
votes
1answer
256 views
Coding Functional style taking longer time than its Imperative style
I wanted to try out F# so I decided to I converted a library file from c# to f#. I have done that successfully(thanks a you people in stackoverflow). At first I ported the code from c# to f#. Than I ...
1
vote
2answers
104 views
More functional approach
I wonder if there is a more "functional" way of implementing something like this:
@months.zipWithIndex.foreach { case (month, index) =>
if (index%3 == 0) {
<tr>
}
...
7
votes
2answers
223 views
“Zip-like” functionality with C++11's range-based for-loop
My goal was to get the following code to work:
#include<iostream>
#include<vector>
#include<list>
#include<algorithm>
#include<array>
#include"functional.hpp"
int main( ...
3
votes
2answers
250 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
1answer
83 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
72 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
177 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.
...
6
votes
3answers
324 views
Refactoring to functional style C#
Hi I'm trying to improve my functional skills and seeing where it fits and where it might not. Please review the code and see where functional practices might be applied, I'm specifically looking at ...
0
votes
0answers
63 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 ...
3
votes
2answers
149 views
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 ...
5
votes
1answer
118 views
helper method returning a call to itself
I wanted to eliminate some duplication and came up with something I've not seen so I figured I post it and see if others had.
Using a delegate to allow the method to return another call to itself.
...
1
vote
1answer
61 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
113 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
192 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
70 views
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 anonymous ...
3
votes
3answers
103 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
80 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
124 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 ...
5
votes
1answer
220 views
Immutable C++ stack - thoughts and 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 ...