Questions tagged [functional-programming]

Functional programming is a programming paradigm based upon building abstractions using functions, avoiding side effects and change of state. Pure functional programming is thread safe.

Filter by
Sorted by
Tagged with
1
vote
1answer
21 views

How to define an order for a fluent lambda passed to a method

In this talk by venkat subramaniam he makes the following Mailer example: class Mailer { public Mailer from(String addr) { System.out.println("from"); return this; } public Mailer to(String addr) ...
0
votes
3answers
48 views

Joining two arrays of JSON objects like an SQL join using functional programming

Consider, I have the following two arrays of objects: const existingAndArchivedBookings = [ {"booking_id":-2}, {"booking_id":-1}, {"booking_id":999} ] const newAndExistingBookings = [ {...
1
vote
0answers
52 views

How to create a function that takes a list of arguments and returns a function that applies arguments to another function [duplicate]

This sounds like a weird question but bear with me. I want to create a function that takes a list of arguments, and returns a function that applies those arguments to another function. I can get this ...
0
votes
2answers
71 views

Chaining of function and bifunction in java 8

Java-8 comes with Function and BiFunction. How we can chain multiple Function or Bifunction instances. So that output of one become input of another Function. I've created simple functions and ...
0
votes
1answer
28 views

Access temp variables in R.pipe. Ramda

Let's say I want to calculate the average cost: const products = [ { cost: 300 }, { cost: 700 } ]; So first pluck the cost property, summarize them, then divide by the nr of items. ...
0
votes
2answers
59 views

C# Method which takes another method as an argument regardless of input/return type

I'm sorry if the title is unclear. Basically I want to DRY my code by moving a lot of repetitive error handling to one place. I'm calling several methods, which all throw similar errors. Each takes ...
0
votes
1answer
47 views

How to pass a private member function as an argument

In ROS, there is a function called NodeHanle::subscribe(Args...): NodeHandle::subscribe. Which lets u pass a PRIVATE member function as callback. However, when I tried it myself (passing private ...
-7
votes
4answers
188 views

Split an array into 2 arrays grouped by object attributes [duplicate]

I can achieve splitting an object to arrays and grouped by attributes using classic for loops and conditions, but I would like to take advantage of JavaScript functional features such as map, filter ...
0
votes
0answers
22 views

a program that accepts two (2) positive integers (m and n). Display a line stating whether or not m divides n [on hold]

Write a program that accepts two (2) positive integers (m and n). Display a line stating whether or not m divides n!. example: (Output) Enter first number: 6 Enter second number: 9 9 divides by 6! ...
0
votes
0answers
24 views

“Wrapping” Sigmoid / Easing

Background I have the following createSigmoid function that provides me with a sigmoid function. const createSigmoid = (a: number) => (x: number) => x ** a / (x ** a + (1 - x) ** a); I also ...
0
votes
2answers
61 views

Possible implementation of Promise.any on JS

In Promise.race the promise returns as soon that the primary promise returns. In Promise.all returns when all promises resolves, but lasts one problem. If any of all promises rejects all others will ...
0
votes
1answer
54 views

How to write function<void()> to pipe/socketpair? [on hold]

There is a need to send std::function to thread inside pipe/socket. Writing to socket is important because I am using pollfd to check if something was written to call queue. For now I am using std::...
2
votes
1answer
32 views

When will new Date() be run in my function?

I'm still fairly new to Node and Javascript and just want to check what's going on here but I'm not sure what to even look for. I've created a couple of functions for comparing dates... exports....
0
votes
1answer
39 views

Groovy Functional Programming: Round-Robin Filter

On my quest to learn (more) functional programming, I have a semi-contrived example that I'm able to write an adequate solution for imperatively, but a functional solution is confounding me. Given a ...
0
votes
1answer
38 views

How to extract the field values from a dataset in spark using scala?

I have a dataframe which reads streams from kafka as a source and it is then converted to a dataset after applying schema, now how to get that particular field value from the dataset to work with it? ...
0
votes
1answer
21 views

Question about enabling/disabling dropout with keras functional API

I am using Keras functional API to build a classifier and I am using the training flag in the dropout layer to enable dropout when predicting new instances (in order to get an estimate of the ...
0
votes
0answers
47 views

Computing the subsets of natural numbers in AGDA

I'm using AGDA to do some classical mathematical proofs. I'd like to prove that the number of subsets of a set of cardinality n is equal to 2^n (i.e pow (2, n)). To do so, my strategy would be the ...
1
vote
2answers
53 views

How can I convert a 2D array to 1D functionally, in F#?

I can do it easily if I use a mutable counter and 2 for loops but I want to do it inside the Array.init function. This is it so far, I just need to change the zeroes in array2D.[0, 0]. let array2D = ...
1
vote
1answer
80 views

All do blocks in mapM are executed before main computation inside do block

In the following, when mapM executes in decryptParallel, the tick pg call in decryptProgress's do block immediately completes for all items in the list encrBytes, instantly completing the progress bar....
2
votes
1answer
60 views

Implementing Either Monad in Typescript

I've been experimenting with TypeScript lately, and I'm trying to implement some basic monads. I've already got a reasonably functioning Maybe (with the same methods of my Either below, at least), but ...
0
votes
1answer
63 views

Does Swift have any concurrency safety guarantees?

Does swift have any concurrency safety guarantees like the data race guarantees of Rust, or decent functional concurrency support?
2
votes
4answers
86 views

List[Try[T]] to Try[List[T]] in Scala

I would like to know how to convert a List[Try[T]] to Try[List[T]] in Scala? I have tried using an accumulator and folding right but it doesn't seem ideal.
3
votes
4answers
86 views

Check if all elements in list have a :ok element

I have one list of tuples like this [ {:ok, {"0000 0000 "}}, {:ok, %{AM01: %{"C4" => "1111", "C5" => "1"}}}, {:ok, %{AM04: %{"C2" => "2222", "C6" => "2"}}} ] The first ...
1
vote
0answers
47 views

Pass a functional component as argument, then use it in JSX

I have a pair of functional components and want to pass one in as an argument to the other as shown below: const compA = () => ( <div> Hello World! </div>); const compB = (AnotherComp)...
0
votes
0answers
80 views

The best way to reach an inverse function

Given a function type type Func<'a> = 'a -> 'a how to develop map function for Func? I have an idea let fmap (f : 'b -> 'a) (g : 'a -> 'b) (l : Func<'a>) : Func<'b> = f ...
-2
votes
1answer
96 views

How to utilize pure functions in C# in case of mutating data?

I have read this link from Microsoft https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/refactoring-into-pure-functions . I also assume that C# support first class ...
4
votes
2answers
107 views

Similar curry functions producing different results

I am learning functional javascript and I came across two different implementations of the curry function. I am trying to understand the difference between the two they seem similar yet one works ...
0
votes
1answer
79 views

Using the bind function to process a list

I am trying to convert a String of numbers (e.g. "2 3 9 10 14") into a list of Maybe [Token]. I have the following code where the function parseToken converts a String into a Maybe Token. data Token ...
0
votes
1answer
39 views

Warning regarding NA values while converting time

I have code and it works correctly: library(tidyverse) library(lubridate) df<-data.frame("id"=c(1,2,3,4,5), "Time"=c("1999-12-31 10:10:10","1999-12-31 09:05:13","1999-12-31 00:05:25","1999-12-31 ...
-2
votes
0answers
31 views

Why vectorization does not works correctly? [duplicate]

This is my code: library(lubridate) library(tidyverse) df<-data.frame("id"=c(1,2,3,4,5), "Time"=c("1999-12-31 10:10:10","1999-12-31 09:05:13","1999-12-31 00:05:25","1999-12-31 07:04","1999-12-31 ...
0
votes
1answer
53 views

Replace elements in one array from elements in other array in Scala

I am having two arrays. Size of first array is larger than second one. var first = (1 to 20).toArray var second = (1 to 5).toArray I want to Replace first n elements of first array with the ...
1
vote
1answer
82 views

Functions of different Data Types in Haskell

I have the DataType data Rose a = Leaf a | Node [Rose a] an example would be: fmfp = Node [Node [Leaf "F"], Node [], Node [Leaf "F", Leaf "P"], Leaf "M"] which graphically looks like this: I ...
0
votes
1answer
69 views

Withlatestfrom not emitting data from async observable

as you can see from the snippet below, withLatestFrom never completes if a promise is passed over. const { combineLatest, range } = rxjs; const { withLatestFrom } = rxjs.operators; const a$ =...
3
votes
2answers
74 views

using `purrr::map` with compose functions

I would like to know how to use purrr::map where .f is a composition of two different functions. First, let's create a list on which to map a composite function: library(tidyverse) # create a list ...
3
votes
5answers
110 views

How to transpose an m*n matrix using recursion?

I'm trying to transpose a matrix using recursion. Now, I know that under normal circumstances this isn't a good idea and a nested loop/nested map, or a similar approach is superior, but I need to ...
0
votes
1answer
49 views

Iterate over string in python changing last digits

I wish to generate multiple strings on a list with the same prefix in python2.7, using python as a functional programming language: I have the string '1234' and the string '5678' and I want to make a ...
3
votes
1answer
67 views

Can F# functions be specialized at runtime?

Let's assume I have an F# function that's supposed to run both locally and remotely. I want to create a proxy of the function, and let that proxy decide where to run the function, so the remote call ...
0
votes
0answers
69 views

Is normal order evaluation (call by name) further divided into call by value, call by reference, call by sharing, etc?

Consider different ways of evaluating a procedure call and its arguments. Essentials of Programming Languages says All the parameter-passing mechanisms (natural parameter passing, call by value, ...
2
votes
1answer
40 views

How to build a Typescript object using a functional compose style

I'd like to use a functional composed pipeline to build up an object, while keeping type safety. What types should I use for the compose-able functions to stitch them together? I have an object with ...
0
votes
0answers
47 views

Indexing edge sequences igraph - Special function and prefix form

I am currently developing a package relying on igraph. I want to access some edges by index. So far I use special functions describe here, but R CMD is complaining because there is no visible global ...
0
votes
2answers
49 views

passing args to fns of n parameters in a syntactically consistent way

Scalaz has a thrush operator |> defined something like this: implicit class ThrushOps[A](self: A) { def |>[B](f: A ⇒ B): B = f(self) } I have a few functions of various arguments: def f1(x:...
1
vote
1answer
41 views

How to effectively and efficiently group items into paired buckets (if they exist)

I'm creating an automated ranked matching system where two candidates optionally rank one another and the system finds the optimal matching pair based on both candidates' rankings. More specifically, ...
1
vote
2answers
72 views

Haskell: Using multiple let and return a value after the do block inside a function

As the title says, i want to return a value after the do block. Example: Writing a function that inserts a variable at a given position in an array: insertAt :: a -> Int -> [a] -> [a] ...
3
votes
3answers
141 views

The function (.).(.) in haskell

I am trying to find the type of (.).(.) in Haskell, manually. My attempt was the following: (.).(.) = \x -> (.).(.) x (.) :: ( b -> c ) -> (( a -> ...
0
votes
1answer
46 views

increment/decrement click component. How to save data to db.json?

I got a react functional component that can increment or decrement a number. When the page is loaded, i want this number to be read in the db.json file from my JSON-SERVER and displayed in my view. I ...
4
votes
2answers
97 views

map map vs map.map

I'm new to Haskell and I thought that the functions map map and map.map are the same in Haskell. My terminal gives me two different types, (map.map) :: (a -> b) -> [[a]] -> [[b]] and (...
1
vote
1answer
68 views

Why delaying evaluation can transform impure functions into pure ones?

I know that a pure function is a function that doesn't rely on system's state, doesn't have side effects, and its output only depends on its inputs. Making an http call is considered to be a side ...
0
votes
2answers
82 views

Problem with recursion using lambda calculus (using church numerals) in Javascript

I have been playing around with with lambda calculus in javascript (node). I created some Church numerals, and I've been trying to create a recursive function that calculates the fibonacci sequence, ...
1
vote
2answers
69 views

How to chain list of functions returning Completable to run one after another?

How do I write the below code in a functional style? I want to chain the executions of methods which return a CompletableFuture (i.e. playRound returns a CompletableFuture<Void>) ...
0
votes
2answers
83 views

Is Typescript capable of doing simple function composition?

Is Typescript capable of doing simple function composition? I wrote out the following basic implementations for compose, map, and filter to test. The types and functions are set up below and then the ...