Stack Exchange Network

Stack Exchange network consists of 174 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
0answers
12 views

how to take the linspace of values between 2 columns in pandas [on hold]

I am trying to take the linspace of between 2 columns of a dataset. Heres an example. a b C D 0 3 13 3 13 1 4 14 4 14 2 5 15 5 15 ...
4
votes
3answers
53 views

Uniformly generate a number from 0 to n-1 that isn't on a blacklist

The task: Given an integer n and a list of integers l, write a function that randomly generates a number from 0 to n-1 that isn't in l (uniform). Solution 1: ...
3
votes
1answer
75 views

Swap even and odd bits of unsigned 8-bit integer

The task: Given an unsigned 8-bit integer, swap its even and odd bits. The 1st and 2nd bit should be swapped, the 3rd and 4th bit should be swapped, and so on. For example, 10101010 should ...
0
votes
1answer
59 views

Return a sorted ordering of courses such that we can finish all courses

The task: We're given a hashmap associating each courseId key with a list of courseIds values, which represents that the prerequisites of courseId are courseIds. Return a sorted ordering of ...
1
vote
1answer
57 views

Find out whether string A can be shifted to get string B

The task: Given two strings A and B, return whether or not A can be shifted some number of times to get B. For example, if A is abcde and B is cdeab, return true. If A is abc and B is acb, ...
2
votes
2answers
66 views

Reach to the last index starting at index 0

The task: Given an integer list where each number represents the number of hops you can make, determine whether you can reach to the last index starting at index 0. For example, ...
2
votes
3answers
61 views

Contiguous elements of the list sum to K

