functional-programming

Parametric Polymorphism vs Subtype polymorphism F#

风格不统一 提交于 2020-01-14 01:44:43
问题 What is the difference (if any) between these two F# type signatures? UseTheStream<'a when 'a :> Stream> : 'a -> unit and UseTheStream : (stream : Stream) -> unit Do they mean the same thing in this case? msdn says the following about the (:>) Type Constraint type-parameter :> type -- The provided type must be equal to or derived from the type specified, or, if the type is an interface, the provided type must implement the interface. This would indicate that the two signatures are saying the

Correct use of term Monoid

荒凉一梦 提交于 2020-01-13 20:27:28
问题 From the following example, I think it is correct to say that String defines a monoid under the concatenation operation since it is an associative binary operation and String happens to have an identity element which is an empty string "" . scala> ("" + "Jane") + "Doe" == "" + ("Jane" + "Doe") res0: Boolean = true From the various texts I have been reading on the subject lately, it seems that the correct use of the term monoid is that the monoid is actually a combination of both the type (in

Can a convolution function written in tail recursive form?

旧巷老猫 提交于 2020-01-13 18:23:05
问题 I have a function that I want to write in tail recursive form. The function calculates the number of ways to get the sum of k by rolling an s sided die n times. I have seen the mathematical solution for this function on this answer. It is as follows: My reference recursive implementation in R is: sum_ways <- function(n_times, k_sum, s_side) { if (k_sum < n_times || k_sum > n_times * s_side) { return(0) } else if (n_times == 1) { return(1) } else { sigma_values <- sapply( 1:s_side, function(j)

prefix(_ maxLength:) is type-erased when used with a struct that conforms to LazySequenceProtocol

纵然是瞬间 提交于 2020-01-13 15:04:31
问题 prefix(_ maxLength:) returns a type-erased Sequence in the following code EXAMPLE : public struct CycleIterator <Base: Sequence>: IteratorProtocol { public typealias Element = Base.Element private var sequence: Base private var iterator: Base.Iterator internal init (_ sequence: Base) { self.sequence = sequence self.iterator = self.sequence.makeIterator() } public mutating func next () -> Element? { var next = self.iterator.next() if next == nil { self.iterator = self.sequence.makeIterator()

prefix(_ maxLength:) is type-erased when used with a struct that conforms to LazySequenceProtocol

空扰寡人 提交于 2020-01-13 15:03:32
问题 prefix(_ maxLength:) returns a type-erased Sequence in the following code EXAMPLE : public struct CycleIterator <Base: Sequence>: IteratorProtocol { public typealias Element = Base.Element private var sequence: Base private var iterator: Base.Iterator internal init (_ sequence: Base) { self.sequence = sequence self.iterator = self.sequence.makeIterator() } public mutating func next () -> Element? { var next = self.iterator.next() if next == nil { self.iterator = self.sequence.makeIterator()

How to sort by local strings letter sensitive?

主宰稳场 提交于 2020-01-13 14:57:28
问题 Suppose I have list of words like a, b, c, ą, ć, z, ż I want to be sorted with polish locale like: a, ą, b, c, ć, z, ż Is it possible to achieve? UPDATE: Suppose I have to sort list by two object parameters and also using collator. For one property I can use: val collator = Collator.getInstance(context.getResources().getConfiguration().locale) myList.sortedWith(compareBy(collator, { it.lastName.toLowerCase() })) How to add to this also to sort by firstName ? (so for example if there will be

How to sort by local strings letter sensitive?

若如初见. 提交于 2020-01-13 14:57:17
问题 Suppose I have list of words like a, b, c, ą, ć, z, ż I want to be sorted with polish locale like: a, ą, b, c, ć, z, ż Is it possible to achieve? UPDATE: Suppose I have to sort list by two object parameters and also using collator. For one property I can use: val collator = Collator.getInstance(context.getResources().getConfiguration().locale) myList.sortedWith(compareBy(collator, { it.lastName.toLowerCase() })) How to add to this also to sort by firstName ? (so for example if there will be

How to sort by local strings letter sensitive?

跟風遠走 提交于 2020-01-13 14:57:03
问题 Suppose I have list of words like a, b, c, ą, ć, z, ż I want to be sorted with polish locale like: a, ą, b, c, ć, z, ż Is it possible to achieve? UPDATE: Suppose I have to sort list by two object parameters and also using collator. For one property I can use: val collator = Collator.getInstance(context.getResources().getConfiguration().locale) myList.sortedWith(compareBy(collator, { it.lastName.toLowerCase() })) How to add to this also to sort by firstName ? (so for example if there will be

Javascript: binding to the right of a function?

心不动则不痛 提交于 2020-01-13 10:15:31
问题 How can I bind to the right of the function? Example: var square = Math.pow.bindRight(2); console.log(square(3)); //desired output: 9 回答1: You're looking for partial functions, which are convenient shorthands for aliases. The "classic" way to do what you're asking for is with: var square = function (x) { return Math.pow(x, 2); }; Using partial functions it would be: var square = Math.pow.partial(undefined, 2); console.log(square(3)); Unfortunately, Function.prototype.partial isn't provided in

Passing through multiple parameters to rxjs operator

雨燕双飞 提交于 2020-01-13 08:32:09
问题 Is there a more elegant way of doing the following? private getHeaders(): Observable<any> { let version; let token; return this.appInfo.getVersion() .pipe( tap(appVersion => version = appVersion), mergeMap(() => this.storage.get('access_token')), tap(accessToken => token = accessToken), mergeMap(accessToken => of(this.createHeaders(version, token))) ); } How can I more fluently remember the two return values of this.appInfo.getVersion() and this.storage.get('access_token') without writing