Questions tagged [functional-programming]

Use this tag for programs where "functional" style is important - likely making use of function composition and without side-effects. NOT simply because your code contains functions!

Filter by
Sorted by
Tagged with
5 votes
1 answer
114 views

Locate and multiply numbers in a 2D grid (Advent of Code 2023 Day 3, Part 2)

I am using the Advent of Code 2023 to study functional programming and the ranges library in C++. This code is for the second part of Day 3 and is the best solution using FB I've created. I'd ...
2 votes
2 answers
84 views

Map coloring algorithm functional implementation in python

I'm an autodidact working on map coloring algorithms with backtracking, see chapter 2 of this book. I tested this implementation with success but I'm still unsure whether it is tricking me somewhere, ...
3 votes
1 answer
97 views

Is it valid to pass struct through std::fold_left to persist state?

I am exploring functional programming design with C++. The code below uses a struct passed to ranges::fold_left for processing. The final result is returned in the ...
2 votes
1 answer
97 views

Move timestamp to the next Monday 10:01 AM [closed]

I've been battling ChatGPT for hours now and can't get satisfied with this simple algorithm in TypeScript. These two (hopefully correct) TypeScript+ReactNative solutions should move the timestamp ...
1 vote
1 answer
56 views

Random ladder for public-key encryption

...
3 votes
1 answer
90 views

Sequentially find the indexes of an element into a collection

Util class to find into a collection the indexes of a given element with multiple occurrences from the first index or relative to a given index. ...
5 votes
2 answers
141 views

Print the three most occurring chars in Java using streams

The three characters that occurs most in a given string should be printed by using streams/lambdas (functional programming). The order of characters that occur equally often should be preserved. My ...
3 votes
4 answers
1k views

Merging sorted integer arrays (without duplicates)

I have written a Python function called merge_arrays which takes two lists and returns the new list merge without duplicate. The function works as expected, but my ...
5 votes
1 answer
130 views

Partial Function Application in JavaScript

I'm trying to make a partial function from any function. Here is a working example: ...
1 vote
1 answer
101 views

Simple Curry function

This is a interview practice question from BFE.dev. Currying is a useful technique used in JavaScript applications. Please implement a curry() function, which ...
1 vote
0 answers
45 views

Functional style dependency injection to make business logic unit-testable

My project is a blogging app, which is a recruitment assignment. It is now finished and working. Following function handles updating a comment votes, very similar as on StackExchange sites, preventing ...
5 votes
2 answers
230 views

A curry function

I am new to the functional style, and I wrote a curry function to practice this new style. This curry function takes a regular function and returns the curried version of it. Currying is a technique, ...
4 votes
2 answers
165 views

Abundant number implementation

I have implementted a simple JavaScript function to find the list of abundant numbers and print them on the screen. an abundant number is a number for which the sum of its proper divisors is greater ...
2 votes
2 answers
782 views

Prime factor tree