The task: Given a list of integers and a number K, return which contiguous elements of the list sum to K. For example, if the list is [1, 2, 3, 4, 5] and K is 9, then it should return [2, ...
2
votes
2answers
53 views

Return the shortest substring containing all the characters in the set

The task: Given a string and a set of characters, return the shortest substring containing all the characters in the set. For example, given the string "figehaeci" and the set of characters ...
0
votes
0answers
13 views

QTP/UFT Prints the time in takes to complete every task in a Log using VBScript

This script is meant to log the time it takes to complete functions in order to find the locations where performance could be improved in QTP. If a duplicate log is attempted, the script changes the ...
0
votes
2answers
71 views

Find the length of the longest consecutive elements

The task: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, given [100, 4, 200, 1, 3, 2], the longest consecutive element ...
0
votes
2answers
35 views

Count Substrings for a binary string

Problem is taken from leet code. Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings ...
1
vote
0answers
22 views

KMP algorithm in scala

I implemented KMP pattern matching algorithm in Scala. My code works but it looks more imperative and a scala translation of C/C++ implementation. I am not able to figure out how to manage multiple ...
1
vote
0answers
31 views

Max heap in Haskell

I wrote a (max) heap in Haskell which is balanced after every (public) operation. Not based on any documentation. Focus is on: speed, that is the right time-complexities (not necessarily optimal); ...
0
votes
1answer
93 views

Managing locking for access

I've been working for a while on locking resources (state objects) so that the only way to access them is by acquiring a lock. This moves the responsibility for locking from the user of the state to ...
0
votes
1answer
60 views

Find first repeating Char in String

Given a string, find the first repeating character in it. Examples: firstUnique("Vikrant")None ...
2
votes
1answer
135 views

Execute promise tree in order of declaration

I have created a little framework for my work to manage loading and unloading plugins by using a modular tree structure that is based on Promises. I would like to be able to create the tree structure,...
0
votes
2answers
30 views

Expand spreadsheet ranges to lists of cells in Scala

Problem Spreadsheet cells are referenced by column and row identifiers. Columns are labeled with alphabetical characters, starting with "A", "B", "C", ...; rows are numbered from 1 in ascending ...
1
vote
0answers
28 views

Closest 3Sum in Scala

This question is taken from Leet code. Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three ...
4
votes
2answers
35 views

Replace array element with multiplication of neighbors in Scala

Given an array of integers, update the index with multiplication of previous and next integers, Input: 2 , 3, 4, 5, 6 Output: 2*3, 2*4, 3*5, 4*6, 5*6 Following ...
0
votes
1answer
28 views

Clockwise rotate a matrix in Scala

Given a matrix, clockwise rotate elements in it. Examples: ...
1
vote
2answers
37 views

Print matrix in spiral order in Scala

Problem: Given a square or rectangular matrix, print its element in spiral order. For example, if input matrix is this: ...
0
votes
1answer
36 views

Print words in decreasing order of frequency in Scala

Given a string print its words in decreasing order of frequency. Example: i/p - "aa bbb ccc aa ddd aa ccc" o/p - aa,ccc,ddd,bbb Scala: ...
1
vote
2answers
43 views

List of Happy Numbers in scala

Definition of Happy numbers taken from Wikipedia. A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits ...
2
votes
0answers
24 views

Finding first duplicated value from “circular” list with a recursive function

Background This year I've taken on the challenge of Advent of Code. The second challenge of the first day is to calculate a frequency for a special device based on a series of numbers. Here's a quote ...
2
votes
1answer
50 views

Game of Life with Scala and Functional Programming

I have been doing OOP and imperative programming my whole career but I wanted to dabble in Scala along with functional programming. Rather than writing Java like code in Scala I wanted to try a more ...
2
votes
1answer
74 views

This technique for functional programming, and testing functional programming

I've been playing around with functional programming and testing with jest recently. An issue I've run into, is that it's hard to mock ES6 module exports directly. See this Stack Overflow question ...
0
votes
1answer
34 views

Non-recursive factorial in Ruby

I've implemented a factorial function in a more "Ruby" way. I would like to get feedback on the algorithm. ...
1
vote
1answer
48 views

Calculate simple arithmetic string in Scala

Problem Given an arithmetic string like 2+2*3 calculate result 8. Assumptions you can make string will be always well formed (no error checking needs to be done) only operators passed in string ...
4
votes
2answers
103 views
3
votes
2answers
386 views

Advent of Code 2018 day 1 part 2: find the first aggregated number that occures twice

I am using the coding puzzels of https://adventofcode.com for improving my F# skills. Problem: Day 1 Part 2: Starting with frequency 0, a list of numbers should be added successive generating new ...
0
votes
1answer
33 views

find if one string is a valid anagram of another , in scala

Question is taken from leet code. Problem Given two strings s and t , write a function to determine if t is an anagram of s. Examples ...
1
vote
1answer
48 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
1answer
29 views

Stack Exchange API Reader

I've been working on some code to pull questions from the realtime feed on stackexchange.com and query more information about them from the API. It works, but I'd love some feedback on how I could ...
0
votes
1answer
76 views

Functional List in C++ [closed]

This is rather toy example of immutable datatype in C++. I've tried to follow functional languages like Scheme, Racket - making list as a cons cell: list is list or is empty. ...
2
votes
0answers
26 views

Sort ascending/descending functionally

I've been trying to get into functional programming with javascript and have tried to make some experiments with currying and function composition. What I've played with so far is a sorter function ...
3
votes
1answer
257 views

Binary search tree in C++, and display, search and delete functions

I feel ready to show you my work on creating BST in C++ using double linked list and 3 more functions for manipulating the tree. There is also one more function checking if the tree is real or not. ...
1
vote
1answer
145 views

Graph implementation in C++ using adjacency list

there is my final code for implementing a Graph in C++ using adjacency list and Object-oriented. I got some help from StackOverflow and it was really helpful, but now I would ask about some advice for ...
2
votes
1answer
102 views

String compression algorithm using functional JavaScript

I've written an algo to compress a string eg. aabcbbba to a2bcb3a I'm pretty new to functional programming, I feel it's easier to debug and learn from functional code if you're good at it already, ...
2
votes
0answers
45 views

Monadic implementation of asynchronous tasks (hidden overhead of threads)

So, I've been reading about monads and I wanted to see if I could implement a system for asynchronous computation in a monadic way. I came up with two solutions: The first one spawns a thread for ...
1
vote
1answer
78 views

Python implementation of Quicksort using list comprehensions

I wrote an implementation of quicksort that I think is pythonic. I based it off of this common Haskell implementation: ...
1
vote
1answer
53 views

FizzBuzz with Lambda Expressions in Java

I wanted to create a FizzBuzz in Java that is supposed to be open for extension. So the initial problem is the good old, if divisible by 3 print fizz, if divisible by 5 print buzz, if divisible by ...
-1
votes
1answer
59 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 ...
1
vote
1answer
102 views

C++ heap CPS: Continuation Passing Style with Recursion

I am willing to see how to write readable and efficient continuations on the heap in c++. I am not quite satisfied. The following is just an exercise: it is coded for fun. I post the code here ...
1
vote
0answers
40 views

Convert Javascript object-style module to functional programming [closed]

I'm working on a simple Javascript module and I'd like to make it follow the functional programming style. This module basically allows you to measure how much time has passed when you started and ...
7
votes
1answer
65 views

Monadic list for a more functional approach to C#

I am learning about monadic structures and have created a sort of monad list that implements the map and binding "operators" (or technically methods) in C#. I understand that Linq already exists and ...
2
votes
0answers
71 views

Lexer function to handle the '/' character

I'm writing a toy compiler, which is in a very early stage of development. The lexer contains the following block of code: ...
1
vote
1answer
24 views

Creating the design matrices for a canonical correlation analysis

I have this code to generate symmetric matrices for testing how the design of the canonical correlation analysis I am performing works out. This is a extension of this solution Each row of the matrix ...
1
vote
2answers
57 views

Project Euler - multiples of numbers

I'm trying to learn functional programming and Scala, following through Project Euler problems. I'm solving get the sum of multiples x & y in a list. So for a list ...
11
votes
1answer
92 views

Counting times a file was committed to Git

A long time ago I implemented some code to walk the git commit graph. I recently updated the accompanying analytics part of the code base to follow file moves/renames in order to get a more accurate ...
3
votes
1answer
57 views

F# Idiomatic Log4Net Wrapper

I put together a log4net wrapper that more closely resembles F#-idiomatic logging functions. The basic idea is that there's a Log module that defines a set of 8 ...