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
7
votes
2answers
707 views

Simple converter: Is it better to use a function within a function?

Below is an example of a simple converter. In the first example temperature I have used a function within a function. In the second example ...
2
votes
0answers
21 views

Finite automaton library in Scala including subset construction and minimization

I built a scanner generator like flex or jflex in Scala for a class last year. Part of this project is a small library for handling and manipulating finite automata. It works completely as intended, ...
-1
votes
1answer
45 views

Functional filter unique elements in sequence is not as good as imperative [closed]

Can you please help me fulfill the following? Requirements: Create an iterable with unique elements. Their order should be preserved: a | b | a | c → a | b | c ...
10
votes
1answer
60 views

C-style `switch` statement implementation using decorators

Yes I know context managers are a much better way to do this, but I spent four hours working on implementing a switch construct using generic decorators, and I'd like to hear feedback on how I did. I'...
1
vote
0answers
64 views

Cartesian product in racket (Is append-map okay?)

I've read several places that say it's really bad to use append, and I understand why. I read somewhere that it was never even correct to use ...
7
votes
2answers
320 views

Chaining statistics in a dice queue

I've previously came up with the question "Rolling dice in a method chain" and was interested if this could be improved. It could, so now I have this class: ...
8
votes
3answers
730 views

Rolling dice in a method chain

I've been writing code for nearly 40 years now and am still not too old to learn and understand new things. Right now, my focus is a bit of OO with functional programming combined and C# with Linq is ...
3
votes
0answers
23 views

How would you convert this code to best take advantage of CSS transform for panning/zooming?

I am using react-flow-chart for a job project, but it doesn't implement zoom, and I would like to take the reigns and try applying it myself. I took a linear ...
12
votes
2answers
1k views

Given an array A[] and a number x, check for pair in A[] with sum as x

LINQ/FP style: Assert.IsTrue(new[] {1,2,3}.AnyPairSum(4)); Where: ...
4
votes
0answers
94 views

Implement Sudoku using LINQ

WiKi and GitHub. Sudoku rules are being checked with the following tests: ...
3
votes
0answers
70 views

Simple queue on F#

...
4
votes
2answers
59 views

User defined function call on a stack based interpreter for concatenative language

I would like to receive some advice on how to improve a small concatenative stack-based interpreter, executing a joy-like programming language. It is really minimal, and is in fact a subset of a ...
1
vote
1answer
47 views

Group items by types: replace nested for loops with reduce

I have a working code that groups items by their type values. The types can be one or multiple, however they are always returned as an array. To illustrate: if an item has types with values ...
4
votes
1answer
63 views

Paginate IEnumerable<T> sequence

I used the following to split sequence into batches: ...
7
votes
1answer
299 views

String Format object extension

I wrote an extension to create a string-representation by an object: ...
5
votes
1answer
93 views

Timeout Error in Fraudulent Activity Notification HackerRank

I am solving this problem: Fraudulent Activity Notifications on HackerRank. I am done with my code and is working, but it is inefficient as well for very large inputs. I don't know but after all ...
2
votes
0answers
25 views

Reorder items in a map

I'm attempting to create a Module, I've called it Timeline, that resembles a drag n' drop list. Given a Timeline, someone could continually add items (I'm not including removing or updating Items here)...
3
votes
0answers
49 views

Node Based Code Flow Builder

Background In my development of a game engine, I realized the need to be able to construct processing units in a serializable way (code below does not show serialization, just the wiring). I broke ...
4
votes
2answers
65 views

Reducing a list of points into a list of pairs of points

I have some data… let points = [(718, 620), (4596, 1280), (410, 333), (4597, 993), (410, 337), (4597, 996), (428, 337), (4597, 1000), (431, 335), (4599, 1044)] …...
8
votes
2answers
178 views

c++17 compatible std::bind_front alternative

One of the things that has been really exciting me in c++20 is std::bind_front. Using placeholders with std::bind and ...
1
vote
1answer
38 views

Join a JSON object of arrays and reduce down to one key [closed]

Given that I have a dataset as follows: { 1: ['Extremely Tall'], 2: ['Tall'], 3: ['Medium', 'Low'], 4: ['Dwarf'] } and I receive a value (e.g. Tall or Medium) I ...
6
votes
3answers
163 views

Generic method to call API functions with simple retrial on errors

I have to develop a small security management application that relies on a Web API. Based on that API, I have generated the REST client classes (Open API generator), but I still have to code on top to ...
5
votes
1answer
300 views

Make utility using LINQ

Make() takes projects with a list of tuples representing project references in the form of (Dependency, Dependent) and returns ...
2
votes
0answers
82 views

Kaleidoscope Effect App

I've written a simple app that draws with a kaleidoscope effect in javascript, could someone please review the code? I'm especially interested in functional practices and design patterns. ...
2
votes
1answer
59 views

Functional n-queens

