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.
0
votes
0answers
14 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 ...
0
votes
0answers
9 views
MergeSort F# Program
I have written the following program to implement MergeSort 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).
let ...
1
vote
2answers
19 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
35 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 ...
3
votes
4answers
62 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
70 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 ...
5
votes
1answer
62 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
52 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
99 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
73 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
129 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
55 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
246 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
102 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>
}
...
6
votes
1answer
112 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
238 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
72 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
52 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
166 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
308 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
56 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
138 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 ...
4
votes
1answer
101 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
55 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
93 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
175 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
63 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
100 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
74 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
118 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
190 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 ...
3
votes
3answers
56 views
GetCommonBaseClass: Readabiliy and Functionality
I have two pieces of they which do almost the same thing. They both return the most common base class of two types. They differ in two aspect:
If one of the arguments is null, code A returns null ...
1
vote
1answer
103 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 ...
4
votes
1answer
248 views
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 ...
1
vote
2answers
47 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
274 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
107 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
548 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
55 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
122 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 ...
1
vote
1answer
170 views
Determining function return types for std::function
I'm using the boost::units library (boost::units) and trying to make use of auto and decltype to deal with all the complicated types it produces.
I have a templated function to integrate a ...
4
votes
2answers
85 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
59 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
158 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
269 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
178 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
237 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
248 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
168 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
63 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 ...