Category "functional-programming"

Count and print unique list items in one chain using Java Streams

I'm trying to achieve this using only functional programming constructs (Streams, Collectors, lambda expressions). Let's say list is a String[]: {"Apple", "Sa

Python - applying tupple to a function in functional toolz.pipe

I want to use a function which accepts some arguments in a toolz.pipe, but data input is a tuple. I know how to solve it, but I think there must be some solutio

Python fuctional style iterative algoritm?

In Haskell there is a simple list function available iterate :: (a -> a) -> a -> [a] iterate f x = x : iterate f (f x) In python it could be implemen

How to pre-fill arguments to a function in Ruby?

Let's say I have a function foo: def foo(A, B, C) A + B + C end And I call it like this, with only the last parameter changing: foo("foo", "bar", "123") foo("

How would a functional language actually define/translate primitives to hardware?

Let's say I have a few primitives defined, here using javascript: const TRUE = x => y => x; const FALSE = x => y => y; const ZERO = f => a =>

Why use Arrow's Options instead of Kotlin nullable

I was having a look at the Arrow library found here. Why would ever want to use an Option type instead of Kotlin's built in nullables?

How can memoize be implemented in pure functional languages?

One advantage of pure functions is that their inputs fully determine their outputs, allowing the result to be cached for later use. However, I don't see how thi

How can I write test cases for instances in Haskell

I have an instance of Num for a type I created called Vec: instance Num Vec where (+) (Vec x) (Vec y) = Vec (zipWith (+) x y) And I am trying to write a test

Project Euler #1 in JavaScript on HackerRank

I'm having some trouble with the test cases for Project Euler #1 on HackerRank and was hoping someone with some JS experience on HackerRank could help out. B

Is there a more Functional way to aggregate a collection into set of Mongo Filters conjugated by OR?

We have a microservice which enables consumers (indirectly) to query a database, where we have our own logic to limit what consumers can do with the queries. Wh

Swift: What's the best way to pair up elements of an Array

I came across a problem that required iterating over an array in pairs. What's the best way to do this? Or, as an alternative, what's the best way of transformi

random number generator function returns a nested tuple in haskell

I am trying to understand why my haskell function returns a nested tuple, and I cannot seem to wrap my head around the problem. I have this function that genera

Improvement of State Machine implemented in scala

I am working on an implementation of a state machine in scala. The original version is written in python, therefore I have a lot of if /else clauses in the co

What are row types? Are they algebraic data types?

I often hear that F# lacks support for OCaml row types, that makes the language more powerful than F#. What are they? Are they algebraic data types, such as su

algorithm to merge two arrays into an array of all possible combinations

Example given in JavaScript: Suppose we have two arrays [0,0,0] and [1,1,1]. What's the algorithm to produce all possible ways these two arrays can be combine.

Computational Complexity of Higher Order Functions?

Map and filter seem like they would be linear O(n) because they only have to traverse a list once, but is their complexity affected by the function being passed

What exactly is a pure function when we are talking about a function within a function

I've learned that a pure function is a function that doesn't alter global state, period. If this is true, functions within functions can alter the state of the

Check if one element exists in an array of objects

I have the following array of objects: var memberships = [ { id: 1, type: 'guest' }, { id: 2, type: 'member' } ]; How can I verify if

getStrLn in ZIO working with flatMap but not inside for comprehension

val askNameFlatMap: ZIO[Console, IOException, Unit] = putStrLn("What is your Name? ") *> getStrLn.flatMap(name => putStrLn(s"Hello $name")) val askNa

Apply multiple functions with map

I have 2D data that I want to apply multiple functions to. The actual code uses xlrd and an .xlsx file, but I'll provide the following boiler-plate so the outpu