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!
1,018 questions
2
votes
1
answer
131
views
Dose tracker with daily and weekly totals
This is an old script I made a while ago.
What it does
It tracks your dose of whatever you want tracked (cigarettes, salt, etc), outputs the total for each day, and the full week.
What I’m looking for
...
2
votes
1
answer
359
views
Find if a dataset of words contains any words containing all vowels by using exactly 5 calls to str_detect
Take the words dataset from stringr. An R4DS exercise challenges the reader to check if there are any words in that dataset that ...
3
votes
2
answers
84
views
3
votes
1
answer
330
views
Print each word of a sentence in a new line
this is my first F# program, very simple concept but took me a few minutes to get it. I'm new to functional programming so I'd appreciate if you could have a look ...
1
vote
1
answer
146
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:
...
3
votes
1
answer
92
views
Finding the difference between the estimated value and the true value in a reward task
I have this function called error, which estimates value based on previous value and rewards in trials. The difference between the estimated value and true value is then calculated, giving the error. ...
1
vote
1
answer
96
views
Factorise numbers
I've started learning Clojure a few days ago and I wrote this code that factorises numbers. How do I make it better? Is there any way to avoid doing it with a loop? Is that how I am supposed to code ...
2
votes
3
answers
150
views
Is My Function to Find Triplets in Scala running in O(N^2)
Given the classical problem to find triplets that sum to zero in an array. Is my Scala implementation correct and executed in O(N^2)? If yes, why? Can someone make a running time analysis.
What other ...
7
votes
1
answer
449
views
A beginner's brainfuck interpreter in Haskell
I am rather new to haskell, and could use some feedback on my code and the decisions I had to make. In my previous project, I made a JSON parser, but relied heavily on guidance from a university ...
0
votes
2
answers
122
views
Functional Programming Style in Javascript Picking a Random Item from an Array
I'm trying to understand the Functional Programming Paradigm (or style) implemented with Javascript. There are a few things I have difficulties with. The naming conventions, and dependency injections. ...
1
vote
1
answer
618
views
"filter" function in Scheme
I know this is a bit elementary, but I am highly uncomfortable writing Scheme code, and I want to make sure that what I'm doing is good practice. I know mutation is frowned upon in Scheme.
I have no ...
1
vote
1
answer
174
views
Retrieving first non-private ip for local system in elixir
I've recently started learning elixir, and the following is my attempt at fetching the local IP address of the system.
It can definitely be improved further, but as this would be my first ever project ...
1
vote
1
answer
951
views
Tic Tac Toe (Haskell)
I implemented tic-tac-toe in Haskell. I'm trying to incorporate suggestions from my last question (which mostly revolved around algorithmic logic) while trying something new - IO logic.
What kind of ...
4
votes
2
answers
868
views
Connect-Four winner-checking algorithm
I'm trying to practice some functional techniques. I haven't worked much in Haskell, so any language-specific tips would be very appreciated, but what I care for even more are general tips towards my ...
2
votes
1
answer
647
views
A Stateless Immutable Event-Driven DDD Pattern in Kotlin
I've been interested in making fully repeatable, reproducible game states. This led me into the world of DDD, and specifically, the concept of Aggregates + Event Sourcing (although that's not ...
4
votes
3
answers
2k
views
Split function using C and dynamic memory allocation
I have programmed a function to split strings, and it gives the expected output.
I am looking forward to write better code and I was told here is a good place to start.
Here's my program:
...
3
votes
1
answer
348
views
Efficiently calculate value of Pascal's Triangle using memoization and recursion
So reading about functional programming, applications to dynamic programming, and learning Scala. I figured I would try it out with a popular example, Pascal's Triangle, tests against known values ...
1
vote
1
answer
86
views
Abstracting device IO / glue logic in a CPU emulator
In my Motorola 6809 CPU emulator (written in C++14) I'm working on an abstraction to represent the wiring between emulated devices, such as the ports on a 6522 VIA, or the interrupt lines connecting a ...
3
votes
2
answers
255
views
Scraping Premier League results
I have a code that scrapes list of URL and appends dataframe as below:
...
5
votes
1
answer
329
views
Two player random number guessing game
I've created a two-player number guessing game in Haskell. My main objective was to practice dealing with "state" in a purely functional language (such as player scores, whose turn it is, ...
2
votes
0
answers
96
views
Dynamic text rendering from form data, Express proxy server API requests, and validation in React
I've created a Node.js/Express application that accepts personal data in a form on the front end with React (name, address, etc). It's purpose is to take a user's name, address, and some other info ...
10
votes
3
answers
794
views
JavaScript OOD: 2048
I wrote a 2048 game in JavaScript with an object-oriented paradigm. The game board is presented with a two-dimensional array and each tile holds an integer.
Here is the implementation:
...
2
votes
0
answers
130
views
Partial Function composability in Haskell
Below is my solution for the CTFP chapter 4 challenges which essentially involves composing partial functions (that don't have defined outputs for all possible inputs i.e. returning a Maybe).
The ...
3
votes
0
answers
130
views
Testing a javascript function that performs side effects
Application context
I am working on a game I call "Apes and Snakes". So far the code only allows players to connect to a host using a room code and WebRTC. Players and the host send messages ...
2
votes
1
answer
266
views
"Sequence full of colors" challenge on HackerRank
This is the challenge:
You are given a sequence of N balls in 4 colors: red, green, yellow and blue. The sequence is full of colors if and only if all of the ...
1
vote
1
answer
2k
views
Processing CSV files with filtering
I have 4 CSV files that i need to process (filter out Part segment and then merge them) but the problem is that they do not fit in my memory. So I decided to: [open - filter - write out] each one of ...
0
votes
1
answer
240
views
JS create array by comparing 2 dictionaries
I have 2 dictionaries that I have to compare, if an element of one dictionary exists in the other dictionary, then add the element to an array but only one time...
here the working code
...
3
votes
2
answers
681
views
5
votes
2
answers
187
views
sum of multiples by 3 or 5 using ranges
This program calculates the sum of all integers in the range \$[1, 1000)\$ which are multiples of either 3 or 5 or both.
Inspired by x86-64 Assembly - Sum of multiples of 3 or 5 the other day, and ...
2
votes
1
answer
1k
views
Alternatives for checking if something is not None in Python [closed]
I recently bumped into dry-python/returns, which seems like a very useful project. In the project description, the authors describe a snippet which can be written with their ...
2
votes
1
answer
2k
views
Simple Haskell Parser
I'm very new to haskell (I never used Monads, Functors and other things) and FP in general. I decided to write the simplest parser I possibly can with some expansion possibilities. (I have a general ...
3
votes
2
answers
210
views
Compute variance via functional programming
So I'm supposed to calculate variance based on this forumula through functional programming:
$$\sigma^2 = \frac{\sum\limits_{i=0}^{n-1} (X -\mu)^2}{n-1}$$
This is the code that works:
...
1
vote
1
answer
93
views
Operations on nested data
Problem description.
I have JSON which comes in bad shape:
...
2
votes
1
answer
142
views
Extract values from English numerals, e.g. "nine million and one"
Though many have done it the other way around, I have not seen such code in many places. And, to be honest, I don't know why this cluster of if-statements, ...
0
votes
1
answer
71
views
Readability vs. maintainability: nested functions [closed]
What are the pros and cons of each option, considering long-term implications (increasing the number of functions / parameters, other developers taking over, etc.)?
Option 1: removes the need to pass <...
4
votes
3
answers
698
views
Functional programming FizzBuzz
I tried to code the fizzbuzz program following the functional programming rules in javascript. Please check my code and give feedback. thank you.
...
1
vote
1
answer
154
views
JavaScript, looping, and functional approach
Data Structure coming back from the server
...
3
votes
1
answer
274
views
React-Redux with functional Components
This is a pretty simple working example using React-Redux to select a user from app.com/user/:userId and then fetch/display that user's info from a remote API
Most ...
4
votes
1
answer
67
views
Feeding stateful and stateful-effectful computations into each other
I am writing code that runs a simulation in an interactive way. The building blocks are:
runStep :: I -> State S O
runWorld :: World m -> O -> StateT W m I
...
3
votes
2
answers
163
views
Cyclomatic Complexity (complexity)
I have a program for finding shortest distance/path and I got a correct answer but I am getting an issue i.e., "Function 'shortestPath' has a complexity of 9. Maximum allowed is 6." This is ...
8
votes
3
answers
4k
views
await user input with async/await syntax
for clarity: moved here from stackoverflow after being pointed to Code Review being the better place for this question
I love async/await and Promises since I got my hands on them. And I might be ...
1
vote
1
answer
755
views
Poker Hand Evaluator in Haskell
The idea is to represent a hand as a list of cards and create a frequency mapping, which can then be used to identify what rank of hand you have and arrange your hand in a way that allows the Ord type ...
2
votes
0
answers
365
views
Splitting a list based on a delimeter into n chunks in Haskell. How can I write my functions more effeciently?
Split: Write a function split that takes a delimiter value “c” and a list “iL”, and it ...
7
votes
3
answers
2k
views
Counting words from stored .md files
The following searches recursively for all the mark down files - i. ending with the extension .md - inside a folder. It then stores the text of the files in an ...
7
votes
4
answers
3k
views
Convert minutes portion of time to decimal
I am making an hours calculator app. It takes a start time, end time and time taken for lunch.
For the start and end time it takes a four-digit hh:mm time. For example: 10:20.
I have made a function ...
2
votes
1
answer
98
views
Keep reading user keyboard input if available, while echoing back the latest available
On StackOverflow I have asked a question a few minutes ago, but then by trying a bit more, I found the answer myself, and I'm posting the resulting working code here for review.
What the code does, is ...
3
votes
1
answer
295
views
Haskell-like function composition function
Much like this question I'm attempting to write in C++ something which can resemble Haskell's (.) function as much as possible, which is basically Chapter 1's ...
2
votes
1
answer
143
views
Projecteuler.net Problem 2 using collection pipeline Pattern
I solve projecteuler.net Problem 2 deferent way
Generate number from 1 to range ex 100 and get the even number
Get Fibonacci numbers from list
Reduce array
I have one problem with a large set of ...
2
votes
2
answers
135
views
HackerRank - Filter huge list for elements that occur more than a given number of times
I'm trying to solve this problem on HackerRank, which basically requires the solution to read an input like this (comments are mine)
...
3
votes
1
answer
65
views
Showing values from different fields in an array with ternary
This small piece of code shows the value of different fields of an array.
I'm looking for a way to improve this nested ternary function. An if statement solution ...