functional-programming

Scheme streams and circular lists

﹥>﹥吖頭↗ 提交于 2019-12-24 08:03:58
问题 In Scheme/Lisp I am trying to make a function that converts a list into a circular list. Therefore, I believe I need to construct an infinite stream in which the tail of the list points to the head of the list. Here is my code so far: (define (rotate-list l1 l1copy) (if (null? (force (cdr l1))) (cons (car l1) (delay l1copy))) (cons (car l1) (delay (rotate-list (force (cdr l1)) l1copy)))) All help is greatly appreciated. 回答1: No, you don't need streams to make a circular list. There are two

Defining a Semigroup instance that depends on itself

試著忘記壹切 提交于 2019-12-24 07:59:40
问题 ... or mishaps of a Haskell programmer that has to code Scala, part 5. I have the following structure in Scala: case class ResourceTree( resources: Map[String, ResourceTree] ) And, using Cats, I would like to define a Semigroup instance of it. object ResourceTreeInstances { implicit val semigroupInstance = new Semigroup[ResourceTree] { override def combine(x: ResourceTree, y: ResourceTree): ResourceTree = { ResourceTree( x.resources |+| y.resources ) } } This will result in the following

Lacking type signature since TypeScript 2.7

早过忘川 提交于 2019-12-24 07:35:06
问题 This piece works in TypeScript 2.6: function resolver<Key extends keyof HashType>(a: Command<Key>): HashType[Key]['out'] { return handlers[a.kind](a); } const handlers: {[k in keyof HashType]: (arg: Command<k>) => HashType[k]['out']} = { a: arg => 1, b: arg => '' }; type Command<Key extends keyof HashType> = HashType[Key]['in'] & { kind: Key } type HashType = { a: { in: { someString: string }, out: number } b: { in: { someNumber: number }, out: string } } However, since 2.7 , it fails with:

Avoiding nesting maps when “pivoting” multidimensional array

倖福魔咒の 提交于 2019-12-24 07:30:12
问题 I have data in the format [ { "timeline_map": { "2017-05-06": 770, "2017-05-07": 760, "2017-05-08": 1250 ... } }, { "timeline_map": { "2017-05-06": 590, "2017-05-07": 210, "2017-05-08": 300 ... } }, { "timeline_map": { "2017-05-06": 890, "2017-05-07": 2200, "2017-05-08": 1032 ... } } ] that in order to use in a google chart I need to change to the format [ ["2017-05-06", 770, 590, 890, ...], ["2017-05-07", 760, 210, 2200, ...], ["2017-05-08", 1250, 300, 1032, ...] ] I wrote the following to

How can I update lists in SML using functions?

有些话、适合烂在心里 提交于 2019-12-24 06:27:02
问题 I need to write an SML function that looks like this: update(FLR, (x,y)) Where FLR is a finite list of tuples that looks like the following: [(1,1),(2,4),(3,9),(4,16)] But it can contain any number of tuples. What this update function needs to do is take in the list of tuples as the first argument and an x / y tuple as the second argument. If there is a tuple in the list that has the same x value as the one given to the function, it needs to update the y value of the list to the y value given

How can I update lists in SML using functions?

╄→гoц情女王★ 提交于 2019-12-24 06:26:09
问题 I need to write an SML function that looks like this: update(FLR, (x,y)) Where FLR is a finite list of tuples that looks like the following: [(1,1),(2,4),(3,9),(4,16)] But it can contain any number of tuples. What this update function needs to do is take in the list of tuples as the first argument and an x / y tuple as the second argument. If there is a tuple in the list that has the same x value as the one given to the function, it needs to update the y value of the list to the y value given

How can I update lists in SML using functions?

ε祈祈猫儿з 提交于 2019-12-24 06:26:00
问题 I need to write an SML function that looks like this: update(FLR, (x,y)) Where FLR is a finite list of tuples that looks like the following: [(1,1),(2,4),(3,9),(4,16)] But it can contain any number of tuples. What this update function needs to do is take in the list of tuples as the first argument and an x / y tuple as the second argument. If there is a tuple in the list that has the same x value as the one given to the function, it needs to update the y value of the list to the y value given

Is there a pure functional equivalent to goog.object.extend?

三世轮回 提交于 2019-12-24 05:52:41
问题 Per Closure documentation: Extends an object with another object. This operates 'in-place'; it does not create a new Object. Example: var o = {}; goog.object.extend(o, {a: 0, b: 1}); o; // {a: 0, b: 1} goog.object.extend(o, {b: 2, c: 3}); o; // {a: 0, b: 2, c: 3} But this is not a functional approach and for many contexts a functional approach is better. Does Closure offer something more modern for this? e.g. goog.object.extend(a, b); becomes a = goog.object.extend(a, b); 回答1: You can create

How to explain scheme expression '(a 'b)

纵然是瞬间 提交于 2019-12-24 04:56:15
问题 '(a 'b) gives out the answer (a 'b). How does this work when there is no binding for a (which is unquoted). 回答1: This is what happens when we evaluate the expression: '(a 'b) => (a 'b) The ' quote is shorthand for the quote special form, see the linked documentation for more details: (quote (a 'b)) => (a 'b) As you can see, it prevents the quoted arguments from being evaluated, so it doesn't matter if a is undefined, because a is not interpreted as a variable inside a quoted expression. It's

Find keys closest to an integer argument in Haskell tree

房东的猫 提交于 2019-12-24 04:26:07
问题 There is a plenty of solutions how to find closest lower and upper keys in binary tree in imperative languages, but a lack of same questions for doing it in purely functional style like that of Haskell. I'm curious to learn how it's possible to walk around a binary serch tree ahead of meeting both closest keys. There is a function and some pattern matches I've so far: data TreeMap v = Leaf | Node { pair::(Integer, v), l::TreeMap v, r::TreeMap v} deriving (Show, Read, Eq, Ord) closestLess ::