functional-programming

Concise way to conditionally update map in State monad

夙愿已清 提交于 2020-01-07 04:19:15
问题 Below is the code from an answer regarding memoization, showing a memoization function used in the State monad, where the state is updated with the result of the passed function if the key is not already in the map. type MyMemo a b = State (Map.Map a b) b myMemo :: Ord a => (a -> MyMemo a b) -> a -> MyMemo a b myMemo f x = do map <- get case Map.lookup x map of Just y -> return y Nothing -> do y <- f x modify $ \map' -> Map.insert x y map' return y It doesn't seem like idiomatic Haskell: it

Wait for latest values from dependent streams in BaconJS?

帅比萌擦擦* 提交于 2020-01-07 02:49:04
问题 I have 3 streams. gradingResult and contextId depend on studentResponse . I need to fire an event and only one event (otherwise, this is trivial) when all 3 have the latest values. I've tried #combineTemplate and #sampledBy studentResponse . Unfortunately, I always see the wrong data--- gradingResult and contextId have the old values in the combined template. How can I wait for all streams to have the latest values? Code is shown below: var studentResponse = new Bacon.Bus(); var gradingResult

convert method to java 8 lambda

扶醉桌前 提交于 2020-01-07 02:15:11
问题 How to write this method in functional programming by using Java 8 lambda / stream() in a short more intuitive way? int animation = R.style.DialogAnimationSlideHorizontal; String body; String subject = mSubjectView.getText().toString(); BaseDialog dialog = dialogFactory.getType(DialogTypes.DIALOG_FULL); if (!checkFields()) { // one of the recipients is invalid. body = getString(R.string.bad_address); dialog.showDialog(body, animation, new DialogBuilder.Positive() { @Override public void

Updating a postmodern row in a more functional way

跟風遠走 提交于 2020-01-07 00:34:29
问题 I have a couple of classes (namely book and user ). I need to update a book by setting its lended slot to t and its lended-to to the borrower's id . I'm using Postmodern as a back-end to a PostgreSQL database This is what I came up with (I hope the names are self-describing enough) (defmethod lend-book ((to-lend book) borrower) ;borrower is a user instance (if (book-lent to-lend) nil (let (to-lend (get-dao 'book (book-id to-lend))) (setf (book-lent-to to-lend) (user-id borrower)) (setf (book

Get function as parameter in haskell

邮差的信 提交于 2020-01-06 23:00:23
问题 I can't figure this, I have a type called Enumeration > type Enumeration a = Int -> [a] And I need to map over it. I wrote this following function: > imapE :: (a -> b) -> Enumeration a -> Enumeration b > imapE f (m fa) = \n -> imapF f fa where imapF is defined like this: > imapF :: (a -> b) -> [a] -> [b] > imapF _ [] = [] > imapF f (x:xs) = f x : imapF f xs but when I try to load my code I get the following error BinaryTrees.lhs:91:14: Parse error in pattern: m regarding my imapE function. I

Get function as parameter in haskell

我的未来我决定 提交于 2020-01-06 23:00:10
问题 I can't figure this, I have a type called Enumeration > type Enumeration a = Int -> [a] And I need to map over it. I wrote this following function: > imapE :: (a -> b) -> Enumeration a -> Enumeration b > imapE f (m fa) = \n -> imapF f fa where imapF is defined like this: > imapF :: (a -> b) -> [a] -> [b] > imapF _ [] = [] > imapF f (x:xs) = f x : imapF f xs but when I try to load my code I get the following error BinaryTrees.lhs:91:14: Parse error in pattern: m regarding my imapE function. I

Deleting Second last ATOM

这一生的挚爱 提交于 2020-01-06 13:52:58
问题 I am trying to delete second last ATOM from the given list - (define (butSecondLastAtom lst1) (cond ((null? lst1) '()) ((null? (cdr lst1)) lst1) ((null? (cddr lst1)) (cond((not(pair? (car lst1))) (cdr lst1)) (else (cons(butLastAtom (car lst1)) (cdr lst1))))) (else (cons (car lst1) (butSecondLastAtom(cdr lst1)))))) (define (butLastAtom x) (cond ((null? (cdr x)) (cond ((not(pair? (car x))) '()) (else (cons (butLastAtom(car x)) '())))) (else (cons (car x) (butLastAtom(cdr x)))))) This code do

Scheme mode function

狂风中的少年 提交于 2020-01-06 13:10:50
问题 I am trying to find the mode of a list Assuming the list is sorted ascending order Here is my mode function (define freq (lambda (l) (cond ((null? l)l) ((null? (cdr l))l) ((not(equal? (car l) (car(cdr l))))(freq(cdr(delete l (car l))))) (else (freq (cdr l))) ))) (freq '(4 4 4 4 5 7 9 9 9)) => should returns 4 but its returning 9 instead 回答1: Here's my solution, which is similar to Óscar's solution but centralises the update of the longest/winning result in one place: (define (longest-run lst)

Is it possible to do that in Scala (without mutating the internal state of a class)?

时间秒杀一切 提交于 2020-01-06 08:15:15
问题 Let's say, there is a class called RemoteIdGetter . It returns a key from a server. But it only makes a request to the server if the key is not "fresh" enough, meaning the last time it's been requested, is more or equal to 5 minutes (300 seconds). Otherwise, it returns the local "cached" value of the key. I need to do that without ( no var ) mutating the internal state of RemoteIdGetter or with a pure functional approach . It might look like this: class RemoteIdGetter { def key = { if (

Execute Fluture task in middle of Sanctuary pipe

半城伤御伤魂 提交于 2020-01-06 05:28:19
问题 I have a pipe like this: S.pipe([ getRequestFile, // not async S.chain(saveTemporary), // not async S.chain(copyImageToPublicPath), // async S.chain(copyFileToPath), // async S.chain(someLoggingFunction), // not async S.chain(sendResponse), // not async ]); There are 2 async functions in middle of this pipe. I want to await for copyImageToPublicPath and then await for copyFileToPath and then continue the normal flow After some search I found that there is Future.tryP function for doing async