Following the book "Structure and interpretation of computer programs" I have tried to implement a functional solution to the problem of N-queens (implemented by the function ...
4
votes
1answer
100 views

Function composition in C++

My aim was to create a function which behaves like the operator . in Haskell. Here is what I made: ...
2
votes
1answer
53 views

Emulating the pipe operator in javascript in a readable way

In this article I found this quite elegant looking Elixir code: ...
3
votes
1answer
72 views

“Real world” JSON transformation using Node.js

Background I transformed a non-trivial JSON response that uses arrays into a nested object eliminating some of the original properties inserting a new summary property that is a unique list of ...
1
vote
1answer
101 views

Constructor function for persons with hobbies

I'm trying to write a constructor function that generates instances of a user. One of the properties of the user class is an array of the user's hobbies. I'd like ...
1
vote
0answers
77 views

Building maps for Dgraph integration tests in Java 8

I want to refactor this in Java to reduce my code duplication. ...
4
votes
1answer
66 views

Finding optimal mapping patterns that transform relational data into a linear format

I'm trying to upgrade my javascript to be more functional. I have used function composition and curry successful on less complicated code but i'm now running into a point where i'm not sure on how to ...
2
votes
3answers
97 views

BST traverse as higher order function [closed]

Consider the following function to traverse a BST ...
1
vote
2answers
108 views

Functional / Declarative FizzBuzz

I spent some time today trying to write FizzBuzz in a functional/declarative style. I thought it would be a good chance to get some feedback on it since I've been doing functional programming for ...
0
votes
1answer
61 views

Provide functional-programming style solution for 2D Array - DS challenge

Problem Statement: Given a 6×6 2D Array, arr: 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 We define an hourglass in A to be ...
15
votes
2answers
2k views

Message-formatting code with pluralization, revised to be more functional

I'm a C++ dev, and I've recently started working my way through Clean Code*. Whenever I encounter an example I think I could improve, I try to re-implement it in C++. On pp. 28-29 there is an example ...
2
votes
1answer
59 views

Toggling the background color of a page using JavaScript closures

This question is to see if I can get some input on the "design pattern" I tried to implement here. I'm just learning about closure in JavaScript and I think I'm starting to get it. I'm wondering if ...
8
votes
2answers
252 views

Function invocation every second time

Recently I got test task at the project, it is fairly simple: ...
3
votes
2answers
95 views

Count character 'a' in first n characters of indefinitely repeating string s

Problem statement: Lilah has a string, s, of lowercase English letters that she repeated infinitely many times. Given an integer, n, find and print the number of letter a's in the first n ...
4
votes
2answers
104 views

Counting valleys traversed below sea level, given elevation changes

Problem Statement: Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly steps. For every step ...
4
votes
3answers
107 views

Functional CSV Parser

I've been starting to migrate some C++ code from imperative to more of a functional style. I'm new to functional programming so I'm trying to get a sense of how well this code fits into the functional ...
12
votes
2answers
244 views

Emulation of SE's text input control for tags

Introduction I used React to write a component, which is meant to be a faithful emulation of the input for "Tags" which you see at the top of every question/topic on SE. Why this project? If you're ...
2
votes
1answer
70 views

JSON Parser, pulls data from a JSON file for analysis

This is the first time I have programmed something that I will actually personally use, so I am happy about that. I tried to write good code whilst applying functional programming concepts. So no ...
5
votes
1answer
84 views

Currying in Lua

Just whipped up this quick curry function in Lua, as a building block for partial application and memoization. Any feedback is appreciated. It takes two arguments; ...
3
votes
1answer
78 views

Pre-calculate the winning combinations for an n-sized tic tac toe board using C++ functional programming

This code pre-calculates the winning combinations for an n-sized tic tac toe board. I first created my function using an imperative approach. This is just how I naturally write code most of the time....
-2
votes
1answer
41 views

A game that deals a high card to one of two players [closed]

So for my class, I have to design a game that will deal out one card to for each player. I also need to use functions to accomplish this one for the high card game, and another for the face value of ...
2
votes
0answers
59 views

A tiny library implementing the Elm Architecture in C++

Elm is a pure functional language for the front-end. It enforces an architecture that allows programs to stay pure in an event-based setting. This simple header-only library implements a variant of ...
4
votes
1answer
74 views

Binary Trie in F#

I have this little exercise writing a binary trie in F#. In C# it can be done in few lines, but here it became more complicated than I had expected (especially the ...
8
votes
3answers
418 views

Story-based adventure with functions and relationships

This is a story-based adventure program I coded by myself (forgive the length). It took a long time but it was fun. It includes loading times, changes in relationships, relationship levels, storyline ...
2
votes
2answers
45 views

Seeking the seeds for writing tidy event handlers in JS

I'm seeking a critique of the simple program below, which displays two counters and increments them through a button, using D3.js. In real life of course no one has a need for displaying buttons that ...
4
votes
1answer
61 views

Print a Connect 4 grid based on some rules for each cell

In a game of Connect4: we start with an empty grid two players place pieces X and O on the grid the first player to achieve 4 pieces in a line wins! this is a text based console game Here is the ...