# Tag Info

## New answers tagged functional-programming

1

Just a few ideas - successfully compiled, not tested further. Using record syntax yields the functions suit, rank for "free", i.e. data Card = Card { suit :: Suit , rank :: Rank } allows for shorter definitions: instance Eq Card where c1 == c2 = rank c1 == rank c2 instance Ord Card where c1 compare c2 = rank c1 `...

1

It's better to cache both happy and unhappy numbers def squareSum(n: Int): Int = n.toString.map(_.asDigit).map(x => x * x).sum def happyNum(n: Int, book: Set[Int], happy: Set[Int], unhappy: Set[Int]): List[Set[Int]] = { if(n==1 || happy.contains(n)) List(happy ++ book, unhappy) else if(book.contains(n) || unhappy.contains(n)) List(happy, unhappy ++ ...

5

I'll leave the alternatives of the code to the other answers, but just to make a comment about functional programming: Functional programming has three core tenets as I see it: Functions are first class citizens (you can pass them as arguments, put them in lists) Functions are deterministic - (for a function call of the same arguments, it always returns the ...

8

Another suggestion: with TypeScript, you only need to note the type of a parameter when TypeScript can't infer it itself. You might find it easier to read and write code when you avoid explicitly typing functions except when necessary for the TS to compile, or when the type of a return value isn't clear at a glance. In other words, if I were you, I'd switch ...

8

How to modify this code, so it doesn't use forEach? One way to achieve this is using Array.prototype.reduce(): const totalWordCount = fileTexts.reduce((big: sum, fileText: string) => { return sum + nlp(fileText).wordCount() }, 0) Notice that totalWordCount is assigned once so const can be used to avoid accidental re-assignment later. If fileTexts is ...

1

With a bit of suggestion/help on Haskell's IRC channels, I came up with this, which doesn't forkIO at every recursion. {-# language LambdaCase #-} import Control.Concurrent import Data.Maybe import System.IO main :: IO () main = do hSetBuffering stdin NoBuffering future_input <- newEmptyMVar forkIO $pollInput future_input processInput ... 4 Better is relative! Lots of ways to write that code. Though I personally prefer terse and precise code like other answers have highlighted, (here is my take on that): const minuteConverter = time => [time.split(':')] .map(([hour, minute]) => +(+hour + +minute / 60).toFixed(2))[0] The pluses could get a little hectic so here is a more descriptive/... 3 Use + instead of Number() on ES6. Use const instead of let or var if the value for this variable not going to change. Use a deconstructing assignment to create variables from the array let [h,m] = time.split(/[.:]/). This will accept 10:30 or 10.30, also in case the time is .30 will add 0 for hours variable. const timeStringToFloatMm = time => { let [h,... 15 Use const instead of let to declare variables if the value doesn't change. You are executing time.split(':') twice. A short method to convert a string to a number is the unary +. JavaScript has the toFixed() method to format a number to a fixed number of digits: function minuteConverter(time) { const [h, m] = time.split(':'); const value = +h + +m /... 6 Simpler technique Per answers to this identical question from six years ago on stack overflow the formula for converting the minutes doesn’t need to be so complex. Many of the answers there use parseInt() to parse numbers from the strings but the unary plus operator + can be used instead for faster operation and simpler syntax (refer to answers to parseInt ... -1 updated my code to use a deferent approach and apply collection pipeline update my code I use collection pipeline refactoring for more information about the topics look https://martinfowler.com/articles/refactoring-pipelines.html I use stride function instead of filter to generate even Integer array Sidenote: I don't know if I will save a couple of ... 1 Looks like you already solved it, so good job! :) I took the liberty to solve the challenge too, so I'll post my code below. I tried to make the code a bit more "functional", or less imperative, if you will: there is less logic in IO. It's also slightly shorter than yours, though that's not really a goal. I also used a number of techniques and ... 1 It seems that the suggestion I got in the comment is enough to pass the HR test: import Data.Maybe (fromJust) import Data.List (foldl', nub) import qualified Data.Map as M main :: IO() main = do nCasesStr <- getLine let nCase = read nCasesStr :: Int sequence_$ replicate nCase $do k <- (str2int. last . words) <$> getLine list &...

Top 50 recent answers are included