Design a program that finds all numbers from 1 to 1000 whose prime factors, when added together, sum up to a prime number (for example, 12 has prime factors of 2, 2, and 3, which sum to 7, which is ...
3 votes
1 answer
204 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
2 answers
52 views

Imrove performance when updating DataFrame rows based on complex criteria

My question got rejected the last time so I am trying a better approach to getting a solution: ...
1 vote
1 answer
54 views

Click temporization on user interface elements

Implementation to accommodate different behaviours based on the frequency a user interface element is clicked. It supports number of clicks and number of clicks in a time frame. ...
4 votes
1 answer
144 views

My implementation of Clojure's assoc-in

This is my implementation of Clojure's assoc-in function. I am looking for tips on making it more idiomatic and in general, better. ...
2 votes
1 answer
113 views

Recursive Factorial Calculation in JavaScript

...
3 votes
1 answer
72 views

Dyalog APL dyadic operator deriving ambivalent function to pair two function results

I'm writing a small utility operator which applies two functions to an argument pair and strands the results. The pair can either be given as two arguments or as a single argument with two elements. ...
1 vote
0 answers
65 views

LeetCode 399 Evaluate Division using Scala

In order to gain familiarity with Scala I am trying to solve the following problem https://leetcode.com/problems/evaluate-division/description/ You are given an array of variable pairs equations and ...
0 votes
1 answer
36 views

Creating functions in a hierarchy where they get the same arguments

When I write code, I tend to follow functional paradigms often. The main thing I try to do is use values returned from functions. What I notice though, is I end up having a sort of function argument ...
1 vote
2 answers
279 views

Removing Elements from an Array Based on a Second Array

I've written a function that takes two arrays of integers as input and removes all the elements in the first array that are also present in the second array. I would like to share my code with you and ...
1 vote
1 answer
51 views

Optimizing the Dig Pow function

I have written a Python function to solve the Dig Pow problem, where the goal is to find a number k such that the sum of each digit of n raised to a specific and ...
4 votes
7 answers
263 views

Breaking a string expression in operator and operands

Requesting for a code review for a scala implementation. Problem Given an expression string made of numbers and operators +,-,/,* , break it in a list of Integer or ...
7 votes
5 answers
1k views

Optimizing a function to find the closest multiple of 10

I have written a Python function called closest_multiple_10() which takes an integer as input and returns the closest multiple of 10. The function works as expected,...
13 votes
3 answers
9k views

Apply a function to each element of a tuple ("map" a tuple)

C++ doesn't (yet) have a 'map' higher-order function, certainly not one which applies to a compile-time sequence of values like a tuple. We have std::transform, and ...
2 votes
0 answers
40 views

Lazy evaluation vs strict one applied to fib

I have this OCaml problem: Recall the polymorphic type lazy seen in class, the function delay which transforms a ...
1 vote
0 answers
62 views

Binary tree zipper with basic operations

I am looking for a review of my Binary Tree Zipper implementation on Ocaml. These are the defined operations: get (gets the substructure, or the trees after the focus element) bempty creates an empty ...
5 votes
1 answer
889 views

Monad object in C++

Have I defined a Monad object for C++? Below is a class called var which I created years ago to implement a lambda calculus like interpreter in C++. The class ...
4 votes
3 answers
214 views

Project Euler #2 in F#

Project Euler Problem 2 asks for the sum of all even Fibonacci numbers below 4 million. My first attempt at this problem using functional programming with F#. I would've liked to use some kind of ...
4 votes
2 answers
3k views

Download entire localStorage as file

I would like to download the entire contents from localStorage to a json file (in a "clear" formatting). I've tried this and it works perfectly. However, ...
1 vote
1 answer
71 views

Transform an array into object and set index for an unique key in javascript

I have a method that merges keys and indexes from an array into an object. I'm stuck with ways to compress this method, and I don't know what I can do to make it simpler. Goal get an array of objects ...
15 votes
1 answer
2k views

Flask view dependency injection

I've been into trying to have an as simple as possible dependency container in Python and with your help managed to conceive TinyDIC, many thanks. Now, I use Flask for my apps, and from by background ...
1 vote
1 answer
685 views

Functional Fibonacci in OCaml

I'm very new to OCaml, and as an exercise I decided to implement the nth Fibonacci algorithm (return a specific Fibonacci number, given an index) in different ways, using what I've learned so far. I ...
2 votes
0 answers
64 views

A Haskell-style "maybe" type in Python, version 2

I have decided on a preferred solution to my question A maybe type in Python. Which implementation is best?, and have expanded it to be more ergonomic. I have implemented a host of utility functions (...
1 vote
1 answer
43 views

Reactjs Component Reusability

I am working on a project in react. Where most of the data on different tabs have a table. So I design the "TableItems" component and passed the data according to the component. Right now ...
3 votes
2 answers
2k views

Bubble sort in Scala

I wanted to implement a bubble sort in Scala following the steps described in: http://www.bbc.co.uk/education/guides/z22wwmn/revision/3 ...
1 vote
1 answer
115 views

Format multiple dates to look good in text, using functional approach

I had a piece of code that solved really simple problem - it formated two dates to look better on a webpage. It looked like this: ...
-2 votes
1 answer
1k views

Create a function with two parameters: a list and a string, where the string has the following values [closed]

I tried so many things to do this task. I have no idea why doesn't it work. Please help! ...
1 vote
0 answers
51 views

Reusable and testable JavaScript code for URL fragment-based tab navigation logic

Foreword As a personal project, I've been creating a SPA blog (available here), which features 4 tabs (Thoughts, Projects, Readings, and CV), each accessible via one of four radio buttons, and several ...
2 votes
1 answer
2k views

Find common characters (LeetCode)

The task is taken from leetcode Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). ...
5 votes
0 answers
149 views

Hutton cipher implemented using recursive functions

Is there a way to rewrite these last 2 recursive functions with fewer arguments (preferably 2) while keeping them recursive? The entire code can be seen here: https://github.com/GirkovArpa/hutton-...
1 vote
1 answer
72 views

Meta Collatz Sequence

Context Chapter 6 of Learn You A Haskell contains a function called chain, which outputs the Collatz sequence of a given input. (In short, it takes a natural number....
1 vote
2 answers
197 views

Simplify a function which performs manipulation of data in Javascript

Upon suggestion in the original question within Stack Overflow, I'm bringing my question here. We have created a function that will convert the data into the desired format, but it has a lot of code ...
2 votes
2 answers
426 views

JavaScript -- Convert decimal to another base

The Source code is maintained on GitHub and may be cloned via the following commands. A Live demo is hosted online, thanks to GitHub Pages. ...
2 votes
1 answer
117 views

Project Euler 11 Haskell - Largest product in a grid

Intro I have been learning haskell and functional programming using random Project Euler problems. Currently, I have solved Problem 11. What is the greatest product of four adjacent numbers in the ...
3 votes
2 answers
579 views

MP3 Playlist Class in Python

Background Info I am an intermediate level Python programmer. This is my implementation of a MP3 Playlist class challenge that was hosted on 101 Computing.Net. The ...
2 votes
1 answer
178 views

Commented Parser Combinators in Lisp-style C

I've attempted to remedy the issues outlined in the answer to my previous question. I've added several hundred blank lines to better illustrate the grouping of functions and generally make things look ...
2 votes
1 answer
144 views

Parser Combinators in C redux

A few more rewrites down the line from my previous question, this is the C version 11 based on the PostScript prototype version 12. The PostScript version is shorter, but it's some crazy ass ...

1
2 3 4 5
21