functional-programming

How to modify a value of a Map which contains Sets, returning a new Map?

杀马特。学长 韩版系。学妹 提交于 2019-12-13 18:59:08
问题 Given a Map[Int, Set[Int]] , how can I modify a single value of the Map, generating a new one in the process, for example: val x = Map(1 -> Set(1,2,3)) x(1) + 5 // This creates a new Set, but not a new Map val y = x(1) change { x => x + 5 } // The previous functionality is what I'm looking for // z: Set[Int]] = List(Set(1, 2, 3, 5)) 回答1: In scala 2.10: implicit class ChangeableMap[K,V]( val m: Map[K,V] ) extends AnyVal { def change( k: K )( transform: V => V ): Map[K,V] = { m.get( k ).map{ v

Is there an existing pattern to generate a list of the applications of a function to every combination of the items in two lists?

与世无争的帅哥 提交于 2019-12-13 18:42:31
问题 I'm just getting into functional programming and i'm in the "try out some non-trivial examples and ask others if I'm doing it wrong" phase. I'm following Don Syme's F# Tutorial and have decided to take a stab at the blackjack exercise at the end of Part II with a twist: he suggests treating Ace as 11 for simplicity's sake, but I decided to ignore that recommendation. The way I'm handling it is by giving each card rank a list of possible values and building up a list of possible hand values

Make a nicely typed generic event-to-handler assigner function

我们两清 提交于 2019-12-13 18:41:27
问题 This is a continuation of this exploration that figures out a reusable mechanism that lets us assign the incoming event(message) to the appropriate event handler and be completely type-reliant along the way. Here's what we wanna make reusable: const handleEvent = <EventKind extends keyof EventsMap> (e: Event<EventKind>): Promise<void> => { const kind: EventKind = e.kind; const handler = <(e: CrmEvent<EventKind>) => Promise<void>>handlers[kind]; // Notice the seemingly unnecessary assertion.

Scheme: Implementing a Quick-Sort

我是研究僧i 提交于 2019-12-13 18:05:13
问题 I'm trying to implement a quick sort using scheme, some dudes here already helped me fixing my split function and now I'm asking for you help with combining everything into one working algorithm. Here is my code so far: (define quick-sort (lambda (lst) (define pivot (lambda (lst) (if (null? lst) null (car lst)))) (define split (lambda (lst pivot) (define lst1 null) (define lst2 null) (define split-helper (lambda (lst pivot lst1 lst2) (if (null? lst) (list lst1 lst2) (if (<= (car lst) pivot)

Is async/await no place in functional programming JavaScript [closed]

故事扮演 提交于 2019-12-13 17:21:41
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . I am learning monad with JavaScript. Promise itself is monad, and is the right way to handle async side effects functionally. How about the es7 new player async/await, can we use it in functional programming of JavaScript? I feel like it falls well defined promise monad back

What is “named let” and how do I use it to implement a map function?

橙三吉。 提交于 2019-12-13 17:05:41
问题 I'm totally new to Scheme and I am trying to implement my own map function . I've tried to find it online, however all the questions I encountered were about some complex versions of map function (such as mapping functions that take two lists as an input). The best answer I've managed to find is here: (For-each and map in Scheme). Here is the code from this question: (define (map func lst) (let recur ((rest lst)) (if (null? rest) '() (cons (func (car rest)) (recur (cdr rest)))))) It doesn't

How to delay event emission with rxpy/rxjs?

不羁岁月 提交于 2019-12-13 14:59:39
问题 I've got two event streams. One is from an inductance loop, the other is an IP camera. Cars will drive over the loop and then hit the camera. I want to combine them if the events are within N milliseconds of each other (car will always hit the loop first), but I also want the unmatched events from each stream (either hardware can fail) all merged into a single stream. Something like this: ---> (only unmatched a's, None) / \ stream_a (loop) \ \ \ --> (a, b) ---------------------------> (Maybe

correct style for element-wise operations on lists without numpy (python)

谁说胖子不能爱 提交于 2019-12-13 14:32:46
问题 I would like to operate on lists element by element without using numpy, for example, i want add([1,2,3], [2,3,4]) = [3,5,7] and mult([1,1,1],[9,9,9]) = [9,9,9] , but i'm not sure which way of doing is it considered 'correct' style. The two solutions i came up with were def add(list1,list2): list3 = [] for x in xrange(0,len(list1)): list3.append(list1[x]+list2[x]) return list3 def mult(list1, list2): list3 = [] for x in xrange(0,len(list1)): list3.append(list1[x]*list2[x]) return list3 def

Stateful computation with different types of short-circuit (Maybe, Either)

天涯浪子 提交于 2019-12-13 14:22:56
问题 I am trying to find the most elegant way of converting the following stateful imperative piece of code to pure functional representation (preferably in Haskell to use abstraction that its Monad implementation offers). However I am not yet good at combining different monads using transformers and the like. It seems to me, that analyzing other's takes on such tasks helps the best when learning how to do it myself. The imperative code: while (true) { while (x = get()) { // Think of this as

convert an expression to a string representation?

坚强是说给别人听的谎言 提交于 2019-12-13 14:08:03
问题 Consider the following Haskell code: module Expr where -- Variables are named by strings, assumed to be identifiers: type Variable = String -- Representation of expressions: data Expr = Const Integer | Var Variable | Plus Expr Expr | Minus Expr Expr | Mult Expr Expr deriving (Eq, Show) simplify :: Expr->Expr simplify (Mult (Const 0)(Var"x")) = Const 0 simplify (Mult (Var "x") (Const 0)) = Const 0 simplify (Plus (Const 0) (Var "x")) = Var "x" simplify (Plus (Var "x") (Const 0)) = Var "x"