Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

1
vote
2answers
29 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
32 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 ...
8
votes
1answer
111 views

Lazy, Functional Parser Combinators

Parser Combinators are the amazingly elegant way to write parsers that has evolved over on the functional programming side of the world, but have been less available or accessible for imperative ...
0
votes
0answers
16 views

Create a template literal tag to remove whitespace from a string

I'm just getting into functional programming (as in, just started reading the book) and as practice I'm trying to refactor a past answer I wrote to fit in line with the functional style. The ...
6
votes
3answers
2k views

GCD of two numbers in Ruby

I am trying to implement a algorithm for the GCD of two numbers in a functional approach. Can this be improved further, also with regards to performance? ...
4
votes
1answer
87 views

Return whether a string is a palindrome if you can delete at most k elements

The task: Given a string which we can delete at most k, return whether you can make a palindrome. For example, given 'waterrfetawx' and a k of 2, you could delete f and x to get '...
2
votes
0answers
57 views

Async in Railway Oriented Programming in F#

In F# the Railway Oriented Programming pattern can be implemented synchronously using the Result<'t,'terr> type as result value for functions, so they can be ...
1
vote
1answer
20 views

Two-Part Hurdle Model Simulation with Bootstrapping

I am running a simulation study where I am using two-part hurdle modeling on different effect sizes. I tried to stream-line my code for the best optimization including foreach loops and ...
4
votes
0answers
17 views

Typescript monad for data being loaded

I have been struggling to make typescript happy about the types. I am pulling data from our backend API and I wanted to give context to the data. Basically it is a monad with 4 shapes: Initial (...
-1
votes
1answer
37 views

Perform KS-test on Random Number Generator [on hold]

I've just started practicing Functional Programming in Scala in some days. This is the code I used to perform KS-test on a random number generator. ...
6
votes
4answers
929 views

Find all letter Combinations of a Phone Number

The task is taken from LeetCode Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just ...
0
votes
1answer
53 views

Is it possible to DRY these 2 methods of a tailrec Node Tree?

I have a Tree structure composed by Node class. I have to @tailrec method that are visiting up to the root. Those 2 methods ...
0
votes
0answers
35 views

Given time intervals determine if a person could attend all meetings

The task is taken from LeetCode Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
3
votes
2answers
47 views

Functional Programming Hangman Practice

Recently, I have been trying to improve my functional programming skills and understanding. While at work today, my coworkers and I were playing Hangman in a group chat. I thought of a program that ...
1
vote
0answers
34 views

Merge k Sorted Lists

The task is taken from LeetCode Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: ...
4
votes
0answers
66 views

Merge Intervals in JavaScript

This task is taken from Leetcode - Given a collection of intervals, merge all overlapping intervals. Example 1: ...
8
votes
1answer
209 views

Cookie Clicker Switches

In Cookie Clicker there are Golden cookies, and Wrath Golden Cookies. Golden cookies are Clickable cookies that randomly appear, and when clicked they give Bonus cookies, or temporary boosts to ...
5
votes
1answer
79 views

Advent Of Code 2017 Day 4 (part 2) in Functional Programming (FP)

I wanted to practice functional programming (FP) without using any library but using vanilla JavaScript only. So I took a problem from Advent of Code (the 2nd part of Day 4) You can only access the ...
4
votes
1answer
26k views

Passing a generic function as parameter

Ran into this idea and I'm curious if it's possible. I'm trying to make a single function that can test ALL of my sorting algorithms dynamically. I want to be able to pass the sorting function as a ...
7
votes
2answers
138 views

Simple function that simulates survey results based on sample size and probability

What is this: This is a simple function, part of a basic Monte Carlo simulation. It takes sample size and probability as parameters. It returns the simulation result (positive answers) plus the input ...
0
votes
0answers
60 views

Rewrite a Haskell function to be total

I've written this function to give the neighbours of a vertex in a graph: ...
1
vote
1answer
85 views

Manipulating arrays to extract unique objects and count occurrences

The data describes cycling activities (key1) and chunks of them (key2). In total there are around 1,000 ...
4
votes
0answers
54 views

Given a string containing just parentheses, determine if the input string is valid

The task is taken from leetcode Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: ...
3
votes
3answers
46 views

Counting duration values

I have a durations as such: [{'duration': 3600}, {'duration': 3600}] And I want the output to be ...
2
votes
1answer
52 views

Finding the most common section from the visible cells in a collection view

My goal is to determine, of all of the currently visible cells in a collection view, which section has the most visible cells. Start by getting the index paths for the visible cells: ...
1
vote
1answer
54 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). ...
3
votes
2answers
46 views

Robot Return to Origin

The task is taken from leetcode There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes ...
1
vote
1answer
103 views

Longest Substring Without Repeating Characters in Scala

Question is taken from Leetcode and solution works for their test cases. I do see a scope of improvement and make it more functional (getting rid of vars and mutables data structures). Please suggest ...
1
vote
2answers
43 views

Flipping an Image

The task is taken from leetcode Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip ...
1
vote
0answers
15 views

Combinable filters for Python Pandas dataframe structure

This is an extension of my other question here. I have applied suggestions from 200_success in real-life problem at hand that deals with modelling extensible and ...
6
votes
1answer
2k views

“Mars Rover” challenge

I have written the solution of the Mars Rover problem in JavaScript, with a sort of functional approach. I would like to know how I can improve it and if there are any additional edge cases I missed ...
1
vote
1answer
97 views

Rotate a N by N matrix by 90 degrees clockwise

The task: Given an N by N matrix, rotate it by 90 degrees clockwise. For example, given the following matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] you should return: [[7, 4, 1], [8, ...
3
votes
1answer
41 views

Utmost function that takes a comparator function

I'm working through Paul Graham's "ANSI Common Lisp". In Chapter 6 (p105), he introduces the idea of utility functions. He points out these are core to bottom-up programming and reusability. He ...
1
vote
0answers
36 views

Transform vcards into contacts using functional programming in JavaScript

Context: I'm building a React app using Ramda as a helper library for functional programming. Users can import contacts into the app. These contacts can be vCards. I've written a function that: ...
3
votes
1answer
43 views

Grouping dates by weeks using reduce()

I am trying to implement a function groupWeeks(dates) using reduce that allows me to group an array of objects ...
1
vote
1answer
69 views

Sort Array By Parity

The task is taken from leetcode Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A. You may return any ...
0
votes
1answer
25 views

Items also occurs in a set

The task You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how ...
2
votes
0answers
68 views

Determine whether there exists a one-to-one character mapping from one string to another

The task Determine whether there exists a one-to-one character mapping from one string s1 to another s2. For example, given s1 = abc and s2 = bcd, return true since we can map a to b, b to ...
1
vote
1answer
288 views

Grouping models without dictionary

I want to group an array of structs only using arrays into an array of new structs. In this example I want to group the Person structs by ...
4
votes
2answers
103 views

Roman Numbers - The Functional Way

Yet another exercise in converting values between the decimal and roman number systems. I have tried the functional way. Any comments are welcome - but I'm especially interested in answers focusing ...
1
vote
2answers
49 views

Sum of two numbers in array

The task You are given two non-empty arrays representing two non-negative integers. The digits are stored in reverse order and each of their items contain a single digit. Add the two numbers ...
1
vote
1answer
72 views

Find busiest period in building

The task You are given a list of data entries that represent entries and exits of groups of people into a building. An entry looks like this: {"timestamp": 1526579928, count: 3, "type": "...
0
votes
1answer
45 views

Find indices of two numbers such that they add up to a target

The task ...is taken from leetcode Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have ...
1
vote
0answers
64 views

Find all pairs of unique indices in a list such that the concatenation of the two words is a palindrome

The task Given a list of words, find all pairs of unique indices such that the concatenation of the two words is a palindrome. For example, given the list ["code", "edoc", "da", "d"], return ...
1
vote
1answer
56 views

Check whether string of brackets are well-formed

The task I asked similar questions here and here. Given a string of round, curly, and square open and closing brackets, return whether the brackets are balanced (well-formed). For example, ...
1
vote
1answer
93 views

Given a list and a number k return whether any two numbers from the list add up to k

The task: Given a list of numbers and a number k, return whether any two numbers from the list add up to k. For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. ...
4
votes
2answers
92 views

Check whether string of braces, brackets, and parentheses is balanced

The Task is taken from codewars: Write a function that takes a string of braces, and determines if the order of the braces is valid. It should return true if ...
1
vote
2answers
77 views

Minimum number of parentheses to be removed to make a string of parentheses balanced

The task: Given a string of parentheses, write a function to compute the minimum number of parentheses to be removed to make the string valid (i.e. each open parenthesis is eventually closed). ...
1
vote
1answer
51 views

Return a new array where each element is the number of smaller elements to the right of that element in the original input

The task: Given an array of integers, return a new array where each element in the new array is the number of smaller elements to the right of that element in the original input array. For ...
6
votes
2answers
84 views

Find duplicate in linear time and space

The task: You are given an array of length n + 1 whose elements belong to the set {1, 2, ..., n}. By the pigeonhole principle, there must be a duplicate. Find it in linear time and space. <...